Skip to content

Commit

Permalink
Backport log
Browse files Browse the repository at this point in the history
Sync with changes from Core backport
  • Loading branch information
ramonjd committed Nov 12, 2024
1 parent d15ce71 commit 35d277b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 60 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.8/7773.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/7773

* https://github.com/WordPress/gutenberg/pull/66294
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function get_item_permissions_check( $request ) {
);
}

if ( ! current_user_can( $post_type->cap->edit_posts ) ) {
if ( ! current_user_can( $post_type->cap->read ) ) {
return new WP_Error(
'rest_cannot_read',
__( 'Sorry, you are not allowed to read post counts for this post type.' ),
Expand Down Expand Up @@ -112,31 +112,32 @@ public function get_item( $request ) {
* @return WP_REST_Response Response object.
*/
public function prepare_item_for_response( $item, $request ) {
$data = array();
$fields = $this->get_fields_for_response( $request );
foreach ( $fields as $field ) {
if ( property_exists( $item, $field ) ) {
$data[ $field ] = (int) $item->$field;
$data = array();

if ( ! empty( $item ) ) {
/*
* The fields comprise all non-internal post statuses,
* including any custom statuses that may be registered.
* 'trash' is an exception, so if it exists, it is added separately.
*/
$post_stati = get_post_stati( array( 'internal' => false ) );

if ( get_post_status_object( 'trash' ) ) {
$post_stati[] = 'trash';
}
// Include all public statuses in the response if there is a count.
foreach ( $post_stati as $status ) {
if ( isset( $item->$status ) ) {
$data[ $status ] = (int) $item->$status;
}
}
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );

$response = rest_ensure_response( $data );

/**
* Filters post type counts data for the REST API.
* Allows modification of the post type counts data right before it is returned.
*
* @since 6.8.0
*
* @param WP_REST_Response $response The response object.
* @param object $item The original post counts object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'rest_prepare_post_counts', $response, $item, $request );
return rest_ensure_response( $data );
}

/**
Expand All @@ -151,32 +152,24 @@ public function get_item_schema() {
return $this->add_additional_fields_schema( $this->schema );
}

/*
* The fields comprise all non-internal post stati,
* including any custom statuses that may be registered.
* 'trash' is an exception, so if it exists, it is added separately.
*/
$post_statuses = get_post_stati( array( 'internal' => false ) );

if ( get_post_status_object( 'trash' ) ) {
$post_statuses[] = 'trash';
}
$schema_properties = array();
foreach ( $post_statuses as $post_status ) {
$schema_properties[ $post_status ] = array(
// translators: %s: Post status.
'description' => sprintf( __( 'The number of posts with the status %s.' ), $post_status ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
);
}

$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'post-counts',
'type' => 'object',
'properties' => $schema_properties,
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'post-counts',
'type' => 'object',
/*
* Use a pattern matcher for post status keys.
* This allows for custom post statuses to be included,
* which can be registered after the schema is generated.
*/
'patternProperties' => array(
'^\w+$' => array(
'description' => __( 'The number of posts for a given status.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
'additionalProperties' => false,
);

$this->schema = $schema;
Expand Down
49 changes: 33 additions & 16 deletions phpunit/class-gutenberg-rest-post-counts-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,17 @@ public function test_register_routes() {
$this->assertArrayHasKey( '/wp/v2/counts/(?P<post_type>[\w-]+)', $routes );
}

public function test_context_param() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/counts/post' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}

/**
* @covers Gutenberg_REST_Post_Counts_Controller::get_item_schema
*/
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/counts/post' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$properties = $data['schema']['patternProperties'];

$this->assertCount( 6, $properties );
$this->assertArrayHasKey( 'publish', $properties );
$this->assertArrayHasKey( 'future', $properties );
$this->assertArrayHasKey( 'draft', $properties );
$this->assertArrayHasKey( 'pending', $properties );
$this->assertArrayHasKey( 'private', $properties );
$this->assertArrayHasKey( 'trash', $properties );
$this->assertCount( 1, $properties );
$this->assertArrayHasKey( '^\w+$', $properties );
}

/**
Expand All @@ -110,6 +97,7 @@ public function test_get_item_response() {
*/
public function test_get_item() {
wp_set_current_user( self::$admin_id );
register_post_status( 'post_counts_status', array( 'public' => true ) );

$published = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$future = self::factory()->post->create(
Expand All @@ -122,6 +110,7 @@ public function test_get_item() {
$pending = self::factory()->post->create( array( 'post_status' => 'pending' ) );
$private = self::factory()->post->create( array( 'post_status' => 'private' ) );
$trashed = self::factory()->post->create( array( 'post_status' => 'trash' ) );
$custom = self::factory()->post->create( array( 'post_status' => 'post_counts_status' ) );

$request = new WP_REST_Request( 'GET', '/wp/v2/counts/post' );
$response = rest_get_server()->dispatch( $request );
Expand All @@ -133,13 +122,34 @@ public function test_get_item() {
$this->assertSame( 1, $data['pending'], 'Pending post count mismatch.' );
$this->assertSame( 1, $data['private'], 'Private post count mismatch.' );
$this->assertSame( 1, $data['trash'], 'Trashed post count mismatch.' );
$this->assertSame( 1, $data['post_counts_status'], 'Custom post count mismatch.' );

wp_delete_post( $published, true );
wp_delete_post( $future, true );
wp_delete_post( $draft, true );
wp_delete_post( $pending, true );
wp_delete_post( $private, true );
wp_delete_post( $trashed, true );
wp_delete_post( $custom, true );
unset( $GLOBALS['wp_post_statuses']['post_counts_status'] );
}

/**
* @covers WP_REST_Post_Counts_Controller::get_item
*/
public function test_get_item_with_sanitized_custom_post_status() {
wp_set_current_user( self::$admin_id );
register_post_status( '#<>post-me_AND9!', array( 'public' => true ) );

$custom = self::factory()->post->create( array( 'post_status' => 'post-me_and9' ) );
$request = new WP_REST_Request( 'GET', '/wp/v2/counts/post' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 1, $data['post-me_and9'], 'Custom post count mismatch.' );

wp_delete_post( $custom, true );
unset( $GLOBALS['wp_post_statuses']['post-me_and9'] );
}

/**
Expand Down Expand Up @@ -209,4 +219,11 @@ public function test_update_item() {
public function test_prepare_item() {
// Controller does not implement test_prepare_item().
}

/**
* @doesNotPerformAssertions
*/
public function test_context_param() {
// Controller does not implement context_param().
}
}

0 comments on commit 35d277b

Please sign in to comment.