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.


git-svn-id: https://develop.svn.wordpress.org/trunk@39346 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Helen Hou-Sandi 2016-11-23 09:52:27 +00:00
parent 9528abe32b
commit f3a59f8632
10 changed files with 291 additions and 43 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

View File

@ -123,10 +123,33 @@ function twentyseventeen_setup() {
'posts' => array(
'home',
'about',
'contact',
'blog',
'homepage-section',
'about' => array(
'thumbnail' => '{{image-sandwich}}',
),
'contact' => array(
'thumbnail' => '{{image-espresso}}',
),
'blog' => array(
'thumbnail' => '{{image-coffee}}',
),
'homepage-section' => array(
'thumbnail' => '{{image-espresso}}',
),
),
'attachments' => array(
'image-espresso' => array(
'post_title' => _x( 'Espresso', 'Theme starter content' ),
'file' => 'assets/images/espresso.jpg',
),
'image-sandwich' => array(
'post_title' => _x( 'Sandwich', 'Theme starter content' ),
'file' => 'assets/images/sandwich.jpg',
),
'image-coffee' => array(
'post_title' => _x( 'Coffee', 'Theme starter content' ),
'file' => 'assets/images/coffee.jpg',
),
),
'options' => array(

View File

@ -916,6 +916,7 @@ final class WP_Customize_Manager {
}
$sidebars_widgets = isset( $starter_content['widgets'] ) && ! empty( $this->widgets ) ? $starter_content['widgets'] : array();
$attachments = isset( $starter_content['attachments'] ) && ! empty( $this->nav_menus ) ? $starter_content['attachments'] : array();
$posts = isset( $starter_content['posts'] ) && ! empty( $this->nav_menus ) ? $starter_content['posts'] : array();
$options = isset( $starter_content['options'] ) ? $starter_content['options'] : array();
$nav_menus = isset( $starter_content['nav_menus'] ) && ! empty( $this->nav_menus ) ? $starter_content['nav_menus'] : array();
@ -965,26 +966,126 @@ final class WP_Customize_Manager {
}
}
$starter_content_auto_draft_post_ids = array();
if ( ! empty( $changeset_data['nav_menus_created_posts']['value'] ) ) {
$starter_content_auto_draft_post_ids = array_merge( $starter_content_auto_draft_post_ids, $changeset_data['nav_menus_created_posts']['value'] );
}
$existing_starter_content_posts = array();
if ( ! empty( $starter_content_auto_draft_post_ids ) ) {
$existing_posts_query = new WP_Query( array(
'post__in' => $starter_content_auto_draft_post_ids,
'post_status' => 'auto-draft',
'post_type' => 'any',
'number' => -1,
) );
foreach ( $existing_posts_query->posts as $existing_post ) {
$existing_starter_content_posts[ $existing_post->post_type . ':' . $existing_post->post_name ] = $existing_post;
}
}
// Attachments are technically posts but handled differently.
if ( ! empty( $attachments ) ) {
// Such is The WordPress Way.
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attachment_ids = array();
foreach ( $attachments as $symbol => $attachment ) {
// A file is required and URLs to files are not currently allowed.
if ( empty( $attachment['file'] ) || preg_match( '#^https?://$#', $attachment['file'] ) ) {
continue;
}
$file_array = array();
$file_path = null;
if ( file_exists( $attachment['file'] ) ) {
$file_path = $attachment['file']; // Could be absolute path to file in plugin.
} elseif ( is_child_theme() && file_exists( get_stylesheet_directory() . '/' . $attachment['file'] ) ) {
$file_path = get_stylesheet_directory() . '/' . $attachment['file'];
} elseif ( file_exists( get_template_directory() . '/' . $attachment['file'] ) ) {
$file_path = get_template_directory() . '/' . $attachment['file'];
} else {
continue;
}
$file_array['name'] = basename( $attachment['file'] );
// Skip file types that are not recognized.
$checked_filetype = wp_check_filetype( $file_array['name'] );
if ( empty( $checked_filetype['type'] ) ) {
continue;
}
// Ensure post_name is set since not automatically derived from post_title for new auto-draft posts.
if ( empty( $attachment['post_name'] ) ) {
if ( ! empty( $attachment['post_title'] ) ) {
$attachment['post_name'] = sanitize_title( $attachment['post_title'] );
} else {
$attachment['post_name'] = sanitize_title( preg_replace( '/\.\w+$/', '', $file_array['name'] ) );
}
}
$attachment_id = null;
$attached_file = null;
if ( isset( $existing_starter_content_posts[ 'attachment:' . $attachment['post_name'] ] ) ) {
$attachment_post = $existing_starter_content_posts[ 'attachment:' . $attachment['post_name'] ];
$attachment_id = $attachment_post->ID;
$attached_file = get_attached_file( $attachment_id );
if ( empty( $attached_file ) || ! file_exists( $attached_file ) ) {
$attachment_id = null;
$attached_file = null;
} elseif ( $this->get_stylesheet() !== get_post_meta( $attachment_post->ID, '_starter_content_theme', true ) ) {
// Re-generate attachment metadata since it was previously generated for a different theme.
$metadata = wp_generate_attachment_metadata( $attachment_post->ID, $attached_file );
wp_update_attachment_metadata( $attachment_id, $metadata );
update_post_meta( $attachment_id, '_starter_content_theme', $this->get_stylesheet() );
}
}
// Insert the attachment auto-draft because it doesn't yet exist or the attached file is gone.
if ( ! $attachment_id ) {
// Copy file to temp location so that original file won't get deleted from theme after sideloading.
$temp_file_name = wp_tempnam( basename( $file_path ) );
if ( $temp_file_name && copy( $file_path, $temp_file_name ) ) {
$file_array['tmp_name'] = $temp_file_name;
}
if ( empty( $file_array['tmp_name'] ) ) {
continue;
}
$attachment_post_data = array_merge(
wp_array_slice_assoc( $attachment, array( 'post_title', 'post_content', 'post_excerpt', 'post_name' ) ),
array(
'post_status' => 'auto-draft', // So attachment will be garbage collected in a week if changeset is never published.
)
);
// In PHP < 5.6 filesize() returns 0 for the temp files unless we clear the file status cache.
// Technically, PHP < 5.6.0 || < 5.5.13 || < 5.4.29 but no need to be so targeted.
// See https://bugs.php.net/bug.php?id=65701
if ( version_compare( PHP_VERSION, '5.6', '<' ) ) {
clearstatcache();
}
$attachment_id = media_handle_sideload( $file_array, 0, null, $attachment_post_data );
if ( is_wp_error( $attachment_id ) ) {
continue;
}
update_post_meta( $attachment_id, '_starter_content_theme', $this->get_stylesheet() );
}
$attachment_ids[ $symbol ] = $attachment_id;
$starter_content_auto_draft_post_ids = array_merge( $starter_content_auto_draft_post_ids, array_values( $attachment_ids ) );
}
}
// Posts & pages.
if ( ! empty( $posts ) ) {
$nav_menus_created_posts = array();
if ( ! empty( $changeset_data['nav_menus_created_posts']['value'] ) ) {
$nav_menus_created_posts = $changeset_data['nav_menus_created_posts']['value'];
}
$existing_posts = array();
if ( ! empty( $nav_menus_created_posts ) ) {
$existing_posts_query = new WP_Query( array(
'post__in' => $nav_menus_created_posts,
'post_status' => 'auto-draft',
'post_type' => 'any',
'number' => -1,
) );
foreach ( $existing_posts_query->posts as $existing_post ) {
$existing_posts[ $existing_post->post_type . ':' . $existing_post->post_name ] = $existing_post;
}
}
foreach ( array_keys( $posts ) as $post_symbol ) {
if ( empty( $posts[ $post_symbol ]['post_type'] ) ) {
continue;
@ -999,24 +1100,36 @@ final class WP_Customize_Manager {
}
// Use existing auto-draft post if one already exists with the same type and name.
if ( isset( $existing_posts[ $post_type . ':' . $post_name ] ) ) {
$posts[ $post_symbol ]['ID'] = $existing_posts[ $post_type . ':' . $post_name ]->ID;
if ( isset( $existing_starter_content_posts[ $post_type . ':' . $post_name ] ) ) {
$posts[ $post_symbol ]['ID'] = $existing_starter_content_posts[ $post_type . ':' . $post_name ]->ID;
continue;
}
// Translate the featured image symbol.
if ( ! empty( $posts[ $post_symbol ]['thumbnail'] )
&& preg_match( '/^{{(?P<symbol>.+)}}$/', $posts[ $post_symbol ]['thumbnail'], $matches )
&& isset( $attachment_ids[ $matches['symbol'] ] ) ) {
$posts[ $post_symbol ]['meta_input']['_thumbnail_id'] = $attachment_ids[ $matches['symbol'] ];
}
if ( ! empty( $posts[ $post_symbol ]['template'] ) ) {
$posts[ $post_symbol ]['meta_input']['_wp_page_template'] = $posts[ $post_symbol ]['template'];
}
$r = $this->nav_menus->insert_auto_draft_post( $posts[ $post_symbol ] );
if ( $r instanceof WP_Post ) {
$posts[ $post_symbol ]['ID'] = $r->ID;
}
}
// The nav_menus_created_posts setting is why nav_menus component is dependency for adding posts.
$starter_content_auto_draft_post_ids = array_merge( $starter_content_auto_draft_post_ids, wp_list_pluck( $posts, 'ID' ) );
}
// The nav_menus_created_posts setting is why nav_menus component is dependency for adding posts.
if ( ! empty( $this->nav_menus ) && ! empty( $starter_content_auto_draft_post_ids ) ) {
$setting_id = 'nav_menus_created_posts';
if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
$nav_menus_created_posts = array_unique( array_merge( $nav_menus_created_posts, wp_list_pluck( $posts, 'ID' ) ) );
$this->set_post_value( $setting_id, array_values( $nav_menus_created_posts ) );
$this->pending_starter_content_settings_ids[] = $setting_id;
}
$this->set_post_value( $setting_id, array_unique( array_values( $starter_content_auto_draft_post_ids ) ) );
$this->pending_starter_content_settings_ids[] = $setting_id;
}
// Nav menus.

View File

@ -1191,8 +1191,10 @@ final class WP_Customize_Nav_Menus {
$post_ids = $setting->post_value();
if ( ! empty( $post_ids ) ) {
foreach ( $post_ids as $post_id ) {
$target_status = 'attachment' === get_post_type( $post_id ) ? 'inherit' : 'publish';
// Note that wp_publish_post() cannot be used because unique slugs need to be assigned.
wp_update_post( array( 'ID' => $post_id, 'post_status' => 'publish' ) );
wp_update_post( array( 'ID' => $post_id, 'post_status' => $target_status ) );
}
}
}

View File

@ -3058,7 +3058,7 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}
$post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash' ) ) ) {
if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) {
$post_status = 'inherit';
}

