diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-09-19 04:51:18 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-09-19 04:51:18 +0000 |
commit | 0e41b5d52fdc6af6442816b5f465c9db9f84e126 (patch) | |
tree | e139a90049b158d4eed892d1662ee7f5c358fa31 /wp-includes/rest-api/endpoints | |
parent | Adding upstream version 6.5.5+dfsg1. (diff) | |
download | wordpress-upstream.tar.xz wordpress-upstream.zip |
Adding upstream version 6.6.1+dfsg1.upstream/6.6.1+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'wp-includes/rest-api/endpoints')
14 files changed, 346 insertions, 361 deletions
diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 804d22a..cbc62b4 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -254,10 +254,21 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { $files = $request->get_file_params(); $headers = $request->get_headers(); + $time = null; + + // Matches logic in media_handle_upload(). + if ( ! empty( $request['post'] ) ) { + $post = get_post( $request['post'] ); + // The post date doesn't usually matter for pages, so don't backdate this upload. + if ( $post && 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) { + $time = $post->post_date; + } + } + if ( ! empty( $files ) ) { - $file = $this->upload_from_file( $files, $headers ); + $file = $this->upload_from_file( $files, $headers, $time ); } else { - $file = $this->upload_from_data( $request->get_body(), $headers ); + $file = $this->upload_from_data( $request->get_body(), $headers, $time ); } if ( is_wp_error( $file ) ) { @@ -293,6 +304,17 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { $attachment->post_mime_type = $type; $attachment->guid = $url; + // If the title was not set, use the original filename. + if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) { + // Remove the file extension (after the last `.`) + $tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) ); + + if ( ! empty( $tmp_title ) ) { + $attachment->post_title = $tmp_title; + } + } + + // Fall back to the original approach. if ( empty( $attachment->post_title ) ) { $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); } @@ -599,12 +621,12 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { case 'crop': $size = $image_editor->get_size(); - $crop_x = round( ( $size['width'] * $args['left'] ) / 100.0 ); - $crop_y = round( ( $size['height'] * $args['top'] ) / 100.0 ); - $width = round( ( $size['width'] * $args['width'] ) / 100.0 ); - $height = round( ( $size['height'] * $args['height'] ) / 100.0 ); + $crop_x = (int) round( ( $size['width'] * $args['left'] ) / 100.0 ); + $crop_y = (int) round( ( $size['height'] * $args['top'] ) / 100.0 ); + $width = (int) round( ( $size['width'] * $args['width'] ) / 100.0 ); + $height = (int) round( ( $size['height'] * $args['height'] ) / 100.0 ); - if ( $size['width'] !== $width && $size['height'] !== $height ) { + if ( $size['width'] !== $width || $size['height'] !== $height ) { $result = $image_editor->crop( $crop_x, $crop_y, $width, $height ); if ( is_wp_error( $result ) ) { @@ -1035,12 +1057,14 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { * Handles an upload via raw POST data. * * @since 4.7.0 + * @since 6.6.0 Added the `$time` parameter. * - * @param string $data Supplied file data. - * @param array $headers HTTP headers from the request. + * @param string $data Supplied file data. + * @param array $headers HTTP headers from the request. + * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. * @return array|WP_Error Data from wp_handle_sideload(). */ - protected function upload_from_data( $data, $headers ) { + protected function upload_from_data( $data, $headers, $time = null ) { if ( empty( $data ) ) { return new WP_Error( 'rest_upload_no_data', @@ -1128,7 +1152,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 'test_form' => false, ); - $sideloaded = wp_handle_sideload( $file_data, $overrides ); + $sideloaded = wp_handle_sideload( $file_data, $overrides, $time ); if ( isset( $sideloaded['error'] ) ) { @unlink( $tmpfname ); @@ -1246,12 +1270,14 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { * Handles an upload via multipart/form-data ($_FILES). * * @since 4.7.0 + * @since 6.6.0 Added the `$time` parameter. * - * @param array $files Data from the `$_FILES` superglobal. - * @param array $headers HTTP headers from the request. + * @param array $files Data from the `$_FILES` superglobal. + * @param array $headers HTTP headers from the request. + * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. * @return array|WP_Error Data from wp_handle_upload(). */ - protected function upload_from_file( $files, $headers ) { + protected function upload_from_file( $files, $headers, $time = null ) { if ( empty( $files ) ) { return new WP_Error( 'rest_upload_no_data', @@ -1293,7 +1319,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { // Include filesystem functions to get access to wp_handle_upload(). require_once ABSPATH . 'wp-admin/includes/file.php'; - $file = wp_handle_upload( $files['file'], $overrides ); + $file = wp_handle_upload( $files['file'], $overrides, $time ); if ( isset( $file['error'] ) ) { return new WP_Error( diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php index d8f0839..065b863 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php @@ -162,6 +162,12 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function prepare_item_for_response( $item, $request ) { + // Resolve pattern blocks so they don't need to be resolved client-side + // in the editor, improving performance. + $blocks = parse_blocks( $item['content'] ); + $blocks = resolve_pattern_blocks( $blocks ); + $item['content'] = serialize_blocks( $blocks ); + $fields = $this->get_fields_for_response( $request ); $keys = array( 'name' => 'name', diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php index c448787..ec59baa 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php @@ -18,7 +18,7 @@ class WP_REST_Font_Faces_Controller extends WP_REST_Posts_Controller { * @since 6.5.0 * @var int */ - const LATEST_THEME_JSON_VERSION_SUPPORTED = 2; + const LATEST_THEME_JSON_VERSION_SUPPORTED = 3; /** * Whether the controller supports batching. diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php index 184b42d..c2d70d0 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php @@ -20,7 +20,7 @@ class WP_REST_Font_Families_Controller extends WP_REST_Posts_Controller { * @since 6.5.0 * @var int */ - const LATEST_THEME_JSON_VERSION_SUPPORTED = 2; + const LATEST_THEME_JSON_VERSION_SUPPORTED = 3; /** * Whether the controller supports batching. diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php index 9837f53..0cb54c9 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php @@ -10,24 +10,24 @@ /** * Base Global Styles REST API Controller. */ -class WP_REST_Global_Styles_Controller extends WP_REST_Controller { - +class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller { /** - * Post type. + * Whether the controller supports batching. * - * @since 5.9.0 - * @var string + * @since 6.6.0 + * @var array */ - protected $post_type; + protected $allow_batch = array( 'v1' => false ); /** * Constructor. - * @since 5.9.0 + * + * @since 6.6.0 + * + * @param string $post_type Post type. */ - public function __construct() { - $this->namespace = 'wp/v2'; - $this->rest_base = 'global-styles'; - $this->post_type = 'wp_global_styles'; + public function __construct( $post_type = 'wp_global_styles' ) { + parent::__construct( $post_type ); } /** @@ -50,6 +50,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { 'type' => 'string', ), ), + 'allow_batch' => $this->allow_batch, ), ) ); @@ -79,6 +80,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { 'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ), ), ), + 'allow_batch' => $this->allow_batch, ), ) ); @@ -106,7 +108,8 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { 'permission_callback' => array( $this, 'update_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), ), - 'schema' => array( $this, 'get_public_item_schema' ), + 'schema' => array( $this, 'get_public_item_schema' ), + 'allow_batch' => $this->allow_batch, ) ); } @@ -194,29 +197,11 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { * @param WP_Post $post Post object. * @return bool Whether the post can be read. */ - protected function check_read_permission( $post ) { + public function check_read_permission( $post ) { return current_user_can( 'read_post', $post->ID ); } /** - * Returns the given global styles config. - * - * @since 5.9.0 - * - * @param WP_REST_Request $request The request instance. - * - * @return WP_REST_Response|WP_Error - */ - public function get_item( $request ) { - $post = $this->get_post( $request['id'] ); - if ( is_wp_error( $post ) ) { - return $post; - } - - return $this->prepare_item_for_response( $post, $request ); - } - - /** * Checks if a given request has access to write a single global styles config. * * @since 5.9.0 @@ -242,59 +227,11 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { } /** - * Checks if a global style can be edited. - * - * @since 5.9.0 - * - * @param WP_Post $post Post object. - * @return bool Whether the post can be edited. - */ - protected function check_update_permission( $post ) { - return current_user_can( 'edit_post', $post->ID ); - } - - /** - * Updates a single global style config. - * - * @since 5.9.0 - * - * @param WP_REST_Request $request Full details about the request. - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. - */ - public function update_item( $request ) { - $post_before = $this->get_post( $request['id'] ); - if ( is_wp_error( $post_before ) ) { - return $post_before; - } - - $changes = $this->prepare_item_for_database( $request ); - if ( is_wp_error( $changes ) ) { - return $changes; - } - - $result = wp_update_post( wp_slash( (array) $changes ), true, false ); - if ( is_wp_error( $result ) ) { - return $result; - } - - $post = get_post( $request['id'] ); - $fields_update = $this->update_additional_fields_for_object( $post, $request ); - if ( is_wp_error( $fields_update ) ) { - return $fields_update; - } - - wp_after_insert_post( $post, true, $post_before ); - - $response = $this->prepare_item_for_response( $post, $request ); - - return rest_ensure_response( $response ); - } - - /** * Prepares a single global styles config for update. * * @since 5.9.0 * @since 6.2.0 Added validation of styles.css property. + * @since 6.6.0 Added registration of block style variations from theme.json sources (theme.json, user theme.json, partials). * * @param WP_REST_Request $request Request object. * @return stdClass|WP_Error Prepared item on success. WP_Error on when the custom CSS is not valid. @@ -327,6 +264,11 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { } elseif ( isset( $existing_config['styles'] ) ) { $config['styles'] = $existing_config['styles']; } + + // Register theme-defined variations e.g. from block style variation partials under `/styles`. + $variations = WP_Theme_JSON_Resolver::get_style_variations( 'block' ); + wp_register_block_style_variations_from_theme_json_partials( $variations ); + if ( isset( $request['settings'] ) ) { $config['settings'] = $request['settings']; } elseif ( isset( $existing_config['settings'] ) ) { @@ -353,6 +295,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { * Prepare a global styles config output for response. * * @since 5.9.0 + * @since 6.6.0 Added custom relative theme file URIs to `_links`. * * @param WP_Post $post Global Styles post object. * @param WP_REST_Request $request Request object. @@ -362,8 +305,10 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { $raw_config = json_decode( $post->post_content, true ); $is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON']; $config = array(); + $theme_json = null; if ( $is_global_styles_user_theme_json ) { - $config = ( new WP_Theme_JSON( $raw_config, 'custom' ) )->get_raw_data(); + $theme_json = new WP_Theme_JSON( $raw_config, 'custom' ); + $config = $theme_json->get_raw_data(); } // Base fields for every post. @@ -405,9 +350,18 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { $links = $this->prepare_links( $post->ID ); + + // Only return resolved URIs for get requests to user theme JSON. + if ( $theme_json ) { + $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json ); + if ( ! empty( $resolved_theme_uris ) ) { + $links['https://api.w.org/theme-file'] = $resolved_theme_uris; + } + } + $response->add_links( $links ); if ( ! empty( $links['self']['href'] ) ) { - $actions = $this->get_available_actions(); + $actions = $this->get_available_actions( $post, $request ); $self = $links['self']['href']; foreach ( $actions as $rel ) { $response->add_link( $rel, $self ); @@ -431,9 +385,12 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); $links = array( - 'self' => array( + 'self' => array( 'href' => rest_url( trailingslashit( $base ) . $id ), ), + 'about' => array( + 'href' => rest_url( 'wp/v2/types/' . $this->post_type ), + ), ); if ( post_type_supports( $this->post_type, 'revisions' ) ) { @@ -454,13 +411,16 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { * * @since 5.9.0 * @since 6.2.0 Added 'edit-css' action. + * @since 6.6.0 Added $post and $request parameters. * + * @param WP_Post $post Post object. + * @param WP_REST_Request $request Request object. * @return array List of link relations. */ - protected function get_available_actions() { + protected function get_available_actions( $post, $request ) { $rels = array(); - $post_type = get_post_type_object( $this->post_type ); + $post_type = get_post_type_object( $post->post_type ); if ( current_user_can( $post_type->cap->publish_posts ) ) { $rels[] = 'https://api.w.org/action-publish'; } @@ -473,21 +433,6 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { } /** - * Overwrites the default protected 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. - * - * @since 5.9.0 - * - * @return string Protected title format. - */ - public function protected_title_format() { - return '%s'; - } - - /** * Retrieves the query params for the global styles collection. * * @since 5.9.0 @@ -588,6 +533,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { * Returns the given theme global styles config. * * @since 5.9.0 + * @since 6.6.0 Added custom relative theme file URIs to `_links`. * * @param WP_REST_Request $request The request instance. * @return WP_REST_Response|WP_Error @@ -622,11 +568,15 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { $response = rest_ensure_response( $data ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { - $links = array( + $links = array( 'self' => array( 'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ), ), ); + $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme ); + if ( ! empty( $resolved_theme_uris ) ) { + $links['https://api.w.org/theme-file'] = $resolved_theme_uris; + } $response->add_links( $links ); } @@ -664,6 +614,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { * * @since 6.0.0 * @since 6.2.0 Returns parent theme variations, if they exist. + * @since 6.6.0 Added custom relative theme file URIs to `_links` for each item. * * @param WP_REST_Request $request The request instance. * @@ -679,9 +630,28 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller { ); } + $response = array(); + + // Register theme-defined variations e.g. from block style variation partials under `/styles`. + $partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' ); + wp_register_block_style_variations_from_theme_json_partials( $partials ); + $variations = WP_Theme_JSON_Resolver::get_style_variations(); + foreach ( $variations as $variation ) { + $variation_theme_json = new WP_Theme_JSON( $variation ); + $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $variation_theme_json ); + $data = rest_ensure_response( $variation ); + if ( ! empty( $resolved_theme_uris ) ) { + $data->add_links( + array( + 'https://api.w.org/theme-file' => $resolved_theme_uris, + ) + ); + } + $response[] = $this->prepare_response_for_collection( $data ); + } - return rest_ensure_response( $variations ); + return rest_ensure_response( $response ); } /** diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php index 5f5ae84..54285fa 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php @@ -14,14 +14,14 @@ * * @see WP_REST_Controller */ -class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { +class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Revisions_Controller { /** - * Parent post type. + * Parent controller. * - * @since 6.3.0 - * @var string + * @since 6.6.0 + * @var WP_REST_Controller */ - protected $parent_post_type; + private $parent_controller; /** * The base of the parent controller's route. @@ -32,22 +32,41 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { protected $parent_base; /** + * Parent post type. + * + * @since 6.6.0 + * @var string + */ + protected $parent_post_type; + + /** * Constructor. * * @since 6.3.0 + * @since 6.6.0 Extends class from WP_REST_Revisions_Controller. + * + * @param string $parent_post_type Post type of the parent. */ - public function __construct() { - $this->parent_post_type = 'wp_global_styles'; - $this->rest_base = 'revisions'; - $this->parent_base = 'global-styles'; - $this->namespace = 'wp/v2'; + public function __construct( $parent_post_type = 'wp_global_styles' ) { + parent::__construct( $parent_post_type ); + $post_type_object = get_post_type_object( $parent_post_type ); + $parent_controller = $post_type_object->get_rest_controller(); + + if ( ! $parent_controller ) { + $parent_controller = new WP_REST_Global_Styles_Controller( $parent_post_type ); + } + + $this->parent_controller = $parent_controller; + $this->rest_base = 'revisions'; + $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; + $this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2'; } /** * Registers the controller's routes. * * @since 6.3.0 - * @since 6.5.0 Added route to fetch individual global styles revisions. + * @since 6.6.0 Added route to fetch individual global styles revisions. */ public function register_routes() { register_rest_route( @@ -63,7 +82,7 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), - 'permission_callback' => array( $this, 'get_item_permissions_check' ), + 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => $this->get_collection_params(), ), 'schema' => array( $this, 'get_public_item_schema' ), @@ -98,29 +117,6 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { } /** - * Retrieves the query params for collections. - * - * Inherits from WP_REST_Controller::get_collection_params(), - * also reflects changes to return value WP_REST_Revisions_Controller::get_collection_params(). - * - * @since 6.3.0 - * - * @return array Collection parameters. - */ - public function get_collection_params() { - $collection_params = parent::get_collection_params(); - $collection_params['context']['default'] = 'view'; - $collection_params['offset'] = array( - 'description' => __( 'Offset the result set by a specific number of items.' ), - 'type' => 'integer', - ); - unset( $collection_params['search'] ); - unset( $collection_params['per_page']['default'] ); - - return $collection_params; - } - - /** * Returns decoded JSON from post content string, * or a 404 if not found. * @@ -269,83 +265,10 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { } /** - * Retrieves one global styles revision from the collection. - * - * @since 6.5.0 - * - * @param WP_REST_Request $request Full details about the request. - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. - */ - public function get_item( $request ) { - $parent = $this->get_parent( $request['parent'] ); - if ( is_wp_error( $parent ) ) { - return $parent; - } - - $revision = $this->get_revision( $request['id'] ); - if ( is_wp_error( $revision ) ) { - return $revision; - } - - $response = $this->prepare_item_for_response( $revision, $request ); - return rest_ensure_response( $response ); - } - - /** - * Gets the global styles revision, if the ID is valid. - * - * @since 6.5.0 - * - * @param int $id Supplied ID. - * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise. - */ - protected function get_revision( $id ) { - $error = new WP_Error( - 'rest_post_invalid_id', - __( 'Invalid global styles revision ID.' ), - array( 'status' => 404 ) - ); - - if ( (int) $id <= 0 ) { - return $error; - } - - $revision = get_post( (int) $id ); - if ( empty( $revision ) || empty( $revision->ID ) || 'revision' !== $revision->post_type ) { - return $error; - } - - return $revision; - } - - /** - * Checks the post_date_gmt or modified_gmt and prepare any post or - * modified date for single post output. - * - * Duplicate of WP_REST_Revisions_Controller::prepare_date_response. - * - * @since 6.3.0 - * - * @param string $date_gmt GMT publication time. - * @param string|null $date Optional. Local publication time. Default null. - * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null. - */ - protected function prepare_date_response( $date_gmt, $date = null ) { - if ( '0000-00-00 00:00:00' === $date_gmt ) { - return null; - } - - if ( isset( $date ) ) { - return mysql_to_rfc3339( $date ); - } - - return mysql_to_rfc3339( $date_gmt ); - } - - /** * Prepares the revision for the REST response. * * @since 6.3.0 + * @since 6.6.0 Added resolved URI links to the response. * * @param WP_Post $post Post revision object. * @param WP_REST_Request $request Request object. @@ -359,11 +282,13 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { return $global_styles_config; } - $fields = $this->get_fields_for_response( $request ); - $data = array(); + $fields = $this->get_fields_for_response( $request ); + $data = array(); + $theme_json = null; if ( ! empty( $global_styles_config['styles'] ) || ! empty( $global_styles_config['settings'] ) ) { - $global_styles_config = ( new WP_Theme_JSON( $global_styles_config, 'custom' ) )->get_raw_data(); + $theme_json = new WP_Theme_JSON( $global_styles_config, 'custom' ); + $global_styles_config = $theme_json->get_raw_data(); if ( rest_is_field_included( 'settings', $fields ) ) { $data['settings'] = ! empty( $global_styles_config['settings'] ) ? $global_styles_config['settings'] : new stdClass(); } @@ -400,17 +325,28 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { $data['parent'] = (int) $parent->ID; } - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); + $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 ); + $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json ); - return rest_ensure_response( $data ); + if ( ! empty( $resolved_theme_uris ) ) { + $response->add_links( + array( + 'https://api.w.org/theme-file' => $resolved_theme_uris, + ) + ); + } + + return $response; } /** * Retrieves the revision's schema, conforming to JSON Schema. * * @since 6.3.0 + * @since 6.6.0 Merged parent and parent controller schema data. * * @return array Item schema data. */ @@ -419,133 +355,40 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { return $this->add_additional_fields_schema( $this->schema ); } - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => "{$this->parent_post_type}-revision", - 'type' => 'object', - // Base properties for every revision. - 'properties' => array( - - /* - * Adds settings and styles from the WP_REST_Revisions_Controller item fields. - * Leaves out GUID as global styles shouldn't be accessible via URL. - */ - 'author' => array( - 'description' => __( 'The ID for the author of the revision.' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit', 'embed' ), - ), - 'date' => array( - 'description' => __( "The date the revision was published, in the site's timezone." ), - 'type' => 'string', - 'format' => 'date-time', - 'context' => array( 'view', 'edit', 'embed' ), - ), - 'date_gmt' => array( - 'description' => __( 'The date the revision was published, as GMT.' ), - 'type' => 'string', - 'format' => 'date-time', - 'context' => array( 'view', 'edit' ), - ), - 'id' => array( - 'description' => __( 'Unique identifier for the revision.' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit', 'embed' ), - ), - 'modified' => array( - 'description' => __( "The date the revision was last modified, in the site's timezone." ), - 'type' => 'string', - 'format' => 'date-time', - 'context' => array( 'view', 'edit' ), - ), - 'modified_gmt' => array( - 'description' => __( 'The date the revision was last modified, as GMT.' ), - 'type' => 'string', - 'format' => 'date-time', - 'context' => array( 'view', 'edit' ), - ), - 'parent' => array( - 'description' => __( 'The ID for the parent of the revision.' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit', 'embed' ), - ), + $schema = parent::get_item_schema(); + $parent_schema = $this->parent_controller->get_item_schema(); + $schema['properties'] = array_merge( $schema['properties'], $parent_schema['properties'] ); - // Adds settings and styles from the WP_REST_Global_Styles_Controller parent schema. - 'styles' => array( - 'description' => __( 'Global styles.' ), - 'type' => array( 'object' ), - 'context' => array( 'view', 'edit' ), - ), - 'settings' => array( - 'description' => __( 'Global settings.' ), - 'type' => array( 'object' ), - 'context' => array( 'view', 'edit' ), - ), - ), + unset( + $schema['properties']['guid'], + $schema['properties']['slug'], + $schema['properties']['meta'], + $schema['properties']['content'], + $schema['properties']['title'] ); - $this->schema = $schema; + $this->schema = $schema; return $this->add_additional_fields_schema( $this->schema ); } /** - * Checks if a given request has access to read a single global style. - * - * @since 6.3.0 - * - * @param WP_REST_Request $request Full details about the request. - * @return true|WP_Error True if the request has read access, WP_Error object otherwise. - */ - public function get_item_permissions_check( $request ) { - $post = $this->get_parent( $request['parent'] ); - if ( is_wp_error( $post ) ) { - return $post; - } - - /* - * The same check as WP_REST_Global_Styles_Controller::get_item_permissions_check. - */ - if ( ! current_user_can( 'read_post', $post->ID ) ) { - return new WP_Error( - 'rest_cannot_view', - __( 'Sorry, you are not allowed to view revisions for this global style.' ), - array( 'status' => rest_authorization_required_code() ) - ); - } - - return true; - } - - /** - * Gets the parent post, if the ID is valid. - * - * Duplicate of WP_REST_Revisions_Controller::get_parent. + * Retrieves the query params for collections. + * Removes params that are not supported by global styles revisions. * - * @since 6.3.0 + * @since 6.6.0 * - * @param int $parent_post_id Supplied ID. - * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. + * @return array Collection parameters. */ - protected function get_parent( $parent_post_id ) { - $error = new WP_Error( - 'rest_post_invalid_parent', - __( 'Invalid post parent ID.' ), - array( 'status' => 404 ) + public function get_collection_params() { + $query_params = parent::get_collection_params(); + unset( + $query_params['exclude'], + $query_params['include'], + $query_params['search'], + $query_params['order'], + $query_params['orderby'] ); - - if ( (int) $parent_post_id <= 0 ) { - return $error; - } - - $parent_post = get_post( (int) $parent_post_id ); - - if ( empty( $parent_post ) || empty( $parent_post->ID ) - || $this->parent_post_type !== $parent_post->post_type - ) { - return $error; - } - - return $parent_post; + return $query_params; } } diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php index 0d3d0e1..d766d5c 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php @@ -246,6 +246,14 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller { $data['rest_namespace'] = $namespace; } + if ( rest_is_field_included( 'template', $fields ) ) { + $data['template'] = $post_type->template ?? array(); + } + + if ( rest_is_field_included( 'template_lock', $fields ) ) { + $data['template_lock'] = ! empty( $post_type->template_lock ) ? $post_type->template_lock : false; + } + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, $context ); @@ -407,6 +415,19 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller { 'context' => array( 'view', 'edit', 'embed' ), 'readonly' => true, ), + 'template' => array( + 'type' => array( 'array' ), + 'description' => __( 'The block template associated with the post type.' ), + 'readonly' => true, + 'context' => array( 'view', 'edit', 'embed' ), + ), + 'template_lock' => array( + 'type' => array( 'string', 'boolean' ), + 'enum' => array( 'all', 'insert', 'contentOnly', false ), + 'description' => __( 'The template_lock associated with the post type, or false if none.' ), + 'readonly' => true, + 'context' => array( 'view', 'edit', 'embed' ), + ), ), ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 4501e47..a29f92c 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -97,6 +97,12 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); + if ( isset( $schema['properties']['excerpt'] ) ) { + $get_item_args['excerpt_length'] = array( + 'description' => __( 'Override the default excerpt length.' ), + 'type' => 'integer', + ); + } if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => __( 'The password for the post if it is password protected.' ), @@ -1872,6 +1878,19 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { } if ( rest_is_field_included( 'excerpt', $fields ) ) { + if ( isset( $request['excerpt_length'] ) ) { + $excerpt_length = $request['excerpt_length']; + $override_excerpt_length = static function () use ( $excerpt_length ) { + return $excerpt_length; + }; + + add_filter( + 'excerpt_length', + $override_excerpt_length, + 20 + ); + } + /** This filter is documented in wp-includes/post-template.php */ $excerpt = apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ); @@ -1883,6 +1902,14 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { 'rendered' => post_password_required( $post ) ? '' : $excerpt, 'protected' => (bool) $post->post_password, ); + + if ( isset( $override_excerpt_length ) ) { + remove_filter( + 'excerpt_length', + $override_excerpt_length, + 20 + ); + } } if ( $has_password_filter ) { @@ -1971,6 +1998,10 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { $data['generated_slug'] = $sample_permalink[1]; } } + + if ( rest_is_field_included( 'class_list', $fields ) ) { + $data['class_list'] = get_post_class( array(), $post->ID ); + } } $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; @@ -2326,6 +2357,16 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { 'context' => array( 'edit' ), 'readonly' => true, ); + + $schema['properties']['class_list'] = array( + 'description' => __( 'An array of the class names for the post container element.' ), + 'type' => 'array', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'items' => array( + 'type' => 'string', + ), + ); } if ( $post_type_obj->hierarchical ) { diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php index cc1c302..55fe1ad 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php @@ -395,7 +395,7 @@ class WP_REST_Search_Controller extends WP_REST_Controller { protected function get_search_handler( $request ) { $type = $request->get_param( self::PROP_TYPE ); - if ( ! $type || ! isset( $this->search_handlers[ $type ] ) ) { + if ( ! $type || ! is_string( $type ) || ! isset( $this->search_handlers[ $type ] ) ) { return new WP_Error( 'rest_search_invalid_type', __( 'Invalid type parameter.' ), diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php index 365afd3..004f585 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php @@ -237,6 +237,7 @@ class WP_REST_Settings_Controller extends WP_REST_Controller { $default_schema = array( 'type' => empty( $args['type'] ) ? null : $args['type'], + 'title' => empty( $args['label'] ) ? '' : $args['label'], 'description' => empty( $args['description'] ) ? '' : $args['description'], 'default' => isset( $args['default'] ) ? $args['default'] : null, ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php index 8d32ecb..22cdc26 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php @@ -156,15 +156,15 @@ class WP_REST_Template_Revisions_Controller extends WP_REST_Revisions_Controller } /** - * Gets the parent post, if the ID is valid. + * Gets the parent post, if the template ID is valid. * * @since 6.4.0 * - * @param int $parent_post_id Supplied ID. + * @param string $parent_template_id Supplied ID. * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. */ - protected function get_parent( $parent_post_id ) { - $template = get_block_template( $parent_post_id, $this->parent_post_type ); + protected function get_parent( $parent_template_id ) { + $template = get_block_template( $parent_template_id, $this->parent_post_type ); if ( ! $template ) { return new WP_Error( diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index 1c2a769..cbf0ee0 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -236,12 +236,28 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { * Checks if a given request has access to read templates. * * @since 5.8.0 + * @since 6.6.0 Allow users with edit_posts capability to read templates. * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access, WP_Error object otherwise. */ public function get_items_permissions_check( $request ) { - return $this->permissions_check( $request ); + if ( current_user_can( 'edit_posts' ) ) { + return true; + } + foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { + if ( current_user_can( $post_type->cap->edit_posts ) ) { + return true; + } + } + + return new WP_Error( + 'rest_cannot_manage_templates', + __( 'Sorry, you are not allowed to access the templates on this site.' ), + array( + 'status' => rest_authorization_required_code(), + ) + ); } /** @@ -277,12 +293,28 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { * Checks if a given request has access to read a single template. * * @since 5.8.0 + * @since 6.6.0 Allow users with edit_posts capability to read individual templates. * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */ public function get_item_permissions_check( $request ) { - return $this->permissions_check( $request ); + if ( current_user_can( 'edit_posts' ) ) { + return true; + } + foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { + if ( current_user_can( $post_type->cap->edit_posts ) ) { + return true; + } + } + + return new WP_Error( + 'rest_cannot_manage_templates', + __( 'Sorry, you are not allowed to access the templates on this site.' ), + array( + 'status' => rest_authorization_required_code(), + ) + ); } /** @@ -636,6 +668,12 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { * @return WP_REST_Response Response object. */ public function prepare_item_for_response( $item, $request ) { + // Resolve pattern blocks so they don't need to be resolved client-side + // in the editor, improving performance. + $blocks = parse_blocks( $item->content ); + $blocks = resolve_pattern_blocks( $blocks ); + $item->content = serialize_blocks( $blocks ); + // Restores the more descriptive, specific name for use within this method. $template = $item; diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php index 064fc2d..fe58480 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php @@ -224,6 +224,7 @@ class WP_REST_Themes_Controller extends WP_REST_Controller { * * @since 5.0.0 * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support. + * @since 6.6.0 Added `stylesheet_uri` and `template_uri` fields. * * @param WP_Theme $item Theme object. * @param WP_REST_Request $request Request object. @@ -331,6 +332,22 @@ class WP_REST_Themes_Controller extends WP_REST_Controller { $data['is_block_theme'] = $theme->is_block_theme(); } + if ( rest_is_field_included( 'stylesheet_uri', $fields ) ) { + if ( $this->is_same_theme( $theme, $current_theme ) ) { + $data['stylesheet_uri'] = get_stylesheet_directory_uri(); + } else { + $data['stylesheet_uri'] = $theme->get_stylesheet_directory_uri(); + } + } + + if ( rest_is_field_included( 'template_uri', $fields ) ) { + if ( $this->is_same_theme( $theme, $current_theme ) ) { + $data['template_uri'] = get_template_directory_uri(); + } else { + $data['template_uri'] = $theme->get_template_directory_uri(); + } + } + $data = $this->add_additional_fields_to_object( $data, $request ); // Wrap the data in a response object. @@ -447,11 +464,23 @@ class WP_REST_Themes_Controller extends WP_REST_Controller { 'type' => 'string', 'readonly' => true, ), + 'stylesheet_uri' => array( + 'description' => __( 'The uri for the theme\'s stylesheet directory.' ), + 'type' => 'string', + 'format' => 'uri', + 'readonly' => true, + ), 'template' => array( 'description' => __( 'The theme\'s template. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet.' ), 'type' => 'string', 'readonly' => true, ), + 'template_uri' => array( + 'description' => __( 'The uri for the theme\'s template directory. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet directory.' ), + 'type' => 'string', + 'format' => 'uri', + 'readonly' => true, + ), 'author' => array( 'description' => __( 'The theme author.' ), 'type' => 'object', diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index c2e0db8..c8d9b11 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -25,6 +25,14 @@ class WP_REST_Users_Controller extends WP_REST_Controller { protected $meta; /** + * Whether the controller supports batching. + * + * @since 6.6.0 + * @var array + */ + protected $allow_batch = array( 'v1' => true ); + + /** * Constructor. * * @since 4.7.0 @@ -61,7 +69,8 @@ class WP_REST_Users_Controller extends WP_REST_Controller { 'permission_callback' => array( $this, 'create_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), ), - 'schema' => array( $this, 'get_public_item_schema' ), + 'allow_batch' => $this->allow_batch, + 'schema' => array( $this, 'get_public_item_schema' ), ) ); @@ -69,7 +78,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller { $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( - 'args' => array( + 'args' => array( 'id' => array( 'description' => __( 'Unique identifier for the user.' ), 'type' => 'integer', @@ -107,7 +116,8 @@ class WP_REST_Users_Controller extends WP_REST_Controller { ), ), ), - 'schema' => array( $this, 'get_public_item_schema' ), + 'allow_batch' => $this->allow_batch, + 'schema' => array( $this, 'get_public_item_schema' ), ) ); |