wp-comments-post.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Handles Comment Post to WordPress and prevents duplicate comment posting.
  4. *
  5. * @package WordPress
  6. */
  7. if ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) {
  8. $protocol = $_SERVER['SERVER_PROTOCOL'];
  9. if ( ! in_array( $protocol, array( 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0', 'HTTP/3' ), true ) ) {
  10. $protocol = 'HTTP/1.0';
  11. }
  12. header( 'Allow: POST' );
  13. header( "$protocol 405 Method Not Allowed" );
  14. header( 'Content-Type: text/plain' );
  15. exit;
  16. }
  17. /** Sets up the WordPress Environment. */
  18. require __DIR__ . '/wp-load.php';
  19. nocache_headers();
  20. $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
  21. if ( is_wp_error( $comment ) ) {
  22. $data = (int) $comment->get_error_data();
  23. if ( ! empty( $data ) ) {
  24. wp_die(
  25. '<p>' . $comment->get_error_message() . '</p>',
  26. __( 'Comment Submission Failure' ),
  27. array(
  28. 'response' => $data,
  29. 'back_link' => true,
  30. )
  31. );
  32. } else {
  33. exit;
  34. }
  35. }
  36. $user = wp_get_current_user();
  37. $cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) );
  38. /**
  39. * Perform other actions when comment cookies are set.
  40. *
  41. * @since 3.4.0
  42. * @since 4.9.6 The `$cookies_consent` parameter was added.
  43. *
  44. * @param WP_Comment $comment Comment object.
  45. * @param WP_User $user Comment author's user object. The user may not exist.
  46. * @param bool $cookies_consent Comment author's consent to store cookies.
  47. */
  48. do_action( 'set_comment_cookies', $comment, $user, $cookies_consent );
  49. $location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) : $_POST['redirect_to'] . '#comment-' . $comment->comment_ID;
  50. // If user didn't consent to cookies, add specific query arguments to display the awaiting moderation message.
  51. if ( ! $cookies_consent && 'unapproved' === wp_get_comment_status( $comment ) && ! empty( $comment->comment_author_email ) ) {
  52. $location = add_query_arg(
  53. array(
  54. 'unapproved' => $comment->comment_ID,
  55. 'moderation-hash' => wp_hash( $comment->comment_date_gmt ),
  56. ),
  57. $location
  58. );
  59. }
  60. /**
  61. * Filters the location URI to send the commenter after posting.
  62. *
  63. * @since 2.0.5
  64. *
  65. * @param string $location The 'redirect_to' URI sent via $_POST.
  66. * @param WP_Comment $comment Comment object.
  67. */
  68. $location = apply_filters( 'comment_post_redirect', $location, $comment );
  69. wp_safe_redirect( $location );
  70. exit;