Make WordPress Core

Ticket #38615: 38615.5.diff

File 38615.5.diff, 8.1 KB (added by flixos90, 8 years ago)
  • src/wp-content/themes/twentyseventeen/functions.php

     
    123123
    124124                'posts' => array(
    125125                        'home',
    126                         'about',
    127                         'contact',
    128                         'blog',
    129                         'homepage-section',
     126                        'about' => array(
     127                                'thumbnail' => '{{featured-image-2}}',
     128                        ),
     129                        'contact' => array(
     130                                'thumbnail' => '{{featured-image-1}}',
     131                        ),
     132                        'blog' => array(
     133                                'thumbnail' => '{{featured-image-3}}',
     134                        ),
     135                        'homepage-section' => array(
     136                                'thumbnail' => '{{featured-image-1}}',
     137                        ),
     138                ),
     139
     140                'attachments' => array(
     141                        'featured-image-1' => array(
     142                                'file_url' => '/assets/images/espresso.jpg',
     143                        ),
     144                        'featured-image-2' => array(
     145                                'file_url' => '/assets/images/sandwich.jpg',
     146                        ),
     147                        'featured-image-3' => array(
     148                                'file_url' => '/assets/images/coffee.jpg',
     149                        ),
    130150                ),
    131151
    132152                'options' => array(
  • src/wp-includes/class-wp-customize-manager.php

     
    916916                }
    917917
    918918                $sidebars_widgets = isset( $starter_content['widgets'] ) && ! empty( $this->widgets ) ? $starter_content['widgets'] : array();
     919                $attachments = isset( $starter_content['attachments'] ) && ! empty( $this->nav_menus ) ? $starter_content['attachments'] : array();
    919920                $posts = isset( $starter_content['posts'] ) && ! empty( $this->nav_menus ) ? $starter_content['posts'] : array();
    920921                $options = isset( $starter_content['options'] ) ? $starter_content['options'] : array();
    921922                $nav_menus = isset( $starter_content['nav_menus'] ) && ! empty( $this->nav_menus ) ? $starter_content['nav_menus'] : array();
     
    965966                        }
    966967                }
    967968
     969                $nav_menus_created_posts = array();
     970
     971                // Attachments are technically posts but handled differently.
     972                if ( ! empty( $attachments ) ) {
     973                        // Such is The WordPress Way.
     974                        require_once( ABSPATH . 'wp-admin/includes/file.php' );
     975                        require_once( ABSPATH . 'wp-admin/includes/media.php' );
     976                        require_once( ABSPATH . 'wp-admin/includes/image.php' );
     977
     978                        $attachment_ids = array();
     979
     980                        foreach( $attachments as $symbol => $attributes ) {
     981                                if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . $attributes['file_url'] ) ) {
     982                                        $file = get_template_directory_uri() . $attributes['file_url'];
     983                                } else {
     984                                        $file = get_stylesheet_directory_uri() . $attributes['file_url'];
     985                                }
     986
     987                                // We have to replicate logic from inside media_sideload_image() because WordPress.
     988                                // See https://core.trac.wordpress.org/ticket/19629
     989                                // Set variables for storage, fix file filename for query strings.
     990                                preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
     991                                if ( ! $matches ) {
     992                                        continue;
     993                                }
     994
     995                                $file_array = array();
     996                                $file_array['name'] = basename( $matches[0] );
     997                                $file_array['tmp_name'] = download_url( $file );
     998
     999                                if ( is_wp_error( $file_array['tmp_name'] ) ) {
     1000                                        continue;
     1001                                }
     1002
     1003                                $attachment_id = media_handle_sideload( $file_array, 0 );
     1004                                // End duplicated logic
     1005
     1006                                if ( is_wp_error( $attachment_id ) ) {
     1007                                        continue;
     1008                                }
     1009
     1010                                // Set this to auto-draft for garbage collection later
     1011// Is this working? I don't think this is working.
     1012                                wp_update_post( array( 'ID' => $attachment_id, 'post_status' => 'auto-draft' ) );
     1013                                $attachment_ids[ $symbol ] = $attachment_id;
     1014
     1015                                $nav_menus_created_posts = array_merge( $nav_menus_created_posts, array_values( $attachment_ids ) );
     1016                        }
     1017                }
     1018
    9681019                // Posts & pages.
    9691020                if ( ! empty( $posts ) ) {
    970                         $nav_menus_created_posts = array();
    9711021                        if ( ! empty( $changeset_data['nav_menus_created_posts']['value'] ) ) {
    9721022                                $nav_menus_created_posts = $changeset_data['nav_menus_created_posts']['value'];
    9731023                        }
     
    10041054                                        continue;
    10051055                                }
    10061056
     1057                                // Translate the featured image symbol
     1058                                if ( ! empty( $posts[ $post_symbol ]['thumbnail'] )
     1059                                        && preg_match( '/^{{(?P<symbol>.+)}}$/', $posts[ $post_symbol ]['thumbnail'], $matches )
     1060                                        && isset( $attachment_ids[ $matches['symbol'] ] ) ) {
     1061                                        $posts[ $post_symbol ][ 'meta_input' ][ '_thumbnail_id' ] = $attachment_ids[ $matches['symbol'] ];
     1062                                }
     1063
     1064                                if ( ! empty( $posts[ $post_symbol ]['page_template'] ) ) {
     1065                                        $posts[ $post_symbol ][ 'meta_input' ][ '_wp_page_template' ] = $posts[ $post_symbol ]['page_template'];
     1066                                }
     1067
    10071068                                $r = $this->nav_menus->insert_auto_draft_post( $posts[ $post_symbol ] );
    10081069                                if ( $r instanceof WP_Post ) {
    10091070                                        $posts[ $post_symbol ]['ID'] = $r->ID;
    10101071                                }
    10111072                        }
    10121073
    1013                         // The nav_menus_created_posts setting is why nav_menus component is dependency for adding posts.
     1074                        $nav_menus_created_posts = array_merge( $nav_menus_created_posts, wp_list_pluck( $posts, 'ID' ) );
     1075                }
     1076
     1077                // The nav_menus_created_posts setting is why nav_menus component is dependency for adding posts.
     1078                if ( ! empty( $this->nav_menus ) && ! empty( $nav_menus_created_posts ) ) {
    10141079                        $setting_id = 'nav_menus_created_posts';
    1015                         if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
    1016                                 $nav_menus_created_posts = array_unique( array_merge( $nav_menus_created_posts, wp_list_pluck( $posts, 'ID' ) ) );
    1017                                 $this->set_post_value( $setting_id, array_values( $nav_menus_created_posts ) );
    1018                                 $this->pending_starter_content_settings_ids[] = $setting_id;
     1080                        if ( ! empty( $changeset_data[ $setting_id ] ) ) {
     1081                                $nav_menus_created_posts = array_merge( $nav_menus_created_posts, $changeset_data[ $setting_id ] );
    10191082                        }
     1083                        $this->set_post_value( $setting_id, array_unique( array_values( $nav_menus_created_posts ) ) );
     1084                        $this->pending_starter_content_settings_ids[] = $setting_id;
    10201085                }
    10211086
    10221087                // Nav menus.
  • src/wp-includes/class-wp-customize-nav-menus.php

     
    11911191                $post_ids = $setting->post_value();
    11921192                if ( ! empty( $post_ids ) ) {
    11931193                        foreach ( $post_ids as $post_id ) {
     1194                                $target_status = 'attachment' === get_post_type( $post_id ) ? 'inherit' : 'publish';
     1195
    11941196                                // Note that wp_publish_post() cannot be used because unique slugs need to be assigned.
    1195                                 wp_update_post( array( 'ID' => $post_id, 'post_status' => 'publish' ) );
     1197                                wp_update_post( array( 'ID' => $post_id, 'post_status' => $target_status ) );
    11961198                        }
    11971199                }
    11981200        }
  • src/wp-includes/theme.php

     
    19721972
    19731973        foreach ( $config as $type => $args ) {
    19741974                switch( $type ) {
    1975                         // Use options and theme_mods as-is.
     1975                        // Use options and theme_mods as-is. Also, attachments, for now.
    19761976                        case 'options' :
    19771977                        case 'theme_mods' :
     1978                        case 'attachments' :
    19781979                                $content[ $type ] = $config[ $type ];
    19791980                                break;
    19801981
     
    20122013                                }
    20132014                                break;
    20142015
    2015                         // Everything else should map at the next level.
    2016                         default :
    2017                                 foreach( $config[ $type ] as $i => $item ) {
    2018                                         if ( is_array( $item ) ) {
    2019                                                 $content[ $type ][ $i ] = $item;
     2016                        // All that's left now are posts (besides attachments). Not a default case for the sake of clarity and future work.
     2017                        case 'posts' :
     2018                                foreach( $config[ $type ] as $id => $item ) {
     2019                                        // Posts and pages can be passed certain other args
     2020                                        if ( is_array( $item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $id ] ) ) {
     2021                                                $content[ $type ][ $id ] = $core_content[ $type ][ $id ];
     2022
     2023                                                foreach( $item as $key => $value ) {
     2024                                                        // This is currently locked down.
     2025                                                        if ( 'thumbnail' === $key || 'page_template' === $key ) {
     2026                                                                $content[ $type ][ $id ][ $key ] = $value;
     2027                                                        }
     2028                                                }
    20202029                                        } elseif ( is_string( $item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $item ] ) ) {
    20212030                                                $content[ $type ][ $item ] = $core_content[ $type ][ $item ];
    20222031                                        }