PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/endomorphosis/falkenstein
PHP | 293 lines | 152 code | 45 blank | 96 comment | 36 complexity | e39e3a2c3c7a5e18effc3315b0584200 MD5 | raw file
  1. <?php
  2. /**
  3. * All the code shared between WP.com Highlander and Jetpack Highlander
  4. */
  5. class Highlander_Comments_Base {
  6. function __construct() {
  7. $this->setup_globals();
  8. $this->setup_actions();
  9. $this->setup_filters();
  10. }
  11. /**
  12. * Set any global variables or class variables
  13. * @since JetpackComments (1.4)
  14. */
  15. protected function setup_globals() {}
  16. /**
  17. * Setup actions for methods in this class
  18. * @since JetpackComments (1.4)
  19. */
  20. protected function setup_actions() {
  21. // Before a comment is posted
  22. add_action( 'pre_comment_on_post', array( $this, 'allow_logged_out_user_to_comment_as_external' ) );
  23. // After a comment is posted
  24. add_action( 'comment_post', array( $this, 'set_comment_cookies' ) );
  25. }
  26. /**
  27. * Setup filters for methods in this class
  28. * @since JetpackComments (1.4)
  29. */
  30. protected function setup_filters() {
  31. add_filter( 'comments_array', array( $this, 'comments_array' ) );
  32. add_filter( 'preprocess_comment', array( $this, 'allow_logged_in_user_to_comment_as_guest' ), 0 );
  33. }
  34. /**
  35. * Is this a Highlander POST request?
  36. * Optionally restrict to one or more credentials slug (facebook, twitter, ...)
  37. *
  38. * @param string Comment credentials slug
  39. * @param ...
  40. * @return false|string false if it's not a Highlander POST request. The matching credentials slug if it is.
  41. */
  42. function is_highlander_comment_post() {
  43. if ( empty( $_POST['hc_post_as'] ) ) {
  44. return false;
  45. }
  46. if ( func_num_args() ) {
  47. foreach ( func_get_args() as $id_source ) {
  48. if ( $id_source === $_POST['hc_post_as'] ) {
  49. return $id_source;
  50. }
  51. }
  52. return false;
  53. }
  54. return is_string( $_POST['hc_post_as'] ) && in_array( $_POST['hc_post_as'], $this->id_sources ) ? $_POST['hc_post_as'] : false;
  55. }
  56. /**
  57. * Signs an array of scalars with the self-hosted blog's Jetpack Token
  58. *
  59. * @param array $parameters
  60. * @param string $key
  61. * @return string HMAC
  62. */
  63. static function sign_remote_comment_parameters( $parameters, $key ) {
  64. unset(
  65. $parameters['sig'], // Don't sign the signature
  66. $parameters['replytocom'] // This parameter is unsigned - it changes dynamically as the comment form moves from parent comment to parent comment
  67. );
  68. ksort( $parameters );
  69. $signing = array();
  70. foreach ( $parameters as $k => $v ) {
  71. if ( !is_scalar( $v ) ) {
  72. return new WP_Error( 'invalid_input', __( 'Invalid request', 'jetpack' ) );
  73. }
  74. $signing[] = "{$k}={$v}";
  75. }
  76. return hash_hmac( 'sha1', implode( ':', $signing ), $key );
  77. }
  78. /*
  79. * After commenting as a guest while logged in, the user needs to see both:
  80. *
  81. * ( user_id = blah AND comment_approved = 0 )
  82. * and
  83. * ( comment_author_email = blah AND comment_approved = 0 )
  84. *
  85. * Core only does the first since the user is logged in.
  86. *
  87. * Add the second to the comments array.
  88. */
  89. function comments_array( $comments ) {
  90. global $wpdb, $post;
  91. $commenter = $this->get_current_commenter();
  92. if ( !$commenter['user_id'] )
  93. return $comments;
  94. if ( !$commenter['comment_author'] )
  95. return $comments;
  96. $in_moderation_comments = $wpdb->get_results( $wpdb->prepare(
  97. "SELECT * FROM `$wpdb->comments` WHERE `comment_post_ID` = %d AND `user_id` = 0 AND `comment_author` = %s AND `comment_author_email` = %s AND `comment_approved` = '0' ORDER BY `comment_date_gmt` /* Highlander_Comments_Base::comments_array() */",
  98. $post->ID,
  99. wp_specialchars_decode( $commenter['comment_author'], ENT_QUOTES ),
  100. $commenter['comment_author_email']
  101. ) );
  102. if ( !$in_moderation_comments )
  103. return $comments;
  104. // @todo ZOMG this is a bad idea
  105. $comments = array_merge( $comments, $in_moderation_comments );
  106. usort( $comments, array( $this, 'sort_comments_by_comment_date_gmt' ) );
  107. return $comments;
  108. }
  109. /**
  110. * Comment sort comparator: comment_date_gmt
  111. *
  112. * @since JetpackComments (1.4)
  113. * @param object $a
  114. * @param object $b
  115. * @return int
  116. */
  117. public function sort_comments_by_comment_date_gmt( $a, $b ) {
  118. if ( $a->comment_date_gmt == $b->comment_date_gmt ) {
  119. return 0;
  120. }
  121. return $a->comment_date_gmt < $b->comment_date_gmt ? -1 : 1;
  122. }
  123. /**
  124. * Get the current commenter's information from their cookie
  125. *
  126. * @since JetpackComments (1.4)
  127. * @return array Commenters information from cookie
  128. */
  129. protected function get_current_commenter() {
  130. // Defaults
  131. $user_id = 0;
  132. $comment_author = '';
  133. $comment_author_email = '';
  134. $comment_author_url = '';
  135. if ( isset( $_COOKIE['comment_author_' . COOKIEHASH] ) ) {
  136. $comment_author = $_COOKIE['comment_author_' . COOKIEHASH];
  137. }
  138. if ( isset( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) {
  139. $comment_author_email = $_COOKIE['comment_author_email_' . COOKIEHASH];
  140. }
  141. if ( isset( $_COOKIE['comment_author_url_' . COOKIEHASH] ) ) {
  142. $comment_author_url = $_COOKIE['comment_author_url_' . COOKIEHASH];
  143. }
  144. if ( is_user_logged_in() ) {
  145. $user = wp_get_current_user();
  146. $user_id = $user->ID;
  147. }
  148. return compact( 'comment_author', 'comment_author_email', 'comment_author_url', 'user_id' );
  149. }
  150. /**
  151. * Allows a logged out user to leave a comment as a facebook or twitter credentialed user.
  152. * Overrides WordPress' core comment_registration option to treat these commenters as "registered" (verified) users.
  153. *
  154. * @since JetpackComments (1.4)
  155. * @return If no
  156. */
  157. function allow_logged_out_user_to_comment_as_external() {
  158. if ( !$this->is_highlander_comment_post( 'facebook', 'twitter', 'googleplus' ) ) {
  159. return;
  160. }
  161. add_filter( 'pre_option_comment_registration', '__return_zero' );
  162. }
  163. /**
  164. * Allow a logged in user to post as a guest, FB, or twitter credentialed request.
  165. * Bypasses WordPress' core overrides that force a logged in user to comment as that user.
  166. * Respects comment_registration option.
  167. *
  168. * @since JetpackComments (1.4)
  169. * @param array $comment_data
  170. * @return int
  171. */
  172. function allow_logged_in_user_to_comment_as_guest( $comment_data ) {
  173. // Bail if user registration is allowed
  174. if ( get_option( 'comment_registration' ) ) {
  175. return $comment_data;
  176. }
  177. // Bail if user is not logged in or not a post request
  178. if ( 'POST' != strtoupper( $_SERVER['REQUEST_METHOD'] ) || !is_user_logged_in() ) {
  179. return $comment_data;
  180. }
  181. // Bail if this is not a guest or external service credentialed request
  182. if ( !$this->is_highlander_comment_post( 'guest', 'facebook', 'twitter', 'googleplus' ) ) {
  183. return $comment_data;
  184. }
  185. $user = wp_get_current_user();
  186. foreach ( array( 'comment_author' => 'display_name', 'comment_author_email' => 'user_email', 'comment_author_url' => 'user_url' ) as $comment_field => $user_field ) {
  187. if ( $comment_data[$comment_field] != addslashes( $user->$user_field ) ) {
  188. return $comment_data; // some other plugin already did something funky
  189. }
  190. }
  191. if ( get_option( 'require_name_email' ) ) {
  192. if ( 6 > strlen( $_POST['email'] ) || empty( $_POST['author'] ) ) {
  193. wp_die( __( 'Error: please fill the required fields (name, email).', 'jetpack' ) );
  194. } elseif ( ! is_email( $_POST['email'] ) ) {
  195. wp_die( __( 'Error: please enter a valid email address.', 'jetpack' ) );
  196. }
  197. }
  198. $author_change = false;
  199. foreach ( array( 'comment_author' => 'author', 'comment_author_email' => 'email', 'comment_author_url' => 'url' ) as $comment_field => $post_field ) {
  200. if ( $comment_data[$comment_field] != $_POST[$post_field] && 'url' != $post_field ) {
  201. $author_change = true;
  202. }
  203. $comment_data[$comment_field] = $_POST[$post_field];
  204. }
  205. // Mark as guest comment if name or email were changed
  206. if ( $author_change ) {
  207. $comment_data['user_id'] = $comment_data['user_ID'] = 0;
  208. }
  209. return $comment_data;
  210. }
  211. /**
  212. * Set the comment cookies or bail if comment is invalid
  213. *
  214. * @since JetpackComments (1.4)
  215. * @param type $comment_id
  216. * @return If comment is invalid
  217. */
  218. public function set_comment_cookies( $comment_id ) {
  219. // Get comment and bail if it's invalid somehow
  220. $comment = get_comment( $comment_id );
  221. if ( empty( $comment ) || is_wp_error( $comment ) ) {
  222. return;
  223. }
  224. $id_source = $this->is_highlander_comment_post();
  225. if ( empty( $id_source ) ) {
  226. return;
  227. }
  228. // Set comment author cookies
  229. if ( ( 'wordpress' != $id_source ) && is_user_logged_in() ) {
  230. $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
  231. setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN );
  232. setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN );
  233. setcookie( 'comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN );
  234. }
  235. }
  236. /**
  237. * Get an avatar from Photon
  238. *
  239. * @since JetpackComments (1.4)
  240. * @param string $url
  241. * @param int $size
  242. * @return string
  243. */
  244. protected function photon_avatar( $url, $size ) {
  245. $size = (int) $size;
  246. return jetpack_photon_url( $url, array( 'resize' => "$size,$size" ) );
  247. }
  248. }