Make WordPress Core

Ticket #38615: 38615.2.diff

File 38615.2.diff, 5.8 KB (added by helen, 8 years ago)
  • src/wp-content/themes/twentyseventeen/functions.php

     
    123123
    124124                'posts' => array(
    125125                        'home',
    126                         'about',
     126                        'about' => array(
     127                                'thumbnail' => '{{featured-image-1}}',
     128                        ),
    127129                        'contact',
    128130                        'blog',
    129131                        'homepage-section',
    130132                ),
    131133
     134                'attachments' => array(
     135                        'featured-image-1' => array(
     136                                'file_url' => '/assets/images/espresso.jpg',
     137                        ),
     138                ),
     139
    132140                'options' => array(
    133141                        'show_on_front' => 'page',
    134142                        'page_on_front' => '{{home}}',
  • 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['posts'] ) && ! 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                // Attachments are technically posts but handled differently.
     970                if ( ! empty( $attachments ) ) {
     971// omg are you serious
     972// how can I avoid these includes omg
     973                        require_once( ABSPATH . 'wp-admin/includes/file.php' );
     974                        require_once( ABSPATH . 'wp-admin/includes/media.php' );
     975                        require_once( ABSPATH . 'wp-admin/includes/image.php' );
     976
     977                        $attachment_ids = array();
     978
     979                        foreach( $attachments as $symbol => $attributes ) {
     980                                $file = get_stylesheet_directory_uri() . $attributes['file_url'];
     981
     982                                // We have to replicate logic from inside media_sideload_image() because WordPress.
     983                                // See https://core.trac.wordpress.org/ticket/19629
     984                                // Set variables for storage, fix file filename for query strings.
     985                                preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
     986                                if ( ! $matches ) {
     987                                        continue;
     988                                }
     989
     990                                $file_array = array();
     991                                $file_array['name'] = basename( $matches[0] );
     992                                $file_array['tmp_name'] = download_url( $file );
     993
     994                                if ( is_wp_error( $file_array['tmp_name'] ) ) {
     995                                        continue;
     996                                }
     997
     998                                $attachment_id = media_handle_sideload( $file_array, 0 );
     999                                // End duplicated logic
     1000                               
     1001                                if ( is_wp_error( $attachment_id ) ) {
     1002                                        continue;
     1003                                }
     1004// Problem: Images are missing the "full" size from metadata. What?
     1005                                // Set this to auto-draft for garbage collection later
     1006// Is this working?
     1007// WHAT LETS THE CUSTOMIZER KNOW TO TRANSITION THIS AFTER WE ARE DONE?
     1008                                wp_insert_attachment( array( 'ID' => $attachment_id, 'post_status' => 'auto-draft' ) );
     1009                                $attachment_ids[ $symbol ] = $attachment_id;
     1010                        }
     1011                }
     1012
    9681013                // Posts & pages.
    9691014                if ( ! empty( $posts ) ) {
    9701015                        $nav_menus_created_posts = array();
     
    10041049                                        continue;
    10051050                                }
    10061051
     1052                                // Translate the featured image symbol
     1053                                if ( ! empty( $posts[ $post_symbol ]['thumbnail'] )
     1054                                        && preg_match( '/^{{(?P<symbol>.+)}}$/', $posts[ $post_symbol ]['thumbnail'], $matches )
     1055                                        && isset( $attachment_ids[ $matches['symbol'] ] ) ) {
     1056                                        $posts[ $post_symbol ][ 'meta_input' ][ '_thumbnail_id' ] = $attachment_ids[ $matches['symbol'] ];
     1057                                }
     1058
    10071059                                $r = $this->nav_menus->insert_auto_draft_post( $posts[ $post_symbol ] );
    10081060                                if ( $r instanceof WP_Post ) {
    10091061                                        $posts[ $post_symbol ]['ID'] = $r->ID;
  • src/wp-includes/theme.php

     
    18301830                $config = array();
    18311831        }
    18321832
     1833        /**
     1834         * Filters the allowed extra post args for starter content.
     1835         *
     1836         * @since 4.7.0
     1837         *
     1838         * @param array $allowed_post_args Array of allowed post args.
     1839         * @param array $config    Array of theme-specific starter content configuration.
     1840         */
     1841        $allowed_post_args = apply_filters( 'starter_content_allowed_post_args', array( 'thumbnail', 'page_template' ), $config );
     1842
    18331843        $core_content = array(
    18341844                'widgets' => array(
    18351845                        'text_business_info' => array( 'text', array(
     
    19721982
    19731983        foreach ( $config as $type => $args ) {
    19741984                switch( $type ) {
    1975                         // Use options and theme_mods as-is.
     1985                        // Use options and theme_mods as-is. Also, attachments, for now.
    19761986                        case 'options' :
    19771987                        case 'theme_mods' :
     1988                        case 'attachments' :
    19781989                                $content[ $type ] = $config[ $type ];
    19791990                                break;
    19801991
     
    20122023                                }
    20132024                                break;
    20142025
    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;
     2026                        // All that's left now are posts (besides attachments). Not a default case for the sake of clarity and future work.
     2027                        case 'posts' :
     2028                                foreach( $config[ $type ] as $id => $item ) {
     2029                                        // Posts and pages can be passed certain other args
     2030                                        if ( is_array( $item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $id ] ) ) {
     2031                                                $content[ $type ][ $id ] = $core_content[ $type ][ $id ];
     2032
     2033                                                foreach( $item as $key => $value ) {
     2034                                                        if ( in_array( $key, $allowed_post_args ) ) {
     2035                                                                $content[ $type ][ $id ][ $key ] = $value;
     2036                                                        }
     2037                                                }
    20202038                                        } elseif ( is_string( $item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $item ] ) ) {
    20212039                                                $content[ $type ][ $item ] = $core_content[ $type ][ $item ];
    20222040                                        }