PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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