-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Site Logo Block: update Site Logo block UI and option syncing #33179
Changes from all commits
de637ab
f528c4d
bb8f36c
ea36e79
a5e9b87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
*/ | ||
function render_block_core_site_logo( $attributes ) { | ||
$adjust_width_height_filter = function ( $image ) use ( $attributes ) { | ||
if ( empty( $attributes['width'] ) ) { | ||
if ( empty( $attributes['width'] ) || empty( $image ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This prevents a PHP warning from the code below if the image was deleted without updating the logo setting. |
||
return $image; | ||
} | ||
$height = (float) $attributes['width'] / ( (float) $image[1] / (float) $image[2] ); | ||
|
@@ -111,54 +111,52 @@ function _override_custom_logo_theme_mod( $custom_logo ) { | |
/** | ||
* Updates the site_logo option when the custom_logo theme-mod gets updated. | ||
* | ||
* 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. | ||
* | ||
* @param mixed $old_value The old option value. | ||
* @param mixed $value The new option value. | ||
* @param mixed $value Attachment ID of the custom logo or an empty value. | ||
* @return mixed | ||
*/ | ||
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( $value['custom_logo'] ) ) { | ||
function _sync_custom_logo_to_site_logo( $value ) { | ||
if ( empty( $value ) ) { | ||
delete_option( 'site_logo' ); | ||
} else { | ||
remove_action( 'update_option_site_logo', '_sync_site_logo_to_custom_logo' ); | ||
update_option( 'site_logo', $value['custom_logo'] ); | ||
add_action( 'update_option_site_logo', '_sync_site_logo_to_custom_logo', 10, 2 ); | ||
update_option( 'site_logo', $value ); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
add_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm restoring the use of the
|
||
|
||
/** | ||
* Hooks `_sync_custom_logo_to_site_logo` in `update_option_theme_mods_$theme`. | ||
* Deletes the site_logo when the custom_logo theme mod is removed. | ||
* | ||
* Runs on `setup_theme` to account for dynamically-switched themes in the Customizer. | ||
* @param array $old_value Previous theme mod settings. | ||
* @param array $value Updated theme mod settings. | ||
*/ | ||
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 ); | ||
function _delete_site_logo_on_remove_custom_logo( $old_value, $value ) { | ||
// 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' ); | ||
} | ||
} | ||
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. | ||
* | ||
* @param mixed $old_value The old option value. | ||
* @param mixed $value The new option value. | ||
* | ||
* @return void | ||
* Deletes the site logo when all theme mods are being removed. | ||
*/ | ||
function _sync_site_logo_to_custom_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( $value ) ) { | ||
remove_theme_mod( 'custom_logo' ); | ||
} else { | ||
remove_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' ); | ||
set_theme_mod( 'custom_logo', $value ); | ||
add_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this line has a bug in it that can delete the |
||
function _delete_site_logo_on_remove_theme_mods() { | ||
if ( false !== get_theme_support( 'custom-logo' ) ) { | ||
delete_option( 'site_logo' ); | ||
} | ||
} | ||
|
||
add_action( 'update_option_site_logo', '_sync_site_logo_to_custom_logo', 10, 2 ); | ||
/** | ||
* Hooks `_delete_site_logo_on_remove_custom_logo` in `update_option_theme_mods_$theme`. | ||
* Hooks `_delete_site_logo_on_remove_theme_mods` in `delete_option_theme_mods_$theme`. | ||
* | ||
* Runs on `setup_theme` to account for dynamically-switched themes in the Customizer. | ||
*/ | ||
function _delete_site_logo_on_remove_custom_logo_on_setup_theme() { | ||
$theme = get_option( 'stylesheet' ); | ||
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' ); | ||
} | ||
add_action( 'setup_theme', '_delete_site_logo_on_remove_custom_logo_on_setup_theme', 11 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These hooks and functions cover deleting the These should allow this change to pass Core phpunit tests (the theme used for the unit tests will need to declare support for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By changing to the
<Placeholder>
component, these overrides don't seem to be needed.