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. if ( false === strpos( $comment->comment_author_url, '/www.facebook.com/' ) && false === strpos( $comment->comment_author_url, '/twitter.com/' ) ) { 
  130. // It's neither FB nor Twitter - bail 
  131. return $avatar; 
  132.  
  133. // It's a FB or Twitter avatar 
  134. $foreign_avatar = get_comment_meta( $comment->comment_ID, 'hc_avatar', true ); 
  135. if ( empty( $foreign_avatar ) ) { 
  136. // Can't find the avatar details - bail 
  137. return $avatar; 
  138.  
  139. // Return the FB or Twitter avatar 
  140. return preg_replace( '#src=([\'"])[^\'"]+\\1#', 'src=\\1' . esc_url( $this->photon_avatar( $foreign_avatar, $size ) ) . '\\1', $avatar ); 
  141.  
  142. /** Output Methods ********************************************************/ 
  143.  
  144. /** 
  145. * Start capturing the core comment_form() output 
  146. * @since JetpackComments (1.4) 
  147. */ 
  148. public function comment_form_before() { 
  149. /** 
  150. * Filters the setting that determines if Jetpagk comments should be enabled for 
  151. * the current post type. 
  152. * @module comments 
  153. * @since 3.8.1 
  154. * @param boolean $return Should comments be enabled? 
  155. */ 
  156. if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type(), true ) ) { 
  157. return; 
  158.  
  159. // Add some JS to the footer 
  160. add_action( 'wp_footer', array( $this, 'watch_comment_parent' ), 100 ); 
  161.  
  162. ob_start(); 
  163.  
  164. /** 
  165. * Noop the default comment form output, get some options, and output our 
  166. * tricked out totally radical comment form. 
  167. * @since JetpackComments (1.4) 
  168. */ 
  169. public function comment_form_after() { 
  170. /** This filter is documented in modules/comments/comments.php */ 
  171. if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type(), true ) ) { 
  172. return; 
  173.  
  174. // Throw it all out and drop in our replacement 
  175. ob_end_clean(); 
  176.  
  177. // If users are required to be logged in, and they're not, then we don't need to do anything else 
  178. if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) { 
  179. /** 
  180. * Changes the log in to comment prompt. 
  181. * @module comments 
  182. * @since 1.4.0 
  183. * @param string $var Default is "You must log in to post a comment." 
  184. */ 
  185. 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>'; 
  186. return; 
  187.  
  188. if ( in_array( 'subscriptions', Jetpack::get_active_modules() ) ) { 
  189. $stb_enabled = get_option( 'stb_enabled', 1 ); 
  190. $stb_enabled = empty( $stb_enabled ) ? 0 : 1; 
  191.  
  192. $stc_enabled = get_option( 'stc_enabled', 1 ); 
  193. $stc_enabled = empty( $stc_enabled ) ? 0 : 1; 
  194. } else { 
  195. $stb_enabled = 0; 
  196. $stc_enabled = 0; 
  197.  
  198. $params = array( 
  199. 'blogid' => Jetpack_Options::get_option( 'id' ),  
  200. 'postid' => get_the_ID(),  
  201. 'comment_registration' => ( get_option( 'comment_registration' ) ? '1' : '0' ), // Need to explicitly send a '1' or a '0' for these 
  202. 'require_name_email' => ( get_option( 'require_name_email' ) ? '1' : '0' ),  
  203. 'stc_enabled' => $stc_enabled,  
  204. 'stb_enabled' => $stb_enabled,  
  205. 'show_avatars' => ( get_option( 'show_avatars' ) ? '1' : '0' ),  
  206. 'avatar_default' => get_option( 'avatar_default' ),  
  207. 'greeting' => get_option( 'highlander_comment_form_prompt', __( 'Leave a Reply', 'jetpack' ) ),  
  208. /** 
  209. * Changes the comment form prompt. 
  210. * @module comments 
  211. * @since 2.3.0 
  212. * @param string $var Default is "Leave a Reply to %s." 
  213. */ 
  214. 'greeting_reply' => apply_filters( 'jetpack_comment_form_prompt_reply', __( 'Leave a Reply to %s' , 'jetpack' ) ),  
  215. 'color_scheme' => get_option( 'jetpack_comment_form_color_scheme', $this->default_color_scheme ),  
  216. 'lang' => get_bloginfo( 'language' ),  
  217. 'jetpack_version' => JETPACK__VERSION,  
  218. ); 
  219.  
  220. // Extra parameters for logged in user 
  221. if ( is_user_logged_in() ) { 
  222. $current_user = wp_get_current_user(); 
  223. $params['hc_post_as'] = 'jetpack'; 
  224. $params['hc_userid'] = $current_user->ID; 
  225. $params['hc_username'] = $current_user->display_name; 
  226. $params['hc_userurl'] = $current_user->user_url; 
  227. $params['hc_useremail'] = md5( strtolower( trim( $current_user->user_email ) ) ); 
  228. if ( current_user_can( 'unfiltered_html' ) ) 
  229. $params['_wp_unfiltered_html_comment'] = wp_create_nonce( 'unfiltered-html-comment_' . get_the_ID() ); 
  230.  
  231. $signature = Jetpack_Comments::sign_remote_comment_parameters( $params, Jetpack_Options::get_option( 'blog_token' ) ); 
  232. if ( is_wp_error( $signature ) ) { 
  233. $signature = 'error'; 
  234.  
  235. $params['sig'] = $signature; 
  236. $url_origin = set_url_scheme( 'http://jetpack.wordpress.com' ); 
  237. $url = "{$url_origin}/jetpack-comment/?" . http_build_query( $params ); 
  238. $url = "{$url}#parent=" . urlencode( set_url_scheme( '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. // The actual iframe (loads comment form from Jetpack server) 
  247. ?> 
  248.  
  249. <div id="respond" class="comment-respond"> 
  250. <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> 
  251. <div id="commentform" class="comment-form"> 
  252. <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> 
  253. </div> 
  254. </div> 
  255.  
  256. <?php // Below is required for comment reply JS to work ?> 
  257.  
  258. <input type="hidden" name="comment_parent" id="comment_parent" value="" /> 
  259.  
  260. <?php 
  261.  
  262. /** 
  263. * Add some JS to wp_footer to watch for hierarchical reply parent change 
  264. * @since JetpackComments (1.4) 
  265. */ 
  266. public function watch_comment_parent() { 
  267. $url_origin = set_url_scheme( 'http://jetpack.wordpress.com' ); 
  268. ?> 
  269.  
  270. <!--[if IE]> 
  271. <script type="text/javascript"> 
  272. if ( 0 === window.location.hash.indexOf( '#comment-' ) ) { 
  273. // window.location.reload() doesn't respect the Hash in IE 
  274. window.location.hash = window.location.hash; 
  275. </script> 
  276. <![endif]--> 
  277. <script type="text/javascript"> 
  278. var comm_par_el = document.getElementById( 'comment_parent' ),  
  279. comm_par = (comm_par_el && comm_par_el.value) ? comm_par_el.value : '',  
  280. frame = document.getElementById( 'jetpack_remote_comment' ),  
  281. tellFrameNewParent; 
  282.  
  283. tellFrameNewParent = function() { 
  284. if ( comm_par ) { 
  285. frame.src = "<?php echo esc_url_raw( $this->signed_url ); ?>" + '&replytocom=' + parseInt( comm_par, 10 ).toString(); 
  286. } else { 
  287. frame.src = "<?php echo esc_url_raw( $this->signed_url ); ?>"; 
  288. }; 
  289.  
  290. <?php if ( get_option( 'thread_comments' ) && get_option( 'thread_comments_depth' ) ) : ?> 
  291.  
  292. if ( 'undefined' !== typeof addComment ) { 
  293. addComment._Jetpack_moveForm = addComment.moveForm; 
  294.  
  295. addComment.moveForm = function( commId, parentId, respondId, postId ) { 
  296. var returnValue = addComment._Jetpack_moveForm( commId, parentId, respondId, postId ), cancelClick, cancel; 
  297.  
  298. if ( false === returnValue ) { 
  299. cancel = document.getElementById( 'cancel-comment-reply-link' ); 
  300. cancelClick = cancel.onclick; 
  301. cancel.onclick = function() { 
  302. var cancelReturn = cancelClick.call( this ); 
  303. if ( false !== cancelReturn ) { 
  304. return cancelReturn; 
  305.  
  306. if ( !comm_par ) { 
  307. return cancelReturn; 
  308.  
  309. comm_par = 0; 
  310.  
  311. tellFrameNewParent(); 
  312.  
  313. return cancelReturn; 
  314. }; 
  315.  
  316. if ( comm_par == parentId ) { 
  317. return returnValue; 
  318.  
  319. comm_par = parentId; 
  320.  
  321. tellFrameNewParent(); 
  322.  
  323. return returnValue; 
  324. }; 
  325.  
  326. <?php endif; ?> 
  327.  
  328. if ( window.postMessage ) { 
  329. if ( document.addEventListener ) { 
  330. window.addEventListener( 'message', function( event ) { 
  331. if ( <?php echo json_encode( esc_url_raw( $url_origin ) ); ?> !== event.origin ) { 
  332. return; 
  333.  
  334. jQuery( frame ).height( event.data ); 
  335. } ); 
  336. } else if ( document.attachEvent ) { 
  337. window.attachEvent( 'message', function( event ) { 
  338. if ( <?php echo json_encode( esc_url_raw( $url_origin ) ); ?> !== event.origin ) { 
  339. return; 
  340.  
  341. jQuery( frame ).height( event.data ); 
  342. } ); 
  343. </script> 
  344.  
  345. <?php 
  346.  
  347. /** 
  348. * Verify the hash included in remote comments. 
  349. * @since JetpackComments (1.4) 
  350. * @param type $comment Not used 
  351. */ 
  352. public function pre_comment_on_post( $comment ) { 
  353. $post_array = stripslashes_deep( $_POST ); 
  354.  
  355. // Bail if missing the Jetpack token 
  356. if ( ! isset( $post_array['sig'] ) ) { 
  357. unset( $_POST['hc_post_as'] ); 
  358. return; 
  359.  
  360. if ( FALSE !== strpos( $post_array['hc_avatar'], '.gravatar.com' ) ) 
  361. $post_array['hc_avatar'] = htmlentities( $post_array['hc_avatar'] ); 
  362.  
  363. $check = Jetpack_Comments::sign_remote_comment_parameters( $post_array, Jetpack_Options::get_option( 'blog_token' ) ); 
  364. if ( is_wp_error( $check ) ) { 
  365. wp_die( $check ); 
  366.  
  367. // Bail if token is expired or not valid 
  368. if ( $check !== $post_array['sig'] ) 
  369. wp_die( __( 'Invalid security token.', 'jetpack' ) ); 
  370.  
  371. /** This filter is documented in modules/comments/comments.php */ 
  372. if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type( $post_array['comment_post_ID'] ), true ) ) { 
  373. // In case the comment POST is legit, but the comments are 
  374. // now disabled, we don't allow the comment 
  375.  
  376. wp_die( __( 'Comments are not allowed.', 'jetpack' ) ); 
  377.  
  378. /** Capabilities **********************************************************/ 
  379.  
  380. /** 
  381. * Add some additional comment meta after comment is saved about what 
  382. * service the comment is from, the avatar, user_id, etc... 
  383. * @since JetpackComments (1.4) 
  384. * @param type $comment_id 
  385. */ 
  386. public function add_comment_meta( $comment_id ) { 
  387. $comment_meta = array(); 
  388.  
  389. switch( $this->is_highlander_comment_post() ) { 
  390. case 'facebook' : 
  391. $comment_meta['hc_post_as'] = 'facebook'; 
  392. $comment_meta['hc_avatar'] = stripslashes( $_POST['hc_avatar'] ); 
  393. $comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] ); 
  394. break; 
  395.  
  396. case 'twitter' : 
  397. $comment_meta['hc_post_as'] = 'twitter'; 
  398. $comment_meta['hc_avatar'] = stripslashes( $_POST['hc_avatar'] ); 
  399. $comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] ); 
  400. break; 
  401.  
  402. case 'wordpress' : 
  403. $comment_meta['hc_post_as'] = 'wordpress'; 
  404. $comment_meta['hc_avatar'] = stripslashes( $_POST['hc_avatar'] ); 
  405. $comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] ); 
  406. $comment_meta['hc_wpcom_id_sig'] = stripslashes( $_POST['hc_wpcom_id_sig'] ); //since 1.9 
  407. break; 
  408.  
  409. case 'jetpack' : 
  410. $comment_meta['hc_post_as'] = 'jetpack'; 
  411. $comment_meta['hc_avatar'] = stripslashes( $_POST['hc_avatar'] ); 
  412. $comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] ); 
  413. break; 
  414.  
  415.  
  416. // Bail if no extra comment meta 
  417. if ( empty( $comment_meta ) ) 
  418. return; 
  419.  
  420. // Loop through extra meta and add values 
  421. foreach ( $comment_meta as $key => $value ) 
  422. add_comment_meta( $comment_id, $key, $value, true ); 
  423. function capture_comment_post_redirect_to_reload_parent_frame( $url ) { 
  424. if ( !isset( $_GET['for'] ) || 'jetpack' != $_GET['for'] ) { 
  425. return $url; 
  426. ?> 
  427. <!DOCTYPE html> 
  428. <html <?php language_attributes(); ?>> 
  429. <!--<![endif]--> 
  430. <head> 
  431. <meta charset="<?php bloginfo( 'charset' ); ?>" /> 
  432. <title><?php printf( __( 'Submitting Comment%s', 'jetpack' ), '…' ); ?></title> 
  433. <style type="text/css"> 
  434. body { 
  435. display: table; 
  436. width: 100%; 
  437. height: 60%; 
  438. position: absolute; 
  439. top: 0; 
  440. left: 0; 
  441. overflow: hidden; 
  442. color: #333; 
  443.  
  444. h1 { 
  445. text-align: center; 
  446. margin: 0; 
  447. padding: 0; 
  448. display: table-cell; 
  449. vertical-align: middle; 
  450. font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", sans-serif; 
  451. font-weight: normal; 
  452.  
  453. .hidden { 
  454. opacity: 0; 
  455.  
  456. h1 span { 
  457. -moz-transition-property: opacity; 
  458. -moz-transition-duration: 1s; 
  459. -moz-transition-timing-function: ease-in-out; 
  460.  
  461. -webkit-transition-property: opacity; 
  462. -webkit-transition-duration: 1s; 
  463. -webbit-transition-timing-function: ease-in-out; 
  464.  
  465. -o-transition-property: opacity; 
  466. -o-transition-duration: 1s; 
  467. -o-transition-timing-function: ease-in-out; 
  468.  
  469. -ms-transition-property: opacity; 
  470. -ms-transition-duration: 1s; 
  471. -ms-transition-timing-function: ease-in-out; 
  472.  
  473. transition-property: opacity; 
  474. transition-duration: 1s; 
  475. transition-timing-function: ease-in-out; 
  476. </style> 
  477. </head> 
  478. <body> 
  479. <h1><?php printf( __( 'Submitting Comment%s', 'jetpack' ), '<span id="ellipsis" class="hidden">…</span>' ); ?></h1> 
  480. <script type="text/javascript"> 
  481. try { 
  482. window.parent.location = <?php echo json_encode( $url ); ?>; 
  483. window.parent.location.reload( true ); 
  484. } catch ( e ) { 
  485. window.location = <?php echo json_encode( $url ); ?>; 
  486. window.location.reload( true ); 
  487. ellipsis = document.getElementById( 'ellipsis' ); 
  488. function toggleEllipsis() { 
  489. ellipsis.className = ellipsis.className ? '' : 'hidden'; 
  490. setInterval( toggleEllipsis, 1200 ); 
  491. </script> 
  492. </body> 
  493. </html> 
  494. <?php 
  495. exit;