Make WordPress Core

Changeset 53093


Ignore:
Timestamp:
04/07/2022 01:33:03 PM (2 years ago)
Author:
gziolo
Message:

Site Editor: Resolve homepage template on server-side

Backports change from Gutenberg to support server-side home template resolution in the Site Editor. Original PR https://github.com/WordPress/gutenberg/pull/38817.

Props Mamaduka.
See #55505.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/site-editor.php

    r52995 r53093  
    2222if ( ! wp_is_block_theme() ) {
    2323    wp_die( __( 'The theme you are currently using is not compatible with Full Site Editing.' ) );
     24}
     25
     26/**
     27 * Do a server-side redirection if missing `postType` and `postId`
     28 * query args when visiting Site Editor.
     29 */
     30$home_template = _resolve_home_block_template();
     31if ( $home_template && empty( $_GET['postType'] ) && empty( $_GET['postId'] ) ) {
     32    $redirect_url = add_query_arg(
     33        $home_template,
     34        admin_url( 'site-editor.php' )
     35    );
     36    wp_safe_redirect( $redirect_url );
     37    exit;
    2438}
    2539
     
    5771    'defaultTemplateTypes'                 => $indexed_template_types,
    5872    'defaultTemplatePartAreas'             => get_allowed_block_template_part_areas(),
     73    '__unstableHomeTemplate'               => $home_template,
    5974    '__experimentalBlockPatterns'          => WP_Block_Patterns_Registry::get_instance()->get_all_registered(),
    6075    '__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
  • trunk/src/wp-includes/block-template.php

    r52697 r53093  
    332332    }
    333333}
     334
     335/**
     336 * Returns the correct template for the site's home page.
     337 *
     338 * @access private
     339 * @since 6.0.0
     340 *
     341 * @return array|null A template object, or null if none could be found.
     342 */
     343function _resolve_home_block_template() {
     344    $show_on_front = get_option( 'show_on_front' );
     345    $front_page_id = get_option( 'page_on_front' );
     346
     347    if ( 'page' === $show_on_front && $front_page_id ) {
     348        return array(
     349            'postType' => 'page',
     350            'postId'   => $front_page_id,
     351        );
     352    }
     353
     354    $hierarchy = array( 'front-page', 'home', 'index' );
     355    $template  = resolve_block_template( 'home', $hierarchy, '' );
     356
     357    if ( ! $template ) {
     358        return null;
     359    }
     360
     361    return array(
     362        'postType' => 'wp_template',
     363        'postId'   => $template->id,
     364    );
     365}
  • trunk/tests/phpunit/tests/block-template.php

    r52697 r53093  
    189189        $this->assertSame( '', $resolved_template_path );
    190190    }
     191
     192    /**
     193     * Covers: https://github.com/WordPress/gutenberg/pull/38817.
     194     *
     195     * @ticket 55505
     196     */
     197    public function test_resolve_home_block_template_default_hierarchy() {
     198        $template = _resolve_home_block_template();
     199
     200        $this->assertSame( 'wp_template', $template['postType'] );
     201        $this->assertSame( get_stylesheet() . '//index', $template['postId'] );
     202    }
     203
     204    /**
     205     * @ticket 55505
     206     */
     207    public function test_resolve_home_block_template_static_homepage() {
     208        $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
     209        update_option( 'show_on_front', 'page' );
     210        update_option( 'page_on_front', $post_id );
     211
     212        $template = _resolve_home_block_template();
     213
     214        $this->assertSame( 'page', $template['postType'] );
     215        $this->assertSame( $post_id, $template['postId'] );
     216
     217        delete_option( 'show_on_front', 'page' );
     218    }
     219
     220    /**
     221     * @ticket 55505
     222     */
     223    public function test_resolve_home_block_template_no_resolution() {
     224        switch_theme( 'stylesheetonly' );
     225        $template = _resolve_home_block_template();
     226
     227        $this->assertNull( $template );
     228    }
    191229}
Note: See TracChangeset for help on using the changeset viewer.