Skip to content

Commit

Permalink
Merge pull request #106 from Automattic/roccotripaldi/fix-live-preview
Browse files Browse the repository at this point in the history
Fix live preview
  • Loading branch information
roccotripaldi authored Jan 13, 2025
2 parents c495c80 + db37838 commit 2afc1cb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
19 changes: 18 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ function ttgarden_comment_markup( $comment, $args ): void {
<?php
}

/**
* Get the Tumblr theme HTML content.
*
* @return string The HTML content.
*/
function ttgarden_get_theme_html(): string {
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.
return $wp_filesystem->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.
Expand All @@ -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.
Expand Down
10 changes: 1 addition & 9 deletions src/Customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
10 changes: 1 addition & 9 deletions src/FeatureSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );

Expand Down
22 changes: 18 additions & 4 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tumblr-theme-garden.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );

0 comments on commit 2afc1cb

Please sign in to comment.