Skip to content

Commit

Permalink
REST API: Cache schema in block pattern and menu item endpoints.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kadamwhite committed Jun 28, 2023
1 parent 80e424a commit 7faab12
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public function prepare_item_for_response( $item, $request ) {
* @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',
Expand All @@ -151,6 +155,8 @@ public function get_item_schema() {
),
);

return $this->add_additional_fields_schema( $schema );
$this->schema = $schema;

return $this->add_additional_fields_schema( $this->schema );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ public function prepare_item_for_response( $item, $request ) {
* @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',
Expand Down Expand Up @@ -287,6 +291,8 @@ public function get_item_schema() {
),
);

return $this->add_additional_fields_schema( $schema );
$this->schema = $schema;

return $this->add_additional_fields_schema( $this->schema );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public function filter_response_by_context( $data, $context ) {
* @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();

Expand All @@ -86,7 +90,9 @@ public function get_item_schema() {
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 );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,10 @@ protected function get_schema_links() {
* @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,
Expand Down Expand Up @@ -914,7 +918,9 @@ public function get_item_schema() {
$schema['links'] = $schema_links;
}

return $this->add_additional_fields_schema( $schema );
$this->schema = $schema;

return $this->add_additional_fields_schema( $this->schema );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,10 @@ protected function handle_locations( $menu_id, $request ) {
* @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'] );

Expand Down Expand Up @@ -566,6 +570,8 @@ public function get_item_schema() {
'type' => 'boolean',
);

return $schema;
$this->schema = $schema;

return $this->add_additional_fields_schema( $this->schema );
}
}

0 comments on commit 7faab12

Please sign in to comment.