Нема описа

base.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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( ...$args ) {
  43. if ( empty( $_POST['hc_post_as'] ) ) {
  44. return false;
  45. }
  46. if ( $args ) {
  47. foreach ( $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' ), array( 'status' => 400 ) );
  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. }
  95. if ( ! $commenter['comment_author'] ) {
  96. return $comments;
  97. }
  98. $in_moderation_comments = $wpdb->get_results(
  99. $wpdb->prepare(
  100. "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() */",
  101. $post->ID,
  102. wp_specialchars_decode( $commenter['comment_author'], ENT_QUOTES ),
  103. $commenter['comment_author_email']
  104. )
  105. );
  106. if ( ! $in_moderation_comments ) {
  107. return $comments;
  108. }
  109. // @todo ZOMG this is a bad idea
  110. $comments = array_merge( $comments, $in_moderation_comments );
  111. usort( $comments, array( $this, 'sort_comments_by_comment_date_gmt' ) );
  112. return $comments;
  113. }
  114. /**
  115. * Comment sort comparator: comment_date_gmt
  116. *
  117. * @since JetpackComments (1.4)
  118. * @param object $a
  119. * @param object $b
  120. * @return int
  121. */
  122. public function sort_comments_by_comment_date_gmt( $a, $b ) {
  123. if ( $a->comment_date_gmt == $b->comment_date_gmt ) {
  124. return 0;
  125. }
  126. return $a->comment_date_gmt < $b->comment_date_gmt ? -1 : 1;
  127. }
  128. /**
  129. * Get the current commenter's information from their cookie
  130. *
  131. * @since JetpackComments (1.4)
  132. * @return array Commenters information from cookie
  133. */
  134. protected function get_current_commenter() {
  135. // Defaults
  136. $user_id = 0;
  137. $comment_author = '';
  138. $comment_author_email = '';
  139. $comment_author_url = '';
  140. if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) {
  141. $comment_author = $_COOKIE[ 'comment_author_' . COOKIEHASH ];
  142. }
  143. if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
  144. $comment_author_email = $_COOKIE[ 'comment_author_email_' . COOKIEHASH ];
  145. }
  146. if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) {
  147. $comment_author_url = $_COOKIE[ 'comment_author_url_' . COOKIEHASH ];
  148. }
  149. if ( is_user_logged_in() ) {
  150. $user = wp_get_current_user();
  151. $user_id = $user->ID;
  152. }
  153. return compact( 'comment_author', 'comment_author_email', 'comment_author_url', 'user_id' );
  154. }
  155. /**
  156. * Allows a logged out user to leave a comment as a facebook or twitter credentialed user.
  157. * Overrides WordPress' core comment_registration option to treat these commenters as "registered" (verified) users.
  158. *
  159. * @since JetpackComments (1.4)
  160. * @return If no
  161. */
  162. function allow_logged_out_user_to_comment_as_external() {
  163. if ( ! $this->is_highlander_comment_post( 'facebook', 'twitter', 'googleplus' ) ) {
  164. return;
  165. }
  166. add_filter( 'pre_option_comment_registration', '__return_zero' );
  167. add_filter( 'pre_option_require_name_email', '__return_zero' );
  168. }
  169. /**
  170. * Allow a logged in user to post as a guest, FB, or twitter credentialed request.
  171. * Bypasses WordPress' core overrides that force a logged in user to comment as that user.
  172. * Respects comment_registration option.
  173. *
  174. * @since JetpackComments (1.4)
  175. * @param array $comment_data
  176. * @return int
  177. */
  178. function allow_logged_in_user_to_comment_as_guest( $comment_data ) {
  179. // Bail if user registration is allowed
  180. if ( get_option( 'comment_registration' ) ) {
  181. return $comment_data;
  182. }
  183. // Bail if user is not logged in or not a post request
  184. if ( 'POST' != strtoupper( $_SERVER['REQUEST_METHOD'] ) || ! is_user_logged_in() ) {
  185. return $comment_data;
  186. }
  187. // Bail if this is not a guest or external service credentialed request
  188. if ( ! $this->is_highlander_comment_post( 'guest', 'facebook', 'twitter', 'googleplus' ) ) {
  189. return $comment_data;
  190. }
  191. $user = wp_get_current_user();
  192. foreach ( array(
  193. 'comment_author' => 'display_name',
  194. 'comment_author_email' => 'user_email',
  195. 'comment_author_url' => 'user_url',
  196. ) as $comment_field => $user_field ) {
  197. if ( $comment_data[ $comment_field ] != addslashes( $user->$user_field ) ) {
  198. return $comment_data; // some other plugin already did something funky
  199. }
  200. }
  201. if ( get_option( 'require_name_email' ) ) {
  202. if ( 6 > strlen( $_POST['email'] ) || empty( $_POST['author'] ) ) {
  203. wp_die( __( 'Error: please fill the required fields (name, email).', 'jetpack' ), 400 );
  204. } elseif ( ! is_email( $_POST['email'] ) ) {
  205. wp_die( __( 'Error: please enter a valid email address.', 'jetpack' ), 400 );
  206. }
  207. }
  208. $author_change = false;
  209. foreach ( array(
  210. 'comment_author' => 'author',
  211. 'comment_author_email' => 'email',
  212. 'comment_author_url' => 'url',
  213. ) as $comment_field => $post_field ) {
  214. if ( $comment_data[ $comment_field ] != $_POST[ $post_field ] && 'url' != $post_field ) {
  215. $author_change = true;
  216. }
  217. $comment_data[ $comment_field ] = $_POST[ $post_field ];
  218. }
  219. // Mark as guest comment if name or email were changed
  220. if ( $author_change ) {
  221. $comment_data['user_id'] = $comment_data['user_ID'] = 0;
  222. }
  223. return $comment_data;
  224. }
  225. /**
  226. * Set the comment cookies or bail if comment is invalid
  227. *
  228. * @since JetpackComments (1.4)
  229. * @param type $comment_id
  230. * @return If comment is invalid
  231. */
  232. public function set_comment_cookies( $comment_id ) {
  233. // Get comment and bail if it's invalid somehow
  234. $comment = get_comment( $comment_id );
  235. if ( empty( $comment ) || is_wp_error( $comment ) ) {
  236. return;
  237. }
  238. $id_source = $this->is_highlander_comment_post();
  239. if ( empty( $id_source ) ) {
  240. return;
  241. }
  242. // Set comment author cookies
  243. // phpcs:ignore WordPress.WP.CapitalPDangit
  244. if ( ( 'wordpress' != $id_source ) && is_user_logged_in() ) {
  245. /** This filter is already documented in core/wp-includes/comment-functions.php */
  246. $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
  247. setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN );
  248. setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN );
  249. setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN );
  250. }
  251. }
  252. /**
  253. * Get an avatar from Photon
  254. *
  255. * @since JetpackComments (1.4)
  256. * @param string $url
  257. * @param int $size
  258. * @return string
  259. */
  260. protected function photon_avatar( $url, $size ) {
  261. $size = (int) $size;
  262. return jetpack_photon_url( $url, array( 'resize' => "$size,$size" ) );
  263. }
  264. }