wp-comments-post.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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' ) ) ) {
  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( dirname(__FILE__) . '/wp-load.php' );
  19. nocache_headers();
  20. $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
  21. if ( is_wp_error( $comment ) ) {
  22. $data = intval( $comment->get_error_data() );
  23. if ( ! empty( $data ) ) {
  24. wp_die( '<p>' . $comment->get_error_message() . '</p>', __( 'Comment Submission Failure' ), array( 'response' => $data, 'back_link' => true ) );
  25. } else {
  26. exit;
  27. }
  28. }
  29. $user = wp_get_current_user();
  30. $cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) );
  31. /**
  32. * Perform other actions when comment cookies are set.
  33. *
  34. * @since 3.4.0
  35. * @since 4.9.6 The `$cookies_consent` parameter was added.
  36. *
  37. * @param WP_Comment $comment Comment object.
  38. * @param WP_User $user Comment author's user object. The user may not exist.
  39. * @param boolean $cookies_consent Comment author's consent to store cookies.
  40. */
  41. do_action( 'set_comment_cookies', $comment, $user, $cookies_consent );
  42. $location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) : $_POST['redirect_to'] . '#comment-' . $comment->comment_ID;
  43. /**
  44. * Filters the location URI to send the commenter after posting.
  45. *
  46. * @since 2.0.5
  47. *
  48. * @param string $location The 'redirect_to' URI sent via $_POST.
  49. * @param WP_Comment $comment Comment object.
  50. */
  51. $location = apply_filters( 'comment_post_redirect', $location, $comment );
  52. wp_safe_redirect( $location );
  53. exit;