-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Display settings 'label' defined by the 'register_setting' method #59243
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
Changes from all commits
f875c12
cffa889
bf0b9ff
30ba923
579dce7
96ee0ea
0c1a177
3f2d0f1
3ad2c56
f95e7af
fb77e6b
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
| /** | ||
| * Options API changes for the Gutenberg plugin. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Updates arguments for default settings available in WordPress. | ||
| * | ||
| * Note: During the core sync, the updates will be made to `register_initial_settings`. | ||
| */ | ||
| function gutenberg_update_initial_settings( $args, $defaults, $option_group, $option_name ) { | ||
| $settings_label_map = array( | ||
| 'blogname' => __( 'Title' ), | ||
| 'blogdescription' => __( 'Tagline' ), | ||
| 'site_logo' => __( 'Logo' ), | ||
| 'site_icon' => __( 'Icon' ), | ||
| 'show_on_front' => __( 'Show on front' ), | ||
| 'page_on_front' => __( 'Page on front' ), | ||
| 'posts_per_page' => __( 'Maximum posts per page' ), | ||
| 'default_comment_status' => __( 'Allow comments on new posts' ), | ||
| ); | ||
|
|
||
| if ( isset( $settings_label_map[ $option_name ] ) ) { | ||
| $args['label'] = $settings_label_map[ $option_name ]; | ||
| } | ||
|
|
||
| // Don't update schema when label isn't provided. | ||
| if ( ! isset( $args['label'] ) ) { | ||
| return $args; | ||
| } | ||
|
|
||
| $schema = array( 'title' => $args['label'] ); | ||
| if ( ! is_array( $args['show_in_rest'] ) ) { | ||
| $args['show_in_rest'] = array( | ||
| 'schema' => $schema, | ||
| ); | ||
| return $args; | ||
| } | ||
|
|
||
| if ( ! empty( $args['show_in_rest']['schema'] ) ) { | ||
| $args['show_in_rest']['schema'] = array_merge( $args['show_in_rest']['schema'], $schema ); | ||
| } else { | ||
| $args['show_in_rest']['schema'] = $schema; | ||
| } | ||
|
|
||
| return $args; | ||
| } | ||
| add_filter( 'register_setting_args', 'gutenberg_update_initial_settings', 10, 4 ); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -61,36 +61,6 @@ export const rootEntitiesConfig = [ | |||||||||||||||||||||
| syncObjectType: 'root/base', | ||||||||||||||||||||||
| getSyncObjectId: () => 'index', | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| label: __( 'Site' ), | ||||||||||||||||||||||
| name: 'site', | ||||||||||||||||||||||
| kind: 'root', | ||||||||||||||||||||||
| baseURL: '/wp/v2/settings', | ||||||||||||||||||||||
| // The entity doesn't support selecting multiple records. | ||||||||||||||||||||||
| // The property is maintained for backward compatibility. | ||||||||||||||||||||||
| plural: 'sites', | ||||||||||||||||||||||
| getTitle: ( record ) => { | ||||||||||||||||||||||
| return record?.title ?? __( 'Site Title' ); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| syncConfig: { | ||||||||||||||||||||||
| fetch: async () => { | ||||||||||||||||||||||
| return apiFetch( { path: '/wp/v2/settings' } ); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| applyChangesToDoc: ( doc, changes ) => { | ||||||||||||||||||||||
| const document = doc.getMap( 'document' ); | ||||||||||||||||||||||
| Object.entries( changes ).forEach( ( [ key, value ] ) => { | ||||||||||||||||||||||
| if ( document.get( key ) !== value ) { | ||||||||||||||||||||||
| document.set( key, value ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } ); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| fromCRDTDoc: ( doc ) => { | ||||||||||||||||||||||
| return doc.getMap( 'document' ).toJSON(); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| syncObjectType: 'root/site', | ||||||||||||||||||||||
| getSyncObjectId: () => 'index', | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| label: __( 'Post Type' ), | ||||||||||||||||||||||
| name: 'postType', | ||||||||||||||||||||||
|
|
@@ -253,6 +223,12 @@ export const rootEntitiesConfig = [ | |||||||||||||||||||||
| export const additionalEntityConfigLoaders = [ | ||||||||||||||||||||||
| { kind: 'postType', loadEntities: loadPostTypeEntities }, | ||||||||||||||||||||||
| { kind: 'taxonomy', loadEntities: loadTaxonomyEntities }, | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| kind: 'root', | ||||||||||||||||||||||
| name: 'site', | ||||||||||||||||||||||
| plural: 'sites', | ||||||||||||||||||||||
| loadEntities: loadSiteEntity, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| ]; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
|
|
@@ -409,6 +385,56 @@ async function loadTaxonomyEntities() { | |||||||||||||||||||||
| } ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Returns the Site entity. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * @return {Promise} Entity promise | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| async function loadSiteEntity() { | ||||||||||||||||||||||
| const entity = { | ||||||||||||||||||||||
| label: __( 'Site' ), | ||||||||||||||||||||||
| name: 'site', | ||||||||||||||||||||||
| kind: 'root', | ||||||||||||||||||||||
| baseURL: '/wp/v2/settings', | ||||||||||||||||||||||
| syncConfig: { | ||||||||||||||||||||||
| fetch: async () => { | ||||||||||||||||||||||
| return apiFetch( { path: '/wp/v2/settings' } ); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| applyChangesToDoc: ( doc, changes ) => { | ||||||||||||||||||||||
| const document = doc.getMap( 'document' ); | ||||||||||||||||||||||
| Object.entries( changes ).forEach( ( [ key, value ] ) => { | ||||||||||||||||||||||
| if ( document.get( key ) !== value ) { | ||||||||||||||||||||||
| document.set( key, value ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } ); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| fromCRDTDoc: ( doc ) => { | ||||||||||||||||||||||
| return doc.getMap( 'document' ).toJSON(); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| syncObjectType: 'root/site', | ||||||||||||||||||||||
| getSyncObjectId: () => 'index', | ||||||||||||||||||||||
| meta: {}, | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const site = await apiFetch( { | ||||||||||||||||||||||
| path: entity.baseURL, | ||||||||||||||||||||||
| method: 'OPTIONS', | ||||||||||||||||||||||
| } ); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const labels = {}; | ||||||||||||||||||||||
| Object.entries( site?.schema?.properties ?? {} ).forEach( | ||||||||||||||||||||||
| ( [ key, value ] ) => { | ||||||||||||||||||||||
| // Ignore properties `title` and `type` keys. | ||||||||||||||||||||||
| if ( typeof value === 'object' && value.title ) { | ||||||||||||||||||||||
| labels[ key ] = value.title; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return [ { ...entity, meta: { labels } } ]; | ||||||||||||||||||||||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Returns the entity's getter method name given its kind and name or plural name. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
|
|
@@ -443,17 +469,21 @@ function registerSyncConfigs( configs ) { | |||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Loads the kind entities into the store. | ||||||||||||||||||||||
| * Loads the entities into the store. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * @param {string} kind Kind | ||||||||||||||||||||||
| * Note: The `name` argument is used for `root` entities requiring additional server data. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * @param {string} kind Kind | ||||||||||||||||||||||
| * @param {string} name Name | ||||||||||||||||||||||
| * @return {(thunkArgs: object) => Promise<Array>} Entities | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| export const getOrLoadEntitiesConfig = | ||||||||||||||||||||||
| ( kind ) => | ||||||||||||||||||||||
| ( kind, name ) => | ||||||||||||||||||||||
|
Contributor
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. Why does this function need a "name" now. I believe the purpose of this function is to load the entities of a given "kind", so "name" is not needed?
Member
Author
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. The
Contributor
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 admin that I'm not following. There's something I'm obviously missing.
Member
Author
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.
gutenberg/packages/core-data/src/entities.js Lines 459 to 468 in ec86a82
Contributor
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. Can we instead update the condition to check whether a key exist in
Contributor
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've been thinking more, basically what we want is a resolver for.
Member
Author
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. Sure, I'm happy to give it a try. However, we might encounter a similar issue without using the entity
Contributor
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. Let's keep your changes but just clarify things in the description. I'm not entirely satisfied with how we're dealing with all of that, but it's not clear to me what's the best path forward is yet:
It seems there's potentially a better way/API to "register" these entities. |
||||||||||||||||||||||
| async ( { select, dispatch } ) => { | ||||||||||||||||||||||
| let configs = select.getEntitiesConfig( kind ); | ||||||||||||||||||||||
| if ( configs && configs.length !== 0 ) { | ||||||||||||||||||||||
| const hasConfig = !! select.getEntityConfig( kind, name ); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if ( configs?.length > 0 && hasConfig ) { | ||||||||||||||||||||||
| if ( window.__experimentalEnableSync ) { | ||||||||||||||||||||||
| if ( process.env.IS_GUTENBERG_PLUGIN ) { | ||||||||||||||||||||||
| registerSyncConfigs( configs ); | ||||||||||||||||||||||
|
|
@@ -463,9 +493,13 @@ export const getOrLoadEntitiesConfig = | |||||||||||||||||||||
| return configs; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const loader = additionalEntityConfigLoaders.find( | ||||||||||||||||||||||
| ( l ) => l.kind === kind | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| const loader = additionalEntityConfigLoaders.find( ( l ) => { | ||||||||||||||||||||||
| if ( ! name || ! l.name ) { | ||||||||||||||||||||||
| return l.kind === kind; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return l.kind === kind && l.name === name; | ||||||||||||||||||||||
| } ); | ||||||||||||||||||||||
| if ( ! loader ) { | ||||||||||||||||||||||
| return []; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.