Changeset 50375
- Timestamp:
- 02/17/2021 01:06:43 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-comments-post.php
r50109 r50375 23 23 nocache_headers(); 24 24 25 if ( isset( $_POST['wp-comment-approved-notification-optin'], $_POST['comment_ID'], $_POST['moderation-hash'] ) ) { 26 $comment = get_comment( $_POST['comment_ID'] ); 27 28 if ( $comment && hash_equals( $_POST['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) { 29 update_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ); 30 } else { 25 $comment = wp_handle_comment_submission( wp_unslash( $_POST ) ); 26 if ( is_wp_error( $comment ) ) { 27 $data = (int) $comment->get_error_data(); 28 if ( ! empty( $data ) ) { 31 29 wp_die( 32 '<p>' . __( 'Invalid comment ID.') . '</p>',33 __( 'Comment Notification Opt-in Failure' ),30 '<p>' . $comment->get_error_message() . '</p>', 31 __( 'Comment Submission Failure' ), 34 32 array( 35 'response' => 404,33 'response' => $data, 36 34 'back_link' => true, 37 35 ) 38 36 ); 37 } else { 38 exit; 39 39 } 40 } else { 41 $comment = wp_handle_comment_submission( wp_unslash( $_POST ) ); 42 if ( is_wp_error( $comment ) ) { 43 $data = (int) $comment->get_error_data(); 44 if ( ! empty( $data ) ) { 45 wp_die( 46 '<p>' . $comment->get_error_message() . '</p>', 47 __( 'Comment Submission Failure' ), 48 array( 49 'response' => $data, 50 'back_link' => true, 51 ) 52 ); 53 } else { 54 exit; 55 } 56 } 40 } 57 41 58 59 42 $user = wp_get_current_user(); 43 $cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) ); 60 44 61 /** 62 * Perform other actions when comment cookies are set. 63 * 64 * @since 3.4.0 65 * @since 4.9.6 The `$cookies_consent` parameter was added. 66 * 67 * @param WP_Comment $comment Comment object. 68 * @param WP_User $user Comment author's user object. The user may not exist. 69 * @param bool $cookies_consent Comment author's consent to store cookies. 70 */ 71 do_action( 'set_comment_cookies', $comment, $user, $cookies_consent ); 72 } 45 /** 46 * Perform other actions when comment cookies are set. 47 * 48 * @since 3.4.0 49 * @since 4.9.6 The `$cookies_consent` parameter was added. 50 * 51 * @param WP_Comment $comment Comment object. 52 * @param WP_User $user Comment author's user object. The user may not exist. 53 * @param bool $cookies_consent Comment author's consent to store cookies. 54 */ 55 do_action( 'set_comment_cookies', $comment, $user, $cookies_consent ); 73 56 74 57 $location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) : $_POST['redirect_to'] . '#comment-' . $comment->comment_ID; -
trunk/src/wp-includes/class-walker-comment.php
r50109 r50375 40 40 'id' => 'comment_ID', 41 41 ); 42 43 /**44 * Whether the comment approval notification opt-in form or message needs to be45 * output automatically.46 *47 * @since 5.7.048 *49 * @var bool50 */51 protected $needs_comment_approval_notification_output = true;52 42 53 43 /** … … 266 256 * Filters the comment text. 267 257 * 268 * - Removes links from the pending comment's text if the commenter did not consent 269 * to the comment cookies 270 * - Prepends the approval notification opt-in form or message to pending comments 258 * Removes links from the pending comment's text if the commenter did not consent 259 * to the comment cookies. 271 260 * 272 261 * @since 5.4.2 273 * @since 5.7.0 Comment approval notification opt-in form is now automatically274 * appended if necessary.275 262 * 276 263 * @param string $comment_text Text of the current comment. … … 286 273 } 287 274 288 /*289 * Checks if we need to output the comment approval notification opt-in form.290 */291 if ( $this->needs_comment_approval_notification_output ) {292 $comment_text = $this->comment_approval_notification_form( $comment ) . "\n" . $comment_text;293 }294 295 $this->needs_comment_approval_notification_output = true;296 297 275 return $comment_text; 298 }299 300 /**301 * Outputs the awaiting moderation text.302 *303 * @since 5.7.0304 *305 * @param WP_Comment $comment Comment to display.306 */307 protected function awaiting_moderation_text( $comment ) {308 if ( '0' !== $comment->comment_approved ) {309 return;310 }311 312 $commenter = wp_get_current_commenter();313 314 if ( $commenter['comment_author_email'] ) {315 $moderation_note = __( 'Your comment is awaiting moderation.' );316 } else {317 $moderation_note = __( 'Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.' );318 }319 320 printf(321 '<em class="comment-awaiting-moderation">%s</em>',322 esc_html( $moderation_note )323 );324 }325 326 /**327 * Gets the comment approval notification opt-in form or message for a pending comment.328 *329 * @since 5.7.0330 *331 * @param WP_Comment $comment Comment to display.332 * @return string HTML output.333 */334 protected function comment_approval_notification_form( $comment ) {335 $comment_approval_output = '';336 337 if ( '0' === $comment->comment_approved && has_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' ) ) {338 if ( get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) {339 $comment_approval_output = sprintf(340 '<p><em class="wp-comment-approved-notification-optedin">%s</em></p>',341 esc_html__( 'You will receive an email when your comment is approved.' )342 );343 } else {344 $comment_approval_output = sprintf(345 '<form action="%1$s" method="post">346 <p>347 <input type="checkbox" id="wp-comment-approved-notification-optin" name="wp-comment-approved-notification-optin">348 <label for="wp-comment-approved-notification-optin">349 %2$s350 </label>351 </p>352 <input type="hidden" name="comment_ID" value="%3$s">353 <input type="hidden" name="moderation-hash" value="%4$s">354 <input type="submit" class="button" value="%5$s">355 </form>',356 esc_url( site_url( '/wp-comments-post.php' ) ),357 esc_html__( 'I want to be notified by email when my comment is approved.' ),358 absint( $comment->comment_ID ),359 wp_hash( $comment->comment_date_gmt ),360 esc_html_x( 'Save', 'comment approval notification form' )361 );362 }363 }364 365 // Disable the backcompat output.366 $this->needs_comment_approval_notification_output = false;367 368 // Return the approval notification opt-in form.369 return $comment_approval_output;370 276 } 371 277 … … 392 298 $commenter = wp_get_current_commenter(); 393 299 $show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author']; 300 301 if ( $commenter['comment_author_email'] ) { 302 $moderation_note = __( 'Your comment is awaiting moderation.' ); 303 } else { 304 $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' ); 305 } 394 306 ?> 395 307 <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>"> … … 417 329 ?> 418 330 </div> 419 420 <?php 421 // Output the comment moderation feedback if needed. 422 $this->awaiting_moderation_text( $comment ); 423 424 // Output the comment approval notification opt-in form if needed. 425 echo $this->comment_approval_notification_form( $comment ); 426 ?> 331 <?php if ( '0' == $comment->comment_approved ) : ?> 332 <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em> 333 <br /> 334 <?php endif; ?> 427 335 428 336 <div class="comment-meta commentmetadata"> … … 494 402 $commenter = wp_get_current_commenter(); 495 403 $show_pending_links = ! empty( $commenter['comment_author'] ); 404 405 if ( $commenter['comment_author_email'] ) { 406 $moderation_note = __( 'Your comment is awaiting moderation.' ); 407 } else { 408 $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' ); 409 } 496 410 ?> 497 411 <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>> … … 537 451 </div><!-- .comment-metadata --> 538 452 539 <?php 540 // Output the comment moderation feedback if needed. 541 $this->awaiting_moderation_text( $comment ); 542 543 // Output the comment approval notification opt-in form if needed. 544 echo $this->comment_approval_notification_form( $comment ); 545 ?> 453 <?php if ( '0' == $comment->comment_approved ) : ?> 454 <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em> 455 <?php endif; ?> 546 456 </footer><!-- .comment-meta --> 547 457 -
trunk/src/wp-includes/comment.php
r50271 r50375 2352 2352 2353 2353 /** 2354 * Notifies the comment author when their comment gets approved.2355 *2356 * This notification is only sent once when the comment status2357 * changes from unapproved to approved.2358 *2359 * @since 5.7.02360 *2361 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.2362 * @return bool Whether the email was sent.2363 */2364 function wp_new_comment_notify_comment_author( $comment_id ) {2365 $comment = get_comment( $comment_id );2366 2367 if ( ! $comment ) {2368 return false;2369 }2370 2371 $post = get_post( $comment->comment_post_ID );2372 2373 if ( ! $post ) {2374 return false;2375 }2376 2377 // Make sure the comment author can be notified by email.2378 if ( empty( $comment->comment_author_email ) ) {2379 return false;2380 }2381 2382 if ( ! get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) {2383 return false;2384 }2385 2386 /**2387 * The blogname option is escaped with esc_html when2388 * saved into the database, we need to reverse this for2389 * the plain text area of the email.2390 */2391 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );2392 2393 $subject = sprintf(2394 /* translators: 1: Blog name, 2: Post title. */2395 __( '[%1$s] Your comment on "%2$s" has been approved' ),2396 $blogname,2397 $post->post_title2398 );2399 2400 if ( ! empty( $comment->comment_author ) ) {2401 $notify_message = sprintf(2402 /* translators: %s: Comment author's name. */2403 __( 'Howdy %s,' ),2404 $comment->comment_author2405 ) . "\r\n\r\n";2406 } else {2407 $notify_message = __( 'Howdy,' ) . "\r\n\r\n";2408 }2409 2410 $notify_message .= sprintf(2411 /* translators: %s: Post title. */2412 __( 'Your comment on "%s" has been approved.' ),2413 $post->post_title2414 ) . "\r\n\r\n";2415 2416 $notify_message .= sprintf(2417 /* translators: %s: Comment permalink. */2418 __( 'View comment: %s' ),2419 get_comment_link( $comment )2420 ) . "\r\n";2421 2422 $email = array(2423 'to' => $comment->comment_author_email,2424 'subject' => $subject,2425 'message' => $notify_message,2426 'headers' => '',2427 );2428 2429 /**2430 * Filters the contents of the email sent to notify a comment author that their comment was approved.2431 *2432 * Content should be formatted for transmission via wp_mail().2433 *2434 * @since 5.7.02435 *2436 * @param array $email {2437 * Used to build wp_mail().2438 *2439 * @type string $to The email address of the comment author.2440 * @type string $subject The subject of the email.2441 * @type string $message The content of the email.2442 * @type string $headers Headers.2443 * }2444 * @param WP_Comment $comment Comment object.2445 */2446 $email = apply_filters( 'comment_approval_notification', $email, $comment );2447 2448 $sent = wp_mail(2449 $email['to'],2450 wp_specialchars_decode( $email['subject'] ),2451 $email['message'],2452 $email['headers']2453 );2454 2455 // Delete the opt-in now the notification has been sent.2456 delete_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin' );2457 2458 return $sent;2459 }2460 2461 /**2462 2354 * Sets the status of a comment. 2463 2355 * -
trunk/src/wp-includes/default-filters.php
r50371 r50375 479 479 add_action( 'register_new_user', 'wp_send_new_user_notifications' ); 480 480 add_action( 'edit_user_created_user', 'wp_send_new_user_notifications', 10, 2 ); 481 add_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' );482 481 483 482 // REST API actions. -
trunk/tests/phpunit/tests/comment.php
r50113 r50375 553 553 self::$notify_message = $notify_message; 554 554 return $notify_message; 555 }556 557 /**558 * @ticket 33717559 */560 public function test_wp_new_comment_notify_comment_author_once_only() {561 $c = self::factory()->comment->create(562 array(563 'comment_post_ID' => self::$post_id,564 'comment_approved' => '0',565 'comment_author_email' => 'foo@bar.mail',566 )567 );568 569 // The comment author subscribed to receive an email once comment is approved.570 update_comment_meta( $c, '_wp_comment_author_notification_optin', true );571 572 // For the purpose of the test we are removing this hook to directly use the function to notify the comment author.573 remove_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' );574 575 // Approve the comment.576 wp_set_comment_status( $c, 'approve' );577 578 $sent = wp_new_comment_notify_comment_author( $c );579 $resent = wp_new_comment_notify_comment_author( $c );580 581 $this->assertTrue( $sent );582 $this->assertFalse( $resent );583 }584 585 /**586 * @ticket 33717587 */588 public function test_wp_new_comment_notify_comment_author_has_not_opted_in() {589 $c = self::factory()->comment->create(590 array(591 'comment_post_ID' => self::$post_id,592 'comment_approved' => '0',593 'comment_author_email' => 'bat@man.mail',594 )595 );596 597 // For the purpose of the test we are removing this hook to directly use the function to notify the comment author.598 remove_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' );599 600 // Approve the comment.601 wp_set_comment_status( $c, 'approve' );602 603 $sent = wp_new_comment_notify_comment_author( $c );604 605 // The comment author hasn't subscribed to receive an email, no email should be sent.606 $this->assertFalse( $sent );607 555 } 608 556
Note: See TracChangeset
for help on using the changeset viewer.