-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add new preferences persistence API, and save editor preferences in user meta #39795
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
74b5e25
48ea1d9
f4a04e0
0061cae
bce05a5
d592cf3
62a5e47
b7a8786
2d6e113
0cb6615
5be7bd5
7d9a302
4477ca5
a4007a8
2af0bd0
3f68f83
ee97cd7
145c79d
4e7a13a
7d5ed02
37c0c6d
8835a59
8a65181
bbcf4d3
528540b
20f8b4e
628a810
997fba6
97c8640
7f3ca74
fe8944c
269589f
b6c847e
bcab787
48e3d56
b09ac17
0c611ce
1cd7d43
08413dd
4192f71
ca3fabc
8289807
32ba8c0
5fb2a6f
8d10fd3
d234b8b
21b66de
315a4fe
feedc80
65d9de5
32ffcf2
ccff85e
b77d4bc
9000901
5ff88d5
62e7520
a56ca83
cde874f
95c96e2
f81418a
5d18fa7
34c7c95
b985c1b
9d7871b
01ad8d7
287e667
769cbfa
46471c2
8fb8b96
049da18
1febe25
99b8f52
e53a9dd
389abbb
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,106 @@ | ||
| <?php | ||
| /** | ||
| * Server-side requirements for database persisted preferences. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Register the user meta for persisted preferences. | ||
| */ | ||
| function gutenberg_register_persisted_preferences_meta() { | ||
| // Create a meta key that incorporates the blog prefix so that each site | ||
| // on a multisite can have distinct user preferences. | ||
| global $wpdb; | ||
| $meta_key = $wpdb->get_blog_prefix() . 'persisted_preferences'; | ||
|
|
||
| register_meta( | ||
| 'user', | ||
| $meta_key, | ||
| array( | ||
| 'type' => 'object', | ||
| 'single' => true, | ||
| 'show_in_rest' => array( | ||
| 'name' => 'persisted_preferences', | ||
| 'type' => 'object', | ||
| 'context' => array( 'edit' ), | ||
| 'schema' => array( | ||
| 'type' => 'object', | ||
|
talldan marked this conversation as resolved.
|
||
| 'properties' => array( | ||
| '_modified' => array( | ||
| 'description' => __( 'The date and time the preferences were updated.', 'default' ), | ||
| 'type' => 'string', | ||
| 'format' => 'date-time', | ||
| 'context' => array( 'edit' ), | ||
| 'readonly' => true, | ||
|
Member
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. Is
Contributor
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. I managed to miss this comment and I've already clicked merge. I'll make a separate PR.
Member
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. All good—easier to iterate than lug this big PR around. Let me know where you landed with #39795 (comment) too which is a comment that I forgot about 😀
Contributor
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. Github makes it too easy to miss these things. You do get an uncaught promise error if the user is offline, or the REST API errors. It doesn't stop the editor from working or anything, so doesn't seem like a critical issue. Local storage is there as a back up, so everything still works. I'll work on a PR to see what the options are for handling this better. |
||
| ), | ||
| ), | ||
| 'additionalProperties' => true, | ||
| ), | ||
| ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| add_action( 'init', 'gutenberg_register_persisted_preferences_meta' ); | ||
|
|
||
| /** | ||
| * Configures the preferences package to use user meta persistence. | ||
| */ | ||
| function gutenberg_configure_persisted_preferences() { | ||
| $user_id = get_current_user_id(); | ||
| if ( empty( $user_id ) ) { | ||
| return; | ||
| } | ||
|
|
||
| global $wpdb; | ||
| $meta_key = $wpdb->get_blog_prefix() . 'persisted_preferences'; | ||
|
|
||
| $preload_data = get_user_meta( $user_id, $meta_key, true ); | ||
|
|
||
| wp_add_inline_script( | ||
| 'wp-preferences', | ||
| sprintf( | ||
| '( function() { | ||
| var serverData = %s; | ||
| var userId = "%s"; | ||
|
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. Minor and belated, but it seems safer to encode sprintf( 'userId = %s', wp_json_encode( $user_id ) );would be identifical to the current form sprintf( 'userId = "%s"', $user_id );The former doesn't rely on our own mental type checking, and redundancy helps with safety — especially around code interpolation. :) |
||
| var persistenceLayer = wp.preferencesPersistence.__unstableCreatePersistenceLayer( serverData, userId ); | ||
| var preferencesStore = wp.preferences.store; | ||
| wp.data.dispatch( preferencesStore ).setPersistenceLayer( persistenceLayer ); | ||
| } ) ();', | ||
| wp_json_encode( $preload_data ), | ||
| $user_id | ||
| ), | ||
| 'after' | ||
| ); | ||
|
Member
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. In my opinion this feels like too much code for an inline script. Inline scripts have limitations: they aren't transpiled and they aren't cached by the browser. Consider putting the bulk of this logic into a function that's in wp.databasePersistence.initPersistenceLayer( serverData, userId ); |
||
|
|
||
| } | ||
|
|
||
| add_action( 'admin_init', 'gutenberg_configure_persisted_preferences' ); | ||
|
|
||
| /** | ||
| * Register dependencies for the inline script that configures the persistence layer. | ||
| * | ||
| * Note: When porting this to core update the code here: | ||
| * https://github.com/WordPress/wordpress-develop/blob/d2ab3d183740c3d1252cb921b18005495007e022/src/wp-includes/script-loader.php#L251-L258 | ||
| * | ||
| * And make the same update to the gutenberg client assets file here: | ||
| * https://github.com/WordPress/gutenberg/blob/3f3c8df23c70a37b7ac4dddebc82030362133593/lib/client-assets.php#L242-L254 | ||
| * | ||
| * The update should be adding a new case like this like this: | ||
| * ``` | ||
| * case 'wp-preferences': | ||
| * array_push( $dependencies, 'wp-preferences-persistence' ); | ||
| * break; | ||
| * ``` | ||
|
talldan marked this conversation as resolved.
|
||
| * | ||
| * @param WP_Scripts $scripts An instance of WP_Scripts. | ||
| */ | ||
| function gutenberg_update_preferences_persistence_deps( $scripts ) { | ||
| $persistence_script = $scripts->query( 'wp-preferences', 'registered' ); | ||
| if ( isset( $persistence_script->deps ) ) { | ||
| array_push( $persistence_script->deps, 'wp-preferences-persistence' ); | ||
| } | ||
| } | ||
|
|
||
| add_action( 'wp_default_scripts', 'gutenberg_update_preferences_persistence_deps', 11 ); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.