/modules/comments/comments.php

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