Make WordPress Core

Ticket #42411: 42411.1.diff

File 42411.1.diff, 9.7 KB (added by westonruter, 6 years ago)

Δ https://github.com/xwp/wordpress-develop/pull/300/files/ef52a05..8c9d653

  • src/wp-admin/customize.php

    diff --git src/wp-admin/customize.php src/wp-admin/customize.php
    index 7f9e5a9dc7..d22b613eeb 100644
    if ( $wp_customize->changeset_post_id() ) { 
    6060        }
    6161}
    6262
     63$is_theme_switch_unavailable = (
     64        ! $wp_customize->is_theme_active()
     65        &&
     66        ! $wp_customize->branching()
     67        &&
     68        $wp_customize->changeset_post_id()
     69        &&
     70        in_array(
     71                get_post_status( $wp_customize->changeset_post_id() ),
     72                array_diff( get_post_stati(), array( 'auto-draft', 'publish', 'trash', 'inherit', 'private' ) ),
     73                true
     74        )
     75);
     76if ( $is_theme_switch_unavailable ) {
     77        $message = sprintf(
     78                /* translators: %s is URL to Customizer with the Publish Settings section auto-focused */
     79                __( 'Because you have drafted or scheduled changes, live previewing other themes is currently disabled. Please <a href="%s">publish your changes</a>, or wait until they publish to preview new themes. ' ),
     80                add_query_arg(
     81                        array(
     82                                'autofocus[section]' => 'publish_settings',
     83                                'return' => admin_url( 'themes.php' ),
     84                        ),
     85                        admin_url( 'customize.php' )
     86                )
     87        );
     88
     89        wp_die(
     90                '<h1>' . __( 'Cheatin&#8217; uh?' ) . '</h1>' .
     91                '<p>' . $message . '</p>',
     92                403
     93        );
     94}
     95
    6396
    6497wp_reset_vars( array( 'url', 'return', 'autofocus' ) );
    6598if ( ! empty( $url ) ) {
  • src/wp-admin/theme-install.php

    diff --git src/wp-admin/theme-install.php src/wp-admin/theme-install.php
    index 43f105f377..0ede57849e 100644
    get_current_screen()->set_help_sidebar( 
    123123
    124124include(ABSPATH . 'wp-admin/admin-header.php');
    125125
     126$is_customize_preview_available = true;
     127
     128/** This filter is documented in wp-includes/class-wp-customize-manager.php */
     129if ( ! apply_filters( 'customize_changeset_branching', false ) ) {
     130        $is_customize_preview_available = count( get_posts( array(
     131                'post_type' => 'customize_changeset',
     132                'post_status' => array_diff( get_post_stati(), array( 'auto-draft', 'publish', 'trash', 'inherit', 'private' ) ),
     133                'author' => 'any',
     134                'posts_per_page' => 1,
     135                'no_found_rows' => true,
     136                'cache_results' => true,
     137                'update_post_meta_cache' => false,
     138                'update_post_term_cache' => false,
     139                'lazy_load_term_meta' => false,
     140        ) ) ) === 0;
     141}
     142
    126143?>
    127144<div class="wrap">
    128145        <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
    include(ABSPATH . 'wp-admin/admin-header.php'); 
    150167                <p><?php _e( 'The Theme Installer screen requires JavaScript.' ); ?></p>
    151168        </div>
    152169
     170        <?php if ( ! $is_customize_preview_available ) : ?>
     171                <div class="notice notice-info">
     172                        <p>
     173                                <?php
     174                                printf(
     175                                        /* translators: %s is URL to Customizer with the Publish Settings section auto-focused */
     176                                        __( 'Because you have drafted or scheduled changes, live previewing other themes is currently disabled. Please <a href="%s">publish your changes</a>, or wait until they publish to preview new themes. ' ),
     177                                        add_query_arg( 'autofocus[section]', 'publish_settings', admin_url( 'customize.php' ) )
     178                                );
     179                                ?>
     180                        </p>
     181                </div>
     182        <?php endif; ?>
     183
    153184        <div class="upload-theme">
    154185        <?php install_themes_upload(); ?>
    155186        </div>
    if ( $tab ) { 
    277308                                <# if ( data.activate_url ) { #>
    278309                                        <a class="button button-primary activate" href="{{ data.activate_url }}" aria-label="<?php echo esc_attr( $aria_label ); ?>"><?php _e( 'Activate' ); ?></a>
    279310                                <# } #>
    280                                 <# if ( data.customize_url ) { #>
     311                                <# if ( data.customize_url && <?php echo wp_json_encode( $is_customize_preview_available ); ?> ) { #>
    281312                                        <a class="button load-customize" href="{{ data.customize_url }}"><?php _e( 'Live Preview' ); ?></a>
    282313                                <# } else { #>
    283314                                        <button class="button preview install-theme-preview"><?php _e( 'Preview' ); ?></button>
  • src/wp-admin/themes.php

    diff --git src/wp-admin/themes.php src/wp-admin/themes.php
    index 9218983239..6ffa58659b 100644
    wp_enqueue_script( 'theme' ); 
    148148wp_enqueue_script( 'updates' );
    149149
    150150require_once( ABSPATH . 'wp-admin/admin-header.php' );
     151
     152$is_customize_preview_available = true;
     153
     154/** This filter is documented in wp-includes/class-wp-customize-manager.php */
     155if ( ! apply_filters( 'customize_changeset_branching', false ) ) {
     156        $is_customize_preview_available = count( get_posts( array(
     157                'post_type' => 'customize_changeset',
     158                'post_status' => array_diff( get_post_stati(), array( 'auto-draft', 'publish', 'trash', 'inherit', 'private' ) ),
     159                'author' => 'any',
     160                'posts_per_page' => 1,
     161                'no_found_rows' => true,
     162                'cache_results' => true,
     163                'update_post_meta_cache' => false,
     164                'update_post_term_cache' => false,
     165                'lazy_load_term_meta' => false,
     166        ) ) ) === 0;
     167}
     168
    151169?>
    152170
    153171<div class="wrap">
    if ( ! validate_current_theme() || isset( $_GET['broken'] ) ) : ?> 
    178196<?php
    179197endif;
    180198
     199if ( ! $is_customize_preview_available ) :
     200        ?>
     201        <div class="notice notice-info">
     202                <p>
     203                        <?php
     204                        printf(
     205                                /* translators: %s is URL to Customizer with the Publish Settings section auto-focused */
     206                                __( 'Because you have drafted or scheduled changes, live previewing other themes is currently disabled. Please <a href="%s">publish your changes</a>, or wait until they publish to preview new themes. ' ),
     207                                add_query_arg( 'autofocus[section]', 'publish_settings', admin_url( 'customize.php' ) )
     208                        );
     209                        ?>
     210                </p>
     211        </div>
     212        <?php
     213endif;
     214
    181215$ct = wp_get_theme();
    182216
    183217if ( $ct->errors() && ( ! is_multisite() || current_user_can( 'manage_network_themes' ) ) ) {
    foreach ( $themes as $theme ) : 
    296330                        $aria_label = sprintf( _x( 'Activate %s', 'theme' ), '{{ data.name }}' );
    297331                        ?>
    298332                        <a class="button activate" href="<?php echo $theme['actions']['activate']; ?>" aria-label="<?php echo esc_attr( $aria_label ); ?>"><?php _e( 'Activate' ); ?></a>
    299                         <?php if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { ?>
     333                        <?php if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) && $is_customize_preview_available ) { ?>
    300334                                <a class="button button-primary load-customize hide-if-no-customize" href="<?php echo $theme['actions']['customize']; ?>"><?php _e( 'Live Preview' ); ?></a>
    301335                        <?php } ?>
    302336                <?php } ?>
    $can_install = current_user_can( 'install_themes' ); 
    431465                                $aria_label = sprintf( _x( 'Activate %s', 'theme' ), '{{ data.name }}' );
    432466                                ?>
    433467                                <a class="button activate" href="{{{ data.actions.activate }}}" aria-label="<?php echo $aria_label; ?>"><?php _e( 'Activate' ); ?></a>
    434                                 <a class="button button-primary load-customize hide-if-no-customize" href="{{{ data.actions.customize }}}"><?php _e( 'Live Preview' ); ?></a>
     468                                <?php if ( $is_customize_preview_available ) : ?>
     469                                        <a class="button button-primary load-customize hide-if-no-customize" href="{{{ data.actions.customize }}}"><?php _e( 'Live Preview' ); ?></a>
     470                                <?php endif; ?>
    435471                        <# } #>
    436472                </div>
    437473        </div>
    $can_install = current_user_can( 'install_themes' ); 
    492528                                <# if ( data.actions.activate ) { #>
    493529                                        <a href="{{{ data.actions.activate }}}" class="button activate" aria-label="<?php echo $aria_label; ?>"><?php _e( 'Activate' ); ?></a>
    494530                                <# } #>
    495                                 <a href="{{{ data.actions.customize }}}" class="button button-primary load-customize hide-if-no-customize"><?php _e( 'Live Preview' ); ?></a>
     531                                <?php if ( $is_customize_preview_available ) : ?>
     532                                        <a href="{{{ data.actions.customize }}}" class="button button-primary load-customize hide-if-no-customize"><?php _e( 'Live Preview' ); ?></a>
     533                                <?php endif; ?>
    496534                        </div>
    497535
    498536                        <# if ( ! data.active && data.actions['delete'] ) { #>
  • 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 12256d2173..3cd93ce423 100644
    final class WP_Customize_Manager { 
    612612                if ( empty( $this->_changeset_uuid ) ) {
    613613                        $changeset_uuid = null;
    614614
    615                         if ( ! $this->branching() && $this->is_theme_active() ) {
     615                        if ( ! $this->branching() ) {
    616616                                $unpublished_changeset_posts = $this->get_changeset_posts( array(
    617617                                        'post_status' => array_diff( get_post_stati(), array( 'auto-draft', 'publish', 'trash', 'inherit', 'private' ) ),
    618618                                        'exclude_restore_dismissed' => false,
    final class WP_Customize_Manager { 
    782782                 *
    783783                 * @since 4.9.0
    784784                 *
    785                  * @param bool                 $allow_branching Whether branching is allowed. If `false`, the default,
    786                  *                                              then only one saved changeset exists at a time.
    787                  * @param WP_Customize_Manager $wp_customize    Manager instance.
     785                 * @param bool $allow_branching Whether branching is allowed. If `false`, the default,
     786                 *                              then only one saved changeset exists at a time.
    788787                 */
    789                 $this->branching = apply_filters( 'customize_changeset_branching', $this->branching, $this );
     788                $this->branching = apply_filters( 'customize_changeset_branching', $this->branching );
    790789
    791790                return $this->branching;
    792791        }
  • tests/phpunit/tests/customize/manager.php

    diff --git tests/phpunit/tests/customize/manager.php tests/phpunit/tests/customize/manager.php
    index 1152efd983..18f00afb1b 100644
    class Tests_WP_Customize_Manager extends WP_UnitTestCase { 
    197197                ) );
    198198                $this->assertNotContains( $wp_customize->changeset_uuid(), array( $uuid1, $uuid2 ) );
    199199                $this->assertEmpty( $wp_customize->changeset_post_id() );
    200 
    201                 // Make sure existing changeset is not autoloaded in the case of previewing a theme switch.
    202                 switch_theme( 'twentyseventeen' );
    203                 $wp_customize = new WP_Customize_Manager( array(
    204                         'changeset_uuid' => false, // Cause UUID to be deferred.
    205                         'branching' => false,
    206                         'theme' => 'twentyfifteen',
    207                 ) );
    208                 $this->assertEmpty( $wp_customize->changeset_post_id() );
    209200        }
    210201
    211202        /**