Making WordPress.org

Changeset 10480


Ignore:
Timestamp:
12/02/2020 04:24:34 AM (4 years ago)
Author:
dd32
Message:

Theme Previews: Starter Content: Polyfill in is_customize_preview() to avoid having to statically define the starter content files to include for 2020/2021/other themes.

See #30.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wp-themes.com/public_html/wp-content/mu-plugins/pub/starter-content.php

    r10450 r10480  
    1515    private $mapping = array();
    1616
     17    public function __construct() {
     18        // This plugin relies upon the object cache being the internal WordPress per-request cache.
     19        if ( wp_using_ext_object_cache() ) {
     20            return;
     21        }
     22
     23        // Some themes require is_customize_preview() before loading starter content.
     24        add_action( 'after_setup_theme', array( $this, 'pre_after_setup_theme' ), -1 * PHP_INT_MAX );
     25
     26        add_action( 'init', array( $this, 'init' ) );
     27        add_action( 'wp_head', array( $this, 'head_debug_info' ), 1 );
     28    }
     29
     30    public function pre_after_setup_theme() {
     31        if ( class_exists( 'WP_Customize_Manager' ) ) {
     32            return;
     33        }
     34
     35        if ( ! $this->is_supported_theme() ) {
     36            return;
     37        }
     38
     39        // Disable customizer loading parameters.
     40        unset(
     41            $_REQUEST['wp_customize'],
     42            $_REQUEST['customize_changeset_uuid'],
     43            $_GET['customize_changeset_uuid'],
     44            $_POST['customize_changeset_uuid']
     45        );
     46
     47        // Define a custom `WP_Customize_Manager` class to force `is_customize_preview()` truthful.
     48        polyfill_wp_customize_manager();
     49
     50        $GLOBALS['wp_customize'] = new WP_Customize_Manager();
     51
     52        // Undo this the override after this action is fully run.
     53        add_action(
     54            'after_setup_theme',
     55            function() {
     56                unset( $GLOBALS['wp_customize'] );
     57            },
     58            PHP_INT_MAX
     59        );
     60    }
     61
     62    private function is_supported_theme() {
     63        $whitelisted_themes = array(
     64            // Other default themes don't have supported starter content.
     65            'twentyseventeen',
     66            'twentytwenty',
     67            'twentytwentyone',
     68        );
     69
     70        return in_array( get_template(), $whitelisted_themes );
     71    }
     72
    1773    public function init() {
     74        if ( ! $this->is_supported_theme() ) {
     75            return;
     76        }
     77
    1878        $this->set_starter_content();
    1979
     
    3090        $this->filter_sidebars();
    3191        $this->filter_theme_mods();
     92    }
     93
     94    public function head_debug_info() {
     95        if ( current_theme_supports( 'starter-content' ) && $this->starter_content ) {
     96            echo "\n<!-- Preview using Starter content -->\n";
     97        } else {
     98            echo "\n<!-- Preview is not using Starter content -->\n";
     99        }
     100       
    32101    }
    33102
     
    442511}
    443512
    444 add_action(
    445     'init',
    446     function () {
    447         // This plugin relies upon the object cache being the internal WordPress per-request cache.
    448         if ( wp_using_ext_object_cache() ) {
    449             return;
    450         }
    451 
    452         $whitelisted_themes = array(
    453             // Other default themes don't have supported starter content.
    454             'twentyseventeen',
    455             'twentytwenty',
    456             'twentytwentyone',
    457         );
    458 
    459         if ( ! in_array( get_template(), $whitelisted_themes ) ) {
    460             return;
    461         }
    462 
    463         // These themes don't always load the starter content, so we'll have to do it for them..
    464         if ( 'twentytwenty' === get_template() ) {
    465             require_once get_template_directory() . '/inc/starter-content.php';
    466             add_theme_support( 'starter-content', twentytwenty_get_starter_content() );
    467         } elseif ( 'twentytwentyone' === get_template() ) {
    468             require_once get_template_directory() . '/inc/starter-content.php';
    469             add_theme_support( 'starter-content', twenty_twenty_one_get_starter_content() );
    470         }
    471 
    472         ( new Starter_Content() )->init();
    473     }
    474 );
     513new Starter_Content();
     514
     515/**
     516 * Define a custom WP_Customize_Manager class that claims this request is a customizer preview request.
     517 *
     518 * This is needed as many themes (including 2020/2021) limit starter content to customizer preview requests.
     519 *
     520 * As PHP cannot handle nested classes, this is defined in a function outside of the above class.
     521 */
     522function polyfill_wp_customize_manager() {
     523    class WP_Customize_Manager {
     524        // Yes, this is a theme preview.
     525        function is_preview() {
     526            return true;
     527        }
     528
     529        // Ensure that any calls return false too.
     530        function __get( $property ) {
     531            return false;
     532        }
     533        function __call( $method, $args ) {
     534            return false;
     535        }
     536    }
     537
     538    // This needs to be defined in the global namespace.
     539    class_alias( __NAMESPACE__ . '\WP_Customize_Manager', 'WP_Customize_Manager', false );
     540}
Note: See TracChangeset for help on using the changeset viewer.