Skip to content

Commit

Permalink
revert 50b57c5
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed May 20, 2021
1 parent d273d1a commit 1880dd1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 35 deletions.
48 changes: 37 additions & 11 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
);
Expand All @@ -218,17 +238,18 @@ 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 );

/**
* 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.
Expand All @@ -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 );
60 changes: 36 additions & 24 deletions packages/block-library/src/site-logo/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit 1880dd1

Please sign in to comment.