PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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