Skip to content

Commit fdad53e

Browse files
committed
Grouped merges for 5.0.12.
* REST API: Allow authors to read their own password protected posts. * About page update. Merges [50717] to the 5.0 branch. git-svn-id: https://develop.svn.wordpress.org/branches/5.0@50731 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 85f99cf commit fdad53e

File tree

3 files changed

+100
-7
lines changed

3 files changed

+100
-7
lines changed

src/wp-admin/about.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@
6262

6363
<div class="changelog point-releases">
6464
<h3><?php _e( 'Maintenance and Security Releases' ); ?></h3>
65+
<p>
66+
<?php
67+
printf(
68+
/* translators: %s: WordPress version number */
69+
__( '<strong>Version %s</strong> addressed some security issues.' ),
70+
'5.0.12'
71+
);
72+
?>
73+
<?php
74+
printf(
75+
/* translators: %s: HelpHub URL */
76+
__( 'For more information, see <a href="%s">the release notes</a>.' ),
77+
sprintf(
78+
/* translators: %s: WordPress version */
79+
esc_url( __( 'https://wordpress.org/support/wordpress-version/version-%s/' ) ),
80+
sanitize_title( '5.0.12' )
81+
)
82+
);
83+
?>
84+
</p>
6585
<p>
6686
<?php
6787
printf(

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
3232
*/
3333
protected $meta;
3434

35+
/**
36+
* Passwordless post access permitted.
37+
*
38+
* @since 5.7.1
39+
* @var int[]
40+
*/
41+
protected $password_check_passed = array();
42+
3543
/**
3644
* Constructor.
3745
*
@@ -137,6 +145,38 @@ public function get_items_permissions_check( $request ) {
137145
return true;
138146
}
139147

148+
/**
149+
* Override the result of the post password check for REST requested posts.
150+
*
151+
* Allow users to read the content of password protected posts if they have
152+
* previously passed a permission check or if they have the `edit_post` capability
153+
* for the post being checked.
154+
*
155+
* @since 5.7.1
156+
*
157+
* @param bool $required Whether the post requires a password check.
158+
* @param WP_Post $post The post been password checked.
159+
* @return bool Result of password check taking in to account REST API considerations.
160+
*/
161+
public function check_password_required( $required, $post ) {
162+
if ( ! $required ) {
163+
return $required;
164+
}
165+
166+
$post = get_post( $post );
167+
168+
if ( ! $post ) {
169+
return $required;
170+
}
171+
172+
if ( ! empty( $this->password_check_passed[ $post->ID ] ) ) {
173+
// Password previously checked and approved.
174+
return false;
175+
}
176+
177+
return ! current_user_can( 'edit_post', $post->ID );
178+
}
179+
140180
/**
141181
* Retrieves a collection of posts.
142182
*
@@ -292,7 +332,7 @@ public function get_items( $request ) {
292332

293333
// Allow access to all password protected posts if the context is edit.
294334
if ( 'edit' === $request['context'] ) {
295-
add_filter( 'post_password_required', '__return_false' );
335+
add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
296336
}
297337

298338
$posts = array();
@@ -308,7 +348,7 @@ public function get_items( $request ) {
308348

309349
// Reset filter.
310350
if ( 'edit' === $request['context'] ) {
311-
remove_filter( 'post_password_required', '__return_false' );
351+
remove_filter( 'post_password_required', array( $this, 'check_password_required' ) );
312352
}
313353

314354
$page = (int) $query_args['paged'];
@@ -406,7 +446,7 @@ public function get_item_permissions_check( $request ) {
406446

407447
// Allow access to all password protected posts if the context is edit.
408448
if ( 'edit' === $request['context'] ) {
409-
add_filter( 'post_password_required', '__return_false' );
449+
add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
410450
}
411451

412452
if ( $post ) {
@@ -434,8 +474,14 @@ public function can_access_password_content( $post, $request ) {
434474
return false;
435475
}
436476

437-
// Edit context always gets access to password-protected posts.
438-
if ( 'edit' === $request['context'] ) {
477+
/*
478+
* Users always gets access to password protected content in the edit
479+
* context if they have the `edit_post` meta capability.
480+
*/
481+
if (
482+
'edit' === $request['context'] &&
483+
current_user_can( 'edit_post', $post->ID )
484+
) {
439485
return true;
440486
}
441487

@@ -1507,8 +1553,9 @@ public function prepare_item_for_response( $post, $request ) {
15071553
$has_password_filter = false;
15081554

15091555
if ( $this->can_access_password_content( $post, $request ) ) {
1556+
$this->password_check_passed[ $post->ID ] = true;
15101557
// Allow access to the post, permissions already checked before.
1511-
add_filter( 'post_password_required', '__return_false' );
1558+
add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
15121559

15131560
$has_password_filter = true;
15141561
}
@@ -1535,7 +1582,7 @@ public function prepare_item_for_response( $post, $request ) {
15351582

15361583
if ( $has_password_filter ) {
15371584
// Reset filter.
1538-
remove_filter( 'post_password_required', '__return_false' );
1585+
remove_filter( 'post_password_required', array( $this, 'check_password_required' ) );
15391586
}
15401587

15411588
if ( in_array( 'author', $fields, true ) ) {

tests/phpunit/tests/rest-api/rest-posts-controller.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,32 @@ public function test_get_post_draft_status_not_authenicated() {
12231223
$this->assertErrorResponse( 'rest_forbidden', $response, 401 );
12241224
}
12251225

1226+
public function test_get_post_draft_edit_context() {
1227+
$post_content = 'Hello World!';
1228+
$this->factory->post->create(
1229+
array(
1230+
'post_title' => 'Hola',
1231+
'post_password' => 'password',
1232+
'post_content' => $post_content,
1233+
'post_excerpt' => $post_content,
1234+
'post_author' => self::$editor_id,
1235+
)
1236+
);
1237+
$draft_id = $this->factory->post->create(
1238+
array(
1239+
'post_status' => 'draft',
1240+
'post_author' => self::$contributor_id,
1241+
'post_content' => '<!-- wp:latest-posts {"displayPostContent":true} /--> <!-- wp:latest-posts {"displayPostContent":true,"displayPostContentRadio":"full_post"} /-->',
1242+
)
1243+
);
1244+
wp_set_current_user( self::$contributor_id );
1245+
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $draft_id ) );
1246+
$request->set_param( 'context', 'edit' );
1247+
$response = rest_get_server()->dispatch( $request );
1248+
$data = $response->get_data();
1249+
$this->assertNotContains( $post_content, $data['content']['rendered'] );
1250+
}
1251+
12261252
public function test_get_post_invalid_id() {
12271253
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
12281254
$response = $this->server->dispatch( $request );

0 commit comments

Comments
 (0)