<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[39015] trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: Docs: Add much more complete and syntactically correct documentation throughout the `WP_REST_Comments_Controller` class.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/39015">39015</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/39015","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>DrewAPicture</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-10-30 06:54:49 +0000 (Sun, 30 Oct 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Docs: Add much more complete and syntactically correct documentation throughout the `WP_REST_Comments_Controller` class.
Props Soean, mrahmadawais, flixos90.
See <a href="https://core.trac.wordpress.org/ticket/38398">#38398</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestcommentscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapiendpointsclasswprestcommentscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php 2016-10-30 06:33:34 UTC (rev 39014)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php 2016-10-30 06:54:49 UTC (rev 39015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,18 +1,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * REST API: WP_REST_Comments_Controller class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 4.7.0
+ */
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Access comments
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Core controller used to access comments via the REST API.
+ *
+ * @since 4.7.0
+ *
+ * @see WP_REST_Controller
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_REST_Comments_Controller extends WP_REST_Controller {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Instance of a comment meta fields object.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
</ins><span class="cx" style="display: block; padding: 0 10px"> * @access protected
</span><span class="cx" style="display: block; padding: 0 10px"> * @var WP_REST_Comment_Meta_Fields
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected $meta;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Constructor.
+ *
+ * @since 4.7.0
+ * @access public
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> public function __construct() {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->namespace = 'wp/v2';
</span><span class="cx" style="display: block; padding: 0 10px"> $this->rest_base = 'comments';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -21,7 +39,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Register the routes for the objects of the controller.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Registers the routes for the objects of the controller.
+ *
+ * @since 4.7.0
+ * @access public
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function register_routes() {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -72,16 +93,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if a given request has access to read comments
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a given request has access to read comments.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|bool True if the request has read access, error object otherwise.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_items_permissions_check( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $request['post'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( (array) $request['post'] as $post_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $post = $this->get_post( $post_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $post_id ) && $post && ! $this->check_read_post_permission( $post ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( 0 === $post_id && ! current_user_can( 'moderate_comments' ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -97,6 +122,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! current_user_can( 'edit_posts' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $protected_params = array( 'author', 'author_exclude', 'karma', 'author_email', 'type', 'status' );
</span><span class="cx" style="display: block; padding: 0 10px"> $forbidden_params = array();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $protected_params as $param ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'status' === $param ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'approve' !== $request[ $param ] ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -110,6 +136,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $forbidden_params[] = $param;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $forbidden_params ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_forbidden_param', sprintf( __( 'Query parameter not permitted: %s' ), implode( ', ', $forbidden_params ) ), array( 'status' => rest_authorization_required_code() ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -119,20 +146,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get a list of comments.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves a list of comment items.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|WP_REST_Response Response object on success, or error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_items( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Retrieve the list of registered collection query parameters.
</span><span class="cx" style="display: block; padding: 0 10px"> $registered = $this->get_collection_params();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // This array defines mappings between public API query parameters whose
- // values are accepted as-passed, and their internal WP_Query parameter
- // name equivalents (some are the same). Only values which are also
- // present in $registered will be set.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * This array defines mappings between public API query parameters whose
+ * values are accepted as-passed, and their internal WP_Query parameter
+ * name equivalents (some are the same). Only values which are also
+ * present in $registered will be set.
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> $parameter_mappings = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'author' => 'author__in',
</span><span class="cx" style="display: block; padding: 0 10px"> 'author_email' => 'author_email',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -153,8 +185,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // For each known parameter which is both registered and present in the request,
- // set the parameter's value on the query $prepared_args.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * For each known parameter which is both registered and present in the request,
+ * set the parameter's value on the query $prepared_args.
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $parameter_mappings as $api_param => $wp_param ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args[ $wp_param ] = $request[ $api_param ];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -175,6 +209,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args['no_found_rows'] = false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args['date_query'] = array();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Set before into date query. Date query must be specified as an array of an array.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $registered['before'], $request['before'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args['date_query'][0]['before'] = $request['before'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -190,10 +225,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Filter arguments, before passing to WP_Comment_Query, when querying comments via the REST API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Filters arguments, before passing to WP_Comment_Query, when querying comments via the REST API.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @see https://developer.wordpress.org/reference/classes/wp_comment_query/
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @link https://developer.wordpress.org/reference/classes/wp_comment_query/
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param array $prepared_args Array of arguments for WP_Comment_Query.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request The current request.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -203,6 +240,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $query_result = $query->query( $prepared_args );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $comments = array();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $query_result as $comment ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $this->check_read_permission( $comment ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -213,10 +251,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $total_comments = (int) $query->found_comments;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $max_pages = (int) $query->max_num_pages;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $max_pages = (int) $query->max_num_pages;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $total_comments < 1 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Out-of-bounds, run the query again without LIMIT for total count
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Out-of-bounds, run the query again without LIMIT for total count.
</ins><span class="cx" style="display: block; padding: 0 10px"> unset( $prepared_args['number'], $prepared_args['offset'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query = new WP_Comment_Query;
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args['count'] = true;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -229,17 +269,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $response->header( 'X-WP-TotalPages', $max_pages );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $request['page'] > 1 ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prev_page = $request['page'] - 1;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $prev_page > $max_pages ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prev_page = $max_pages;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $prev_link = add_query_arg( 'page', $prev_page, $base );
</span><span class="cx" style="display: block; padding: 0 10px"> $response->link_header( 'prev', $prev_link );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $max_pages > $request['page'] ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $next_page = $request['page'] + 1;
</span><span class="cx" style="display: block; padding: 0 10px"> $next_link = add_query_arg( 'page', $next_page, $base );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response->link_header( 'next', $next_link );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -247,10 +292,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if a given request has access to read the comment
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a given request has access to read the comment.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|bool True if the request has read access for the item, error object otherwise.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_item_permissions_check( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $id = (int) $request['id'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -279,10 +327,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get a comment.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves a comment.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|WP_REST_Response Response object on success, or error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_item( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $id = (int) $request['id'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -306,10 +357,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if a given request has access to create a comment
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a given request has access to create a comment.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|bool True if the request has access to create items, error object otherwise.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function create_item_permissions_check( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -321,9 +375,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_invalid_author', __( 'Comment author invalid.' ), array( 'status' => rest_authorization_required_code() ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['karma'] ) && $request['karma'] > 0 && ! current_user_can( 'moderate_comments' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_invalid_karma', __( 'Sorry, you cannot set karma for comments.' ), array( 'status' => rest_authorization_required_code() ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_invalid_status', __( 'Sorry, you cannot set status for comments.' ), array( 'status' => rest_authorization_required_code() ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -354,10 +410,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Create a comment.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Creates a comment.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|WP_REST_Response Response object on success, or error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function create_item( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $request['id'] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -365,26 +424,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment = $this->prepare_item_for_database( $request );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $prepared_comment ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $prepared_comment;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
</ins><span class="cx" style="display: block; padding: 0 10px"> * Do not allow a comment to be created with an empty string for
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * comment_content.
- * See `wp_handle_comment_submission()`.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * comment_content. See wp_handle_comment_submission().
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> if ( '' === $prepared_comment['comment_content'] ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_content_invalid', __( 'Comment content is invalid.' ), array( 'status' => 400 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Setting remaining values before wp_insert_comment so we can
- // use wp_allow_comment().
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Setting remaining values before wp_insert_comment so we can use wp_allow_comment().
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $prepared_comment['comment_date_gmt'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment['comment_date_gmt'] = current_time( 'mysql', true );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Set author data if the user's logged in
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Set author data if the user's logged in.
</ins><span class="cx" style="display: block; padding: 0 10px"> $missing_author = empty( $prepared_comment['user_id'] )
</span><span class="cx" style="display: block; padding: 0 10px"> && empty( $prepared_comment['comment_author'] )
</span><span class="cx" style="display: block; padding: 0 10px"> && empty( $prepared_comment['comment_author_email'] )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -392,21 +450,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_user_logged_in() && $missing_author ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $user = wp_get_current_user();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment['user_id'] = $user->ID;
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment['comment_author'] = $user->display_name;
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment['comment_author_email'] = $user->user_email;
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment['comment_author_url'] = $user->user_url;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Honor the discussion setting that requires a name and email address
- // of the comment author.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Honor the discussion setting that requires a name and email address of the comment author.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( get_option( 'require_name_email' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $prepared_comment['comment_author'] ) && ! isset( $prepared_comment['comment_author_email'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_author_data_required', __( 'Creating a comment requires valid author name and email values.' ), array( 'status' => 400 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $prepared_comment['comment_author'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_author_required', __( 'Creating a comment requires a valid author name.' ), array( 'status' => 400 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $prepared_comment['comment_author_email'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_author_email_required', __( 'Creating a comment requires a valid author email.' ), array( 'status' => 400 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -415,6 +475,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $prepared_comment['comment_author_email'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment['comment_author_email'] = '';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $prepared_comment['comment_author_url'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment['comment_author_url'] = '';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -426,7 +487,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment['comment_approved'] = wp_allow_comment( $prepared_comment, true );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $prepared_comment['comment_approved'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $error_code = $prepared_comment['comment_approved']->get_error_code();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $error_code = $prepared_comment['comment_approved']->get_error_code();
</ins><span class="cx" style="display: block; padding: 0 10px"> $error_message = $prepared_comment['comment_approved']->get_error_message();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'comment_duplicate' === $error_code ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -441,52 +502,65 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Filter a comment before it is inserted via the REST API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Filters a comment before it is inserted via the REST API.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Allows modification of the comment right before it is inserted via `wp_insert_comment`.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Allows modification of the comment right before it is inserted via wp_insert_comment().
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param array $prepared_comment The prepared comment data for `wp_insert_comment`.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ *
+ * @param array $prepared_comment The prepared comment data for wp_insert_comment().
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Request used to insert the comment.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment, $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $comment_id = wp_insert_comment( $prepared_comment );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $comment_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_failed_create', __( 'Creating comment failed.' ), array( 'status' => 500 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['status'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $comment = get_comment( $comment_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->handle_status_param( $request['status'], $comment );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $schema = $this->get_item_schema();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_update = $this->meta->update_value( $request['meta'], $comment_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $meta_update ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $meta_update;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $comment = get_comment( $comment_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $fields_update = $this->update_additional_fields_for_object( $comment, $request );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $fields_update ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $fields_update;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $context = current_user_can( 'moderate_comments' ) ? 'edit' : 'view';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'context', $context );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = $this->prepare_item_for_response( $comment, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_ensure_response( $response );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response->set_status( 201 );
</span><span class="cx" style="display: block; padding: 0 10px"> $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment_id ) ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Fires after a comment is created or updated via the REST API.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param array $comment Comment as it exists in the database.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request The request sent to the API.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param boolean $creating True when creating a comment, false when updating.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param bool $creating True when creating a comment, false when updating.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> do_action( 'rest_insert_comment', $comment, $request, true );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -494,10 +568,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if a given request has access to update a comment
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a given REST request has access to update a comment.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|bool True if the request has access to update the item, error object otherwise.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function update_item_permissions_check( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -513,15 +590,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Edit a comment
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Updates a comment.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|WP_REST_Response Response object on success, or error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function update_item( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $id = (int) $request['id'];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $comment = get_comment( $id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $comment ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -531,6 +612,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args = $this->prepare_item_for_database( $request );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $prepared_args ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $prepared_args;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -538,6 +620,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $prepared_args ) && isset( $request['status'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // Only the comment status is being changed.
</span><span class="cx" style="display: block; padding: 0 10px"> $change = $this->handle_status_param( $request['status'], $comment );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $change ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment status failed.' ), array( 'status' => 500 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -549,6 +632,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args['comment_ID'] = $id;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $updated = wp_update_comment( $prepared_args );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( 0 === $updated ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment failed.' ), array( 'status' => 500 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -559,20 +643,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $schema = $this->get_item_schema();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_update = $this->meta->update_value( $request['meta'], $id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $meta_update ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $meta_update;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $comment = get_comment( $id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $fields_update = $this->update_additional_fields_for_object( $comment, $request );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $fields_update ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $fields_update;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'context', 'edit' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = $this->prepare_item_for_response( $comment, $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /* This action is documented in lib/endpoints/class-wp-rest-comments-controller.php */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -582,17 +671,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if a given request has access to delete a comment
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a given request has access to delete a comment.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|bool True if the request has access to delete the item, error object otherwise.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function delete_item_permissions_check( $request ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $id = (int) $request['id'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $id = (int) $request['id'];
</ins><span class="cx" style="display: block; padding: 0 10px"> $comment = get_comment( $id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $comment ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $this->check_edit_permission( $comment ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you can not delete this comment.' ), array( 'status' => rest_authorization_required_code() ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -600,37 +694,44 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Delete a comment.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Deletes a comment.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|WP_REST_Response Response object on success, or error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function delete_item( $request ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $id = (int) $request['id'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $id = (int) $request['id'];
</ins><span class="cx" style="display: block; padding: 0 10px"> $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $comment = get_comment( $id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $comment ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Filter whether a comment is trashable.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Filters whether a comment can be trashed.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Return false to disable trash support for the post.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param boolean $supports_trash Whether the post type support trashing.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ *
+ * @param bool $supports_trash Whether the post type support trashing.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Post $comment The comment object being considered for trashing support.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $supports_trash = apply_filters( 'rest_comment_trashable', ( EMPTY_TRASH_DAYS > 0 ), $comment );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'context', 'edit' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = $this->prepare_item_for_response( $comment, $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $force ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $result = wp_delete_comment( $comment->comment_ID, true );
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // If we don't support trashing for this type, error out
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // If this type doesn't support trashing, error out.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $supports_trash ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_trash_not_supported', __( 'The comment does not support trashing.' ), array( 'status' => 501 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -649,7 +750,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Fires after a comment is deleted via the REST API.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param object $comment The deleted comment data.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ *
+ * @param WP_Comment $comment The deleted comment data.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Response $response The response returned from the API.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request The request sent to the API.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -659,11 +762,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Prepare a single comment output for response.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Prepares a single comment output for response.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param object $comment Comment object.
- * @param WP_REST_Request $request Request object.
- * @return WP_REST_Response $response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_Comment $comment Comment object.
+ * @param WP_REST_Request $request Request object.
+ * @return WP_REST_Response Response object.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function prepare_item_for_response( $comment, $request ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $data = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -679,6 +785,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'date' => mysql_to_rfc3339( $comment->comment_date ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'date_gmt' => mysql_to_rfc3339( $comment->comment_date_gmt ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'content' => array(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /** This filter is documented in wp-includes/comment-template.php */
</ins><span class="cx" style="display: block; padding: 0 10px"> 'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'raw' => $comment->comment_content,
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -699,30 +806,35 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $data = $this->add_additional_fields_to_object( $data, $request );
- $data = $this->filter_response_by_context( $data, $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $data = $this->add_additional_fields_to_object( $data, $request );
+ $data = $this->filter_response_by_context( $data, $context );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Wrap the data in a response object
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Wrap the data in a response object.
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_ensure_response( $data );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $response->add_links( $this->prepare_links( $comment ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Filter a comment returned from the API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Filters a comment returned from the API.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Allows modification of the comment right before it is returned.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Response $response The response object.
- * @param object $comment The original comment object.
- * @param WP_REST_Request $request Request used to generate the response.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ *
+ * @param WP_REST_Response $response The response object.
+ * @param WP_Comment $comment The original comment object.
+ * @param WP_REST_Request $request Request used to generate the response.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> return apply_filters( 'rest_prepare_comment', $response, $comment, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Prepare links for the request.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Prepares links for the request.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param object $comment Comment object.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
+ * @param WP_Comment $comment Comment object.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return array Links for the given comment.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function prepare_links( $comment ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -744,6 +856,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 0 !== (int) $comment->comment_post_ID ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $post = $this->get_post( $comment->comment_post_ID );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $post->ID ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $obj = get_post_type_object( $post->post_type );
</span><span class="cx" style="display: block; padding: 0 10px"> $base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -764,9 +877,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Only grab one comment to verify the comment has children.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $comment_children = $comment->get_children( array( 'number' => 1, 'count' => true ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $comment_children = $comment->get_children( array(
+ 'number' => 1,
+ 'count' => true
+ ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $comment_children ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $args = array( 'parent' => $comment->comment_ID );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $args = array(
+ 'parent' => $comment->comment_ID
+ );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $rest_url = add_query_arg( $args, rest_url( $this->namespace . '/' . $this->rest_base ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $links['children'] = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -778,10 +898,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Prepend internal property prefix to query parameters to match our response fields.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Prepends internal property prefix to query parameters to match our response fields.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $query_param
- * @return string $normalized
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
+ * @param string $query_param Query parameter.
+ * @return string The normalized query parameter.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function normalize_query_param( $query_param ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prefix = 'comment_';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -808,10 +931,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check comment_approved to set comment status for single comment output.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks comment_approved to set comment status for single comment output.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string|int $comment_approved
- * @return string $status
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
+ * @param string|int $comment_approved comment status.
+ * @return string Comment status.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function prepare_status_response( $comment_approved ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -837,15 +963,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Prepare a single comment to be inserted into the database.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Prepares a single comment to be inserted into the database.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Request object.
- * @return array|WP_Error $prepared_comment
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
+ * @param WP_REST_Request $request Request object.
+ * @return array|WP_Error Prepared comment, otherwise WP_Error object.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function prepare_item_for_database( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
</ins><span class="cx" style="display: block; padding: 0 10px"> * Allow the comment_content to be set via the 'content' or
</span><span class="cx" style="display: block; padding: 0 10px"> * the 'content.raw' properties of the Request object.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -865,6 +994,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['author'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $user = new WP_User( $request['author'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $user->exists() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment['user_id'] = $user->ID;
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_comment['comment_author'] = $user->display_name;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -918,18 +1048,30 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Require 'comment_content' unless only the 'comment_status' is being
- // updated.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Require 'comment_content' unless only the 'comment_status' is being updated.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $prepared_comment ) && ! isset( $prepared_comment['comment_content'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_comment_content_required', __( 'Missing comment content.' ), array( 'status' => 400 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters a comment after it is prepared for the database.
+ *
+ * Allows modification of the comment right after it is prepared for the database.
+ *
+ * @since 4.7.0
+ *
+ * @param array $prepared_comment The prepared comment data for `wp_insert_comment`.
+ * @param WP_REST_Request $request The current request.
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> return apply_filters( 'rest_preprocess_comment', $prepared_comment, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get the Comment's schema, conforming to JSON Schema
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the comment's schema, conforming to JSON Schema.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return array
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_item_schema() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1091,9 +1233,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get the query params for collections
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the query params for collections.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return array
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @return array Comments collection parameters.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_collection_params() {
</span><span class="cx" style="display: block; padding: 0 10px"> $query_params = parent::get_collection_params();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1106,16 +1251,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'format' => 'date-time',
</span><span class="cx" style="display: block; padding: 0 10px"> 'validate_callback' => 'rest_validate_request_arg',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['author'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to comments assigned to specific user ids. Requires authorization.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'wp_parse_id_list',
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['author_exclude'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Ensure result set excludes comments assigned to specific user ids. Requires authorization.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'wp_parse_id_list',
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['author_email'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => null,
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to that from a specific author email. Requires authorization.' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1123,24 +1271,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'sanitize_email',
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['before'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> 'format' => 'date-time',
</span><span class="cx" style="display: block; padding: 0 10px"> 'validate_callback' => 'rest_validate_request_arg',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['exclude'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Ensure result set excludes specific ids.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'wp_parse_id_list',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['include'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to specific ids.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'wp_parse_id_list',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['karma'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => null,
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to that of a particular comment karma. Requires authorization.' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1148,12 +1300,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'integer',
</span><span class="cx" style="display: block; padding: 0 10px"> 'validate_callback' => 'rest_validate_request_arg',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['offset'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Offset the result set by a specific number of comments.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'integer',
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'absint',
</span><span class="cx" style="display: block; padding: 0 10px"> 'validate_callback' => 'rest_validate_request_arg',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['order'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Order sort attribute ascending or descending.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1165,6 +1319,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'desc',
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['orderby'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Sort collection by object attribute.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1181,24 +1336,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type',
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['parent'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to resources of specific parent ids.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'wp_parse_id_list',
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['parent_exclude'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Ensure result set excludes specific parent ids.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'wp_parse_id_list',
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['post'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to resources assigned to specific post ids.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'wp_parse_id_list',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['status'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => 'approve',
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to comments assigned a specific status. Requires authorization.' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1206,6 +1365,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> 'validate_callback' => 'rest_validate_request_arg',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['type'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => 'comment',
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to comments assigned a specific type. Requires authorization.' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1213,15 +1373,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> 'validate_callback' => 'rest_validate_request_arg',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $query_params;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Set the comment_status of a given comment object when creating or updating a comment.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Sets the comment_status of a given comment object when creating or updating a comment.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string|int $new_status
- * @param object $comment
- * @return boolean $changed
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
+ * @param string|int $new_status New comment status.
+ * @param WP_Comment $comment Comment data.
+ * @return bool Whether the status was changed.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function handle_status_param( $new_status, $comment ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $old_status = wp_get_comment_status( $comment->comment_ID );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1261,12 +1425,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if we can read a post.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if the post can be read.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Correctly handles posts with the inherit status.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_Post $post Post Object.
- * @return boolean Can we read it?
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
+ * @param WP_Post $post Post Object.
+ * @return bool Whether post can be read.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function check_read_post_permission( $post ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $posts_controller = new WP_REST_Posts_Controller( $post->post_type );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1275,10 +1442,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if we can read a comment.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if the comment can be read.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param object $comment Comment object.
- * @return boolean Can we read it?
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
+ * @param WP_Comment $comment Comment object.
+ * @return bool Whether the comment can be read.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function check_read_permission( $comment ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $comment->comment_post_ID ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1306,10 +1476,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if we can edit or delete a comment.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a comment can be edited or deleted.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param object $comment Comment object.
- * @return boolean Can we edit or delete it?
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
+ * @param object $comment Comment object.
+ * @return bool Whether the comment can be edited or deleted.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function check_edit_permission( $comment ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 0 === (int) get_current_user_id() ) {
</span></span></pre>
</div>
</div>
</body>
</html>