-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
102 lines (94 loc) · 3.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Internal dependencies
*/
import moveFeaturePreferences from './move-feature-preferences';
import moveThirdPartyFeaturePreferences from './move-third-party-feature-preferences';
import moveIndividualPreference from './move-individual-preference';
import moveInterfaceEnableItems from './move-interface-enable-items';
import convertEditPostPanels from './convert-edit-post-panels';
/**
* Gets the legacy local storage data for a given user.
*
* @param {string | number} userId The user id.
*
* @return {Object | null} The local storage data.
*/
function getLegacyData( userId ) {
const key = `WP_DATA_USER_${ userId }`;
const unparsedData = window.localStorage.getItem( key );
return JSON.parse( unparsedData );
}
/**
* Converts data from the old `@wordpress/data` package format.
*
* @param {Object | null | undefined} data The legacy data in its original format.
*
* @return {Object | undefined} The converted data or `undefined` if there was
* nothing to convert.
*/
export function convertLegacyData( data ) {
if ( ! data ) {
return;
}
// Move boolean feature preferences from each editor into the
// preferences store data structure.
data = moveFeaturePreferences( data, 'core/edit-widgets' );
data = moveFeaturePreferences( data, 'core/customize-widgets' );
data = moveFeaturePreferences( data, 'core/edit-post' );
data = moveFeaturePreferences( data, 'core/edit-site' );
// Move third party boolean feature preferences from the interface package
// to the preferences store data structure.
data = moveThirdPartyFeaturePreferences( data );
// Move and convert the interface store's `enableItems` data into the
// preferences data structure.
data = moveInterfaceEnableItems( data );
// Move individual ad-hoc preferences from various packages into the
// preferences store data structure.
data = moveIndividualPreference(
data,
{ from: 'core/edit-post', to: 'core/edit-post' },
'hiddenBlockTypes'
);
data = moveIndividualPreference(
data,
{ from: 'core/edit-post', to: 'core/edit-post' },
'editorMode'
);
data = moveIndividualPreference(
data,
{ from: 'core/edit-post', to: 'core/edit-post' },
'preferredStyleVariations'
);
data = moveIndividualPreference(
data,
{ from: 'core/edit-post', to: 'core/edit-post' },
'panels',
convertEditPostPanels
);
data = moveIndividualPreference(
data,
{ from: 'core/editor', to: 'core/edit-post' },
'isPublishSidebarEnabled'
);
data = moveIndividualPreference(
data,
{ from: 'core/edit-site', to: 'core/edit-site' },
'editorMode'
);
// The new system is only concerned with persisting
// 'core/preferences' preferences reducer, so only return that.
return data?.[ 'core/preferences' ]?.preferences;
}
/**
* Gets the legacy local storage data for the given user and returns the
* data converted to the new format.
*
* @param {string | number} userId The user id.
*
* @return {Object | undefined} The converted data or undefined if no local
* storage data could be found.
*/
export default function convertLegacyLocalStorageData( userId ) {
const data = getLegacyData( userId );
return convertLegacyData( data );
}