Highlander_Comments_Base

All the code shared between WP.com Highlander and Jetpack Highlander.

Defined (1)

The class is defined in the following location(s).

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