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.

git-svn-id: https://develop.svn.wordpress.org/trunk@54182 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork
2022-09-15 16:43:39 +00:00
parent 2b4d385298
commit fc6bc030d4
7 changed files with 3542 additions and 147 deletions

View File

@@ -0,0 +1,61 @@
<?php
/**
* @group user
*
* @covers ::wp_register_persisted_preferences_meta
*/
class Tests_User_WpRegisterPersistedPreferencesMeta extends WP_UnitTestCase {
/**
* Test that user persisted preferences meta is registered.
*
* @ticket 56467
*/
public function test_should_register_persisted_preferences_meta() {
global $wpdb, $wp_meta_keys;
$meta_key = $wpdb->get_blog_prefix() . 'persisted_preferences';
// Test that meta key is registered.
unregister_meta_key( 'user', $meta_key );
wp_register_persisted_preferences_meta();
$this->assertIsArray( $wp_meta_keys, 'No meta keys exist' );
$this->assertArrayHasKey(
$meta_key,
$wp_meta_keys['user'][''],
'The expected meta key was not registered'
);
// Test to detect changes in meta key structure.
$this->assertSame(
array(
'type' => 'object',
'description' => '',
'single' => true,
'sanitize_callback' => null,
'auth_callback' => '__return_true',
'show_in_rest' => array(
'name' => 'persisted_preferences',
'type' => 'object',
'context' => array( 'edit' ),
'schema' => array(
'type' => 'object',
'properties' => array(
'_modified' => array(
'description' => __( 'The date and time the preferences were updated.' ),
'type' => 'string',
'format' => 'date-time',
'readonly' => false,
),
),
'additionalProperties' => true,
),
),
),
$wp_meta_keys['user'][''][ $meta_key ],
'The registered metadata did not have the expected structure'
);
}
}