Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: add WP_REST_Posts_Controller route to fetch page counts #67719

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/67719
24 changes: 0 additions & 24 deletions lib/compat/wordpress-6.7/post-formats.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* REST API: Gutenberg_REST_Posts_Controller_6_8 class
*
* @package gutenberg
*/

/**
* Gutenberg_REST_Posts_Controller_6_8 class
*
* Adds a /counts route to return total posts count.
*/
class Gutenberg_REST_Posts_Controller_6_8 extends Gutenberg_REST_Posts_Controller_6_7 {
/**
* Registers the routes for attachments.
*
* @see register_rest_route()
*/
public function register_routes() {
parent::register_routes();

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/count',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_count' ),
'permission_callback' => array( $this, 'get_count_permissions_check' ),
),
'schema' => array( $this, 'get_count_schema' ),
)
);
}

/**
* Retrieves post counts for the post type.
*
* @since 6.8.0
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_count() {
$counts = wp_count_posts( $this->post_type );
$data = array();

if ( ! empty( $counts ) ) {
/*
* 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( $counts->$status ) ) {
$data[ $status ] = (int) $counts->$status;
}
}
}
return rest_ensure_response( $data );
}

/**
* Checks if a given request has access to read post counts.
*
* @since 6.8.0
*
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
*/
public function get_count_permissions_check() {
$post_type = get_post_type_object( $this->post_type );

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.' ),
array( 'status' => rest_authorization_required_code() )
);
}

return true;
}

/**
* Retrieves the post counts schema, conforming to JSON Schema.
*
* @since 6.8.0
*
* @return array Item schema data.
*/
public function get_count_schema() {
return array(
'$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,
);
}
}
24 changes: 24 additions & 0 deletions lib/compat/wordpress-6.8/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ function gutenberg_add_post_type_rendering_mode() {
}
add_action( 'rest_api_init', 'gutenberg_add_post_type_rendering_mode' );

if ( ! function_exists( 'gutenberg_rest_posts_controller_6_8' ) ) {
function gutenberg_rest_posts_controller_6_8( $args, $post_type ) {
/*
* This was carried over from the Gutenberg_REST_Posts_Controller_6_7 compatibility class,
* so when that is deleted, this condition block can be removed.
*/
if ( ! empty( $args['supports'] ) && in_array( 'post-formats', $args['supports'], true ) ) {
$args['rest_controller_class'] = 'Gutenberg_REST_Posts_Controller_6_8';
}

/*
* Gutenberg only supports page counts for now.
* In the Core sync, WP_REST_Posts_Controller will be extended
* to support this for all post types that use/inherit from it.
*/
if ( 'page' === $post_type ) {
$args['rest_controller_class'] = 'Gutenberg_REST_Posts_Controller_6_8';
}
return $args;
}
}

add_filter( 'register_post_type_args', 'gutenberg_rest_posts_controller_6_8', 10, 2 );

// When querying terms for a given taxonomy in the REST API, respect the default
// query arguments set for that taxonomy upon registration.
function gutenberg_respect_taxonomy_default_args_in_rest_api( $args ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.8/block-comments.php';
require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php';
require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php';
require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-posts-controller-6-8.php';
require __DIR__ . '/compat/wordpress-6.8/rest-api.php';

// Plugin specific code.
Expand Down Expand Up @@ -91,7 +92,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.7/script-modules.php';
require __DIR__ . '/compat/wordpress-6.7/class-wp-block-templates-registry.php';
require __DIR__ . '/compat/wordpress-6.7/compat.php';
require __DIR__ . '/compat/wordpress-6.7/post-formats.php';

// WordPress 6.8 compat.
require __DIR__ . '/compat/wordpress-6.8/preload.php';
Expand Down
Loading
Loading