Skip to content
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

Use custom_logo theme-mod instead of site_logo setting for the site-logo block #31994

Merged
merged 5 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,81 @@ 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',
'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' ),
),
),
),
)
);

/**
* 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.
*
* 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 ) {
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 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 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_set_setting_filter_theme_mods', 10, 3 );
62 changes: 37 additions & 25 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.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 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, {
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 ) {
Expand Down
51 changes: 0 additions & 51 deletions packages/block-library/src/site-logo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,56 +71,5 @@ 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.
*/
function override_custom_logo_theme_mod( $custom_logo ) {
$site_logo = get_option( 'site_logo' );
return false === $site_logo ? $custom_logo : $site_logo;
}

/**
* Syncs the site logo with the theme modified logo.
*
* @param string $custom_logo The custom logo set by a theme.
*
* @return string The custom logo.
*/
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 );
}
return $custom_logo;
}

/**
* Register a core site setting for a site logo
*/
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.' ),
)
);
}

add_action( 'rest_api_init', 'register_block_core_site_logo_setting', 10 );