From f1441f71c988a7b9a92cbfae7fddedadb9b202b9 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 19 May 2021 16:11:56 +0300 Subject: [PATCH 1/5] Use custom_logo theme-mod instead of site_logo setting --- packages/block-library/src/site-logo/edit.js | 63 +++++++----- .../block-library/src/site-logo/index.php | 99 ++++++++++++------- 2 files changed, 101 insertions(+), 61 deletions(-) diff --git a/packages/block-library/src/site-logo/edit.js b/packages/block-library/src/site-logo/edit.js index 05e225513392d..3b593c690753a 100644 --- a/packages/block-library/src/site-logo/edit.js +++ b/packages/block-library/src/site-logo/edit.js @@ -255,33 +255,46 @@ export default function LogoEdit( { const [ logoUrl, setLogoUrl ] = useState(); const [ error, setError ] = useState(); const ref = useRef(); - const { mediaItemData, siteLogo, url } = useSelect( ( select ) => { - const siteSettings = select( coreStore ).getEditedEntityRecord( - 'root', - 'site' - ); - const mediaItem = siteSettings.site_logo - ? select( coreStore ).getEntityRecord( - 'root', - 'media', - siteSettings.site_logo - ) - : null; - return { - mediaItemData: mediaItem && { - url: mediaItem.source_url, - alt: mediaItem.alt_text, - }, - siteLogo: siteSettings.site_logo, - url: siteSettings.url, - }; - }, [] ); + const { mediaItemData, siteLogo, url, stylesheet } = useSelect( + ( select ) => { + const siteSettings = select( coreStore ).getEditedEntityRecord( + 'root', + 'site' + ); + const mediaItem = siteSettings[ + `theme_mods_${ siteSettings.stylesheet }` + ].custom_logo + ? select( coreStore ).getEntityRecord( + 'root', + 'media', + siteSettings[ + `theme_mods_${ siteSettings.stylesheet }` + ].custom_logo + ) + : null; + return { + mediaItemData: mediaItem && { + url: mediaItem.source_url, + alt: mediaItem.alt_text, + }, + siteLogo: + siteSettings[ `theme_mods_${ siteSettings.stylesheet }` ] + .custom_logo, + url: siteSettings.url, + stylesheet: siteSettings.stylesheet, + }; + }, + [] + ); const { editEntityRecord } = useDispatch( coreStore ); - const setLogo = ( newValue ) => - editEntityRecord( 'root', 'site', undefined, { - site_logo: newValue, - } ); + const setLogo = ( newValue ) => { + const settingsVal = {}; + settingsVal[ `theme_mods_${ stylesheet }` ] = { + custom_logo: newValue, + }; + editEntityRecord( 'root', 'site', undefined, settingsVal ); + }; let alt = null; if ( mediaItemData ) { diff --git a/packages/block-library/src/site-logo/index.php b/packages/block-library/src/site-logo/index.php index 8bcfbd486346a..7c462f24ff3ba 100644 --- a/packages/block-library/src/site-logo/index.php +++ b/packages/block-library/src/site-logo/index.php @@ -71,56 +71,83 @@ function register_block_core_site_logo() { 'render_callback' => 'render_block_core_site_logo', ) ); - add_filter( 'pre_set_theme_mod_custom_logo', 'sync_site_logo_to_theme_mod' ); - add_filter( 'theme_mod_custom_logo', 'override_custom_logo_theme_mod' ); } add_action( 'init', 'register_block_core_site_logo' ); /** - * Overrides the custom logo with a site logo, if the option is set. - * - * @param string $custom_logo The custom logo set by a theme. - * - * @return string The site logo if set. + * Expose the custom_logo theme-mod in the settings REST API. */ -function override_custom_logo_theme_mod( $custom_logo ) { - $site_logo = get_option( 'site_logo' ); - return false === $site_logo ? $custom_logo : $site_logo; -} +register_setting( + 'general', + 'theme_mods_' . get_option( 'stylesheet' ), + array( + 'type' => 'object', + 'show_in_rest' => array( + 'name' => 'theme_mods_' . get_option( 'stylesheet' ), + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'custom_logo' => array( 'type' => 'integer' ), + ), + ), + ), + ) +); /** - * Syncs the site logo with the theme modified logo. + * Expose the "stylesheet" setting in the REST API. + */ +register_setting( + 'general', + 'stylesheet', + array( + 'type' => 'string', + 'show_in_rest' => true, + ) +); + +/** + * Filters the value of a setting recognized by the REST API. * - * @param string $custom_logo The custom logo set by a theme. + * Hijacks the value for custom_logo theme-mod. * - * @return string The custom logo. + * @param mixed $result Value to use for the requested setting. Can be a scalar + * matching the registered schema for the setting, or null to + * follow the default get_option() behavior. + * @param string $name Setting name (as shown in REST API responses). + * + * @return null|array */ -function sync_site_logo_to_theme_mod( $custom_logo ) { - // 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 ) ) { - delete_option( 'site_logo' ); - } else { - update_option( 'site_logo', $custom_logo ); +function gutenberg_rest_pre_get_setting_filter_custom_logo( $result, $name ) { + if ( 'theme_mods_' . get_option( 'stylesheet' ) === $name ) { + return array( + 'custom_logo' => get_theme_mod( 'custom_logo' ), + ); } - return $custom_logo; } +add_filter( 'rest_pre_get_setting', 'gutenberg_rest_pre_get_setting_filter_custom_logo', 10, 2 ); /** - * Register a core site setting for a site logo + * Filters whether to preempt a setting value update via the REST API. + * + * Hijacks the saving method for theme-mods. + * + * @param bool $result Whether to override the default behavior for updating the + * value of a setting. + * @param string $name Setting name (as shown in REST API responses). + * @param mixed $value Updated setting value. + * + * @return bool */ -function register_block_core_site_logo_setting() { - register_setting( - 'general', - 'site_logo', - array( - 'show_in_rest' => array( - 'name' => 'site_logo', - ), - 'type' => 'integer', - 'description' => __( 'Site logo.' ), - ) - ); +function gutenberg_rest_pre_set_setting_filter_theme_mods( $result, $name, $value ) { + $theme_mods_setting_name = 'theme_mods_' . get_option( 'stylesheet' ); + if ( $theme_mods_setting_name === $name ) { + $value = (array) $value; + $value = wp_parse_args( $value, get_option( $theme_mods_setting_name, array() ) ); + + update_option( $theme_mods_setting_name, $value ); + return true; + } } -add_action( 'rest_api_init', 'register_block_core_site_logo_setting', 10 ); +add_filter( 'rest_pre_update_setting', 'gutenberg_rest_pre_set_setting_filter_theme_mods', 10, 3 ); From d4d22c1eef998f3b61350abac9342eedabe4f37a Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 19 May 2021 16:26:25 +0300 Subject: [PATCH 2/5] fix issue when theme-mod is unset --- packages/block-library/src/site-logo/edit.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/site-logo/edit.js b/packages/block-library/src/site-logo/edit.js index 3b593c690753a..852446ed635cc 100644 --- a/packages/block-library/src/site-logo/edit.js +++ b/packages/block-library/src/site-logo/edit.js @@ -261,15 +261,16 @@ export default function LogoEdit( { 'root', 'site' ); - const mediaItem = siteSettings[ - `theme_mods_${ siteSettings.stylesheet }` - ].custom_logo + + const themeModOptionName = `theme_mods_${ siteSettings.stylesheet }`; + + siteSettings[ themeModOptionName ] = + siteSettings[ themeModOptionName ] || {}; + const mediaItem = siteSettings[ themeModOptionName ].custom_logo ? select( coreStore ).getEntityRecord( 'root', 'media', - siteSettings[ - `theme_mods_${ siteSettings.stylesheet }` - ].custom_logo + siteSettings[ themeModOptionName ].custom_logo ) : null; return { @@ -277,9 +278,7 @@ export default function LogoEdit( { url: mediaItem.source_url, alt: mediaItem.alt_text, }, - siteLogo: - siteSettings[ `theme_mods_${ siteSettings.stylesheet }` ] - .custom_logo, + siteLogo: siteSettings[ themeModOptionName ].custom_logo, url: siteSettings.url, stylesheet: siteSettings.stylesheet, }; From d5fb7d289e7aa2fdba9ec939522ae2a58a9f3848 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 19 May 2021 17:47:09 +0300 Subject: [PATCH 3/5] expose custom_logo as a top-level item. --- packages/block-library/src/site-logo/edit.js | 60 ++++++++----------- .../block-library/src/site-logo/index.php | 48 ++++----------- 2 files changed, 35 insertions(+), 73 deletions(-) diff --git a/packages/block-library/src/site-logo/edit.js b/packages/block-library/src/site-logo/edit.js index 852446ed635cc..ccbe80dd89934 100644 --- a/packages/block-library/src/site-logo/edit.js +++ b/packages/block-library/src/site-logo/edit.js @@ -255,45 +255,33 @@ export default function LogoEdit( { const [ logoUrl, setLogoUrl ] = useState(); const [ error, setError ] = useState(); const ref = useRef(); - const { mediaItemData, siteLogo, url, stylesheet } = useSelect( - ( select ) => { - const siteSettings = select( coreStore ).getEditedEntityRecord( - 'root', - 'site' - ); - - const themeModOptionName = `theme_mods_${ siteSettings.stylesheet }`; - - siteSettings[ themeModOptionName ] = - siteSettings[ themeModOptionName ] || {}; - const mediaItem = siteSettings[ themeModOptionName ].custom_logo - ? select( coreStore ).getEntityRecord( - 'root', - 'media', - siteSettings[ themeModOptionName ].custom_logo - ) - : null; - return { - mediaItemData: mediaItem && { - url: mediaItem.source_url, - alt: mediaItem.alt_text, - }, - siteLogo: siteSettings[ themeModOptionName ].custom_logo, - url: siteSettings.url, - stylesheet: siteSettings.stylesheet, - }; - }, - [] - ); + const { mediaItemData, siteLogo, url } = useSelect( ( select ) => { + const siteSettings = select( coreStore ).getEditedEntityRecord( + 'root', + 'site' + ); + const mediaItem = siteSettings.custom_logo + ? select( coreStore ).getEntityRecord( + 'root', + 'media', + siteSettings.custom_logo + ) + : null; + return { + mediaItemData: mediaItem && { + url: mediaItem.source_url, + alt: mediaItem.alt_text, + }, + siteLogo: siteSettings.custom_logo, + url: siteSettings.url, + }; + }, [] ); const { editEntityRecord } = useDispatch( coreStore ); - const setLogo = ( newValue ) => { - const settingsVal = {}; - settingsVal[ `theme_mods_${ stylesheet }` ] = { + const setLogo = ( newValue ) => + editEntityRecord( 'root', 'site', undefined, { custom_logo: newValue, - }; - editEntityRecord( 'root', 'site', undefined, settingsVal ); - }; + } ); let alt = null; if ( mediaItemData ) { diff --git a/packages/block-library/src/site-logo/index.php b/packages/block-library/src/site-logo/index.php index 7c462f24ff3ba..f4789997ca536 100644 --- a/packages/block-library/src/site-logo/index.php +++ b/packages/block-library/src/site-logo/index.php @@ -79,29 +79,9 @@ function register_block_core_site_logo() { */ register_setting( 'general', - 'theme_mods_' . get_option( 'stylesheet' ), + 'custom_logo', array( - 'type' => 'object', - 'show_in_rest' => array( - 'name' => 'theme_mods_' . get_option( 'stylesheet' ), - 'schema' => array( - 'type' => 'object', - 'properties' => array( - 'custom_logo' => array( 'type' => 'integer' ), - ), - ), - ), - ) -); - -/** - * Expose the "stylesheet" setting in the REST API. - */ -register_setting( - 'general', - 'stylesheet', - array( - 'type' => 'string', + 'type' => 'integer', 'show_in_rest' => true, ) ); @@ -119,10 +99,9 @@ function register_block_core_site_logo() { * @return null|array */ function gutenberg_rest_pre_get_setting_filter_custom_logo( $result, $name ) { - if ( 'theme_mods_' . get_option( 'stylesheet' ) === $name ) { - return array( - 'custom_logo' => get_theme_mod( 'custom_logo' ), - ); + error_log( print_r( $name, true ) ); + if ( 'custom_logo' === $name ) { + return get_theme_mod( 'custom_logo' ); } } add_filter( 'rest_pre_get_setting', 'gutenberg_rest_pre_get_setting_filter_custom_logo', 10, 2 ); @@ -130,7 +109,7 @@ function gutenberg_rest_pre_get_setting_filter_custom_logo( $result, $name ) { /** * Filters whether to preempt a setting value update via the REST API. * - * Hijacks the saving method for theme-mods. + * Hijacks the saving method for the custom_logo theme-mod. * * @param bool $result Whether to override the default behavior for updating the * value of a setting. @@ -139,15 +118,10 @@ function gutenberg_rest_pre_get_setting_filter_custom_logo( $result, $name ) { * * @return bool */ -function gutenberg_rest_pre_set_setting_filter_theme_mods( $result, $name, $value ) { - $theme_mods_setting_name = 'theme_mods_' . get_option( 'stylesheet' ); - if ( $theme_mods_setting_name === $name ) { - $value = (array) $value; - $value = wp_parse_args( $value, get_option( $theme_mods_setting_name, array() ) ); - - update_option( $theme_mods_setting_name, $value ); - return true; - } +function gutenberg_rest_pre_update_setting_filter_custom_logo( $result, $name, $value ) { + return 'custom_logo' === $name + ? set_theme_mod( 'custom_logo', $value ) + : $result; } -add_filter( 'rest_pre_update_setting', 'gutenberg_rest_pre_set_setting_filter_theme_mods', 10, 3 ); +add_filter( 'rest_pre_update_setting', 'gutenberg_rest_pre_update_setting_filter_custom_logo', 10, 3 ); From d273d1ad2521de678d653bd380a7206dfb1c167f Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 19 May 2021 17:56:03 +0300 Subject: [PATCH 4/5] Move REST-API tweaks from the block to lib --- lib/rest-api.php | 52 +++++++++++++++++++ .../block-library/src/site-logo/index.php | 52 ------------------- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/lib/rest-api.php b/lib/rest-api.php index 19a6baa8c82a0..a897b19cbb94e 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -192,3 +192,55 @@ function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $na return $permalink; } add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 ); + +/** + * Expose the custom_logo theme-mod in the settings REST API. + */ +register_setting( + 'general', + 'custom_logo', + array( + 'type' => 'integer', + 'show_in_rest' => true, + ) +); + +/** + * Filters the value of a setting recognized by the REST API. + * + * Hijacks the value for custom_logo theme-mod. + * + * @param mixed $result Value to use for the requested setting. Can be a scalar + * matching the registered schema for the setting, or null to + * follow the default get_option() behavior. + * @param string $name Setting name (as shown in REST API responses). + * + * @return null|array + */ +function gutenberg_rest_pre_get_setting_filter_custom_logo( $result, $name ) { + error_log( print_r( $name, true ) ); + if ( 'custom_logo' === $name ) { + return get_theme_mod( 'custom_logo' ); + } +} +add_filter( 'rest_pre_get_setting', 'gutenberg_rest_pre_get_setting_filter_custom_logo', 10, 2 ); + +/** + * Filters whether to preempt a setting value update via the REST API. + * + * Hijacks the saving method for the custom_logo theme-mod. + * + * @param bool $result Whether to override the default behavior for updating the + * value of a setting. + * @param string $name Setting name (as shown in REST API responses). + * @param mixed $value Updated setting value. + * + * @return bool + */ +function gutenberg_rest_pre_update_setting_filter_custom_logo( $result, $name, $value ) { + return 'custom_logo' === $name + ? set_theme_mod( 'custom_logo', $value ) + : $result; +} + +add_filter( 'rest_pre_update_setting', 'gutenberg_rest_pre_update_setting_filter_custom_logo', 10, 3 ); diff --git a/packages/block-library/src/site-logo/index.php b/packages/block-library/src/site-logo/index.php index f4789997ca536..1127f4790a3a7 100644 --- a/packages/block-library/src/site-logo/index.php +++ b/packages/block-library/src/site-logo/index.php @@ -73,55 +73,3 @@ function register_block_core_site_logo() { ); } add_action( 'init', 'register_block_core_site_logo' ); - -/** - * Expose the custom_logo theme-mod in the settings REST API. - */ -register_setting( - 'general', - 'custom_logo', - array( - 'type' => 'integer', - 'show_in_rest' => true, - ) -); - -/** - * Filters the value of a setting recognized by the REST API. - * - * Hijacks the value for custom_logo theme-mod. - * - * @param mixed $result Value to use for the requested setting. Can be a scalar - * matching the registered schema for the setting, or null to - * follow the default get_option() behavior. - * @param string $name Setting name (as shown in REST API responses). - * - * @return null|array - */ -function gutenberg_rest_pre_get_setting_filter_custom_logo( $result, $name ) { - error_log( print_r( $name, true ) ); - if ( 'custom_logo' === $name ) { - return get_theme_mod( 'custom_logo' ); - } -} -add_filter( 'rest_pre_get_setting', 'gutenberg_rest_pre_get_setting_filter_custom_logo', 10, 2 ); - -/** - * Filters whether to preempt a setting value update via the REST API. - * - * Hijacks the saving method for the custom_logo theme-mod. - * - * @param bool $result Whether to override the default behavior for updating the - * value of a setting. - * @param string $name Setting name (as shown in REST API responses). - * @param mixed $value Updated setting value. - * - * @return bool - */ -function gutenberg_rest_pre_update_setting_filter_custom_logo( $result, $name, $value ) { - return 'custom_logo' === $name - ? set_theme_mod( 'custom_logo', $value ) - : $result; -} - -add_filter( 'rest_pre_update_setting', 'gutenberg_rest_pre_update_setting_filter_custom_logo', 10, 3 ); From 1880dd19d20a4b8c8020e0b98e7e1ce1fe78169d Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 May 2021 09:02:36 +0300 Subject: [PATCH 5/5] revert https://github.com/WordPress/gutenberg/pull/31994/commits/50b57c50b650d09e95c646fbb36343a50889a9c5 --- lib/rest-api.php | 48 ++++++++++++---- packages/block-library/src/site-logo/edit.js | 60 ++++++++++++-------- 2 files changed, 73 insertions(+), 35 deletions(-) diff --git a/lib/rest-api.php b/lib/rest-api.php index a897b19cbb94e..f1336d86b33a4 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -198,9 +198,29 @@ function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $na */ register_setting( 'general', - 'custom_logo', + 'theme_mods_' . get_option( 'stylesheet' ), array( - 'type' => 'integer', + 'type' => 'object', + 'show_in_rest' => array( + 'name' => 'theme_mods_' . get_option( 'stylesheet' ), + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'custom_logo' => array( 'type' => 'integer' ), + ), + ), + ), + ) +); + +/** + * Expose the "stylesheet" setting in the REST API. + */ +register_setting( + 'general', + 'stylesheet', + array( + 'type' => 'string', 'show_in_rest' => true, ) ); @@ -218,9 +238,10 @@ function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $na * @return null|array */ function gutenberg_rest_pre_get_setting_filter_custom_logo( $result, $name ) { - error_log( print_r( $name, true ) ); - if ( 'custom_logo' === $name ) { - return get_theme_mod( 'custom_logo' ); + if ( 'theme_mods_' . get_option( 'stylesheet' ) === $name ) { + return array( + 'custom_logo' => get_theme_mod( 'custom_logo' ), + ); } } add_filter( 'rest_pre_get_setting', 'gutenberg_rest_pre_get_setting_filter_custom_logo', 10, 2 ); @@ -228,7 +249,7 @@ function gutenberg_rest_pre_get_setting_filter_custom_logo( $result, $name ) { /** * Filters whether to preempt a setting value update via the REST API. * - * Hijacks the saving method for the custom_logo theme-mod. + * Hijacks the saving method for theme-mods. * * @param bool $result Whether to override the default behavior for updating the * value of a setting. @@ -237,10 +258,15 @@ function gutenberg_rest_pre_get_setting_filter_custom_logo( $result, $name ) { * * @return bool */ -function gutenberg_rest_pre_update_setting_filter_custom_logo( $result, $name, $value ) { - return 'custom_logo' === $name - ? set_theme_mod( 'custom_logo', $value ) - : $result; +function gutenberg_rest_pre_set_setting_filter_theme_mods( $result, $name, $value ) { + $theme_mods_setting_name = 'theme_mods_' . get_option( 'stylesheet' ); + if ( $theme_mods_setting_name === $name ) { + $value = (array) $value; + $value = wp_parse_args( $value, get_option( $theme_mods_setting_name, array() ) ); + + update_option( $theme_mods_setting_name, $value ); + return true; + } } -add_filter( 'rest_pre_update_setting', 'gutenberg_rest_pre_update_setting_filter_custom_logo', 10, 3 ); +add_filter( 'rest_pre_update_setting', 'gutenberg_rest_pre_set_setting_filter_theme_mods', 10, 3 ); diff --git a/packages/block-library/src/site-logo/edit.js b/packages/block-library/src/site-logo/edit.js index ccbe80dd89934..852446ed635cc 100644 --- a/packages/block-library/src/site-logo/edit.js +++ b/packages/block-library/src/site-logo/edit.js @@ -255,33 +255,45 @@ export default function LogoEdit( { const [ logoUrl, setLogoUrl ] = useState(); const [ error, setError ] = useState(); const ref = useRef(); - const { mediaItemData, siteLogo, url } = useSelect( ( select ) => { - const siteSettings = select( coreStore ).getEditedEntityRecord( - 'root', - 'site' - ); - const mediaItem = siteSettings.custom_logo - ? select( coreStore ).getEntityRecord( - 'root', - 'media', - siteSettings.custom_logo - ) - : null; - return { - mediaItemData: mediaItem && { - url: mediaItem.source_url, - alt: mediaItem.alt_text, - }, - siteLogo: siteSettings.custom_logo, - url: siteSettings.url, - }; - }, [] ); + const { mediaItemData, siteLogo, url, stylesheet } = useSelect( + ( select ) => { + const siteSettings = select( coreStore ).getEditedEntityRecord( + 'root', + 'site' + ); + + const themeModOptionName = `theme_mods_${ siteSettings.stylesheet }`; + + siteSettings[ themeModOptionName ] = + siteSettings[ themeModOptionName ] || {}; + const mediaItem = siteSettings[ themeModOptionName ].custom_logo + ? select( coreStore ).getEntityRecord( + 'root', + 'media', + siteSettings[ themeModOptionName ].custom_logo + ) + : null; + return { + mediaItemData: mediaItem && { + url: mediaItem.source_url, + alt: mediaItem.alt_text, + }, + siteLogo: siteSettings[ themeModOptionName ].custom_logo, + url: siteSettings.url, + stylesheet: siteSettings.stylesheet, + }; + }, + [] + ); const { editEntityRecord } = useDispatch( coreStore ); - const setLogo = ( newValue ) => - editEntityRecord( 'root', 'site', undefined, { + const setLogo = ( newValue ) => { + const settingsVal = {}; + settingsVal[ `theme_mods_${ stylesheet }` ] = { custom_logo: newValue, - } ); + }; + editEntityRecord( 'root', 'site', undefined, settingsVal ); + }; let alt = null; if ( mediaItemData ) {