| 1 |
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
/** |
| 3 |
* Jetpack comments base file - where the code shared between WP.com Highlander and Jetpack Highlander is defined |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
/** |
| 8 |
* All the code shared between WP.com Highlander and Jetpack Highlander |
| 9 |
*/ |
| 10 |
class Highlander_Comments_Base { |
| 11 |
|
| 12 |
/** |
| 13 |
* Constructor |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
$this->setup_globals(); |
| 17 |
$this->setup_actions(); |
| 18 |
$this->setup_filters(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Set any global variables or class variables |
| 23 |
* |
| 24 |
* @since JetpackComments (1.4) |
| 25 |
*/ |
| 26 |
protected function setup_globals() {} |
| 27 |
|
| 28 |
/** |
| 29 |
* Setup actions for methods in this class |
| 30 |
* |
| 31 |
* @since JetpackComments (1.4) |
| 32 |
*/ |
| 33 |
protected function setup_actions() { |
| 34 |
// Before a comment is posted. |
| 35 |
add_action( 'pre_comment_on_post', array( $this, 'allow_logged_out_user_to_comment_as_external' ) ); |
| 36 |
|
| 37 |
// After a comment is posted. |
| 38 |
add_action( 'comment_post', array( $this, 'set_comment_cookies' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Setup filters for methods in this class |
| 43 |
* |
| 44 |
* @since JetpackComments (1.4) |
| 45 |
*/ |
| 46 |
protected function setup_filters() { |
| 47 |
add_filter( 'comments_array', array( $this, 'comments_array' ) ); |
| 48 |
add_filter( 'preprocess_comment', array( $this, 'allow_logged_in_user_to_comment_as_guest' ), 0 ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Is this a Highlander POST request? |
| 53 |
* Optionally restrict to one or more credentials slug (facebook, twitter, ...) |
| 54 |
* |
| 55 |
* @param mixed ...$args Comments credentials slugs. |
| 56 |
* @return false|string false if it's not a Highlander POST request. The matching credentials slug if it is. |
| 57 |
*/ |
| 58 |
public function is_highlander_comment_post( ...$args ) { |
| 59 |
|
| 60 |
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verification should happen in Jetpack_Comments::pre_comment_on_post(). Internal ref for details: p1645643468937519/1645189749.180299-slack-C02HQGKMFJ8 |
| 61 |
if ( empty( $_POST['hc_post_as'] ) ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
$hc_post_as = wp_unslash( $_POST['hc_post_as'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized here by comparing against known values. |
| 65 |
// phpcs:enable WordPress.Security.NonceVerification.Missing |
| 66 |
|
| 67 |
if ( $args ) { |
| 68 |
foreach ( $args as $id_source ) { |
| 69 |
if ( $id_source === $hc_post_as ) { |
| 70 |
return $id_source; |
| 71 |
} |
| 72 |
} |
| 73 |
return false; |
| 74 |
} |
| 75 |
return is_string( $hc_post_as ) && in_array( $hc_post_as, $this->id_sources, true ) ? $hc_post_as : false; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Signs an array of scalars with the self-hosted blog's Jetpack Token |
| 80 |
* |
| 81 |
* If parameter values are not scalars a WP_Error is returned, otherwise a keyed hash value is returned using the HMAC method. |
| 82 |
* |
| 83 |
* @param array $parameters Comment parameters. |
| 84 |
* @param string $key Key used for generating the HMAC variant of the message digest. |
| 85 |
* @return string HMAC |
| 86 |
*/ |
| 87 |
public static function sign_remote_comment_parameters( $parameters, $key ) { |
| 88 |
unset( |
| 89 |
$parameters['sig'], // Don't sign the signature. |
| 90 |
$parameters['replytocom'] // This parameter is unsigned - it changes dynamically as the comment form moves from parent comment to parent comment. |
| 91 |
); |
| 92 |
|
| 93 |
ksort( $parameters ); |
| 94 |
|
| 95 |
$signing = array(); |
| 96 |
foreach ( $parameters as $k => $v ) { |
| 97 |
if ( ! is_scalar( $v ) ) { |
| 98 |
return new WP_Error( 'invalid_input', __( 'Invalid request', 'jetpack' ), array( 'status' => 400 ) ); |
| 99 |
} |
| 100 |
|
| 101 |
$signing[] = "{$k}={$v}"; |
| 102 |
} |
| 103 |
|
| 104 |
return hash_hmac( 'sha1', implode( ':', $signing ), $key ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Adds comment author email and whether the comment is approved to the comments array |
| 109 |
* |
| 110 |
* After commenting as a guest while logged in, the user needs to see both: |
| 111 |
* ( user_id = blah AND comment_approved = 0 ) |
| 112 |
* and ( comment_author_email = blah AND comment_approved = 0 ) |
| 113 |
* Core only does the first since the user is logged in, so this adds the second to the comments array. |
| 114 |
* |
| 115 |
* @param array $comments All comment data. |
| 116 |
* @return array A modified array of comment data. |
| 117 |
*/ |
| 118 |
public function comments_array( $comments ) { |
| 119 |
global $wpdb, $post; |
| 120 |
|
| 121 |
$commenter = $this->get_current_commenter(); |
| 122 |
|
| 123 |
if ( ! $commenter['user_id'] ) { |
| 124 |
return $comments; |
| 125 |
} |
| 126 |
|
| 127 |
if ( ! $commenter['comment_author'] ) { |
| 128 |
return $comments; |
| 129 |
} |
| 130 |
|
| 131 |
$in_moderation_comments = $wpdb->get_results( |
| 132 |
$wpdb->prepare( |
| 133 |
"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() */", |
| 134 |
$post->ID, |
| 135 |
wp_specialchars_decode( $commenter['comment_author'], ENT_QUOTES ), |
| 136 |
$commenter['comment_author_email'] |
| 137 |
) |
| 138 |
); |
| 139 |
|
| 140 |
if ( ! $in_moderation_comments ) { |
| 141 |
return $comments; |
| 142 |
} |
| 143 |
|
| 144 |
// @todo ZOMG this is a bad idea |
| 145 |
$comments = array_merge( $comments, $in_moderation_comments ); |
| 146 |
usort( $comments, array( $this, 'sort_comments_by_comment_date_gmt' ) ); |
| 147 |
|
| 148 |
return $comments; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Comment sort comparator: comment_date_gmt |
| 153 |
* |
| 154 |
* @since JetpackComments (1.4) |
| 155 |
* @param object $a The first comment to compare dates with. |
| 156 |
* @param object $b The second comment to compare dates with. |
| 157 |
* @return int |
| 158 |
*/ |
| 159 |
public function sort_comments_by_comment_date_gmt( $a, $b ) { |
| 160 |
if ( $a->comment_date_gmt === $b->comment_date_gmt ) { |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
return $a->comment_date_gmt < $b->comment_date_gmt ? -1 : 1; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get the current commenter's information from their cookie |
| 169 |
* |
| 170 |
* @since JetpackComments (1.4) |
| 171 |
* @return array Commenters information from cookie |
| 172 |
*/ |
| 173 |
protected function get_current_commenter() { |
| 174 |
// Defaults. |
| 175 |
$user_id = 0; |
| 176 |
$comment_author = ''; |
| 177 |
$comment_author_email = ''; |
| 178 |
$comment_author_url = ''; |
| 179 |
|
| 180 |
if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) { |
| 181 |
$comment_author = sanitize_text_field( wp_unslash( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ); |
| 182 |
} |
| 183 |
|
| 184 |
if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) { |
| 185 |
$comment_author_email = sanitize_email( wp_unslash( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ); |
| 186 |
} |
| 187 |
|
| 188 |
if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) { |
| 189 |
$comment_author_url = esc_url_raw( wp_unslash( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ); |
| 190 |
} |
| 191 |
|
| 192 |
if ( is_user_logged_in() ) { |
| 193 |
$user = wp_get_current_user(); |
| 194 |
$user_id = $user->ID; |
| 195 |
} |
| 196 |
|
| 197 |
return compact( 'comment_author', 'comment_author_email', 'comment_author_url', 'user_id' ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Allows a logged out user to leave a comment as a facebook or twitter credentialed user. |
| 202 |
* Overrides WordPress' core comment_registration option to treat these commenters as "registered" (verified) users. |
| 203 |
* |
| 204 |
* @since JetpackComments (1.4) |
| 205 |
*/ |
| 206 |
public function allow_logged_out_user_to_comment_as_external() { |
| 207 |
if ( ! $this->is_highlander_comment_post( 'facebook', 'twitter' ) ) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
add_filter( 'pre_option_comment_registration', '__return_zero' ); |
| 212 |
add_filter( 'pre_option_require_name_email', '__return_zero' ); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Allow a logged in user to post as a guest, FB, or twitter credentialed request. |
| 217 |
* Bypasses WordPress' core overrides that force a logged in user to comment as that user. |
| 218 |
* Respects comment_registration option. |
| 219 |
* |
| 220 |
* @since JetpackComments (1.4) |
| 221 |
* @param array $comment_data All data for a specific comment. |
| 222 |
* @return array Modified comment data, or an error if the required fields or a valid email address are not entered. |
| 223 |
*/ |
| 224 |
public function allow_logged_in_user_to_comment_as_guest( $comment_data ) { |
| 225 |
// Bail if user registration is allowed. |
| 226 |
if ( get_option( 'comment_registration' ) ) { |
| 227 |
return $comment_data; |
| 228 |
} |
| 229 |
|
| 230 |
// Bail if user is not logged in or not a post request. |
| 231 |
if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) || ! is_user_logged_in() ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- simple comparison |
| 232 |
return $comment_data; |
| 233 |
} |
| 234 |
|
| 235 |
// Bail if this is not a guest or external service credentialed request. |
| 236 |
if ( ! $this->is_highlander_comment_post( 'guest', 'facebook', 'twitter' ) ) { |
| 237 |
return $comment_data; |
| 238 |
} |
| 239 |
|
| 240 |
$user = wp_get_current_user(); |
| 241 |
|
| 242 |
foreach ( array( |
| 243 |
'comment_author' => 'display_name', |
| 244 |
'comment_author_email' => 'user_email', |
| 245 |
'comment_author_url' => 'user_url', |
| 246 |
) as $comment_field => $user_field ) { |
| 247 |
if ( addslashes( $user->$user_field ) !== $comment_data[ $comment_field ] ) { |
| 248 |
return $comment_data; // some other plugin already did something funky. |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verification should happen in Jetpack_Comments::pre_comment_on_post() |
| 253 |
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitization too |
| 254 |
if ( get_option( 'require_name_email' ) ) { |
| 255 |
if ( isset( $_POST['email'] ) && 6 > strlen( wp_unslash( $_POST['email'] ) ) || empty( $_POST['author'] ) ) { |
| 256 |
wp_die( esc_html__( 'Error: please fill the required fields (name, email).', 'jetpack' ), 400 ); |
| 257 |
} elseif ( ! isset( $_POST['email'] ) || ! is_email( wp_unslash( $_POST['email'] ) ) ) { |
| 258 |
wp_die( esc_html__( 'Error: please enter a valid email address.', 'jetpack' ), 400 ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
$author_change = false; |
| 263 |
foreach ( array( |
| 264 |
'comment_author' => 'author', |
| 265 |
'comment_author_email' => 'email', |
| 266 |
'comment_author_url' => 'url', |
| 267 |
) as $comment_field => $post_field ) { |
| 268 |
if ( ( ! isset( $_POST[ $post_field ] ) || $comment_data[ $comment_field ] !== $_POST[ $post_field ] ) && 'url' !== $post_field ) { |
| 269 |
$author_change = true; |
| 270 |
} |
| 271 |
$comment_data[ $comment_field ] = isset( $_POST[ $post_field ] ) ? wp_unslash( $_POST[ $post_field ] ) : null; |
| 272 |
} |
| 273 |
// phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 274 |
|
| 275 |
// Mark as guest comment if name or email were changed. |
| 276 |
if ( $author_change ) { |
| 277 |
$comment_data['user_ID'] = 0; |
| 278 |
$comment_data['user_id'] = $comment_data['user_ID']; |
| 279 |
} |
| 280 |
|
| 281 |
return $comment_data; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Set the comment cookies or bail if comment is invalid |
| 286 |
* |
| 287 |
* @since JetpackComments (1.4) |
| 288 |
* @param int $comment_id The comment ID. |
| 289 |
*/ |
| 290 |
public function set_comment_cookies( $comment_id ) { |
| 291 |
// Get comment and bail if it's invalid somehow. |
| 292 |
$comment = get_comment( $comment_id ); |
| 293 |
if ( empty( $comment ) || is_wp_error( $comment ) ) { |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
$id_source = $this->is_highlander_comment_post(); |
| 298 |
if ( empty( $id_source ) ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
// Set comment author cookies. |
| 303 |
// phpcs:ignore WordPress.WP.CapitalPDangit |
| 304 |
if ( ( 'wordpress' !== $id_source ) && is_user_logged_in() ) { |
| 305 |
/** This filter is already documented in core/wp-includes/comment-functions.php */ |
| 306 |
$comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 ); |
| 307 |
setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true ); |
| 308 |
setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true ); |
| 309 |
setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true ); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Get an avatar from Photon |
| 315 |
* |
| 316 |
* @since JetpackComments (1.4) |
| 317 |
* @param string $url The avatar URL. |
| 318 |
* @param int $size The avatar size. |
| 319 |
* @return string |
| 320 |
*/ |
| 321 |
protected function photon_avatar( $url, $size ) { |
| 322 |
$size = (int) $size; |
| 323 |
|
| 324 |
return jetpack_photon_url( $url, array( 'resize' => "$size,$size" ) ); |
| 325 |
} |
| 326 |
} |
| 327 |
|