Skip to content

Commit

Permalink
Fix: Error while Calling edit-site getCurrentTemplateTemplateParts se…
Browse files Browse the repository at this point in the history
…lector (WordPress#63818)

Co-authored-by: jorgefilipecosta <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
  • Loading branch information
3 people authored Jul 23, 2024
1 parent 28fb876 commit 8446fc7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 81 deletions.
2 changes: 2 additions & 0 deletions docs/reference-guides/data/data-core-edit-site.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ _Returns_
### getCurrentTemplateTemplateParts

> **Deprecated**
Returns the template parts and their blocks for the current edited template.

_Parameters_
Expand Down
43 changes: 37 additions & 6 deletions packages/edit-site/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
* WordPress dependencies
*/
import { store as coreDataStore } from '@wordpress/core-data';
import { createRegistrySelector } from '@wordpress/data';
import { createRegistrySelector, createSelector } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import { Platform } from '@wordpress/element';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as editorStore } from '@wordpress/editor';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';
import { TEMPLATE_PART_POST_TYPE } from '../utils/constants';
import getFilteredTemplatePartBlocks from '../utils/get-filtered-template-parts';

/**
* @typedef {'template'|'template_type'} TemplateType Template type.
Expand Down Expand Up @@ -240,18 +243,46 @@ export function isSaveViewOpened( state ) {
return state.saveViewPanel;
}

function getBlocksAndTemplateParts( select ) {
const templateParts = select( coreDataStore ).getEntityRecords(
'postType',
TEMPLATE_PART_POST_TYPE,
{ per_page: -1 }
);

const { getBlocksByName, getBlocksByClientId } = select( blockEditorStore );

const clientIds = getBlocksByName( 'core/template-part' );
const blocks = getBlocksByClientId( clientIds );
return [ blocks, templateParts ];
}

/**
* Returns the template parts and their blocks for the current edited template.
*
* @deprecated
* @param {Object} state Global application state.
* @return {Array} Template parts and their blocks in an array.
*/
export const getCurrentTemplateTemplateParts = createRegistrySelector(
( select ) => () => {
return unlock(
select( editorStore )
).getCurrentTemplateTemplateParts();
}
( select ) =>
createSelector(
() => {
deprecated(
`select( 'core/edit-site' ).getCurrentTemplateTemplateParts()`,
{
since: '6.7',
version: '6.9',
alternative: `select( 'core/block-editor' ).getBlocksByName( 'core/template-part' )`,
}
);

return getFilteredTemplatePartBlocks(
...getBlocksAndTemplateParts( select )
);
},
() => getBlocksAndTemplateParts( select )
)
);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import memoize from 'memize';

/**
* WordPress dependencies
*/
Expand All @@ -20,7 +15,10 @@ const EMPTY_ARRAY = [];
* @param {?Array} templateParts Available template parts.
* @return {Array} An array of template parts and their blocks.
*/
function getFilteredTemplatePartBlocks( blocks = EMPTY_ARRAY, templateParts ) {
export default function getFilteredTemplatePartBlocks(
blocks = EMPTY_ARRAY,
templateParts
) {
const templatePartsById = templateParts
? // Key template parts by their ID.
templateParts.reduce(
Expand Down Expand Up @@ -61,9 +59,3 @@ function getFilteredTemplatePartBlocks( blocks = EMPTY_ARRAY, templateParts ) {

return result;
}

const memoizedGetFilteredTemplatePartBlocks = memoize(
getFilteredTemplatePartBlocks
);

export { memoizedGetFilteredTemplatePartBlocks as getFilteredTemplatePartBlocks };
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { getFilteredTemplatePartBlocks } from '../get-filtered-template-parts';
import getFilteredTemplatePartBlocks from '../get-filtered-template-parts';

const NESTED_BLOCKS = [
{
Expand Down Expand Up @@ -98,16 +98,6 @@ const FLATTENED_BLOCKS = [
},
];

const SINGLE_TEMPLATE_PART_BLOCK = {
clientId: '1',
name: 'core/template-part',
innerBlocks: [],
attributes: {
slug: 'aside',
theme: 'my-theme',
},
};

const TEMPLATE_PARTS = [
{
id: 'my-theme//header',
Expand All @@ -134,56 +124,4 @@ describe( 'getFilteredTemplatePartBlocks', () => {
);
expect( flattenedFilteredTemplateParts ).toEqual( FLATTENED_BLOCKS );
} );

it( 'returns a cached result when passed the same params', () => {
// Clear the cache and call the function twice.
getFilteredTemplatePartBlocks.clear();
getFilteredTemplatePartBlocks( NESTED_BLOCKS, TEMPLATE_PARTS );
expect(
getFilteredTemplatePartBlocks( NESTED_BLOCKS, TEMPLATE_PARTS )
).toEqual( FLATTENED_BLOCKS );

// The function has been called twice with the same params, so the cache size should be 1.
/**
* TODO what should be done about this?
* Can it be tested another way?
* Is it necessary?
*/
// const [ , , originalSize ] =
// getFilteredTemplatePartBlocks.getCache();
// expect( originalSize ).toBe( 1 );

// Call the function again, with different params.
expect(
getFilteredTemplatePartBlocks(
[ SINGLE_TEMPLATE_PART_BLOCK ],
TEMPLATE_PARTS
)
).toEqual( [
{
block: {
clientId: '1',
name: 'core/template-part',
attributes: {
slug: 'aside',
theme: 'my-theme',
},
},
templatePart: {
id: 'my-theme//aside',
slug: 'aside',
theme: 'my-theme',
},
},
] );

// The function has been called with different params, so the cache size should now be 2.
/**
* TODO what should be done about this?
* Can it be tested another way?
* Is it necessary?
*/
// const [ , , finalSize ] = getFilteredTemplatePartBlocks.getCache();
// expect( finalSize ).toBe( 2 );
} );
} );

0 comments on commit 8446fc7

Please sign in to comment.