wp/wp-comments-post.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 11:14:17 +0000
changeset 6 490d5cc509ed
parent 5 5e2f62d02dcd
child 7 cf61fcea0001
permissions -rw-r--r--
update portfolio
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Handles Comment Post to WordPress and prevents duplicate comment posting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
	header('Allow: POST');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
	header('HTTP/1.1 405 Method Not Allowed');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
	header('Content-Type: text/plain');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
	exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
/** Sets up the WordPress Environment. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
require( dirname(__FILE__) . '/wp-load.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
nocache_headers();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
$comment_post_ID = isset($_POST['comment_post_ID']) ? (int) $_POST['comment_post_ID'] : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
$post = get_post($comment_post_ID);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
if ( empty( $post->comment_status ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	 * Fires when a comment is attempted on a post that does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    28
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
	 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
	 * @param int $comment_post_ID Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
	do_action( 'comment_id_not_found', $comment_post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
	exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
// get_post_status() will get the parent status for attachments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
$status = get_post_status($post);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
$status_obj = get_post_status_object($status);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
if ( ! comments_open( $comment_post_ID ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	 * Fires when a comment is attempted on a post that has comments closed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
	 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	 * @param int $comment_post_ID Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	do_action( 'comment_closed', $comment_post_ID );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    50
	wp_die( __( 'Sorry, comments are closed for this item.' ), 403 );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
} elseif ( 'trash' == $status ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
	 * Fires when a comment is attempted on a trashed post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
	 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
	 * @param int $comment_post_ID Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	do_action( 'comment_on_trash', $comment_post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
} elseif ( ! $status_obj->public && ! $status_obj->private ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
	 * Fires when a comment is attempted on a post in draft mode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
	 * @since 1.5.1
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
	 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	 * @param int $comment_post_ID Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	do_action( 'comment_on_draft', $comment_post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
} elseif ( post_password_required( $comment_post_ID ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
	 * Fires when a comment is attempted on a password-protected post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    75
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
	 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
	 * @param int $comment_post_ID Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
	do_action( 'comment_on_password_protected', $comment_post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	 * Fires before a comment is posted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
	 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
	 * @param int $comment_post_ID Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
	do_action( 'pre_comment_on_post', $comment_post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
$comment_author       = ( isset($_POST['author']) )  ? trim(strip_tags($_POST['author'])) : null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
$comment_author_email = ( isset($_POST['email']) )   ? trim($_POST['email']) : null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
$comment_author_url   = ( isset($_POST['url']) )     ? trim($_POST['url']) : null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
$comment_content      = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
// If the user is logged in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
$user = wp_get_current_user();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
if ( $user->exists() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
	if ( empty( $user->display_name ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
		$user->display_name=$user->user_login;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	$comment_author       = wp_slash( $user->display_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
	$comment_author_email = wp_slash( $user->user_email );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
	$comment_author_url   = wp_slash( $user->user_url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	if ( current_user_can( 'unfiltered_html' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		if ( ! isset( $_POST['_wp_unfiltered_html_comment'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
			|| ! wp_verify_nonce( $_POST['_wp_unfiltered_html_comment'], 'unfiltered-html-comment_' . $comment_post_ID )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
			kses_remove_filters(); // start with a clean slate
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
			kses_init_filters(); // set up the filters
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
	if ( get_option( 'comment_registration' ) || 'private' == $status ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
		wp_die( __( 'Sorry, you must be logged in to post a comment.' ), 403 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
$comment_type = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
if ( get_option('require_name_email') && !$user->exists() ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   122
	if ( 6 > strlen( $comment_author_email ) || '' == $comment_author ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   123
		wp_die( __( '<strong>ERROR</strong>: please fill the required fields (name, email).' ), 200 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   124
	} elseif ( ! is_email( $comment_author_email ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   125
		wp_die( __( '<strong>ERROR</strong>: please enter a valid email address.' ), 200 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   126
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   129
if ( '' == $comment_content ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   130
	wp_die( __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   131
}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
$comment_parent = isset($_POST['comment_parent']) ? absint($_POST['comment_parent']) : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
$comment_id = wp_new_comment( $commentdata );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   138
if ( ! $comment_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   139
	wp_die( __( "<strong>ERROR</strong>: The comment could not be saved. Please try again later." ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   142
$comment = get_comment( $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
 * Perform other actions when comment cookies are set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
 * @param object $comment Comment object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
 * @param WP_User $user   User object. The user may not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
do_action( 'set_comment_cookies', $comment, $user );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
$location = empty($_POST['redirect_to']) ? get_comment_link($comment_id) : $_POST['redirect_to'] . '#comment-' . $comment_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
 * Filter the location URI to send the commenter after posting.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
 * @since 2.0.5
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
 * @param string $location The 'redirect_to' URI sent via $_POST.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
 * @param object $comment  Comment object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
$location = apply_filters( 'comment_post_redirect', $location, $comment );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
wp_safe_redirect( $location );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
exit;