Skip to content

Commit

Permalink
Fix fatal error in Site Logo block in WP 5.9 (#36195)
Browse files Browse the repository at this point in the history
Restore the _delete_site_logo_on_remove_custom_logo functions
_delete_site_logo_on_remove_theme_mods so that they exist when
block-library/src/site-logo/index.php is copied to
wp-includes/blocks/site-logo.php in Core for WP 5.9.

These functions were renamed to have a _gutenberg prefix in
0140ba0 to prevent an infinite loop,
see https://github.com/WordPress/gutenberg/pull/34820/files#r710647763.

We can prevent an infinite loop using a global variable instead.
  • Loading branch information
noisysocks authored Nov 5, 2021
1 parent 2dfc08d commit f01e9c5
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions packages/block-library/src/site-logo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ function _sync_custom_logo_to_site_logo( $value ) {
* @param array $old_value Previous theme mod settings.
* @param array $value Updated theme mod settings.
*/
function _gutenberg_delete_site_logo_on_remove_custom_logo( $old_value, $value ) {
function _delete_site_logo_on_remove_custom_logo( $old_value, $value ) {
global $_ignore_site_logo_changes;

if ( $_ignore_site_logo_changes ) {
return;
}

// If the custom_logo is being unset, it's being removed from theme mods.
if ( isset( $old_value['custom_logo'] ) && ! isset( $value['custom_logo'] ) ) {
delete_option( 'site_logo' );
Expand All @@ -142,7 +148,13 @@ function _gutenberg_delete_site_logo_on_remove_custom_logo( $old_value, $value )
/**
* Deletes the site logo when all theme mods are being removed.
*/
function _gutenberg_delete_site_logo_on_remove_theme_mods() {
function _delete_site_logo_on_remove_theme_mods() {
global $_ignore_site_logo_changes;

if ( $_ignore_site_logo_changes ) {
return;
}

if ( false !== get_theme_support( 'custom-logo' ) ) {
delete_option( 'site_logo' );
}
Expand All @@ -165,27 +177,16 @@ function _delete_site_logo_on_remove_custom_logo_on_setup_theme() {
* Removes the custom_logo theme-mod when the site_logo option gets deleted.
*/
function _delete_custom_logo_on_remove_site_logo() {
$theme = get_option( 'stylesheet' );

// Unhook update and delete actions for custom_logo to prevent a loop of hooks.
// Remove Gutenberg hooks.
remove_action( "update_option_theme_mods_$theme", '_gutenberg_delete_site_logo_on_remove_custom_logo', 10 );
remove_action( "delete_option_theme_mods_$theme", '_gutenberg_delete_site_logo_on_remove_theme_mods' );
global $_ignore_site_logo_changes;

// Remove Core hooks.
remove_action( "update_option_theme_mods_$theme", '_delete_site_logo_on_remove_custom_logo', 10 );
remove_action( "delete_option_theme_mods_$theme", '_delete_site_logo_on_remove_theme_mods' );
// Prevent _delete_site_logo_on_remove_custom_logo and
// _delete_site_logo_on_remove_theme_mods from firing and causing an
// infinite loop.
$_ignore_site_logo_changes = true;

// Remove the custom logo.
remove_theme_mod( 'custom_logo' );

// Restore update and delete actions.
// Restore Gutenberg hooks.
add_action( "update_option_theme_mods_$theme", '_gutenberg_delete_site_logo_on_remove_custom_logo', 10, 2 );
add_action( "delete_option_theme_mods_$theme", '_gutenberg_delete_site_logo_on_remove_theme_mods' );

// Restore Core hooks.
add_action( "update_option_theme_mods_$theme", '_delete_site_logo_on_remove_custom_logo', 10, 2 );
add_action( "delete_option_theme_mods_$theme", '_delete_site_logo_on_remove_theme_mods' );
$_ignore_site_logo_changes = false;
}
add_action( 'delete_option_site_logo', '_delete_custom_logo_on_remove_site_logo' );

0 comments on commit f01e9c5

Please sign in to comment.