REST API: Cache schema in block pattern and menu item endpoints.

Performance improvement to add schema caching to pattern and menu item REST endpoints, so identical schema object are not needlessly regenerated.

Props spacedmonkey.
Fixes #58657. See [45811].



git-svn-id: https://develop.svn.wordpress.org/trunk@56093 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White 2023-06-28 15:51:23 +00:00
parent 80e424ad55
commit 7faab129f5
5 changed files with 35 additions and 5 deletions

View File

@ -125,6 +125,10 @@ class WP_REST_Block_Pattern_Categories_Controller extends WP_REST_Controller {
* @return array Item schema data.
*/
public function get_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'block-pattern-category',
@ -151,6 +155,8 @@ class WP_REST_Block_Pattern_Categories_Controller extends WP_REST_Controller {
),
);
return $this->add_additional_fields_schema( $schema );
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );
}
}

View File

@ -199,6 +199,10 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
* @return array Item schema data.
*/
public function get_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'block-pattern',
@ -287,6 +291,8 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
),
);
return $this->add_additional_fields_schema( $schema );
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );
}
}

View File

@ -67,6 +67,10 @@ class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller {
* @return array Item schema data.
*/
public function get_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
// Do not cache this schema because all properties are derived from parent controller.
$schema = parent::get_item_schema();
@ -86,7 +90,9 @@ class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller {
unset( $schema['properties']['title']['properties']['rendered'] );
unset( $schema['properties']['content']['properties']['rendered'] );
return $schema;
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );
}
}

View File

@ -710,6 +710,10 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
* @return array Item schema data.
*/
public function get_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => $this->post_type,
@ -914,7 +918,9 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
$schema['links'] = $schema_links;
}
return $this->add_additional_fields_schema( $schema );
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );
}
/**

View File

@ -523,6 +523,10 @@ class WP_REST_Menus_Controller extends WP_REST_Terms_Controller {
* @return array Item schema data.
*/
public function get_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
$schema = parent::get_item_schema();
unset( $schema['properties']['count'], $schema['properties']['link'], $schema['properties']['taxonomy'] );
@ -566,6 +570,8 @@ class WP_REST_Menus_Controller extends WP_REST_Terms_Controller {
'type' => 'boolean',
);
return $schema;
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );
}
}