|
2 | 2 | * WordPress dependencies
|
3 | 3 | */
|
4 | 4 | import { store as coreDataStore } from '@wordpress/core-data';
|
| 5 | +import { createSelector } from '@wordpress/data'; |
5 | 6 |
|
6 | 7 | /**
|
7 | 8 | * Internal dependencies
|
@@ -34,42 +35,56 @@ import { unlock } from '../lock-unlock';
|
34 | 35 | * }
|
35 | 36 | * ```
|
36 | 37 | */
|
37 |
| -function getPostMetaFields( select, context ) { |
38 |
| - const { getEditedEntityRecord } = select( coreDataStore ); |
39 |
| - const { getRegisteredPostMeta } = unlock( select( coreDataStore ) ); |
| 38 | +const getPostMetaFields = createSelector( |
| 39 | + ( select, context ) => { |
| 40 | + const { getEditedEntityRecord } = select( coreDataStore ); |
| 41 | + const { getRegisteredPostMeta } = unlock( select( coreDataStore ) ); |
40 | 42 |
|
41 |
| - let entityMetaValues; |
42 |
| - // Try to get the current entity meta values. |
43 |
| - if ( context?.postType && context?.postId ) { |
44 |
| - entityMetaValues = getEditedEntityRecord( |
45 |
| - 'postType', |
46 |
| - context?.postType, |
47 |
| - context?.postId |
48 |
| - ).meta; |
49 |
| - } |
50 |
| - |
51 |
| - const registeredFields = getRegisteredPostMeta( context?.postType ); |
52 |
| - const metaFields = {}; |
53 |
| - Object.entries( registeredFields || {} ).forEach( ( [ key, props ] ) => { |
54 |
| - // Don't include footnotes or private fields. |
55 |
| - if ( key !== 'footnotes' && key.charAt( 0 ) !== '_' ) { |
56 |
| - metaFields[ key ] = { |
57 |
| - label: props.title || key, |
58 |
| - value: |
59 |
| - // When using the entity value, an empty string IS a valid value. |
60 |
| - entityMetaValues?.[ key ] ?? |
61 |
| - // When using the default, an empty string IS NOT a valid value. |
62 |
| - ( props.default || undefined ), |
63 |
| - }; |
| 43 | + let entityMetaValues; |
| 44 | + // Try to get the current entity meta values. |
| 45 | + if ( context?.postType && context?.postId ) { |
| 46 | + entityMetaValues = getEditedEntityRecord( |
| 47 | + 'postType', |
| 48 | + context?.postType, |
| 49 | + context?.postId |
| 50 | + ).meta; |
64 | 51 | }
|
65 |
| - } ); |
66 | 52 |
|
67 |
| - if ( ! Object.keys( metaFields || {} ).length ) { |
68 |
| - return null; |
69 |
| - } |
| 53 | + const registeredFields = getRegisteredPostMeta( context?.postType ); |
| 54 | + const metaFields = {}; |
| 55 | + Object.entries( registeredFields || {} ).forEach( |
| 56 | + ( [ key, props ] ) => { |
| 57 | + // Don't include footnotes or private fields. |
| 58 | + if ( key !== 'footnotes' && key.charAt( 0 ) !== '_' ) { |
| 59 | + metaFields[ key ] = { |
| 60 | + label: props.title || key, |
| 61 | + value: |
| 62 | + // When using the entity value, an empty string IS a valid value. |
| 63 | + entityMetaValues?.[ key ] ?? |
| 64 | + // When using the default, an empty string IS NOT a valid value. |
| 65 | + ( props.default || undefined ), |
| 66 | + }; |
| 67 | + } |
| 68 | + } |
| 69 | + ); |
70 | 70 |
|
71 |
| - return metaFields; |
72 |
| -} |
| 71 | + if ( ! Object.keys( metaFields || {} ).length ) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + return metaFields; |
| 76 | + }, |
| 77 | + ( select, context ) => [ |
| 78 | + select( coreDataStore ).getEditedEntityRecord( |
| 79 | + 'postType', |
| 80 | + context.postType, |
| 81 | + context.postId |
| 82 | + ).meta, |
| 83 | + unlock( select( coreDataStore ) ).getRegisteredPostMeta( |
| 84 | + context.postType |
| 85 | + ), |
| 86 | + ] |
| 87 | +); |
73 | 88 |
|
74 | 89 | export default {
|
75 | 90 | name: 'core/post-meta',
|
|
0 commit comments