diff --git a/functions.php b/functions.php index 71a7456..41bda00 100644 --- a/functions.php +++ b/functions.php @@ -192,6 +192,23 @@ function ttgarden_comment_markup( $comment, $args ): void { get_contents( get_template_directory() . '/templates/index.html' ); +} + /** * This is the main output function for the plugin. * This function pulls in the Tumblr theme content and then processes it. @@ -200,7 +217,7 @@ function ttgarden_comment_markup( $comment, $args ): void { */ function ttgarden_page_output(): void { // Get the HTML content from the themes template part. - $content = file_get_contents( get_template_directory() . '/templates/index.html' ); + $content = ttgarden_get_theme_html(); // Shortcodes don't currently have a doing_shortcode() or similar. // So we need a global to track the context. diff --git a/src/Customizer.php b/src/Customizer.php index 2f38482..5de33d0 100644 --- a/src/Customizer.php +++ b/src/Customizer.php @@ -378,16 +378,8 @@ public function theme_specific_options( $wp_customize ): void { ); // Parse the theme HTML. - global $wp_filesystem; - require_once ABSPATH . 'wp-admin/includes/file.php'; - - if ( ! WP_Filesystem() ) { - wp_die( 'Failed to access the filesystem.' ); - } - // Get the HTML content from our templates/index.html file. - $theme_html = $wp_filesystem->get_contents( get_template_directory() . '/templates/index.html' ); - + $theme_html = ttgarden_get_theme_html(); $processor = new \WP_HTML_Tag_Processor( $theme_html ); $select_options = array(); diff --git a/src/FeatureSniffer.php b/src/FeatureSniffer.php index 89e4d09..bf030e8 100644 --- a/src/FeatureSniffer.php +++ b/src/FeatureSniffer.php @@ -98,15 +98,7 @@ public function find_unsupported_features(): void { if ( '' !== $this->html ) { $html = strtolower( $this->html ); } else { - global $wp_filesystem; - require_once ABSPATH . 'wp-admin/includes/file.php'; - - if ( ! WP_Filesystem() ) { - wp_die( 'Failed to access the filesystem.' ); - } - - // Get the HTML content from our templates/index.html file. - $html = strtolower( $wp_filesystem->get_contents( get_template_directory() . '/templates/index.html' ) ); + $html = strtolower( ttgarden_get_theme_html() ); } // Check each unsupported feature. diff --git a/src/Hooks.php b/src/Hooks.php index 9e4f760..ac1b69f 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -46,7 +46,7 @@ public function initialize( $is_ttgarden_active ): void { add_filter( 'wp_prepare_themes_for_js', array( $this, 'prepare_themes_for_js' ) ); - // Only run these if the TumblrThemeGarden theme is active. + // Only run these if the TumblrThemeGarden theme is active or we're in a customizer preview. if ( $this->is_ttgarden_active ) { add_filter( 'template_include', array( $this, 'template_include' ) ); diff --git a/src/Plugin.php b/src/Plugin.php index 705ccd9..2c5d8c3 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -27,6 +27,16 @@ class Plugin { */ public bool $ttgarden_active = false; + /** + * The customize preview status. + * + * @since 1.0.0 + * @version 1.0.0 + * + * @var bool + */ + public bool $customize_preview = false; + /** * The hooks component. * @@ -138,10 +148,14 @@ public static function get_instance(): self { * @return void */ public function initialize(): void { - $theme = wp_get_theme(); - $theme_tags = $theme->get( 'Tags' ); - - $this->ttgarden_active = ( is_array( $theme_tags ) ) ? in_array( 'tumblr-theme', $theme_tags, true ) : false; + $theme = wp_get_theme(); + $theme_tags = $theme->get( 'Tags' ); + $is_tumblr_theme_active = is_array( $theme_tags ) && in_array( 'tumblr-theme', $theme_tags, true ); + + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Get is only checked, not consumed. + $theme_slug = isset( $_GET['theme'] ) ? sanitize_text_field( wp_unslash( $_GET['theme'] ) ) : ''; + $this->customize_preview = is_customize_preview() && str_contains( $theme_slug, 'tumblr' ); + $this->ttgarden_active = $is_tumblr_theme_active || $this->customize_preview; // Setup all plugin hooks. $this->hooks = new Hooks(); diff --git a/tumblr-theme-garden.php b/tumblr-theme-garden.php index 1c9e39e..dc55320 100644 --- a/tumblr-theme-garden.php +++ b/tumblr-theme-garden.php @@ -77,4 +77,4 @@ static function () { require_once TTGARDEN_PATH . '/vendor/autoload.php'; require_once TTGARDEN_PATH . 'functions.php'; -add_action( 'plugins_loaded', array( ttgarden_get_plugin_instance(), 'initialize' ) ); +add_action( 'after_setup_theme', array( ttgarden_get_plugin_instance(), 'initialize' ) );