Skip to content

Commit 12ea3ba

Browse files
committed
Add testing, some refactor
1 parent 3b696d7 commit 12ea3ba

File tree

15 files changed

+2330
-137
lines changed

15 files changed

+2330
-137
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* SCF Field Metadata Cache
3+
*
4+
* @since 6.7.0
5+
* Simple cache for field metadata used in block bindings.
6+
*/
7+
8+
let fieldMetadataCache = {};
9+
10+
/**
11+
* Set field metadata, replacing all existing data.
12+
*
13+
* @param {Object} fields - Field metadata object keyed by field key.
14+
*/
15+
export const setFieldMetadata = ( fields ) => {
16+
fieldMetadataCache = fields || {};
17+
};
18+
19+
/**
20+
* Add field metadata, merging with existing data.
21+
*
22+
* @param {Object} fields - Field metadata object keyed by field key.
23+
*/
24+
export const addFieldMetadata = ( fields ) => {
25+
fieldMetadataCache = { ...fieldMetadataCache, ...fields };
26+
};
27+
28+
/**
29+
* Clear all field metadata.
30+
*/
31+
export const clearFieldMetadata = () => {
32+
fieldMetadataCache = {};
33+
};
34+
35+
/**
36+
* Get field metadata for a specific field.
37+
*
38+
* @param {string} fieldKey - The field key to retrieve metadata for.
39+
* @return {Object|null} Field metadata object or null if not found.
40+
*/
41+
export const getFieldMetadata = ( fieldKey ) => {
42+
return fieldMetadataCache[ fieldKey ] || null;
43+
};
44+
45+
/**
46+
* Get all field metadata.
47+
*
48+
* @return {Object} All field metadata.
49+
*/
50+
export const getAllFieldMetadata = () => {
51+
return fieldMetadataCache;
52+
};
53+
54+
/**
55+
* Check if field metadata exists for a given key.
56+
*
57+
* @param {string} fieldKey - The field key to check.
58+
* @return {boolean} True if metadata exists, false otherwise.
59+
*/
60+
export const hasFieldMetadata = ( fieldKey ) => {
61+
return !! fieldMetadataCache[ fieldKey ];
62+
};

assets/src/js/bindings/hooks.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
*/
44

55
import { useState, useEffect } from '@wordpress/element';
6-
import { useSelect, useDispatch } from '@wordpress/data';
6+
import { useSelect } from '@wordpress/data';
77
import { store as coreDataStore } from '@wordpress/core-data';
88
import { store as editorStore } from '@wordpress/editor';
99
import apiFetch from '@wordpress/api-fetch';
1010
import { addQueryArgs } from '@wordpress/url';
1111

1212
import { extractPostTypeFromTemplate, formatFieldGroupsData } from './utils';
13-
import { STORE_NAME } from './store';
13+
import { addFieldMetadata } from './fieldMetadataCache';
1414

1515
/**
1616
* Custom hook to detect if we're in the site editor and get the template info.
@@ -100,7 +100,6 @@ export function useSiteEditorFields( postType ) {
100100
const [ fields, setFields ] = useState( {} );
101101
const [ isLoading, setIsLoading ] = useState( false );
102102
const [ error, setError ] = useState( null );
103-
const { addFieldMetadata } = useDispatch( STORE_NAME );
104103

105104
useEffect( () => {
106105
if ( ! postType ) {
@@ -149,7 +148,7 @@ export function useSiteEditorFields( postType ) {
149148
return () => {
150149
isCancelled = true;
151150
};
152-
}, [ postType, addFieldMetadata ] );
151+
}, [ postType ] );
153152

154153
return { fields, isLoading, error };
155154
}

assets/src/js/bindings/sources.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
processFieldBinding,
1515
formatFieldLabel,
1616
} from './field-processing';
17-
import { STORE_NAME } from './store';
17+
import { getFieldMetadata } from './fieldMetadataCache';
1818

1919
/**
2020
* Register the SCF field binding source.
@@ -29,7 +29,7 @@ registerBlockBindingsSource( {
2929
return __( 'SCF Fields', 'secure-custom-fields' );
3030
}
3131

32-
const fieldMetadata = select( STORE_NAME ).getFieldMetadata( fieldKey );
32+
const fieldMetadata = getFieldMetadata( fieldKey );
3333

3434
if ( fieldMetadata?.label ) {
3535
return fieldMetadata.label;
@@ -53,8 +53,7 @@ registerBlockBindingsSource( {
5353
return;
5454
}
5555

56-
const fieldMetadata =
57-
select( STORE_NAME ).getFieldMetadata( fieldKey );
56+
const fieldMetadata = getFieldMetadata( fieldKey );
5857
result[ attribute ] =
5958
fieldMetadata?.label || formatFieldLabel( fieldKey );
6059
}

assets/src/js/bindings/store.js

Lines changed: 0 additions & 79 deletions
This file was deleted.

assets/src/js/pro/blocks-v3/components/error-boundary.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Component, createContext } from '@wordpress/element';
2+
import { Placeholder, Button, Icon } from '@wordpress/components';
3+
import { blockDefault as blockIcon } from '@wordpress/icons';
24

35
// Create context outside the class
46
export const ErrorBoundaryContext = createContext( null );

0 commit comments

Comments
 (0)