Make WordPress Core

Ticket #38615: 38615.11.diff

File 38615.11.diff, 10.5 KB (added by westonruter, 8 years ago)

Δ https://github.com/xwp/wordpress-develop/pull/201/files/1da2f6e..d400b0c

  • src/wp-content/themes/twentyseventeen/functions.php

    diff --git src/wp-content/themes/twentyseventeen/functions.php src/wp-content/themes/twentyseventeen/functions.php
    index 1bcb95f..19770b3 100644
    function twentyseventeen_setup() { 
    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                                'post_title' => _x( 'Espresso', 'Theme starter content' ),
     143                                'file' => 'assets/images/espresso.jpg',
     144                        ),
     145                        'featured-image-2' => array(
     146                                'post_title' => _x( 'Sandwich', 'Theme starter content' ),
     147                                'file' => 'assets/images/sandwich.jpg',
     148                        ),
     149                        'featured-image-3' => array(
     150                                'post_title' => _x( 'Coffee', 'Theme starter content' ),
     151                                'file' => 'assets/images/coffee.jpg',
     152                        ),
    130153                ),
    131154
    132155                'options' => array(
  • src/wp-includes/class-wp-customize-manager.php

    diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php
    index ec7c251..01605b6 100644
    final class WP_Customize_Manager { 
    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();
    final class WP_Customize_Manager { 
    965966                        }
    966967                }
    967968
    968                 // Posts & pages.
    969                 if ( ! empty( $posts ) ) {
    970                         $nav_menus_created_posts = array();
    971                         if ( ! empty( $changeset_data['nav_menus_created_posts']['value'] ) ) {
    972                                 $nav_menus_created_posts = $changeset_data['nav_menus_created_posts']['value'];
     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 => $attachment ) {
     981                                if ( empty( $attachment['file'] ) ) {
     982                                        continue;
     983                                }
     984
     985                                // @todo Prevent loading sideloading attachment again if it was already loaded for this theme.
     986                                $file_array = array();
     987                                if ( preg_match( '#^https?://$#', $attachment['file'] ) ) {
     988                                        $file_array['name'] = basename( wp_parse_url( $attachment['file'], PHP_URL_PATH ) );
     989                                        $file_array['tmp_name'] = download_url( $attachment['file_url'] );
     990                                        if ( is_wp_error( $file_array['tmp_name'] ) ) {
     991                                                continue;
     992                                        }
     993                                } else {
     994                                        $file_array['name'] = basename( $attachment['file'] );
     995                                        $file_path = null;
     996                                        if ( file_exists( $attachment['file'] ) ) {
     997                                                $file_path = $attachment['file']; // Could be absolute path to file in plugin.
     998                                        } elseif ( is_child_theme() && file_exists( get_stylesheet_directory() . '/' . $attachment['file'] ) ) {
     999                                                $file_path = get_stylesheet_directory() . '/' . $attachment['file'];
     1000                                        } elseif ( file_exists( get_template_directory() . '/' . $attachment['file'] ) ) {
     1001                                                $file_path = get_template_directory() . '/' . $attachment['file'];
     1002                                        } else {
     1003                                                continue;
     1004                                        }
     1005
     1006                                        // Copy file to temp location so that original file won't get deleted from theme after sideloading.
     1007                                        $tmpfname = wp_tempnam( basename( $file_path ) );
     1008                                        if ( $tmpfname && copy( $file_path, $tmpfname ) ) {
     1009                                                $file_array['tmp_name'] = $tmpfname;
     1010                                        }
     1011                                }
     1012
     1013                                if ( empty( $file_array['tmp_name'] ) ) {
     1014                                        continue;
     1015                                }
     1016
     1017                                /*
     1018                                 * Skip files that don't have the allowed extensions.
     1019                                 * @todo Use wp_get_mime_types() here?
     1020                                 */
     1021                                if ( ! preg_match( '/.(jpe?g|jpe|gif|png)$/i', $file_array['name'] ) ) {
     1022                                        continue;
     1023                                }
     1024
     1025                                // Ensure post_name is set since not automatically derived from post_title for new auto-draft posts.
     1026                                if ( empty( $attachment['post_name'] ) ) {
     1027                                        if ( ! empty( $attachment['post_title'] ) ) {
     1028                                                $attachment['post_name'] = sanitize_title( $attachment['post_title'] );
     1029                                        } else {
     1030                                                $attachment['post_name'] = sanitize_title( preg_replace( '/\.\w+$/', '', $file_array['name'] ) );
     1031                                        }
     1032                                }
     1033
     1034                                $attachment_post_data = array_merge(
     1035                                        wp_array_slice_assoc( $attachment, array( 'post_title', 'post_content', 'post_excerpt', 'post_name' ) ),
     1036                                        array(
     1037                                                'post_status' => 'auto-draft', // So attachment will be garbage collected in a week if changeset is never published.
     1038                                        )
     1039                                );
     1040
     1041                                $attachment_id = media_handle_sideload( $file_array, 0, null, $attachment_post_data );
     1042                                if ( is_wp_error( $attachment_id ) ) {
     1043                                        continue;
     1044                                }
     1045
     1046                                $attachment_ids[ $symbol ] = $attachment_id;
     1047                                $nav_menus_created_posts = array_merge( $nav_menus_created_posts, array_values( $attachment_ids ) );
    9731048                        }
     1049                }
    9741050
     1051                // Posts & pages.
     1052                if ( ! empty( $posts ) ) {
    9751053                        $existing_posts = array();
    9761054                        if ( ! empty( $nav_menus_created_posts ) ) {
    9771055                                $existing_posts_query = new WP_Query( array(
    final class WP_Customize_Manager { 
    10041082                                        continue;
    10051083                                }
    10061084
     1085                                // Translate the featured image symbol
     1086                                if ( ! empty( $posts[ $post_symbol ]['thumbnail'] )
     1087                                        && preg_match( '/^{{(?P<symbol>.+)}}$/', $posts[ $post_symbol ]['thumbnail'], $matches )
     1088                                        && isset( $attachment_ids[ $matches['symbol'] ] ) ) {
     1089                                        $posts[ $post_symbol ]['meta_input']['_thumbnail_id'] = $attachment_ids[ $matches['symbol'] ];
     1090                                }
     1091
     1092                                if ( ! empty( $posts[ $post_symbol ]['template'] ) ) {
     1093                                        $posts[ $post_symbol ]['meta_input']['_wp_page_template'] = $posts[ $post_symbol ]['template'];
     1094                                }
     1095
    10071096                                $r = $this->nav_menus->insert_auto_draft_post( $posts[ $post_symbol ] );
    10081097                                if ( $r instanceof WP_Post ) {
    10091098                                        $posts[ $post_symbol ]['ID'] = $r->ID;
    10101099                                }
    10111100                        }
    10121101
    1013                         // The nav_menus_created_posts setting is why nav_menus component is dependency for adding posts.
     1102                        $nav_menus_created_posts = array_merge( $nav_menus_created_posts, wp_list_pluck( $posts, 'ID' ) );
     1103                }
     1104
     1105                // The nav_menus_created_posts setting is why nav_menus component is dependency for adding posts.
     1106                if ( ! empty( $this->nav_menus ) && ! empty( $nav_menus_created_posts ) ) {
    10141107                        $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;
     1108                        if ( ! empty( $changeset_data[ $setting_id ]['value'] ) ) {
     1109                                $nav_menus_created_posts = array_merge( $nav_menus_created_posts, $changeset_data[ $setting_id ]['value'] );
    10191110                        }
     1111                        $this->set_post_value( $setting_id, array_unique( array_values( $nav_menus_created_posts ) ) );
     1112                        $this->pending_starter_content_settings_ids[] = $setting_id;
    10201113                }
    10211114
    10221115                // Nav menus.
  • src/wp-includes/class-wp-customize-nav-menus.php

    diff --git src/wp-includes/class-wp-customize-nav-menus.php src/wp-includes/class-wp-customize-nav-menus.php
    index 7ce9e18..1305a14 100644
    final class WP_Customize_Nav_Menus { 
    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/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index d3c68ca..620d1e4 100644
    function wp_insert_post( $postarr, $wp_error = false ) { 
    30583058        }
    30593059
    30603060        $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
    3061         if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash' ) ) ) {
     3061        if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) {
    30623062                $post_status = 'inherit';
    30633063        }
    30643064
  • src/wp-includes/theme.php

    diff --git src/wp-includes/theme.php src/wp-includes/theme.php
    index 13bf9ca..8e2690f 100644
    function get_theme_starter_content() { 
    20122012                                }
    20132013                                break;
    20142014
    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;
     2015                        // Attachments are posts but have special treatment.
     2016                        case 'attachments' :
     2017                                foreach ( $config[ $type ] as $id => $item ) {
     2018                                        if ( ! empty( $item['file'] ) ) {
     2019                                                $content[ $type ][ $id ] = $item;
     2020                                        }
     2021                                }
     2022                                break;
     2023
     2024                        // All that's left now are posts (besides attachments). Not a default case for the sake of clarity and future work.
     2025                        case 'posts' :
     2026                                foreach ( $config[ $type ] as $id => $item ) {
     2027                                        if ( is_array( $item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $id ] ) ) {
     2028                                                $content[ $type ][ $id ] = $core_content[ $type ][ $id ];
     2029
     2030                                                // Extra fields - currently locked down because they require special treatment.
     2031                                                foreach ( $item as $key => $value ) {
     2032                                                        if ( 'thumbnail' === $key || 'template' === $key ) {
     2033                                                                $content[ $type ][ $id ][ $key ] = $value;
     2034                                                        }
     2035                                                }
    20202036                                        } elseif ( is_string( $item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $item ] ) ) {
    20212037                                                $content[ $type ][ $item ] = $core_content[ $type ][ $item ];
    20222038                                        }