@wordpress/data 6.7.0 → 6.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
- import { merge, isPlainObject, identity } from 'lodash';
4
+ import { merge, isPlainObject } from 'lodash';
5
5
 
6
6
  /**
7
7
  * Internal dependencies
@@ -222,424 +222,6 @@ function persistencePlugin( registry, pluginOptions ) {
222
222
  };
223
223
  }
224
224
 
225
- /**
226
- * Move the 'features' object in local storage from the sourceStoreName to the
227
- * preferences store.
228
- *
229
- * @param {Object} persistence The persistence interface.
230
- * @param {string} sourceStoreName The name of the store that has persisted
231
- * preferences to migrate to the preferences
232
- * package.
233
- */
234
- export function migrateFeaturePreferencesToPreferencesStore(
235
- persistence,
236
- sourceStoreName
237
- ) {
238
- const preferencesStoreName = 'core/preferences';
239
- const interfaceStoreName = 'core/interface';
240
-
241
- const state = persistence.get();
242
-
243
- // Features most recently (and briefly) lived in the interface package.
244
- // If data exists there, prioritize using that for the migration. If not
245
- // also check the original package as the user may have updated from an
246
- // older block editor version.
247
- const interfaceFeatures =
248
- state[ interfaceStoreName ]?.preferences?.features?.[ sourceStoreName ];
249
- const sourceFeatures = state[ sourceStoreName ]?.preferences?.features;
250
- const featuresToMigrate = interfaceFeatures
251
- ? interfaceFeatures
252
- : sourceFeatures;
253
-
254
- if ( featuresToMigrate ) {
255
- const existingPreferences = state[ preferencesStoreName ]?.preferences;
256
-
257
- // Avoid migrating features again if they've previously been migrated.
258
- if ( ! existingPreferences?.[ sourceStoreName ] ) {
259
- // Set the feature values in the interface store, the features
260
- // object is keyed by 'scope', which matches the store name for
261
- // the source.
262
- persistence.set( preferencesStoreName, {
263
- preferences: {
264
- ...existingPreferences,
265
- [ sourceStoreName ]: featuresToMigrate,
266
- },
267
- } );
268
-
269
- // Remove migrated feature preferences from `interface`.
270
- if ( interfaceFeatures ) {
271
- const otherInterfaceState = state[ interfaceStoreName ];
272
- const otherInterfaceScopes =
273
- state[ interfaceStoreName ]?.preferences?.features;
274
-
275
- persistence.set( interfaceStoreName, {
276
- ...otherInterfaceState,
277
- preferences: {
278
- features: {
279
- ...otherInterfaceScopes,
280
- [ sourceStoreName ]: undefined,
281
- },
282
- },
283
- } );
284
- }
285
-
286
- // Remove migrated feature preferences from the source.
287
- if ( sourceFeatures ) {
288
- const otherSourceState = state[ sourceStoreName ];
289
- const sourcePreferences = state[ sourceStoreName ]?.preferences;
290
-
291
- persistence.set( sourceStoreName, {
292
- ...otherSourceState,
293
- preferences: {
294
- ...sourcePreferences,
295
- features: undefined,
296
- },
297
- } );
298
- }
299
- }
300
- }
301
- }
302
-
303
- /**
304
- * Migrates an individual item inside the `preferences` object for a store.
305
- *
306
- * @param {Object} persistence The persistence interface.
307
- * @param {Object} migrate An options object that contains details of the migration.
308
- * @param {string} migrate.from The name of the store to migrate from.
309
- * @param {string} migrate.scope The scope in the preferences store to migrate to.
310
- * @param {string} key The key in the preferences object to migrate.
311
- * @param {?Function} convert A function that converts preferences from one format to another.
312
- */
313
- export function migrateIndividualPreferenceToPreferencesStore(
314
- persistence,
315
- { from: sourceStoreName, scope },
316
- key,
317
- convert = identity
318
- ) {
319
- const preferencesStoreName = 'core/preferences';
320
- const state = persistence.get();
321
- const sourcePreference = state[ sourceStoreName ]?.preferences?.[ key ];
322
-
323
- // There's nothing to migrate, exit early.
324
- if ( sourcePreference === undefined ) {
325
- return;
326
- }
327
-
328
- const targetPreference =
329
- state[ preferencesStoreName ]?.preferences?.[ scope ]?.[ key ];
330
-
331
- // There's existing data at the target, so don't overwrite it, exit early.
332
- if ( targetPreference ) {
333
- return;
334
- }
335
-
336
- const otherScopes = state[ preferencesStoreName ]?.preferences;
337
- const otherPreferences =
338
- state[ preferencesStoreName ]?.preferences?.[ scope ];
339
-
340
- // Pass an object with the key and value as this allows the convert
341
- // function to convert to a data structure that has different keys.
342
- const convertedPreferences = convert( { [ key ]: sourcePreference } );
343
-
344
- persistence.set( preferencesStoreName, {
345
- preferences: {
346
- ...otherScopes,
347
- [ scope ]: {
348
- ...otherPreferences,
349
- ...convertedPreferences,
350
- },
351
- },
352
- } );
353
-
354
- // Remove migrated feature preferences from the source.
355
- const otherSourceState = state[ sourceStoreName ];
356
- const allSourcePreferences = state[ sourceStoreName ]?.preferences;
357
- persistence.set( sourceStoreName, {
358
- ...otherSourceState,
359
- preferences: {
360
- ...allSourcePreferences,
361
- [ key ]: undefined,
362
- },
363
- } );
364
- }
365
-
366
- /**
367
- * Convert from:
368
- * ```
369
- * {
370
- * panels: {
371
- * tags: {
372
- * enabled: true,
373
- * opened: true,
374
- * },
375
- * permalinks: {
376
- * enabled: false,
377
- * opened: false,
378
- * },
379
- * },
380
- * }
381
- * ```
382
- *
383
- * to:
384
- * {
385
- * inactivePanels: [
386
- * 'permalinks',
387
- * ],
388
- * openPanels: [
389
- * 'tags',
390
- * ],
391
- * }
392
- *
393
- * @param {Object} preferences A preferences object.
394
- *
395
- * @return {Object} The converted data.
396
- */
397
- export function convertEditPostPanels( preferences ) {
398
- const panels = preferences?.panels ?? {};
399
- return Object.keys( panels ).reduce(
400
- ( convertedData, panelName ) => {
401
- const panel = panels[ panelName ];
402
-
403
- if ( panel?.enabled === false ) {
404
- convertedData.inactivePanels.push( panelName );
405
- }
406
-
407
- if ( panel?.opened === true ) {
408
- convertedData.openPanels.push( panelName );
409
- }
410
-
411
- return convertedData;
412
- },
413
- { inactivePanels: [], openPanels: [] }
414
- );
415
- }
416
-
417
- export function migrateThirdPartyFeaturePreferencesToPreferencesStore(
418
- persistence
419
- ) {
420
- const interfaceStoreName = 'core/interface';
421
- const preferencesStoreName = 'core/preferences';
422
-
423
- let state = persistence.get();
424
-
425
- const interfaceScopes = state[ interfaceStoreName ]?.preferences?.features;
426
-
427
- for ( const scope in interfaceScopes ) {
428
- // Don't migrate any core 'scopes'.
429
- if ( scope.startsWith( 'core' ) ) {
430
- continue;
431
- }
432
-
433
- // Skip this scope if there are no features to migrate.
434
- const featuresToMigrate = interfaceScopes[ scope ];
435
- if ( ! featuresToMigrate ) {
436
- continue;
437
- }
438
-
439
- const existingPreferences = state[ preferencesStoreName ]?.preferences;
440
-
441
- // Add the data to the preferences store structure.
442
- persistence.set( preferencesStoreName, {
443
- preferences: {
444
- ...existingPreferences,
445
- [ scope ]: featuresToMigrate,
446
- },
447
- } );
448
-
449
- // Remove the data from the interface store structure.
450
- // Call `persistence.get` again to make sure `state` is up-to-date with
451
- // any changes from the previous iteration of this loop.
452
- state = persistence.get();
453
- const otherInterfaceState = state[ interfaceStoreName ];
454
- const otherInterfaceScopes =
455
- state[ interfaceStoreName ]?.preferences?.features;
456
-
457
- persistence.set( interfaceStoreName, {
458
- ...otherInterfaceState,
459
- preferences: {
460
- features: {
461
- ...otherInterfaceScopes,
462
- [ scope ]: undefined,
463
- },
464
- },
465
- } );
466
- }
467
- }
468
-
469
- /**
470
- * Migrates interface 'enableItems' data to the preferences store.
471
- *
472
- * The interface package stores this data in this format:
473
- * ```js
474
- * {
475
- * enableItems: {
476
- * singleEnableItems: {
477
- * complementaryArea: {
478
- * 'core/edit-post': 'edit-post/document',
479
- * 'core/edit-site': 'edit-site/global-styles',
480
- * }
481
- * },
482
- * multipleEnableItems: {
483
- * pinnedItems: {
484
- * 'core/edit-post': {
485
- * 'plugin-1': true,
486
- * },
487
- * 'core/edit-site': {
488
- * 'plugin-2': true,
489
- * },
490
- * },
491
- * }
492
- * }
493
- * }
494
- * ```
495
- * and it should be migrated it to:
496
- * ```js
497
- * {
498
- * 'core/edit-post': {
499
- * complementaryArea: 'edit-post/document',
500
- * pinnedItems: {
501
- * 'plugin-1': true,
502
- * },
503
- * },
504
- * 'core/edit-site': {
505
- * complementaryArea: 'edit-site/global-styles',
506
- * pinnedItems: {
507
- * 'plugin-2': true,
508
- * },
509
- * },
510
- * }
511
- * ```
512
- *
513
- * @param {Object} persistence The persistence interface.
514
- */
515
- export function migrateInterfaceEnableItemsToPreferencesStore( persistence ) {
516
- const interfaceStoreName = 'core/interface';
517
- const preferencesStoreName = 'core/preferences';
518
- const state = persistence.get();
519
- const sourceEnableItems = state[ interfaceStoreName ]?.enableItems;
520
-
521
- // There's nothing to migrate, exit early.
522
- if ( ! sourceEnableItems ) {
523
- return;
524
- }
525
-
526
- const allPreferences = state[ preferencesStoreName ]?.preferences ?? {};
527
-
528
- // First convert complementaryAreas into the right format.
529
- // Use the existing preferences as the accumulator so that the data is
530
- // merged.
531
- const sourceComplementaryAreas =
532
- sourceEnableItems?.singleEnableItems?.complementaryArea ?? {};
533
-
534
- const convertedComplementaryAreas = Object.keys(
535
- sourceComplementaryAreas
536
- ).reduce( ( accumulator, scope ) => {
537
- const data = sourceComplementaryAreas[ scope ];
538
-
539
- // Don't overwrite any existing data in the preferences store.
540
- if ( accumulator[ scope ]?.complementaryArea ) {
541
- return accumulator;
542
- }
543
-
544
- return {
545
- ...accumulator,
546
- [ scope ]: {
547
- ...accumulator[ scope ],
548
- complementaryArea: data,
549
- },
550
- };
551
- }, allPreferences );
552
-
553
- // Next feed the converted complementary areas back into a reducer that
554
- // converts the pinned items, resulting in the fully migrated data.
555
- const sourcePinnedItems =
556
- sourceEnableItems?.multipleEnableItems?.pinnedItems ?? {};
557
- const allConvertedData = Object.keys( sourcePinnedItems ).reduce(
558
- ( accumulator, scope ) => {
559
- const data = sourcePinnedItems[ scope ];
560
- // Don't overwrite any existing data in the preferences store.
561
- if ( accumulator[ scope ]?.pinnedItems ) {
562
- return accumulator;
563
- }
564
-
565
- return {
566
- ...accumulator,
567
- [ scope ]: {
568
- ...accumulator[ scope ],
569
- pinnedItems: data,
570
- },
571
- };
572
- },
573
- convertedComplementaryAreas
574
- );
575
-
576
- persistence.set( preferencesStoreName, {
577
- preferences: allConvertedData,
578
- } );
579
-
580
- // Remove migrated preferences.
581
- const otherInterfaceItems = state[ interfaceStoreName ];
582
- persistence.set( interfaceStoreName, {
583
- ...otherInterfaceItems,
584
- enableItems: undefined,
585
- } );
586
- }
587
-
588
- persistencePlugin.__unstableMigrate = ( pluginOptions ) => {
589
- const persistence = createPersistenceInterface( pluginOptions );
590
-
591
- // Boolean feature preferences.
592
- migrateFeaturePreferencesToPreferencesStore(
593
- persistence,
594
- 'core/edit-widgets'
595
- );
596
- migrateFeaturePreferencesToPreferencesStore(
597
- persistence,
598
- 'core/customize-widgets'
599
- );
600
- migrateFeaturePreferencesToPreferencesStore(
601
- persistence,
602
- 'core/edit-post'
603
- );
604
- migrateFeaturePreferencesToPreferencesStore(
605
- persistence,
606
- 'core/edit-site'
607
- );
608
- migrateThirdPartyFeaturePreferencesToPreferencesStore( persistence );
609
-
610
- // Other ad-hoc preferences.
611
- migrateIndividualPreferenceToPreferencesStore(
612
- persistence,
613
- { from: 'core/edit-post', scope: 'core/edit-post' },
614
- 'hiddenBlockTypes'
615
- );
616
- migrateIndividualPreferenceToPreferencesStore(
617
- persistence,
618
- { from: 'core/edit-post', scope: 'core/edit-post' },
619
- 'editorMode'
620
- );
621
- migrateIndividualPreferenceToPreferencesStore(
622
- persistence,
623
- { from: 'core/edit-post', scope: 'core/edit-post' },
624
- 'preferredStyleVariations'
625
- );
626
- migrateIndividualPreferenceToPreferencesStore(
627
- persistence,
628
- { from: 'core/edit-post', scope: 'core/edit-post' },
629
- 'panels',
630
- convertEditPostPanels
631
- );
632
- migrateIndividualPreferenceToPreferencesStore(
633
- persistence,
634
- { from: 'core/editor', scope: 'core/edit-post' },
635
- 'isPublishSidebarEnabled'
636
- );
637
- migrateIndividualPreferenceToPreferencesStore(
638
- persistence,
639
- { from: 'core/edit-site', scope: 'core/edit-site' },
640
- 'editorMode'
641
- );
642
- migrateInterfaceEnableItemsToPreferencesStore( persistence );
643
- };
225
+ persistencePlugin.__unstableMigrate = () => {};
644
226
 
645
227
  export default persistencePlugin;