Jetpack_Comments

Main Comments class.

Defined (1)

The class is defined in the following location(s).

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