Skip to content

Commit

Permalink
Fix site-logo not getting removed on remove_theme_mod (#32370)
Browse files Browse the repository at this point in the history
* change site-logo sync hook

* Don't pollute the global variables

Co-authored-by: Weston Ruter <[email protected]>

* cs

* Hook in setup_theme

Co-authored-by: Weston Ruter <[email protected]>
  • Loading branch information
2 people authored and youknowriad committed Jun 7, 2021
1 parent ebfe9f7 commit fcf8da8
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions packages/block-library/src/site-logo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,35 @@ function _override_custom_logo_theme_mod( $custom_logo ) {
/**
* Updates the site_logo option when the custom_logo theme-mod gets updated.
*
* @param string $custom_logo The custom logo set by a theme.
* This function is hooked on "update_option_theme_mods_$theme" and not
* "pre_set_theme_mod_custom_logo" because by hooking in `update_option`
* the function accounts for remove_theme_mod() as well.
*
* @return string The custom logo.
* @param mixed $old_value The old option value.
* @param mixed $value The new option value.
*/
function _sync_custom_logo_to_site_logo( $custom_logo ) {
function _sync_custom_logo_to_site_logo( $old_value, $value ) {
// Delete the option when the custom logo does not exist or was removed.
// This step ensures the option stays in sync.
if ( empty( $custom_logo ) ) {
if ( empty( $value['custom_logo'] ) ) {
delete_option( 'site_logo' );
} else {
remove_action( 'update_option_site_logo', '_sync_site_logo_to_custom_logo' );
update_option( 'site_logo', $custom_logo );
update_option( 'site_logo', $value['custom_logo'] );
add_action( 'update_option_site_logo', '_sync_site_logo_to_custom_logo', 10, 2 );
}
return $custom_logo;
}

add_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' );
/**
* Hooks `_sync_custom_logo_to_site_logo` in `update_option_theme_mods_$theme`.
*
* Runs on `setup_theme` to account for dynamically-switched themes in the Customizer.
*/
function _sync_custom_logo_to_site_logo_on_setup_theme() {
$theme = get_option( 'stylesheet' );
add_action( "update_option_theme_mods_$theme", '_sync_custom_logo_to_site_logo', 10, 2 );
}
add_action( 'setup_theme', '_sync_custom_logo_to_site_logo_on_setup_theme', 11 );

/**
* Updates the custom_logo theme-mod when the site_logo option gets updated.
Expand Down

0 comments on commit fcf8da8

Please sign in to comment.