Skip to content

Commit edfc2b0

Browse files
committed
REST API: Remove post status prefix from REST API responses.
When using the /posts or /pages endpoints, for private posts or pages, you get the following title property: { raw: "Some title", rendered: "Private: Some title" } this commit removes the prefix from rendered private posts titles (just like what we do for protected posts) Props youknowriad, swissspidy, timothyblynjacobs, sergeybiryukov, ramonopoly. Fixes #61639. git-svn-id: https://develop.svn.wordpress.org/trunk@58783 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fc33ab6 commit edfc2b0

File tree

6 files changed

+22
-10
lines changed

6 files changed

+22
-10
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,12 @@ public function prepare_item_for_response( $post, $request ) {
327327
}
328328
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
329329
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
330+
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
330331

331332
$data['title']['rendered'] = get_the_title( $post->ID );
332333

333334
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
335+
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
334336
}
335337

336338
if ( rest_is_field_included( 'settings', $fields ) ) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,15 @@ public function prepare_item_for_response( $item, $request ) {
510510

511511
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
512512
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
513+
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
513514

514515
/** This filter is documented in wp-includes/post-template.php */
515516
$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
516517

517518
$data['title']['rendered'] = $title;
518519

519520
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
521+
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
520522
}
521523

522524
if ( rest_is_field_included( 'status', $fields ) ) {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,10 +1844,12 @@ public function prepare_item_for_response( $item, $request ) {
18441844
}
18451845
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
18461846
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
1847+
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
18471848

18481849
$data['title']['rendered'] = get_the_title( $post->ID );
18491850

18501851
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
1852+
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
18511853
}
18521854

18531855
$has_password_filter = false;
@@ -2047,15 +2049,15 @@ public function prepare_item_for_response( $item, $request ) {
20472049
}
20482050

20492051
/**
2050-
* Overwrites the default protected title format.
2052+
* Overwrites the default protected and private title format.
20512053
*
2052-
* By default, WordPress will show password protected posts with a title of
2053-
* "Protected: %s", as the REST API communicates the protected status of a post
2054-
* in a machine-readable format, we remove the "Protected: " prefix.
2054+
* By default, WordPress will show password protected or private posts with a title of
2055+
* "Protected: %s" or "Private: %s", as the REST API communicates the status of a post
2056+
* in a machine-readable format, we remove the prefix.
20552057
*
20562058
* @since 4.7.0
20572059
*
2058-
* @return string Protected title format.
2060+
* @return string Title format.
20592061
*/
20602062
public function protected_title_format() {
20612063
return '%s';

src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ public function prepare_item( $id, array $fields ) {
132132
if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
133133
if ( post_type_supports( $post->post_type, 'title' ) ) {
134134
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
135+
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
135136
$data[ WP_REST_Search_Controller::PROP_TITLE ] = get_the_title( $post->ID );
136137
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
138+
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
137139
} else {
138140
$data[ WP_REST_Search_Controller::PROP_TITLE ] = '';
139141
}
@@ -183,15 +185,15 @@ public function prepare_item_links( $id ) {
183185
}
184186

185187
/**
186-
* Overwrites the default protected title format.
188+
* Overwrites the default protected and private title format.
187189
*
188-
* By default, WordPress will show password protected posts with a title of
189-
* "Protected: %s". As the REST API communicates the protected status of a post
190-
* in a machine-readable format, we remove the "Protected: " prefix.
190+
* By default, WordPress will show password protected or private posts with a title of
191+
* "Protected: %s" or "Private: %s", as the REST API communicates the status of a post
192+
* in a machine-readable format, we remove the prefix.
191193
*
192194
* @since 5.0.0
193195
*
194-
* @return string Protected title format.
196+
* @return string Title format.
195197
*/
196198
public function protected_title_format() {
197199
return '%s';

tests/phpunit/includes/testcase-rest-post-type-controller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ protected function check_post_data( $post, $data, $context, $links ) {
107107
// Check filtered values.
108108
if ( post_type_supports( $post->post_type, 'title' ) ) {
109109
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
110+
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
110111
$this->assertSame( get_the_title( $post->ID ), $data['title']['rendered'] );
111112
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
113+
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
112114
if ( 'edit' === $context ) {
113115
$this->assertSame( $post->post_title, $data['title']['raw'] );
114116
} else {

tests/phpunit/tests/rest-api/wpRestMenuItemsController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,10 @@ protected function check_menu_item_data( $post, $data, $context, $links ) {
862862
// Check filtered values.
863863
if ( post_type_supports( self::POST_TYPE, 'title' ) ) {
864864
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
865+
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
865866
$this->assertSame( $post->title, $data['title']['rendered'] );
866867
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
868+
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
867869
if ( 'edit' === $context ) {
868870
$this->assertSame( $post->title, $data['title']['raw'] );
869871
} else {

0 commit comments

Comments
 (0)