westonruter / syntax-highlighting-code-block

Extending the WordPress Code block with syntax highlighting rendered on the server, thus having faster frontend performance and being AMP-compatible.
https://wordpress.org/plugins/syntax-highlighting-code-block/
GNU General Public License v2.0
118 stars 11 forks source link

fix stylesheet name issue #731

Closed bianqui149 closed 1 year ago

bianqui149 commented 1 year ago

Hotfix for critical error in customize api. Fatal error: Uncaught DomainException: There is no stylesheet by the name of '' in /app/wp-content/plugins/syntax-highlighting-code-block/vendor/scrivo/highlight-php/HighlightUtilities/_themeColors.php:475 Stack trace: #0 /app/wp-content/plugins/syntax-highlighting-code-block/vendor/scrivo/highlight-php/HighlightUtilities/functions.php(91): _getThemeBackgroundColor(false) #1 /app/wp-content/plugins/syntax-highlighting-code-block/syntax-highlighting-code-block.php(127): HighlightUtilities\getThemeBackgroundColor(false) #2 /app/wp-content/plugins/syntax-highlighting-code-block/syntax-highlighting-code-block.php(776): Syntax_Highlighting_Code_Block\get_default_line_background_color(false) #3 /app/wp-includes/class-wp-hook.php(308): Syntax_Highlighting_Code_Block\customize_register(Object(WP_Customize_Manager)) #4 /app/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(NULL, Array) #5 /app/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #6 /app/wp-includes/class-wp-customize-manager.php(934): do_action('customize_regis...', Object(WP_Customize_Manager)) #7 /app/wp-includes/class-wp-hook.php(308): WP_Customize_Manager->wp_loaded('') #8 /app/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(NULL, Array) #9 /app/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #10 /app/wp-settings.php(645): do_action('wp_loaded') #11 /app/wp-config.php(121): require_once('/app/wp-setting...') #12 /app/wp-load.php(50): require_once('/app/wp-config....') #13 /app/wp-admin/admin.php(34): require_once('/app/wp-load.ph...') #14 /app/wp-admin/customize.php(13): require_once('/app/wp-admin/a...') #15 {main} thrown in /app/wp-content/plugins/syntax-highlighting-code-block/vendor/scrivo/highlight-php/HighlightUtilities/_themeColors.php on line 475 Tested environments:

WordPress 6.2.2
PHP 8.1
westonruter commented 1 year ago

How can the issue be reproduced?

bianqui149 commented 1 year ago

How can the issue be reproduced?

wp-admin-> Appearance ->Customize

$theme_name = get_theme_name(); is equal to false

westonruter commented 1 year ago

That's unexpected. Do you have a syntax_highlighting_code_block_style filter?

The function is supposed to always return a theme, though I can see it won't possibly if you've filtered syntax_highlighting_code_block_style:

https://github.com/westonruter/syntax-highlighting-code-block/blob/a158c67411e0436b06344b868a70d260076f697b/syntax-highlighting-code-block.php#L331-L350

For example:

add_filter( 'syntax_highlighting_code_block_style', '__return_false' );

The customizer setting validates the theme_name:

https://github.com/westonruter/syntax-highlighting-code-block/blob/a158c67411e0436b06344b868a70d260076f697b/syntax-highlighting-code-block.php#L751-L758

So a call to get_plugin_option( 'theme_name' ) should return a valid theme.

Is there something else you did on your site to get to this state?

bianqui149 commented 1 year ago

yupe, i have this filter

add_filter(
    'syntax_highlighting_code_block_style',
    '__return_false'
);

inside me theme functions Screenshot 2023-06-27 at 09 18 32 I also have this filter syntax_highlighting_code_block_auto_detect_languages. The only thing I did was updating the versions of PHP and WordPress

westonruter commented 1 year ago

OK, then as a part of this, we should also update the get_theme_name() function like so:

  1. Let the @return tag be: @return string|null Theme name or null if disabled.
  2. Let the function return with: return is_string( $style ) ? $style : null;
bianqui149 commented 1 year ago

okay, tested when okay

westonruter commented 1 year ago

Thank you for the contribution.