wp-comments-post.php 2.23 KB
Newer Older
godog's avatar
godog committed
1 2 3 4 5 6 7 8
<?php
/**
 * Handles Comment Post to WordPress and prevents duplicate comment posting.
 *
 * @package WordPress
 */

if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
lucha's avatar
lucha committed
9 10 11 12 13
	$protocol = $_SERVER['SERVER_PROTOCOL'];
	if ( ! in_array( $protocol, array( 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0' ) ) ) {
		$protocol = 'HTTP/1.0';
	}

samba's avatar
samba committed
14 15 16
	header( 'Allow: POST' );
	header( "$protocol 405 Method Not Allowed" );
	header( 'Content-Type: text/plain' );
godog's avatar
godog committed
17 18 19 20
	exit;
}

/** Sets up the WordPress Environment. */
samba's avatar
samba committed
21
require( dirname( __FILE__ ) . '/wp-load.php' );
godog's avatar
godog committed
22 23 24

nocache_headers();

lechuck's avatar
lechuck committed
25 26 27 28
$comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
if ( is_wp_error( $comment ) ) {
	$data = intval( $comment->get_error_data() );
	if ( ! empty( $data ) ) {
samba's avatar
samba committed
29 30 31 32 33 34 35 36
		wp_die(
			'<p>' . $comment->get_error_message() . '</p>',
			__( 'Comment Submission Failure' ),
			array(
				'response'  => $data,
				'back_link' => true,
			)
		);
lechuck's avatar
lechuck committed
37 38
	} else {
		exit;
lechuck's avatar
lechuck committed
39
	}
godog's avatar
godog committed
40 41
}

samba's avatar
samba committed
42
$user            = wp_get_current_user();
kiki's avatar
kiki committed
43
$cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) );
lucha's avatar
lucha committed
44 45 46 47 48

/**
 * Perform other actions when comment cookies are set.
 *
 * @since 3.4.0
kiki's avatar
kiki committed
49
 * @since 4.9.6 The `$cookies_consent` parameter was added.
lucha's avatar
lucha committed
50
 *
kiki's avatar
kiki committed
51 52 53
 * @param WP_Comment $comment         Comment object.
 * @param WP_User    $user            Comment author's user object. The user may not exist.
 * @param boolean    $cookies_consent Comment author's consent to store cookies.
lucha's avatar
lucha committed
54
 */
kiki's avatar
kiki committed
55
do_action( 'set_comment_cookies', $comment, $user, $cookies_consent );
godog's avatar
godog committed
56

lechuck's avatar
lechuck committed
57
$location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) : $_POST['redirect_to'] . '#comment-' . $comment->comment_ID;
lucha's avatar
lucha committed
58

samba's avatar
samba committed
59 60 61 62 63 64 65 66 67 68 69
// Add specific query arguments to display the awaiting moderation message.
if ( 'unapproved' === wp_get_comment_status( $comment ) && ! empty( $comment->comment_author_email ) ) {
	$location = add_query_arg(
		array(
			'unapproved'      => $comment->comment_ID,
			'moderation-hash' => wp_hash( $comment->comment_date_gmt ),
		),
		$location
	);
}

lucha's avatar
lucha committed
70
/**
lucha's avatar
lucha committed
71
 * Filters the location URI to send the commenter after posting.
lucha's avatar
lucha committed
72
 *
lechuck's avatar
lechuck committed
73
 * @since 2.0.5
lucha's avatar
lucha committed
74
 *
lechuck's avatar
lechuck committed
75 76
 * @param string     $location The 'redirect_to' URI sent via $_POST.
 * @param WP_Comment $comment  Comment object.
lucha's avatar
lucha committed
77 78
 */
$location = apply_filters( 'comment_post_redirect', $location, $comment );
godog's avatar
godog committed
79

lechuck's avatar
lechuck committed
80
wp_safe_redirect( $location );
root's avatar
root committed
81
exit;