Skip to content

Commit

Permalink
REST API: Remove post status prefix from REST API responses.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
youknowriad committed Jul 23, 2024
1 parent fc33ab6 commit edfc2b0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,12 @@ public function prepare_item_for_response( $post, $request ) {
}
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );

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

remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
}

if ( rest_is_field_included( 'settings', $fields ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,15 @@ public function prepare_item_for_response( $item, $request ) {

if ( rest_is_field_included( 'title.rendered', $fields ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );

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

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

remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
}

if ( rest_is_field_included( 'status', $fields ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1844,10 +1844,12 @@ public function prepare_item_for_response( $item, $request ) {
}
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );

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

remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
}

$has_password_filter = false;
Expand Down Expand Up @@ -2047,15 +2049,15 @@ public function prepare_item_for_response( $item, $request ) {
}

/**
* Overwrites the default protected title format.
* Overwrites the default protected and private title format.
*
* By default, WordPress will show password protected posts with a title of
* "Protected: %s", as the REST API communicates the protected status of a post
* in a machine-readable format, we remove the "Protected: " prefix.
* By default, WordPress will show password protected or private posts with a title of
* "Protected: %s" or "Private: %s", as the REST API communicates the status of a post
* in a machine-readable format, we remove the prefix.
*
* @since 4.7.0
*
* @return string Protected title format.
* @return string Title format.
*/
public function protected_title_format() {
return '%s';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ public function prepare_item( $id, array $fields ) {
if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
if ( post_type_supports( $post->post_type, 'title' ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
$data[ WP_REST_Search_Controller::PROP_TITLE ] = get_the_title( $post->ID );
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
} else {
$data[ WP_REST_Search_Controller::PROP_TITLE ] = '';
}
Expand Down Expand Up @@ -183,15 +185,15 @@ public function prepare_item_links( $id ) {
}

/**
* Overwrites the default protected title format.
* Overwrites the default protected and private title format.
*
* By default, WordPress will show password protected posts with a title of
* "Protected: %s". As the REST API communicates the protected status of a post
* in a machine-readable format, we remove the "Protected: " prefix.
* By default, WordPress will show password protected or private posts with a title of
* "Protected: %s" or "Private: %s", as the REST API communicates the status of a post
* in a machine-readable format, we remove the prefix.
*
* @since 5.0.0
*
* @return string Protected title format.
* @return string Title format.
*/
public function protected_title_format() {
return '%s';
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/includes/testcase-rest-post-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ protected function check_post_data( $post, $data, $context, $links ) {
// Check filtered values.
if ( post_type_supports( $post->post_type, 'title' ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
$this->assertSame( get_the_title( $post->ID ), $data['title']['rendered'] );
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
if ( 'edit' === $context ) {
$this->assertSame( $post->post_title, $data['title']['raw'] );
} else {
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestMenuItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,10 @@ protected function check_menu_item_data( $post, $data, $context, $links ) {
// Check filtered values.
if ( post_type_supports( self::POST_TYPE, 'title' ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
$this->assertSame( $post->title, $data['title']['rendered'] );
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
if ( 'edit' === $context ) {
$this->assertSame( $post->title, $data['title']['raw'] );
} else {
Expand Down

0 comments on commit edfc2b0

Please sign in to comment.