PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.6.6
Jetpack – WP Security, Backup, Speed, & Growth v2.6.6
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / comments / comments.php

comments.php in Jetpack – WP Security, Backup, Speed, & Growth 2.6.6, at modules/comments/comments.php

522 lines 16.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require dirname( __FILE__ ) . '/base.php';
4
5 /**
6 * Main Jetpack Comments class
7 *
8 * @package JetpackComments
9 * @version 1.4
10 * @since 1.4
11 */
12 class Jetpack_Comments extends Highlander_Comments_Base {
13
14 /** Variables *************************************************************/
15
16 /**
17 * Possible comment form sources
18 * @var array
19 */
20 var $id_sources = array();
21
22 /**
23 * URL
24 * @var string
25 */
26 var $signed_url = '';
27
28 /**
29 * The default comment form color scheme
30 * @var string
31 * @see ::set_default_color_theme_based_on_theme_settings()
32 */
33 var $default_color_scheme = 'light';
34
35 /** Methods ***************************************************************/
36
37 public static function init() {
38 static $instance = false;
39
40 if ( !$instance ) {
41 $instance = new Jetpack_Comments;
42 }
43
44 return $instance;
45 }
46
47 /**
48 * Main constructor for Jetpack Comments
49 *
50 * @since JetpackComments (1.4)
51 */
52 public function __construct() {
53 parent::__construct();
54
55 // Jetpack Comments is loaded
56 do_action_ref_array( 'jetpack_comments_loaded', array( $this ) );
57 add_action( 'after_setup_theme', array( $this, 'set_default_color_theme_based_on_theme_settings' ), 100 );
58 }
59
60 public function set_default_color_theme_based_on_theme_settings() {
61 if ( function_exists( 'twentyeleven_get_theme_options' ) ) {
62 $theme_options = twentyeleven_get_theme_options();
63 $theme_color_scheme = isset( $theme_options['color_scheme'] ) ? $theme_options['color_scheme'] : 'transparent';
64 } else {
65 $theme_color_scheme = get_theme_mod( 'color_scheme', 'transparent' );
66 }
67 // Default for $theme_color_scheme is 'transparent' just so it doesn't match 'light' or 'dark'
68 // The default for Jetpack's color scheme is still defined above as 'light'
69
70 if ( false !== stripos( $theme_color_scheme, 'light' ) ) {
71 $this->default_color_scheme = 'light';
72 } elseif ( false !== stripos( $theme_color_scheme, 'dark' ) ) {
73 $this->default_color_scheme = 'dark';
74 }
75 }
76
77 /** Private Methods *******************************************************/
78
79 /**
80 * Set any global variables or class variables
81 * @since JetpackComments (1.4)
82 */
83 protected function setup_globals() {
84 parent::setup_globals();
85
86 // Sources
87 $this->id_sources = array(
88 'guest',
89 'jetpack',
90 'wordpress',
91 'twitter',
92 'facebook'
93 );
94 }
95
96 /**
97 * Setup actions for methods in this class
98 * @since JetpackComments (1.4)
99 */
100 protected function setup_actions() {
101 parent::setup_actions();
102
103 // Selfishly remove everything from the existing comment form
104 remove_all_actions( 'comment_form_before' );
105 remove_all_actions( 'comment_form_after' );
106
107 // Selfishly add only our actions back to the comment form
108 add_action( 'comment_form_before', array( $this, 'comment_form_before' ) );
109 add_action( 'comment_form_after', array( $this, 'comment_form_after' ) );
110
111 // Before a comment is posted
112 add_action( 'pre_comment_on_post', array( $this, 'pre_comment_on_post' ), 1 );
113
114 // After a comment is posted
115 add_action( 'comment_post', array( $this, 'add_comment_meta' ) );
116 }
117
118 /**
119 * Setup filters for methods in this class
120 * @since 1.6.2
121 */
122 protected function setup_filters() {
123 parent::setup_filters();
124
125 add_filter( 'comment_post_redirect', array( $this, 'capture_comment_post_redirect_to_reload_parent_frame' ), 100 );
126 add_filter( 'get_avatar', array( $this, 'get_avatar' ), 10, 4 );
127 }
128
129 /**
130 * Get the comment avatar from Gravatar, Twitter, or Facebook
131 *
132 * @since JetpackComments (1.4)
133 * @param string $avatar Current avatar URL
134 * @param string $comment Comment for the avatar
135 * @param int $size Size of the avatar
136 * @param string $default Not used
137 * @return string New avatar
138 */
139 public function get_avatar( $avatar, $comment, $size, $default ) {
140 if ( ! isset( $comment->comment_post_ID ) || ! isset( $comment->comment_ID ) ) {
141 // it's not a comment - bail
142 return $avatar;
143 }
144
145 if ( false === strpos( $comment->comment_author_url, '/www.facebook.com/' ) && false === strpos( $comment->comment_author_url, '/twitter.com/' ) ) {
146 // It's neither FB nor Twitter - bail
147 return $avatar;
148 }
149
150 // It's a FB or Twitter avatar
151 $foreign_avatar = get_comment_meta( $comment->comment_ID, 'hc_avatar', true );
152 if ( empty( $foreign_avatar ) ) {
153 // Can't find the avatar details - bail
154 return $avatar;
155 }
156
157 // Return the FB or Twitter avatar
158 return preg_replace( '#src=([\'"])[^\'"]+\\1#', 'src=\\1' . esc_url( $this->photon_avatar( $foreign_avatar, $size ) ) . '\\1', $avatar );
159 }
160
161 /** Output Methods ********************************************************/
162
163 /**
164 * Start capturing the core comment_form() output
165 * @since JetpackComments (1.4)
166 */
167 public function comment_form_before() {
168 // Add some JS to the footer
169 add_action( 'wp_footer', array( $this, 'watch_comment_parent' ), 100 );
170
171 ob_start();
172 }
173
174 /**
175 * Noop teh default comment form output, get some options, and output our
176 * tricked out totally radical comment form.
177 *
178 * @since JetpackComments (1.4)
179 */
180 public function comment_form_after() {
181
182 // Throw it all out and drop in our replacement
183 ob_end_clean();
184
185 // If users are required to be logged in, and they're not, then we don't need to do anything else
186 if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) {
187 echo '<p class="must-log-in">' . sprintf( apply_filters( 'jetpack_must_log_in_to_comment', __( 'You must <a href="%s">log in</a> to post a comment.', 'jetpack' ) ), wp_login_url( get_permalink() . '#respond' ) ) . '</p>';
188 return;
189 }
190
191 if ( in_array( 'subscriptions', Jetpack::get_active_modules() ) ) {
192 $stb_enabled = get_option( 'stb_enabled', 1 );
193 $stb_enabled = empty( $stb_enabled ) ? 0 : 1;
194
195 $stc_enabled = get_option( 'stc_enabled', 1 );
196 $stc_enabled = empty( $stc_enabled ) ? 0 : 1;
197 } else {
198 $stb_enabled = 0;
199 $stc_enabled = 0;
200 }
201
202 $params = array(
203 'blogid' => Jetpack_Options::get_option( 'id' ),
204 'postid' => get_the_ID(),
205 'comment_registration' => ( get_option( 'comment_registration' ) ? '1' : '0' ), // Need to explicitly send a '1' or a '0' for these
206 'require_name_email' => ( get_option( 'require_name_email' ) ? '1' : '0' ),
207 'stc_enabled' => $stc_enabled,
208 'stb_enabled' => $stb_enabled,
209 'show_avatars' => ( get_option( 'show_avatars' ) ? '1' : '0' ),
210 'avatar_default' => get_option( 'avatar_default' ),
211 'greeting' => get_option( 'highlander_comment_form_prompt', __( 'Leave a Reply', 'jetpack' ) ),
212 'greeting_reply' => apply_filters( 'jetpack_comment_form_prompt_reply', __( 'Leave a Reply to %s' , 'jetpack' ) ),
213 'color_scheme' => get_option( 'jetpack_comment_form_color_scheme', $this->default_color_scheme ),
214 'lang' => get_bloginfo( 'language' ),
215 'jetpack_version' => JETPACK__VERSION,
216 );
217
218 // Extra parameters for logged in user
219 if ( is_user_logged_in() ) {
220 $current_user = wp_get_current_user();
221 $params['hc_post_as'] = 'jetpack';
222 $params['hc_userid'] = $current_user->ID;
223 $params['hc_username'] = $current_user->display_name;
224 $params['hc_userurl'] = $current_user->user_url;
225 $params['hc_useremail'] = md5( strtolower( trim( $current_user->user_email ) ) );
226 if ( current_user_can( 'unfiltered_html' ) )
227 $params['_wp_unfiltered_html_comment'] = wp_create_nonce( 'unfiltered-html-comment_' . get_the_ID() );
228 }
229
230 $signature = Jetpack_Comments::sign_remote_comment_parameters( $params, Jetpack_Options::get_option( 'blog_token' ) );
231 if ( is_wp_error( $signature ) ) {
232 $signature = 'error';
233 }
234
235 $params['sig'] = $signature;
236 $url_origin = ( is_ssl() ? 'https' : 'http' ) . '://jetpack.wordpress.com';
237 $url = "{$url_origin}/jetpack-comment/?" . http_build_query( $params );
238 $url = "{$url}#parent=" . urlencode( ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
239 $this->signed_url = $url;
240 $height = $params['comment_registration'] || is_user_logged_in() ? '315' : '430'; // Iframe can be shorter if we're not allowing guest commenting
241 $transparent = ( $params['color_scheme'] == 'transparent' ) ? 'true' : 'false';
242
243 if ( isset( $_GET['replytocom'] ) ) {
244 $url .= '&replytocom=' . (int) $_GET['replytocom'];
245 }
246
247 // The actual iframe (loads comment form from Jetpack server)
248 ?>
249
250 <div id="respond" class="comment-respond">
251 <h3 id="reply-title" class="comment-reply-title"><?php comment_form_title( esc_html( $params['greeting'] ), esc_html( $params['greeting_reply'] ) ); ?> <small><?php cancel_comment_reply_link( esc_html__( 'Cancel reply' , 'jetpack') ); ?></small></h3>
252 <div id="commentform" class="comment-form">
253 <iframe src="<?php echo esc_url( $url ); ?>" allowtransparency="<?php echo $transparent; ?>" style="width:100%; height: <?php echo $height; ?>px;border:0px;" frameBorder="0" scrolling="no" name="jetpack_remote_comment" id="jetpack_remote_comment"></iframe>
254 </div>
255 </div>
256
257 <?php // Below is required for comment reply JS to work ?>
258
259 <input type="hidden" name="comment_parent" id="comment_parent" value="" />
260
261 <?php
262 }
263
264 /**
265 * Add some JS to wp_footer to watch for hierarchical reply parent change
266 *
267 * @since JetpackComments (1.4)
268 */
269 public function watch_comment_parent() {
270 $url_origin = ( is_ssl() ? 'https' : 'http' ) . '://jetpack.wordpress.com';
271 ?>
272
273 <!--[if IE]>
274 <script type="text/javascript">
275 if ( 0 === window.location.hash.indexOf( '#comment-' ) ) {
276 // window.location.reload() doesn't respect the Hash in IE
277 window.location.hash = window.location.hash;
278 }
279 </script>
280 <![endif]-->
281 <script type="text/javascript">
282 var comm_par_el = document.getElementById( 'comment_parent' ),
283 comm_par = (comm_par_el && comm_par_el.value) ? comm_par_el.value : '',
284 frame = document.getElementById( 'jetpack_remote_comment' ),
285 tellFrameNewParent;
286
287 tellFrameNewParent = function() {
288 if ( comm_par ) {
289 frame.src = <?php echo json_encode( esc_url_raw( $this->signed_url ) ); ?> + '&replytocom=' + parseInt( comm_par, 10 ).toString();
290 } else {
291 frame.src = <?php echo json_encode( esc_url_raw( $this->signed_url ) ); ?>;
292 }
293 };
294
295 <?php if ( get_option( 'thread_comments' ) && get_option( 'thread_comments_depth' ) ) : ?>
296
297 if ( 'undefined' !== typeof addComment ) {
298 addComment._Jetpack_moveForm = addComment.moveForm;
299
300 addComment.moveForm = function( commId, parentId, respondId, postId ) {
301 var returnValue = addComment._Jetpack_moveForm( commId, parentId, respondId, postId ), cancelClick, cancel;
302
303 if ( false === returnValue ) {
304 cancel = document.getElementById( 'cancel-comment-reply-link' );
305 cancelClick = cancel.onclick;
306 cancel.onclick = function() {
307 var cancelReturn = cancelClick.call( this );
308 if ( false !== cancelReturn ) {
309 return cancelReturn;
310 }
311
312 if ( !comm_par ) {
313 return cancelReturn;
314 }
315
316 comm_par = 0;
317
318 tellFrameNewParent();
319
320 return cancelReturn;
321 };
322 }
323
324 if ( comm_par == parentId ) {
325 return returnValue;
326 }
327
328 comm_par = parentId;
329
330 tellFrameNewParent();
331
332 return returnValue;
333 };
334 }
335
336 <?php endif; ?>
337
338 if ( window.postMessage ) {
339 if ( document.addEventListener ) {
340 window.addEventListener( 'message', function( event ) {
341 if ( <?php echo json_encode( esc_url_raw( $url_origin ) ); ?> !== event.origin ) {
342 return;
343 }
344
345 jQuery( frame ).height( event.data );
346 } );
347 } else if ( document.attachEvent ) {
348 window.attachEvent( 'message', function( event ) {
349 if ( <?php echo json_encode( esc_url_raw( $url_origin ) ); ?> !== event.origin ) {
350 return;
351 }
352
353 jQuery( frame ).height( event.data );
354 } );
355 }
356 }
357 </script>
358
359 <?php
360 }
361
362 /**
363 * Verify the hash included in remote comments.
364 *
365 * @since JetpackComments (1.4)
366 * @param type $comment Not used
367 */
368 public function pre_comment_on_post( $comment ) {
369 $post_array = stripslashes_deep( $_POST );
370
371 // Bail if missing the Jetpack token
372 if ( ! isset( $post_array['sig'] ) ) {
373 unset( $_POST['hc_post_as'] );
374 return;
375 }
376
377 if ( FALSE !== strpos( $post_array['hc_avatar'], '.gravatar.com' ) )
378 $post_array['hc_avatar'] = htmlentities( $post_array['hc_avatar'] );
379
380 $check = Jetpack_Comments::sign_remote_comment_parameters( $post_array, Jetpack_Options::get_option( 'blog_token' ) );
381 if ( is_wp_error( $check ) ) {
382 wp_die( $check );
383 }
384
385 // Bail if token is expired or not valid
386 if ( $check !== $post_array['sig'] )
387 wp_die( __( 'Invalid security token.', 'jetpack' ) );
388 }
389
390 /** Capabilities **********************************************************/
391
392 /**
393 * Add some additional comment meta after comment is saved about what
394 * service the comment is from, the avatar, user_id, etc...
395 *
396 * @since JetpackComments (1.4)
397 * @param type $comment_id
398 */
399 public function add_comment_meta( $comment_id ) {
400 $comment_meta = array();
401
402 switch( $this->is_highlander_comment_post() ) {
403 case 'facebook' :
404 $comment_meta['hc_post_as'] = 'facebook';
405 $comment_meta['hc_avatar'] = stripslashes( $_POST['hc_avatar'] );
406 $comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
407 break;
408
409 case 'twitter' :
410 $comment_meta['hc_post_as'] = 'twitter';
411 $comment_meta['hc_avatar'] = stripslashes( $_POST['hc_avatar'] );
412 $comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
413 break;
414
415 case 'wordpress' :
416 $comment_meta['hc_post_as'] = 'wordpress';
417 $comment_meta['hc_avatar'] = stripslashes( $_POST['hc_avatar'] );
418 $comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
419 $comment_meta['hc_wpcom_id_sig'] = stripslashes( $_POST['hc_wpcom_id_sig'] ); //since 1.9
420 break;
421
422 case 'jetpack' :
423 $comment_meta['hc_post_as'] = 'jetpack';
424 $comment_meta['hc_avatar'] = stripslashes( $_POST['hc_avatar'] );
425 $comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
426 break;
427
428 }
429
430 // Bail if no extra comment meta
431 if ( empty( $comment_meta ) )
432 return;
433
434 // Loop through extra meta and add values
435 foreach ( $comment_meta as $key => $value )
436 add_comment_meta( $comment_id, $key, $value, true );
437 }
438 function capture_comment_post_redirect_to_reload_parent_frame( $url ) {
439 if ( !isset( $_GET['for'] ) || 'jetpack' != $_GET['for'] ) {
440 return $url;
441 }
442 ?>
443 <!DOCTYPE html>
444 <html <?php language_attributes(); ?>>
445 <!--<![endif]-->
446 <head>
447 <meta charset="<?php bloginfo( 'charset' ); ?>" />
448 <title><?php printf( __( 'Submitting Comment%s', 'jetpack' ), '&hellip;' ); ?></title>
449 <style type="text/css">
450 body {
451 display: table;
452 width: 100%;
453 height: 60%;
454 position: absolute;
455 top: 0;
456 left: 0;
457 overflow: hidden;
458 color: #333;
459 }
460
461 h1 {
462 text-align: center;
463 margin: 0;
464 padding: 0;
465 display: table-cell;
466 vertical-align: middle;
467 font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", sans-serif;
468 font-weight: normal;
469 }
470
471 .hidden {
472 opacity: 0;
473 }
474
475 h1 span {
476 -moz-transition-property: opacity;
477 -moz-transition-duration: 1s;
478 -moz-transition-timing-function: ease-in-out;
479
480 -webkit-transition-property: opacity;
481 -webkit-transition-duration: 1s;
482 -webbit-transition-timing-function: ease-in-out;
483
484 -o-transition-property: opacity;
485 -o-transition-duration: 1s;
486 -o-transition-timing-function: ease-in-out;
487
488 -ms-transition-property: opacity;
489 -ms-transition-duration: 1s;
490 -ms-transition-timing-function: ease-in-out;
491
492 transition-property: opacity;
493 transition-duration: 1s;
494 transition-timing-function: ease-in-out;
495 }
496 </style>
497 </head>
498 <body>
499 <h1><?php printf( __( 'Submitting Comment%s', 'jetpack' ), '<span id="ellipsis" class="hidden">&hellip;</span>' ); ?></h1>
500 <script type="text/javascript">
501 try {
502 window.parent.location = <?php echo json_encode( $url ); ?>;
503 window.parent.location.reload( true );
504 } catch ( e ) {
505 window.location = <?php echo json_encode( $url ); ?>;
506 window.location.reload( true );
507 }
508 ellipsis = document.getElementById( 'ellipsis' );
509 function toggleEllipsis() {
510 ellipsis.className = ellipsis.className ? '' : 'hidden';
511 }
512 setInterval( toggleEllipsis, 1200 );
513 </script>
514 </body>
515 </html>
516 <?php
517 exit;
518 }
519 }
520
521 Jetpack_Comments::init();
522