Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stylesheet name issue #731

Merged
merged 3 commits into from
Jun 27, 2023
Merged

Conversation

bianqui149
Copy link
Contributor

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
Copy link
Owner

How can the issue be reproduced?

@bianqui149
Copy link
Contributor Author

How can the issue be reproduced?

wp-admin-> Appearance ->Customize

$theme_name = get_theme_name(); is equal to false

@westonruter
Copy link
Owner

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:

function get_theme_name() {
if ( has_filter( BLOCK_STYLE_FILTER ) ) {
/**
* Filters the style used for the code syntax block.
*
* The string returned must correspond to the filenames found at <https://github.com/scrivo/highlight.php/tree/master/styles>,
* minus the file extension.
*
* This filter takes precedence over any settings set in the database as an option. Additionally, if this filter
* is provided, then a theme selector will not be provided in Customizer.
*
* @since 1.0.0
* @param string $style Style.
*/
$style = apply_filters( BLOCK_STYLE_FILTER, DEFAULT_THEME );
} else {
$style = get_plugin_option( 'theme_name' );
}
return $style;
}

For example:

add_filter( 'syntax_highlighting_code_block_style', '__return_false' );

The customizer setting validates the theme_name:

$setting = $wp_customize->add_setting(
'syntax_highlighting[theme_name]',
[
'type' => 'option',
'default' => DEFAULT_THEME,
'validate_callback' => __NAMESPACE__ . '\validate_theme_name',
]
);

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
Copy link
Contributor Author

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
Copy link
Owner

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
Copy link
Contributor Author

okay, tested when okay

@@ -308,7 +308,7 @@ function register_editor_assets( WP_Block_Type $block ): void {
/**
* Get highlight theme name.
*
* @return string Theme name.
* @return string|null Theme name or null if disabled.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this won't work because of the PHP string return type. So it will need to rather be:

Suggested change
* @return string|null Theme name or null if disabled.
* @return string Theme name or empty string if disabled.

@@ -331,7 +331,7 @@ function get_theme_name(): string {
} else {
$style = get_plugin_options()['theme_name'];
}
return $style;
return is_string( $style ) ? $style : null;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return is_string( $style ) ? $style : null;
return is_string( $style ) ? $style : '';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay fixed

@westonruter westonruter merged commit 4837460 into westonruter:develop Jun 27, 2023
@westonruter
Copy link
Owner

Thank you for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants