Make WordPress Core


Ignore:
Timestamp:
10/10/2023 02:03:03 PM (16 months ago)
Author:
spacedmonkey
Message:

REST API: Fix issue with Template and Template Part Revision/Autosave REST API controllers.

The Template and Template Part REST API controllers have unique characteristics compared to other post type REST API controllers. They do not rely on integer IDs to reference objects; instead, they use a combination of the theme name and slug of the template, like 'twentytwentyfourhome.' Consequently, when the post types template and template part were introduced in [52062], it led to the registration of REST API endpoints for autosaves and revisions with invalid URL structures.

In this commit, we introduce new functionality to enable custom autosave and revisions endpoints to be registered at the post type level. Similar to the 'rest_controller_class' parameter, developers can now define 'revisions_rest_controller' and 'autosave_rest_controller.' This empowers developers to create custom controllers for these functionalities. Additionally, we introduce a 'late_route_registration' parameter, which proves helpful when dealing with custom URL patterns and regex pattern matching issues.
This commit registers new classes for template and template part autosave and revisions controllers, differentiating them from standard controllers in the following ways:

  • The response shape now matches that of the template controller.
  • Permission checks align with the template controller.
  • A custom URL pattern is introduced to support slug-based identification of templates.

Furthermore, we've updated the utility function '_build_block_template_result_from_post' to support passing revision post objects. This enhancement ensures compatibility with the custom revisions controller.

Props spacedmonkey, revgeorge, andraganescu, hellofromTonya, antonvlasenko, kadamwhite, ironprogrammer, costdev, mukesh27, timothyblynjacobs, adamsilverstein.
Fixes 56922.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r56805 r56819  
    725725 * @since 5.9.0
    726726 * @since 6.3.0 Added `modified` property to template objects.
     727 * @since 6.4.0 Added support for a revision post to be passed to this function.
    727728 * @access private
    728729 *
     
    732733function _build_block_template_result_from_post( $post ) {
    733734    $default_template_types = get_default_block_template_types();
    734     $terms                  = get_the_terms( $post, 'wp_theme' );
     735
     736    $post_id = wp_is_post_revision( $post );
     737    if ( ! $post_id ) {
     738        $post_id = $post;
     739    }
     740    $parent_post = get_post( $post_id );
     741
     742    $terms = get_the_terms( $parent_post, 'wp_theme' );
    735743
    736744    if ( is_wp_error( $terms ) ) {
     
    746754    $has_theme_file = get_stylesheet() === $theme && null !== $template_file;
    747755
    748     $origin           = get_post_meta( $post->ID, 'origin', true );
    749     $is_wp_suggestion = get_post_meta( $post->ID, 'is_wp_suggestion', true );
     756    $origin           = get_post_meta( $parent_post->ID, 'origin', true );
     757    $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true );
    750758
    751759    $template                 = new WP_Block_Template();
    752760    $template->wp_id          = $post->ID;
    753     $template->id             = $theme . '//' . $post->post_name;
     761    $template->id             = $theme . '//' . $parent_post->post_name;
    754762    $template->theme          = $theme;
    755763    $template->content        = $post->post_content;
     
    766774    $template->modified       = $post->post_modified;
    767775
    768     if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
     776    if ( 'wp_template' === $parent_post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
    769777        $template->post_types = $template_file['postTypes'];
    770778    }
    771779
    772     if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
     780    if ( 'wp_template' === $parent_post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
    773781        $template->is_custom = false;
    774782    }
    775783
    776     if ( 'wp_template_part' === $post->post_type ) {
    777         $type_terms = get_the_terms( $post, 'wp_template_part_area' );
     784    if ( 'wp_template_part' === $parent_post->post_type ) {
     785        $type_terms = get_the_terms( $parent_post, 'wp_template_part_area' );
    778786        if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
    779787            $template->area = $type_terms[0]->name;
     
    782790
    783791    // Check for a block template without a description and title or with a title equal to the slug.
    784     if ( 'wp_template' === $post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) {
     792    if ( 'wp_template' === $parent_post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) {
    785793        $matches = array();
    786794
Note: See TracChangeset for help on using the changeset viewer.