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