wp-comments-post.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. /**
  31. * Perform other actions when comment cookies are set.
  32. *
  33. * @since 3.4.0
  34. *
  35. * @param WP_Comment $comment Comment object.
  36. * @param WP_User $user User object. The user may not exist.
  37. */
  38. do_action( 'set_comment_cookies', $comment, $user );
  39. $location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) : $_POST['redirect_to'] . '#comment-' . $comment->comment_ID;
  40. /**
  41. * Filters the location URI to send the commenter after posting.
  42. *
  43. * @since 2.0.5
  44. *
  45. * @param string $location The 'redirect_to' URI sent via $_POST.
  46. * @param WP_Comment $comment Comment object.
  47. */
  48. $location = apply_filters( 'comment_post_redirect', $location, $comment );
  49. wp_safe_redirect( $location );
  50. exit;