From ae2a2b395fd8a058517fbfb334e9e7edbb2e472b Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 1 Jun 2021 14:29:47 +0300 Subject: [PATCH 1/4] change site-logo sync hook --- packages/block-library/src/site-logo/index.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/site-logo/index.php b/packages/block-library/src/site-logo/index.php index 46af15ad6dbc66..b4c9fec6298826 100644 --- a/packages/block-library/src/site-logo/index.php +++ b/packages/block-library/src/site-logo/index.php @@ -111,24 +111,27 @@ 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' ); +$theme = get_option( 'stylesheet' ); +add_action( "update_option_theme_mods_$theme", '_sync_custom_logo_to_site_logo', 10, 2 ); /** * Updates the custom_logo theme-mod when the site_logo option gets updated. From f6b1b8b8d9010d2480df3292fec12186cb3a0d0a Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 2 Jun 2021 08:15:41 +0300 Subject: [PATCH 2/4] Don't pollute the global variables Co-authored-by: Weston Ruter --- packages/block-library/src/site-logo/index.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/block-library/src/site-logo/index.php b/packages/block-library/src/site-logo/index.php index b4c9fec6298826..e939c5b0cb2772 100644 --- a/packages/block-library/src/site-logo/index.php +++ b/packages/block-library/src/site-logo/index.php @@ -130,8 +130,7 @@ function _sync_custom_logo_to_site_logo( $old_value, $value ) { } } -$theme = get_option( 'stylesheet' ); -add_action( "update_option_theme_mods_$theme", '_sync_custom_logo_to_site_logo', 10, 2 ); +add_action( 'update_option_theme_mods_' . get_option('stylesheet'), '_sync_custom_logo_to_site_logo', 10, 2 ); /** * Updates the custom_logo theme-mod when the site_logo option gets updated. From 86b6f44e5b9e7bdcb5d67eed7288c3e114060afe Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 2 Jun 2021 08:28:58 +0300 Subject: [PATCH 3/4] cs --- packages/block-library/src/site-logo/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/site-logo/index.php b/packages/block-library/src/site-logo/index.php index e939c5b0cb2772..e27b0f0ee6b127 100644 --- a/packages/block-library/src/site-logo/index.php +++ b/packages/block-library/src/site-logo/index.php @@ -130,7 +130,7 @@ function _sync_custom_logo_to_site_logo( $old_value, $value ) { } } -add_action( 'update_option_theme_mods_' . get_option('stylesheet'), '_sync_custom_logo_to_site_logo', 10, 2 ); +add_action( 'update_option_theme_mods_' . get_option( 'stylesheet' ), '_sync_custom_logo_to_site_logo', 10, 2 ); /** * Updates the custom_logo theme-mod when the site_logo option gets updated. From a1925ffa3f9ff2dd1e15540b2794ef250e000032 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 3 Jun 2021 17:39:07 +0300 Subject: [PATCH 4/4] Hook in setup_theme --- packages/block-library/src/site-logo/index.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/site-logo/index.php b/packages/block-library/src/site-logo/index.php index e27b0f0ee6b127..44d63505cc4ffb 100644 --- a/packages/block-library/src/site-logo/index.php +++ b/packages/block-library/src/site-logo/index.php @@ -130,7 +130,16 @@ function _sync_custom_logo_to_site_logo( $old_value, $value ) { } } -add_action( 'update_option_theme_mods_' . get_option( 'stylesheet' ), '_sync_custom_logo_to_site_logo', 10, 2 ); +/** + * 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.