-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-comment-tweets.php
355 lines (265 loc) · 10.3 KB
/
class-comment-tweets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* Comment Tweets
*
* @package Comment_Tweets
* @author Tom McFarlin <[email protected]>
* @license GPL-2.0+
* @link http://tommcfarlin.com
* @copyright 2011 - 2015 Tom McFarlin
*/
class Comment_Tweets {
/*--------------------------------------------*
* Attributes
*--------------------------------------------*/
/**
* Instance of this class.
*
* @since 2.0.0
*
* @var object
*/
protected static $instance = null;
/*--------------------------------------------*
* Constructor
*--------------------------------------------*/
/**
* Return an instance of this class.
*
* @since 2.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
} // end if
return self::$instance;
} // end get_instance
/**
* Initializes the plugin by setting localization, filters, and administration functions.
*
* @since 1.0
*/
private function __construct() {
// Setup Localization
load_plugin_textdomain( 'comment-tweets', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
// Register admin stylesheets and JavaScript
add_action( 'admin_print_styles', array( $this, 'register_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) );
// Register plugin stylesheets
add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_styles' ) );
// Setup the Tweet URL meta box
add_action( 'add_meta_boxes', array( $this, 'add_tweet_url_metabox' ) );
add_action( 'save_post', array( $this, 'save_tweet_url' ) );
// Display the tweets
add_action( 'comment_form_before', array( $this, 'display_tweets' ) ) ;
} // end constructor
/*--------------------------------------------*
* Deactivation
*--------------------------------------------*/
/**
* Fired when the plugin is deactivated.
*
* @params $network_wide True if WPMU superadmin uses "Network Activate" action, false if Multisite is disabled or plugin is activated on an individual blog
* @since 1.0
*/
public function deactivate( $network_wide ) {
// Loop through all of the published posts
$arguments = array (
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => -1
);
$posts_query = new WP_Query( $arguments );
// If any posts are found...
if( $posts_query->have_posts() ) {
// ...loop through them all
while( $posts_query->have_posts() ) {
$posts_query->the_post();
// Look through the meta data for the current post
foreach( get_post_meta( get_the_ID() ) as $meta_key => $meta_value ) {
// If any Tweet URL's are found, remove them.
if( false != strstr( $meta_key, 'tweet_url' ) ) {
delete_post_meta( get_the_ID(), $meta_key );
} // end if
} // end foreach
} // end while
} // end if
// Reset the post query
wp_reset_postdata();
} // end deactivate
/*--------------------------------------------*
* Enqueue Stylesheets
*--------------------------------------------*/
/**
* Registers and enqueues plugin-specific styles.
*
* @since 1.0
*/
public function register_admin_styles() {
wp_enqueue_style( 'comment-tweets-admin', plugins_url( 'comment-tweets/css/admin.css' ) );
} // end register_plugin_styles
/**
* Registers and enqueues plugin-specific JavaScript.
*
* @since 1.0
*/
public function register_admin_scripts() {
wp_enqueue_script( 'comment-tweets-admin', plugins_url( 'comment-tweets/js/admin.min.js' ) );
} // end register_admin_scripts
/**
* Registers and enqueues plugin-specific styles.
*
* @since 1.0
*/
public function register_plugin_styles() {
wp_enqueue_style( 'comment-tweets', plugins_url( 'comment-tweets/css/plugin.css' ) );
} // end register_widget_styles
/*--------------------------------------------*
* Tweet URL Post Meta Box
*--------------------------------------------*/
/**
* Initializes the Meta Box used to display the option for including the Tweet URL
* for a given post.
*
* @since 1.0
*/
function add_tweet_url_metabox() {
add_meta_box(
'tweet_url', // The ID attribute of the 'Edit' screen
__( 'Tweet URL', 'comment-tweets' ), // The localized versiokn of the title of the meta box
array( $this, 'tweet_url_display' ), // A reference to the function for rendering the meta box
'post', // Where to display the meta box in the dashboard
'normal', // The priority of the meta box (or where it should be displayed)
'high' // Where the box should be displayed. Here, directly under the Post Editor
);
} // end add_tweet_url_metabox
/**
* Initializes the Tweet URL display panel in the Post Edit dashboard.
*
* @param $post The post on which this should be displayed
* @since 1.0
*/
function tweet_url_display( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'tweet_url_nonce' );
// Open the Tweet URL container
$html = '<div id="tweet-url-container">';
$html .= '<p class="description">' . __( 'Paste Tweet URLs to display below the comments feed on your blog. Delete a Tweet URL to remove it.', 'comment-tweets' ) . '</p>';
// Initialize a tweet counter so we know if the user has saved any
$tweet_url_counter = 0;
// Loop through all of this post's meta data looking for Tweet URL's
foreach( get_post_meta( $post->ID ) as $meta_key => $meta_value ) {
// If we find a Tweet URL...
if( false != strstr( $meta_key, 'tweet_url' ) ) {
// ...render the Tweet URL input elemenet...
$html .= '<input id="' . $meta_key . '" name="tweet_url[]" class="tweet_url" placeholder="' . __( 'Enter the URL of the Tweet you want to display', 'comment-tweets' ) . '" value="' . $meta_value[0] . '" />';
// Incremenet the tweet counter
$tweet_url_counter++;
} // end if
} // end foreach
// If the tweet count is zero, there are no tweets so we need to provide a default input element.
if( 0 == $tweet_url_counter ) {
$html .= '<input id="tweet_url_0" name="tweet_url[]" class="tweet_url" placeholder="' . __( 'Enter the URL of the Tweet you want to display', 'comment-tweets' ) . '" value="" />';
} // end if
// Close the Tweet URL container
$html .= '</div><!-- /#tweet-url-container -->';
// Add the 'Add New Tweet' button
$html .= '<a href="javascript:;" id="add-new-tweet" class="button">' . __( 'Add New Tweet', 'comments-tweet' ) . '</a>';
// Render everything to the screen
echo $html;
} // end tweet_url_display
/**
* Saves the Tweet URL for the given post.
*
* @params $post_id The ID of the post that we're serializing
* @return If the seccurity checks fail
* @since 1.0
*/
function save_tweet_url( $post_id ) {
if( isset( $_POST['tweet_url_nonce'] ) && isset( $_POST['post_type'] ) ) {
/* -- Serialization Security -- */
// Don't save if the user hasn't submitted the changes
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
} // end if
// Verify that the input is coming from the proper form
if( ! wp_verify_nonce( $_POST['tweet_url_nonce'], plugin_basename( __FILE__ ) ) ) {
return;
} // end if
// Make sure the user has permissions to post
if( 'post' == $_POST['post_type']) {
if( ! current_user_can( 'edit_post', $post_id ) ) {
return;
} // end if
} // end if/else
/* -- /Serialization Security -- */
// Set the ID of the current Tweet URL
$tweet_url_counter = 0;
// If the first index of the Tweet URL is empty, then there are no tweets...
if( '' == $_POST['tweet_url'][0] ) {
// ...so we need to delete all of the post meta for this post
foreach( get_post_meta( $post_id ) as $meta_key => $meta_value ) {
if( false != strstr( $meta_key, 'tweet_url' ) ) {
delete_post_meta( $post_id, $meta_key );
} // end if
} // end foreach
// Otherwise, there are Tweet URL's...
} else {
// ...so loop through the list of Tweet URL's
foreach( $_POST['tweet_url'] as $tweet_url ) {
// Create an ID for the HTML element
$tweet_url_id = 'tweet_url_' . $tweet_url_counter;
// Store the tweet URL (or set an empty string if it doesn't exist
$tweet_url = isset( $tweet_url ) ? esc_url ( $tweet_url ) : '';
// First, check to see if the value exists. If so, go ahead and delete it - we don't want to write extra rows into the database
if( '' != trim( get_post_meta( $post_id, $tweet_url_id, true ) ) ) {
delete_post_meta( $post_id, $tweet_url_id );
} // end if
// Next, update the post meta
if( '' != trim ( $tweet_url ) ) {
update_post_meta( $post_id, $tweet_url_id, $tweet_url );
} // end if
// Increment the Tweet URL counter
$tweet_url_counter++;
} // end foreach
} // end if
} // end if
} // end save_notice
/*--------------------------------------------*
* Display The Tweets
*--------------------------------------------*/
/**
* Append the tweets after the actual comment form.
*
* @since 1.0
*/
function display_tweets() {
// Initialize the Tweet HTML string
$tweet_html = '';
// Loop through all of the post meta looking for any Tweet URLs
foreach( get_post_meta( get_the_ID() ) as $meta_key => $meta_value ) {
// If we find a URL...
if( false != strstr( $meta_key, 'tweet_url' ) ) {
// ...append it to the Tweet HTML string.
$tweet_html .= '<blockquote class="twitter-tweet">';
$tweet_html .= '<p>Search API will now always return "real" Twitter user IDs. The with_twitter_user_id parameter is no longer necessary. An era has ended. ^TS</p>— Twitter API (@twitterapi)';
$tweet_html .= '<a href="' . $meta_value[0] . '" data-datetime="2011-11-07T20:21:07+00:00">November7, 2011</a>';
$tweet_html .= '</blockquote>';
} // end if
} // end foreach
// If there are actual tweets to render...
if( '' != $tweet_html ) {
// ...then create the HTML container...
$html = '<div id="comment-tweets">';
// ...append the Tweet HTML...
$html .= $tweet_html;
$html .= '<script src="//platform.twitter.com/widgets.js" charset="utf-8"></script>';
// ... close the comment tweet elements...
$html .= '</div><!-- /#comment-tweets -->';
// ...and render it to the screen.
echo $html;
} // end if
} // end display_tweets
} // end class