WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/23/2016 09:52:27 AM (5 years ago)
Author:
helen
Message:

Theme starter content: Add support for featured images and page templates.

Featured image support means that attachments can now be imported. Media can be sideloaded from within theme or plugin directories. Like other posts, attachments are auto-drafts until customizer changes are published, and are not duplicated when they already exist in the customized state. Attachment IDs can be used for any number of purposes, much like post IDs. Twenty Seventeen now includes 3 images used as featured images to best showcase the multi-section homepage setup.

As featured image IDs are stored in post meta, it also made sense to add support for page templates. Twenty Seventeen does not include any such templates, but the functionality can be quite important for displaying themes to their best effect.

props westonruter, helen, flixos90.
fixes #38615.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/manager.php

    r39332 r39346  
    314314    function test_import_theme_starter_content() {
    315315        wp_set_current_user( self::$admin_user_id );
     316        register_nav_menu( 'top', 'Top' );
    316317
    317318        global $wp_customize;
     
    344345            'posts' => array(
    345346                'home',
    346                 'about',
     347                'about' => array(
     348                    'template' => 'sample-page-template.php',
     349                ),
    347350                'blog',
    348351                'custom' => array(
    349352                    'post_type' => 'post',
    350353                    'post_title' => 'Custom',
     354                    'thumbnail' => '{{featured-image-logo}}',
     355                ),
     356            ),
     357            'attachments' => array(
     358                'featured-image-logo' => array(
     359                    'post_title' => 'Featured Image',
     360                    'post_content' => 'Attachment Description',
     361                    'post_excerpt' => 'Attachment Caption',
     362                    'file' => DIR_TESTDATA . '/images/waffles.jpg',
    351363                ),
    352364            ),
     
    395407
    396408        $posts_by_name = array();
     409        $this->assertCount( 5, $changeset_values['nav_menus_created_posts'] );
    397410        foreach ( $changeset_values['nav_menus_created_posts'] as $post_id ) {
    398411            $post = get_post( $post_id );
     
    400413            $posts_by_name[ $post->post_name ] = $post->ID;
    401414        }
     415        $this->assertEquals( array( 'featured-image', 'home', 'about', 'blog', 'custom' ), array_keys( $posts_by_name ) );
     416        $this->assertEquals( 'Custom', get_post( $posts_by_name['custom'] )->post_title );
     417        $this->assertEquals( 'sample-page-template.php', get_page_template_slug( $posts_by_name['about'] ) );
     418        $this->assertEquals( '', get_page_template_slug( $posts_by_name['blog'] ) );
     419        $this->assertEquals( $posts_by_name['featured-image'], get_post_thumbnail_id( $posts_by_name['custom'] ) );
     420        $this->assertEquals( '', get_post_thumbnail_id( $posts_by_name['blog'] ) );
     421        $attachment_metadata = wp_get_attachment_metadata( $posts_by_name['featured-image'] );
     422        $this->assertEquals( 'Featured Image', get_post( $posts_by_name['featured-image'] )->post_title );
     423        $this->assertArrayHasKey( 'file', $attachment_metadata );
     424        $this->assertContains( 'waffles', $attachment_metadata['file'] );
    402425
    403426        $this->assertEquals( 'page', $changeset_values['show_on_front'] );
     
    419442        }
    420443
     444        // Ensure that re-importing doesn't cause auto-drafts to balloon.
     445        $wp_customize->import_theme_starter_content();
     446        $changeset_data = $wp_customize->changeset_data();
     447        $this->assertEqualSets( array_values( $posts_by_name ), $changeset_data['nav_menus_created_posts']['value'], 'Auto-drafts should not get re-created and amended with each import.' );
     448
    421449        // Test that saving non-starter content on top of the changeset clears the starter_content flag.
    422450        $wp_customize->save_changeset_post( array(
     
    443471        $this->assertNotEquals( $previous_blogdescription, $changeset_data['blogdescription']['value'] );
    444472        $this->assertArrayHasKey( 'starter_content', $changeset_data['blogdescription'] );
     473
     474        // Publish.
     475        $this->assertEquals( 'auto-draft', get_post( $posts_by_name['about'] )->post_status );
     476        $this->assertEquals( 'auto-draft', get_post( $posts_by_name['featured-image'] )->post_status );
     477        $this->assertNotEquals( $changeset_data['blogname']['value'], get_option( 'blogname' ) );
     478        $r = $wp_customize->save_changeset_post( array( 'status' => 'publish' ) );
     479        $this->assertInternalType( 'array', $r );
     480        $this->assertEquals( 'publish', get_post( $posts_by_name['about'] )->post_status );
     481        $this->assertEquals( 'inherit', get_post( $posts_by_name['featured-image'] )->post_status );
     482        $this->assertEquals( $changeset_data['blogname']['value'], get_option( 'blogname' ) );
    445483    }
    446484
Note: See TracChangeset for help on using the changeset viewer.