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