View File

@ -1986,8 +1986,17 @@ function get_theme_starter_content() {
// Widgets are grouped into sidebars.
case 'widgets' :
foreach ( $config[ $type ] as $sidebar_id => $widgets ) {
foreach ( $widgets as $widget ) {
foreach ( $widgets as $id => $widget ) {
if ( is_array( $widget ) ) {
// Item extends core content.
if ( ! empty( $core_content[ $type ][ $id ] ) ) {
$widget = array(
$core_content[ $type ][ $id ][0],
array_merge( $core_content[ $type ][ $id ][1], $widget ),
);
}
$content[ $type ][ $sidebar_id ][] = $widget;
} elseif ( is_string( $widget ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $widget ] ) ) {
$content[ $type ][ $sidebar_id ][] = $core_content[ $type ][ $widget ];
@ -2007,8 +2016,14 @@ function get_theme_starter_content() {
$content[ $type ][ $nav_menu_location ]['name'] = $nav_menu['name'];
foreach ( $nav_menu['items'] as $nav_menu_item ) {
foreach ( $nav_menu['items'] as $id => $nav_menu_item ) {
if ( is_array( $nav_menu_item ) ) {
// Item extends core content.
if ( ! empty( $core_content[ $type ][ $id ] ) ) {
$nav_menu_item = array_merge( $core_content[ $type ][ $id ], $nav_menu_item );
}
$content[ $type ][ $nav_menu_location ]['items'][] = $nav_menu_item;
} elseif ( is_string( $nav_menu_item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $nav_menu_item ] ) ) {
$content[ $type ][ $nav_menu_location ]['items'][] = $core_content[ $type ][ $nav_menu_item ];
@ -2017,12 +2032,41 @@ function get_theme_starter_content() {
}
break;
// Everything else should map at the next level.
default :
foreach( $config[ $type ] as $i => $item ) {
// Attachments are posts but have special treatment.
case 'attachments' :
foreach ( $config[ $type ] as $id => $item ) {
if ( ! empty( $item['file'] ) ) {
$content[ $type ][ $id ] = $item;
}
}
break;
// All that's left now are posts (besides attachments). Not a default case for the sake of clarity and future work.
case 'posts' :
foreach ( $config[ $type ] as $id => $item ) {
if ( is_array( $item ) ) {
$content[ $type ][ $i ] = $item;
} elseif ( is_string( $item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $item ] ) ) {
// Item extends core content.
if ( ! empty( $core_content[ $type ][ $id ] ) ) {
$item = array_merge( $core_content[ $type ][ $id ], $item );
}
// Enforce a subset of fields.
$content[ $type ][ $id ] = wp_array_slice_assoc(
$item,
array(
'post_type',
'post_title',
'post_excerpt',
'post_name',
'post_content',
'menu_order',
'comment_status',
'thumbnail',
'template',
)
);
} elseif ( is_string( $item ) && ! empty( $core_content[ $type ][ $item ] ) ) {
$content[ $type ][ $item ] = $core_content[ $type ][ $item ];
}
}

View File

@ -313,6 +313,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
*/
function test_import_theme_starter_content() {
wp_set_current_user( self::$admin_user_id );
register_nav_menu( 'top', 'Top' );
global $wp_customize;
$wp_customize = new WP_Customize_Manager();
@ -343,11 +344,22 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
),
'posts' => array(
'home',
'about',
'about' => array(
'template' => 'sample-page-template.php',
),
'blog',
'custom' => array(
'post_type' => 'post',
'post_title' => 'Custom',
'thumbnail' => '{{featured-image-logo}}',
),
),
'attachments' => array(
'featured-image-logo' => array(
'post_title' => 'Featured Image',
'post_content' => 'Attachment Description',
'post_excerpt' => 'Attachment Caption',
'file' => DIR_TESTDATA . '/images/waffles.jpg',
),
),
'options' => array(
@ -394,11 +406,22 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
$this->assertEquals( array( 'text-2', 'meta-3' ), $changeset_values['sidebars_widgets[sidebar-1]'] );
$posts_by_name = array();
$this->assertCount( 5, $changeset_values['nav_menus_created_posts'] );
foreach ( $changeset_values['nav_menus_created_posts'] as $post_id ) {
$post = get_post( $post_id );
$this->assertEquals( 'auto-draft', $post->post_status );
$posts_by_name[ $post->post_name ] = $post->ID;
}
$this->assertEquals( array( 'featured-image', 'home', 'about', 'blog', 'custom' ), array_keys( $posts_by_name ) );
$this->assertEquals( 'Custom', get_post( $posts_by_name['custom'] )->post_title );
$this->assertEquals( 'sample-page-template.php', get_page_template_slug( $posts_by_name['about'] ) );
$this->assertEquals( '', get_page_template_slug( $posts_by_name['blog'] ) );
$this->assertEquals( $posts_by_name['featured-image'], get_post_thumbnail_id( $posts_by_name['custom'] ) );
$this->assertEquals( '', get_post_thumbnail_id( $posts_by_name['blog'] ) );
$attachment_metadata = wp_get_attachment_metadata( $posts_by_name['featured-image'] );
$this->assertEquals( 'Featured Image', get_post( $posts_by_name['featured-image'] )->post_title );
$this->assertArrayHasKey( 'file', $attachment_metadata );
$this->assertContains( 'waffles', $attachment_metadata['file'] );
$this->assertEquals( 'page', $changeset_values['show_on_front'] );
$this->assertEquals( $posts_by_name['home'], $changeset_values['page_on_front'] );
@ -418,6 +441,11 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
$this->assertTrue( $setting_params['starter_content'] );
}
// Ensure that re-importing doesn't cause auto-drafts to balloon.
$wp_customize->import_theme_starter_content();
$changeset_data = $wp_customize->changeset_data();
$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.' );
// Test that saving non-starter content on top of the changeset clears the starter_content flag.
$wp_customize->save_changeset_post( array(
'data' => array(
@ -442,6 +470,16 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
$this->assertArrayNotHasKey( 'starter_content', $changeset_data['blogname'] );
$this->assertNotEquals( $previous_blogdescription, $changeset_data['blogdescription']['value'] );
$this->assertArrayHasKey( 'starter_content', $changeset_data['blogdescription'] );
// Publish.
$this->assertEquals( 'auto-draft', get_post( $posts_by_name['about'] )->post_status );
$this->assertEquals( 'auto-draft', get_post( $posts_by_name['featured-image'] )->post_status );
$this->assertNotEquals( $changeset_data['blogname']['value'], get_option( 'blogname' ) );
$r = $wp_customize->save_changeset_post( array( 'status' => 'publish' ) );
$this->assertInternalType( 'array', $r );
$this->assertEquals( 'publish', get_post( $posts_by_name['about'] )->post_status );
$this->assertEquals( 'inherit', get_post( $posts_by_name['featured-image'] )->post_status );
$this->assertEquals( $changeset_data['blogname']['value'], get_option( 'blogname' ) );
}
/**

View File

@ -41,7 +41,9 @@ class Tests_WP_Theme_Get_Theme_Starter_Content extends WP_UnitTestCase {
'widgets' => array(
'sidebar-1' => array(
'text_business_info',
'text_about',
'text_about' => array(
'title' => 'Our Story',
),
'archives',
'calendar',
'categories',
@ -63,7 +65,9 @@ class Tests_WP_Theme_Get_Theme_Starter_Content extends WP_UnitTestCase {
'page_about',
'page_blog',
'page_news',
'page_contact',
'page_contact' => array(
'title' => 'Email Us',
),
'link_email',
'link_facebook',
'link_foursquare',
@ -86,13 +90,28 @@ class Tests_WP_Theme_Get_Theme_Starter_Content extends WP_UnitTestCase {
'home',
'about',
'contact',
'blog',
'blog' => array(
'template' => 'blog.php',
'post_excerpt' => 'Extended',
),
'news',
'homepage-section',
'unknown',
'custom' => array(
'post_type' => 'post',
'post_title' => 'Custom',
'thumbnail' => '{{featured-image-logo}}',
),
),
'attachments' => array(
'featured-image-logo' => array(
'post_title' => 'Title',
'post_content' => 'Description',
'post_excerpt' => 'Caption',
'file' => DIR_TESTDATA . '/images/waffles.jpg',
),
'featured-image-skipped' => array(
'post_title' => 'Skipped',
),
),
'options' => array(
@ -115,6 +134,9 @@ class Tests_WP_Theme_Get_Theme_Starter_Content extends WP_UnitTestCase {
$this->assertSame( $hydrated_starter_content['options'], $dehydrated_starter_content['options'] );
$this->assertCount( 16, $hydrated_starter_content['nav_menus']['top']['items'], 'Unknown should be dropped, custom should be present.' );
$this->assertCount( 10, $hydrated_starter_content['widgets']['sidebar-1'], 'Unknown should be dropped.' );
$this->assertCount( 1, $hydrated_starter_content['attachments'], 'Attachment with missing file is filtered out.' );
$this->assertArrayHasKey( 'featured-image-logo', $hydrated_starter_content['attachments'] );
$this->assertSame( $dehydrated_starter_content['attachments']['featured-image-logo'], $hydrated_starter_content['attachments']['featured-image-logo'] );
foreach ( $hydrated_starter_content['widgets']['sidebar-1'] as $widget ) {
$this->assertInternalType( 'array', $widget );
@ -123,11 +145,14 @@ class Tests_WP_Theme_Get_Theme_Starter_Content extends WP_UnitTestCase {
$this->assertInternalType( 'array', $widget[1] );
$this->assertArrayHasKey( 'title', $widget[1] );
}
$this->assertEquals( 'text', $hydrated_starter_content['widgets']['sidebar-1'][1][0], 'Core content extended' );
$this->assertEquals( 'Our Story', $hydrated_starter_content['widgets']['sidebar-1'][1][1]['title'], 'Core content extended' );
foreach ( $hydrated_starter_content['nav_menus']['top']['items'] as $nav_menu_item ) {
$this->assertInternalType( 'array', $nav_menu_item );
$this->assertTrue( ! empty( $nav_menu_item['object_id'] ) || ! empty( $nav_menu_item['url'] ) );
}
$this->assertEquals( 'Email Us', $hydrated_starter_content['nav_menus']['top']['items'][4]['title'], 'Core content extended' );
foreach ( $hydrated_starter_content['posts'] as $key => $post ) {
$this->assertInternalType( 'string', $key );
@ -136,6 +161,9 @@ class Tests_WP_Theme_Get_Theme_Starter_Content extends WP_UnitTestCase {
$this->assertArrayHasKey( 'post_type', $post );
$this->assertArrayHasKey( 'post_title', $post );
}
$this->assertEquals( 'Extended', $hydrated_starter_content['posts']['blog']['post_excerpt'], 'Core content extended' );
$this->assertEquals( 'blog.php', $hydrated_starter_content['posts']['blog']['template'], 'Core content extended' );
$this->assertEquals( '{{featured-image-logo}}', $hydrated_starter_content['posts']['custom']['thumbnail'], 'Core content extended' );
}
/**