Ticket #33717: 33717.15.diff
| File 33717.15.diff, 14.7 KB (added by , 6 years ago) |
|---|
-
src/wp-comments-post.php
22 22 23 23 nocache_headers(); 24 24 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 ) ) { 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 { 29 31 wp_die( 30 '<p>' . $comment->get_error_message() . '</p>',31 __( 'Comment Submission Failure' ),32 '<p>' . __( 'Invalid comment ID.' ) . '</p>', 33 __( 'Comment Notification Opt-in Failure' ), 32 34 array( 33 'response' => $data,35 'response' => 404, 34 36 'back_link' => true, 35 37 ) 36 38 ); 37 } else {38 exit;39 39 } 40 } 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 } 41 57 42 $user = wp_get_current_user();43 $cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) );58 $user = wp_get_current_user(); 59 $cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) ); 44 60 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 ); 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 } 56 73 57 74 $location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) : $_POST['redirect_to'] . '#comment-' . $comment->comment_ID; 58 75 -
src/wp-includes/class-walker-comment.php
41 41 ); 42 42 43 43 /** 44 * Backward compability checks. 45 * 46 * @since 5.7.0 47 * 48 * @var bool[] 49 */ 50 public $backcompat = array( 51 'needs_comment_approval_notification_output' => true, 52 ); 53 54 /** 44 55 * Starts the list before the elements are added. 45 56 * 46 57 * @since 2.7.0 … … 259 270 * to the comment cookies. 260 271 * 261 272 * @since 5.4.2 273 * @since 5.7.0 Adds backcompatibilty output if needed. 262 274 * 263 275 * @param string $comment_text Text of the current comment. 264 276 * @param WP_Comment|null $comment The comment object. Null if not found. … … 272 284 $comment_text = wp_kses( $comment_text, array() ); 273 285 } 274 286 287 /* 288 * Checks if we need to output the comment approval notification/form. 289 */ 290 if ( $this->backcompat['needs_comment_approval_notification_output'] ) { 291 $comment_text = $this->comment_approval_notification_form( $comment, true ) . "\n" . $comment_text; 292 } 293 275 294 return $comment_text; 276 295 } 277 296 278 297 /** 298 * Outputs the awaiting moderation text. 299 * 300 * @since 5.7.0 301 * 302 * @param WP_Comment $comment Comment to display. 303 */ 304 protected function awaiting_moderation_text( $comment ) { 305 if ( '0' !== $comment->comment_approved ) { 306 return; 307 } 308 309 $commenter = wp_get_current_commenter(); 310 311 if ( $commenter['comment_author_email'] ) { 312 $moderation_note = __( 'Your comment is awaiting moderation.' ); 313 } else { 314 $moderation_note = __( 'Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.' ); 315 } 316 317 printf( 318 '<em class="comment-awaiting-moderation">%s</em>', 319 esc_html( $moderation_note ) 320 ); 321 } 322 323 /** 324 * Gets the comment's approval notification or form for opting in. 325 * 326 * @since 5.7.0 327 * 328 * @param WP_Comment $comment Comment to display. 329 * @param bool $skip_backcompat True to skip the backward compability output. 330 * False otherwise. 331 * @return string HTML output. 332 */ 333 protected function comment_approval_notification_form( $comment, $skip_backcompat = false ) { 334 $comment_approval_output = ''; 335 336 if ( '0' === $comment->comment_approved && has_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' ) ) { 337 if ( get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) { 338 $comment_approval_output = sprintf( 339 '<p><em class="wp-comment-approved-notification-optedin">%s</em></p>', 340 esc_html__( 'You will receive an email when your comment is approved.' ) 341 ); 342 } else { 343 $comment_approval_output = sprintf( 344 '<form action="%1$s" method="post"> 345 <p> 346 <label for="wp-comment-approved-notification-optin"> 347 <input type="checkbox" id="wp-comment-approved-notification-optin" name="wp-comment-approved-notification-optin"> 348 %2$s 349 </label> 350 </p> 351 <input type="hidden" name="comment_ID" value="%3$s"> 352 <input type="hidden" name="moderation-hash" value="%4$s"> 353 <input type="submit" class="button" value="%5$s"> 354 </form>', 355 esc_url( site_url( '/wp-comments-post.php' ) ), 356 esc_html__( 'I want to be notified by email when my comment is approved.' ), 357 absint( $comment->comment_ID ), 358 wp_hash( $comment->comment_date_gmt ), 359 esc_html_x( 'Save', 'comment approval notification form' ) 360 ); 361 } 362 } 363 364 // Skip the backcompat output. 365 if ( $skip_backcompat ) { 366 $this->backcompat['needs_comment_approval_notification_output'] = false; 367 } 368 369 // Return the approval notification/form. 370 return $comment_approval_output; 371 } 372 373 /** 279 374 * Outputs a single comment. 280 375 * 281 376 * @since 3.6.0 … … 297 392 298 393 $commenter = wp_get_current_commenter(); 299 394 $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 }306 395 ?> 307 396 <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>"> 308 397 <?php if ( 'div' !== $args['style'] ) : ?> … … 328 417 ); 329 418 ?> 330 419 </div> 331 <?php if ( '0' == $comment->comment_approved ) : ?>332 <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>333 <br />334 <?php endif; ?>335 420 421 <?php 422 // Output the comment moderation feedback if needed. 423 $this->awaiting_moderation_text( $comment ); 424 425 // Output the comment approval notification form if needed. 426 echo $this->comment_approval_notification_form( $comment, true ); 427 ?> 428 336 429 <div class="comment-meta commentmetadata"> 337 430 <?php 338 431 printf( … … 401 494 402 495 $commenter = wp_get_current_commenter(); 403 496 $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 }410 497 ?> 411 498 <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>> 412 499 <article id="div-comment-<?php comment_ID(); ?>" class="comment-body"> … … 450 537 ?> 451 538 </div><!-- .comment-metadata --> 452 539 453 <?php if ( '0' == $comment->comment_approved ) : ?> 454 <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em> 455 <?php endif; ?> 540 <?php 541 // Output the comment moderation feedback if needed. 542 $this->awaiting_moderation_text( $comment ); 543 544 // Output the comment approval notification form if needed. 545 echo $this->comment_approval_notification_form( $comment, true ); 546 ?> 456 547 </footer><!-- .comment-meta --> 457 548 458 549 <div class="comment-content"> -
src/wp-includes/comment.php
2349 2349 } 2350 2350 2351 2351 /** 2352 * Notifies the comment author when their comment gets approved. 2353 * 2354 * This notification is only sent once when the comment status 2355 * changes from unapproved to approved. 2356 * 2357 * @since 5.7.0 2358 * 2359 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. 2360 * @return bool Whether the email was sent. 2361 */ 2362 function wp_new_comment_notify_comment_author( $comment_id ) { 2363 $comment = get_comment( $comment_id ); 2364 2365 if ( ! $comment ) { 2366 return false; 2367 } 2368 2369 $post = get_post( $comment->comment_post_ID ); 2370 2371 if ( ! $post ) { 2372 return false; 2373 } 2374 2375 // Make sure the comment author can be notified by email. 2376 if ( empty( $comment->comment_author_email ) ) { 2377 return false; 2378 } 2379 2380 if ( ! get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) { 2381 return false; 2382 } 2383 2384 /** 2385 * The blogname option is escaped with esc_html when 2386 * saved into the database, we need to reverse this for 2387 * the plain text area of the email. 2388 */ 2389 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 2390 2391 $subject = sprintf( 2392 /* translators: 1: blog name, 2: post title */ 2393 __( '[%1$s] Your comment on "%2$s" has been approved' ), 2394 $blogname, 2395 $post->post_title 2396 ); 2397 2398 if ( ! empty( $comment->comment_author ) ) { 2399 $notify_message = sprintf( 2400 /* translators: 1: comment author's name */ 2401 __( 'Howdy %s,' ), 2402 $comment->comment_author 2403 ) . "\r\n\r\n"; 2404 } else { 2405 $notify_message = __( 'Howdy,' ) . "\r\n\r\n"; 2406 } 2407 2408 $notify_message .= sprintf( 2409 /* translators: 1: post title */ 2410 __( 'Your comment on "%s" has been approved.' ), 2411 $post->post_title 2412 ) . "\r\n\r\n"; 2413 2414 $notify_message .= sprintf( 2415 /* translators: 1: comment permalink */ 2416 __( 'View comment: %s' ), 2417 get_comment_link( $comment ) 2418 ) . "\r\n"; 2419 2420 $email = array( 2421 'to' => $comment->comment_author_email, 2422 'subject' => $subject, 2423 'message' => $notify_message, 2424 'headers' => '', 2425 ); 2426 2427 /** 2428 * Filters the contents of the email sent to notify a comment author that their comment was approved. 2429 * 2430 * Content should be formatted for transmission via wp_mail(). 2431 * 2432 * @since 5.7.0 2433 * 2434 * @param array $email { 2435 * Used to build wp_mail(). 2436 * 2437 * @type string $to The email address of the comment author. 2438 * @type string $subject The subject of the email. 2439 * @type string $message The content of the email. 2440 * @type string $headers Headers. 2441 * } 2442 * @param WP_Comment $comment Comment object. 2443 */ 2444 $email = apply_filters( 'comment_approval_notification', $email, $comment ); 2445 2446 $sent = wp_mail( 2447 $email['to'], 2448 wp_specialchars_decode( $email['subject'] ), 2449 $email['message'], 2450 $email['headers'] 2451 ); 2452 2453 // Delete the opt-in now the notification has been sent. 2454 delete_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin' ); 2455 2456 return $sent; 2457 } 2458 2459 /** 2352 2460 * Sets the status of a comment. 2353 2461 * 2354 2462 * The {@see 'wp_set_comment_status'} action is called after the comment is handled. -
src/wp-includes/default-filters.php
467 467 add_action( 'after_password_reset', 'wp_password_change_notification' ); 468 468 add_action( 'register_new_user', 'wp_send_new_user_notifications' ); 469 469 add_action( 'edit_user_created_user', 'wp_send_new_user_notifications', 10, 2 ); 470 add_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' ); 470 471 471 472 // REST API actions. 472 473 add_action( 'init', 'rest_api_init' ); -
tests/phpunit/tests/comment.php
555 555 } 556 556 557 557 /** 558 * @ticket 33717 559 */ 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 33717 587 */ 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 } 608 609 /** 558 610 * @ticket 12431 559 611 */ 560 612 public function test_wp_new_comment_with_meta() {
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)