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