PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/comments/comments.php

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