Make WordPress Core


Ignore:
Timestamp:
09/20/2022 09:19:10 PM (15 months ago)
Author:
hellofromTonya
Message:

Editor: Adds template types, is_wp_suggestion, and fallback template content.

This commit improves site editor templates by:

  • Adds a post meta is_wp_suggestion to templates created from the site editor.

Why? To differentiate the templates created from the post editor in the Template panel in inspector controls and the templates suggested in site editor.

See Gutenberg PR 41387 for more details.

  • Expands the template types that can be added to the site editor to include single custom post type and specific posts templates.

See Gutenberg PR 41189 for more details.

  • Adds fallback template content on creation in site editor:
    • Introduces get_template_hierarchy() to get the template hierarchy for a given template slug to be created.
    • Adds a lookup route to WP_REST_Templates_Controller to get the fallback template content.

See Gutenberg PR 42520 for more details.

  • Fixes a typo in default category template's description within get_default_block_template_types().

See Gutenberg PR 42586 for more details.

  • Changes field checks from in_array() to rest_is_field_included() in WP_REST_Post_Types_Controller.
  • Adds an icon field to WP_REST_Post_Types_Controller

Follow-up to [53129], [52331], [52275], [52062], [51962], [43087].

Props ntsekouras, spacedmonkey, mamaduka, mburridge, jameskoster, bernhard-reiter, mcsf, hellofromTonya.
See #56467.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r54058 r54269  
    5757     * @covers WP_REST_Templates_Controller::register_routes
    5858     * @ticket 54596
     59     * @ticket 56467
    5960     */
    6061    public function test_register_routes() {
     
    6970            $routes,
    7071            'Single template based on the given ID route does not exist'
     72        );
     73        $this->assertArrayHasKey(
     74            '/wp/v2/templates/lookup',
     75            $routes,
     76            'Get template fallback content route does not exist'
    7177        );
    7278    }
     
    679685    }
    680686
     687    /**
     688     * @dataProvider data_create_item_with_is_wp_suggestion
     689     * @ticket 56467
     690     * @covers WP_REST_Templates_Controller::create_item
     691     *
     692     * @param array $body_params Data set to test.
     693     * @param array $expected    Expected results.
     694     */
     695    public function test_create_item_with_is_wp_suggestion( array $body_params, array $expected ) {
     696        // Set up the user.
     697        $body_params['author'] = self::$admin_id;
     698        $expected['author']    = self::$admin_id;
     699        wp_set_current_user( self::$admin_id );
     700
     701        $request = new WP_REST_Request( 'POST', '/wp/v2/templates' );
     702        $request->set_body_params( $body_params );
     703        $response = rest_get_server()->dispatch( $request );
     704        $data     = $response->get_data();
     705        unset( $data['_links'] );
     706        unset( $data['wp_id'] );
     707
     708        $this->assertSame( $expected, $data );
     709    }
     710
     711    /**
     712     * Data provider.
     713     *
     714     * @return array
     715     */
     716    public function data_create_item_with_is_wp_suggestion() {
     717        $expected = array(
     718            'id'             => 'default//page-rigas',
     719            'theme'          => 'default',
     720            'content'        => array(
     721                'raw' => 'Content',
     722            ),
     723            'slug'           => 'page-rigas',
     724            'source'         => 'custom',
     725            'origin'         => null,
     726            'type'           => 'wp_template',
     727            'description'    => 'Just a description',
     728            'title'          => array(
     729                'raw'      => 'My Template',
     730                'rendered' => 'My Template',
     731            ),
     732            'status'         => 'publish',
     733            'has_theme_file' => false,
     734            'is_custom'      => false,
     735            'author'         => null,
     736        );
     737
     738        return array(
     739            'is_wp_suggestion: true'  => array(
     740                'body_params' => array(
     741                    'slug'             => 'page-rigas',
     742                    'description'      => 'Just a description',
     743                    'title'            => 'My Template',
     744                    'content'          => 'Content',
     745                    'is_wp_suggestion' => true,
     746                    'author'           => null,
     747                ),
     748                'expected'    => $expected,
     749            ),
     750            'is_wp_suggestion: false' => array(
     751                'body_params' => array(
     752                    'slug'             => 'page-hi',
     753                    'description'      => 'Just a description',
     754                    'title'            => 'My Template',
     755                    'content'          => 'Content',
     756                    'is_wp_suggestion' => false,
     757                    'author'           => null,
     758                ),
     759                'expected'    => array_merge(
     760                    $expected,
     761                    array(
     762                        'id'        => 'default//page-hi',
     763                        'slug'      => 'page-hi',
     764                        'is_custom' => true,
     765                    )
     766                ),
     767            ),
     768        );
     769    }
     770
     771    /**
     772     * @ticket 56467
     773     * @covers WP_REST_Templates_Controller::get_template_fallback
     774     */
     775    public function test_get_template_fallback() {
     776        wp_set_current_user( self::$admin_id );
     777        switch_theme( 'block-theme' );
     778        $request = new WP_REST_Request( 'GET', '/wp/v2/templates/lookup' );
     779        // Should fallback to `index.html`.
     780        $request->set_param( 'slug', 'tag-status' );
     781        $request->set_param( 'is_custom', false );
     782        $request->set_param( 'template_prefix', 'tag' );
     783        $response = rest_get_server()->dispatch( $request );
     784        $this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html`.' );
     785        // Should fallback to `page.html`.
     786        $request->set_param( 'slug', 'page-hello' );
     787        $request->set_param( 'is_custom', false );
     788        $request->set_param( 'template_prefix', 'page' );
     789        $response = rest_get_server()->dispatch( $request );
     790        $this->assertSame( 'page', $response->get_data()['slug'], 'Should fallback to `page.html`.' );
     791    }
    681792}
Note: See TracChangeset for help on using the changeset viewer.