Editor: Persist preferences in user meta.
Adds a new feature to persist editor UI preferences between page loads and browsers. * Adds a new preferences persistence API. * Saves editor preferences in user meta instead of in browser's local storage. Why? Due to the transient nature of browser storage, this persistence is not as sticky as it is expected to be, including: switching browsers (unique storage between browsers), or using private browsing tabs (storage cleared between sessions), or the same user across a network of sites (storage unique by domain). This is a backport from Gutenberg.[https://github.com/WordPress/gutenberg/pull/39795 See WordPress/gutenberg PR 39795]. Props talldanwp, youknowriad, noisysocks, mamaduka, costdev, ironprogrammer, hellofromTonya. See #56467. Built from https://develop.svn.wordpress.org/trunk@54182 git-svn-id: http://core.svn.wordpress.org/trunk@53741 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
@@ -294,6 +294,9 @@ function wp_default_packages_scripts( $scripts ) {
|
||||
case 'wp-edit-post':
|
||||
array_push( $dependencies, 'media-models', 'media-views', 'postbox', 'wp-dom-ready' );
|
||||
break;
|
||||
case 'wp-preferences':
|
||||
array_push( $dependencies, 'wp-preferences-persistence' );
|
||||
break;
|
||||
}
|
||||
|
||||
$scripts->add( $handle, $path, $dependencies, $package_data['version'], 1 );
|
||||
@@ -324,11 +327,12 @@ function wp_default_packages_scripts( $scripts ) {
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @global WP_Locale $wp_locale WordPress date and time locale object.
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @param WP_Scripts $scripts WP_Scripts object.
|
||||
*/
|
||||
function wp_default_packages_inline_scripts( $scripts ) {
|
||||
global $wp_locale;
|
||||
global $wp_locale, $wpdb;
|
||||
|
||||
if ( isset( $scripts->registered['wp-api-fetch'] ) ) {
|
||||
$scripts->registered['wp-api-fetch']->deps[] = 'wp-hooks';
|
||||
@@ -360,19 +364,22 @@ function wp_default_packages_inline_scripts( $scripts ) {
|
||||
),
|
||||
'after'
|
||||
);
|
||||
|
||||
$meta_key = $wpdb->get_blog_prefix() . 'persisted_preferences';
|
||||
$user_id = get_current_user_ID();
|
||||
$preload_data = get_user_meta( $user_id, $meta_key, true );
|
||||
$scripts->add_inline_script(
|
||||
'wp-data',
|
||||
implode(
|
||||
"\n",
|
||||
array(
|
||||
'( function() {',
|
||||
' var userId = ' . get_current_user_ID() . ';',
|
||||
' var storageKey = "WP_DATA_USER_" + userId;',
|
||||
' wp.data',
|
||||
' .use( wp.data.plugins.persistence, { storageKey: storageKey } );',
|
||||
' wp.data.plugins.persistence.__unstableMigrate( { storageKey: storageKey } );',
|
||||
'} )();',
|
||||
)
|
||||
'wp-preferences',
|
||||
sprintf(
|
||||
'( function() {
|
||||
var serverData = %s;
|
||||
var userId = "%d";
|
||||
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
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user