Skip to content

Commit

Permalink
Fix: Multiple overwrites on rest_controller_class for wp_template/wp_…
Browse files Browse the repository at this point in the history
…template_part. (#48078)
  • Loading branch information
jorgefilipecosta committed Feb 17, 2023
1 parent 0b5ca22 commit 0414d3a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,65 @@ public function get_template_fallback( $request ) {
$response = $this->prepare_item_for_response( $fallback_template, $request );
return rest_ensure_response( $response );
}

/**
* Add revisions to the response.
*
* @param WP_Block_Template $item Template instance.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response Response object.
*/
public function prepare_item_for_response( $item, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$template = $item;

$fields = $this->get_fields_for_response( $request );

$response = parent::prepare_item_for_response( $item, $request );

if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$links = $this->prepare_revision_links( $template );
$response->add_links( $links );
if ( ! empty( $links['self']['href'] ) ) {
$actions = $this->get_available_actions();
$self = $links['self']['href'];
foreach ( $actions as $rel ) {
$response->add_link( $rel, $self );
}
}
}

return $response;
}

/**
* Adds revisions to links.
*
* @since 6.2.0
*
* @param WP_Block_Template $template Template instance.
* @return array Links for the given post.
*/
protected function prepare_revision_links( $template ) {
$links = array();

if ( post_type_supports( $this->post_type, 'revisions' ) && (int) $template->wp_id ) {
$revisions = wp_get_latest_revision_id_and_total_count( (int) $template->wp_id );
$revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0;
$revisions_base = sprintf( '/%s/%s/%s/revisions', $this->namespace, $this->rest_base, $template->id );

$links['version-history'] = array(
'href' => rest_url( $revisions_base ),
'count' => $revisions_count,
);

if ( $revisions_count > 0 ) {
$links['predecessor-version'] = array(
'href' => rest_url( $revisions_base . '/' . $revisions['latest_id'] ),
'id' => $revisions['latest_id'],
);
}
}

return $links;
}
}
75 changes: 0 additions & 75 deletions lib/experimental/class-gutenberg-rest-template-revision-count.php

This file was deleted.

21 changes: 0 additions & 21 deletions lib/experimental/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,6 @@ function gutenberg_register_block_editor_settings() {
add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' );


/**
* Hook in to the template and template part post types and decorate
* the rest endpoint with the revision count.
*
* When merging to core, this can be removed once Gutenberg_REST_Template_Revision_Count is
* merged with WP_REST_Template_Controller.
*
* @param array $args Current registered post type args.
* @param string $post_type Name of post type.
*
* @return array
*/
function wp_api_template_revision_args( $args, $post_type ) {
if ( 'wp_template' === $post_type || 'wp_template_part' === $post_type ) {
$args['rest_controller_class'] = 'Gutenberg_REST_Template_Revision_Count';
}

return $args;
}
add_filter( 'register_post_type_args', 'wp_api_template_revision_args', 10, 2 );

/**
* Shim for get_sample_permalink() to add support for auto-draft status.
*
Expand Down
1 change: 0 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ function gutenberg_is_experiment_enabled( $name ) {
if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) {
require_once __DIR__ . '/experimental/class-wp-rest-customizer-nonces.php';
}
require_once __DIR__ . '/experimental/class-gutenberg-rest-template-revision-count.php';

require_once __DIR__ . '/experimental/rest-api.php';
}
Expand Down
2 changes: 1 addition & 1 deletion phpunit/class-gutenberg-rest-templates-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function test_get_template_fallback() {
$request->set_param( 'is_custom', false );
$request->set_param( 'template_prefix', 'tag' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 'tag', $response->get_data()['slug'], 'Should fallback to `index.html`.' );
$this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html`.' );
}

public function test_context_param() {
Expand Down

1 comment on commit 0414d3a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 0414d3a.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4205474792
📝 Reported issues:

Please sign in to comment.