From 723a2c718e232e209e488af58575f6a98523fab4 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 4 Nov 2021 10:14:51 +0000 Subject: [PATCH 1/2] Respect fields param for REST API requests. --- ...utenberg-rest-global-styles-controller.php | 66 +++++++++++++++---- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php b/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php index a516eeef730ca1..2611593b49388b 100644 --- a/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php +++ b/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php @@ -218,18 +218,46 @@ protected function prepare_item_for_database( $request ) { public function prepare_item_for_response( $post, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $config = json_decode( $post->post_content, true ); $is_global_styles_user_theme_json = isset( $config['isGlobalStylesUserThemeJSON'] ) && true === $config['isGlobalStylesUserThemeJSON']; - $result = array( - 'id' => $post->ID, - 'settings' => ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass(), - 'styles' => ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $config['styles'] : new stdClass(), - 'title' => array( - 'raw' => $post->post_title, - 'rendered' => get_the_title( $post ), - ), - ); - $result = $this->add_additional_fields_to_object( $result, $request ); - $response = rest_ensure_response( $result ); - $links = $this->prepare_links( $post->id ); + + $fields = $this->get_fields_for_response( $request ); + + // Base fields for every post. + $data = array(); + + if ( rest_is_field_included( 'id', $fields ) ) { + $data['id'] = $post->ID; + } + + if ( rest_is_field_included( 'title', $fields ) ) { + $data['title'] = array(); + } + if ( rest_is_field_included( 'title.raw', $fields ) ) { + $data['title']['raw'] = $post->post_title; + } + if ( rest_is_field_included( 'title.rendered', $fields ) ) { + add_filter( 'protected_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' ) ); + } + + if ( rest_is_field_included( 'settings', $fields ) ) { + $data['settings'] = ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass(); + } + + if ( rest_is_field_included( 'styles', $fields ) ) { + $data['styles'] = ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $config['styles'] : new stdClass(); + } + + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + + // Wrap the data in a response object. + $response = rest_ensure_response( $data ); + + $links = $this->prepare_links( $post->ID ); $response->add_links( $links ); if ( ! empty( $links['self']['href'] ) ) { $actions = $this->get_available_actions(); @@ -280,6 +308,20 @@ protected function get_available_actions() { return $rels; } + /** + * 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. + * + * + * @return string Protected title format. + */ + public function protected_title_format() { + return '%s'; + } + /** * Retrieves the query params for the global styles collection. * From 71fd2385d661051c1b56fa275d5d34b43b410df8 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 4 Nov 2021 10:32:45 +0000 Subject: [PATCH 2/2] Fix lint --- .../class-gutenberg-rest-global-styles-controller.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php b/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php index 2611593b49388b..e2b2af15dd3c1f 100644 --- a/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php +++ b/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php @@ -315,7 +315,6 @@ protected function get_available_actions() { * "Protected: %s", as the REST API communicates the protected status of a post * in a machine readable format, we remove the "Protected: " prefix. * - * * @return string Protected title format. */ public function protected_title_format() {