PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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