<!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>[39349] trunk: REST API: Add support for comments of password-protected posts.</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/39349">39349</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/39349","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>joehoyle</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-11-23 16:14:08 +0000 (Wed, 23 Nov 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'>REST API: Add support for comments of password-protected posts.

Core requires the post password to view and create comments on password protected posts, so we must support a ?\226?\128?\156password?\226?\128?\157 param on the comments endpoint when fetch comments for a specific post and creating a comment on a password protected post.

Props flixos90, jnylen0.
Fixes <a href="https://core.trac.wordpress.org/ticket/38692">#38692</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>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestcommentscontrollerphp">trunk/tests/phpunit/tests/rest-api/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-11-23 15:32:25 UTC (rev 39348)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php      2016-11-23 16:14:08 UTC (rev 39349)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -69,6 +69,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'permission_callback' => array( $this, 'get_item_permissions_check' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                'args'     => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'          => $this->get_context_param( array( 'default' => 'view' ) ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        'password' => array(
+                                               'description' => __( 'The password for the post if it is password protected.' ),
+                                               'type'        => 'string',
+                                       ),
</ins><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">                        array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -87,6 +91,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                'default'     => false,
</span><span class="cx" style="display: block; padding: 0 10px">                                                'description' => __( 'Whether to bypass trash and force deletion.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        'password' => array(
+                                               'description' => __( 'The password for the post if it is password protected.' ),
+                                               'type'        => 'string',
+                                       ),
</ins><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' => array( $this, 'get_public_item_schema' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -108,7 +116,7 @@
</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 = get_post( $post_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( ! empty( $post_id ) && $post && ! $this->check_read_post_permission( $post ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( ! empty( $post_id ) && $post && ! $this->check_read_post_permission( $post, $request ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                         return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you are not allowed to 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="cx" style="display: block; padding: 0 10px">                                        return new WP_Error( 'rest_cannot_read', __( 'Sorry, you are not allowed to read comments without a post.' ), array( 'status' => rest_authorization_required_code() ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -242,7 +250,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $comments = array();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $query_result as $comment ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( ! $this->check_read_permission( $comment ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! $this->check_read_permission( $comment, $request ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 continue;
</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">@@ -309,18 +317,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return 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">-                if ( ! $this->check_read_permission( $comment ) ) {
-                       return new WP_Error( 'rest_cannot_read', __( 'Sorry, you are not allowed to read this comment.' ), array( 'status' => rest_authorization_required_code() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! empty( $request['context'] ) && 'edit' === $request['context'] && ! current_user_can( 'moderate_comments' ) ) {
+                       return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit comments.' ), array( 'status' => rest_authorization_required_code() ) );
</ins><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">                $post = get_post( $comment->comment_post_ID );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $post && ! $this->check_read_post_permission( $post ) ) {
-                       return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you are not allowed to read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! $this->check_read_permission( $comment, $request ) ) {
+                       return new WP_Error( 'rest_cannot_read', __( 'Sorry, you are not allowed to read this comment.' ), array( 'status' => rest_authorization_required_code() ) );
</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">-                if ( ! empty( $request['context'] ) && 'edit' === $request['context'] && ! current_user_can( 'moderate_comments' ) ) {
-                       return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit comments.' ), array( 'status' => rest_authorization_required_code() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $post && ! $this->check_read_post_permission( $post, $request ) ) {
+                       return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you are not allowed to read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
</ins><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">                return true;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -433,7 +441,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return new WP_Error( 'rest_comment_trash_post', __( 'Sorry, you are not allowed to create a comment on this post.' ), array( 'status' => 403 ) );
</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">-                if ( ! $this->check_read_post_permission( $post ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! $this->check_read_post_permission( $post, $request ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you are not allowed to read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
</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">@@ -1412,6 +1420,11 @@
</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><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $query_params['password'] = array(
+                       'description' => __( 'The password for the post if it is password protected.' ),
+                       'type'        => 'string',
+               );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Filter collection parameters for the comments controller.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1481,18 +1494,36 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @access protected
</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.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param WP_Post         $post    Post object.
+        * @param WP_REST_Request $request Request data to check.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return bool Whether post can be read.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function check_read_post_permission( $post ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function check_read_post_permission( $post, $request ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $posts_controller = new WP_REST_Posts_Controller( $post->post_type );
</span><span class="cx" style="display: block; padding: 0 10px">                $post_type = get_post_type_object( $post->post_type );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $has_password_filter = false;
+
+               // Only check password if a specific post was queried for or a single comment
+               $requested_post = ! empty( $request['post'] ) && 1 === count( $request['post'] );
+               $requested_comment = ! empty( $request['id'] );
+               if ( ( $requested_post || $requested_comment ) && $posts_controller->can_access_password_content( $post, $request ) ) {
+                       add_filter( 'post_password_required', '__return_false' );
+
+                       $has_password_filter = true;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( post_password_required( $post ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return current_user_can( $post_type->cap->edit_post, $post->ID );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $result = current_user_can( $post_type->cap->edit_post, $post->ID );
+               } else {
+                       $result = $posts_controller->check_read_permission( $post );
</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">-                return $posts_controller->check_read_permission( $post );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $has_password_filter ) {
+                       remove_filter( 'post_password_required', '__return_false' );
+               }
+
+               return $result;
</ins><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="lines" style="display: block; padding: 0 10px; color: #888">@@ -1501,14 +1532,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @access protected
</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_Comment $comment Comment object.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param WP_Comment      $comment Comment object.
+        * @param WP_REST_Request $request Request data to check.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return bool Whether the comment can be read.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function check_read_permission( $comment ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function check_read_permission( $comment, $request ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! empty( $comment->comment_post_ID ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $post = get_post( $comment->comment_post_ID );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( $post ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( $this->check_read_post_permission( $post ) && 1 === (int) $comment->comment_approved ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( $this->check_read_post_permission( $post, $request ) && 1 === (int) $comment->comment_approved ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                         return true;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp"></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-posts-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-posts-controller.php       2016-11-23 15:32:25 UTC (rev 39348)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2016-11-23 16:14:08 UTC (rev 39349)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -383,13 +383,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * check in core with a filter.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @access protected
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @access public
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_Post         $post    Post to check against.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_REST_Request $request Request data to check.
</span><span class="cx" style="display: block; padding: 0 10px">         * @return bool True if the user can access password-protected content, otherwise false.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function can_access_password_content( $post, $request ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function can_access_password_content( $post, $request ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( empty( $post->post_password ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        // No filter required.
</span><span class="cx" style="display: block; padding: 0 10px">                        return false;
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestcommentscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php   2016-11-23 15:32:25 UTC (rev 39348)
+++ trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php     2016-11-23 16:14:08 UTC (rev 39349)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -146,6 +146,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'page',
</span><span class="cx" style="display: block; padding: 0 10px">                        'parent',
</span><span class="cx" style="display: block; padding: 0 10px">                        'parent_exclude',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'password',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'per_page',
</span><span class="cx" style="display: block; padding: 0 10px">                        'post',
</span><span class="cx" style="display: block; padding: 0 10px">                        'search',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -167,6 +168,69 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertCount( 7, $comments );
</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">+        /**
+        * @ticket 38692
+        */
+       public function test_get_items_with_password() {
+               wp_set_current_user( 0 );
+
+               $args = array(
+                       'comment_approved' => 1,
+                       'comment_post_ID'  => self::$password_id,
+               );
+               $password_comment = $this->factory->comment->create( $args );
+
+               $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
+               $request->set_param( 'password', 'toomanysecrets' );
+               $request->set_param( 'post', self::$password_id );
+
+               $response = $this->server->dispatch( $request );
+               $this->assertEquals( 200, $response->get_status() );
+
+               $collection_data = $response->get_data();
+               $this->assertTrue( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
+       }
+
+       /**
+        * @ticket 38692
+        */
+       public function test_get_items_with_password_without_post() {
+               wp_set_current_user( 0 );
+               $args = array(
+                       'comment_approved' => 1,
+                       'comment_post_ID'  => self::$password_id,
+               );
+               $password_comment = $this->factory->comment->create( $args );
+
+               $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
+               $request->set_param( 'password', 'toomanysecrets' );
+
+               $response = $this->server->dispatch( $request );
+               $this->assertEquals( 200, $response->get_status() );
+
+               $collection_data = $response->get_data();
+               $this->assertFalse( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
+       }
+
+       /**
+        * @ticket 38692
+        */
+       public function test_get_items_with_password_with_multiple_post() {
+               wp_set_current_user( 0 );
+               $args = array(
+                       'comment_approved' => 1,
+                       'comment_post_ID'  => self::$password_id,
+               );
+               $password_comment = $this->factory->comment->create( $args );
+
+               $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
+               $request->set_param( 'password', 'toomanysecrets' );
+               $request->set_param( 'post', array( self::$password_id, self::$post_id ) );
+
+               $response = $this->server->dispatch( $request );
+               $this->assertErrorResponse( 'rest_cannot_read_post', $response, 401 );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_get_password_items_without_edit_post_permission() {
</span><span class="cx" style="display: block; padding: 0 10px">                wp_set_current_user( 0 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -853,6 +917,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
</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">+        /**
+        * @ticket 38692
+        */
+       public function test_get_comment_with_password_with_valid_password() {
+               wp_set_current_user( self::$subscriber_id );
+
+               $args = array(
+                       'comment_approved' => 1,
+                       'comment_post_ID'  => self::$password_id,
+               );
+               $password_comment = $this->factory->comment->create( $args );
+
+               $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) );
+               $request->set_param( 'password', 'toomanysecrets' );
+
+               $response = $this->server->dispatch( $request );
+               $this->assertEquals( 200, $response->get_status() );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_create_item() {
</span><span class="cx" style="display: block; padding: 0 10px">                wp_set_current_user( self::$admin_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1727,6 +1810,44 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertErrorResponse( 'comment_content_column_length', $response, 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">+        public function test_create_comment_without_password() {
+               wp_set_current_user( self::$subscriber_id );
+
+               $params = array(
+                       'post'         => self::$password_id,
+                       'author_name'  => 'Bleeding Gums Murphy',
+                       'author_email' => 'murphy@gingivitis.com',
+                       'author_url'   => 'http://jazz.gingivitis.com',
+                       'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
+               );
+               $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
+
+               $request->add_header( 'content-type', 'application/json' );
+               $request->set_body( wp_json_encode( $params ) );
+               $response = $this->server->dispatch( $request );
+
+               $this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 );
+       }
+
+       public function test_create_comment_with_password() {
+               add_filter( 'rest_allow_anonymous_comments', '__return_true' );
+
+               $params = array(
+                       'post'         => self::$password_id,
+                       'author_name'  => 'Bleeding Gums Murphy',
+                       'author_email' => 'murphy@gingivitis.com',
+                       'author_url'   => 'http://jazz.gingivitis.com',
+                       'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
+                       'password'     => 'toomanysecrets',
+               );
+               $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
+
+               $request->add_header( 'content-type', 'application/json' );
+               $request->set_body( wp_json_encode( $params ) );
+               $response = $this->server->dispatch( $request );
+               $this->assertEquals( 201, $response->get_status() );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_update_item() {
</span><span class="cx" style="display: block; padding: 0 10px">                $post_id = $this->factory->post->create();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre>
</div>
</div>

</body>
</html>