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