Make WordPress Core


Ignore:
Timestamp:
09/15/2022 04:43:39 PM (4 years ago)
Author:
hellofromTonya
Message:

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.​See WordPress/gutenberg PR 39795.

Props talldanwp, youknowriad, noisysocks, mamaduka, costdev, ironprogrammer, hellofromTonya.
See #56467.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-loader.php

    r54155 r54182  
    295295                                array_push( $dependencies, 'media-models', 'media-views', 'postbox', 'wp-dom-ready' );
    296296                                break;
     297                        case 'wp-preferences':
     298                                array_push( $dependencies, 'wp-preferences-persistence' );
     299                                break;
    297300                }
    298301
    … …  
    325328 *
    326329 * @global WP_Locale $wp_locale WordPress date and time locale object.
     330 * @global wpdb      $wpdb      WordPress database abstraction object.
    327331 *
    328332 * @param WP_Scripts $scripts WP_Scripts object.
    329333 */
    330334function wp_default_packages_inline_scripts( $scripts ) {
    331         global $wp_locale;
     335        global $wp_locale, $wpdb;
    332336
    333337        if ( isset( $scripts->registered['wp-api-fetch'] ) ) {
    … …  
    361365                'after'
    362366        );
     367
     368        $meta_key     = $wpdb->get_blog_prefix() . 'persisted_preferences';
     369        $user_id      = get_current_user_ID();
     370        $preload_data = get_user_meta( $user_id, $meta_key, true );
    363371        $scripts->add_inline_script(
    364                 'wp-data',
    365                 implode(
    366                         "\n",
    367                         array(
    368                                 '( function() {',
    369                                 '       var userId = ' . get_current_user_ID() . ';',
    370                                 '       var storageKey = "WP_DATA_USER_" + userId;',
    371                                 '       wp.data',
    372                                 '               .use( wp.data.plugins.persistence, { storageKey: storageKey } );',
    373                                 '       wp.data.plugins.persistence.__unstableMigrate( { storageKey: storageKey } );',
    374                                 '} )();',
    375                         )
     372                'wp-preferences',
     373                sprintf(
     374                        '( function() {
     375                                var serverData = %s;
     376                                var userId = "%d";
     377                                var persistenceLayer = wp.preferencesPersistence.__unstableCreatePersistenceLayer( serverData, userId );
     378                                var preferencesStore = wp.preferences.store;
     379                                wp.data.dispatch( preferencesStore ).setPersistenceLayer( persistenceLayer );
     380                        } ) ();',
     381                        wp_json_encode( $preload_data ),
     382                        $user_id
    376383                )
    377384        );
Note: See TracChangeset for help on using the changeset viewer.