diff --git a/blocks/api/index.js b/blocks/api/index.js index a03da6ee9eb70a..643cbe4b0bcbd3 100644 --- a/blocks/api/index.js +++ b/blocks/api/index.js @@ -16,6 +16,3 @@ export { hasBlockSupport, } from './registration'; -// Deprecated matchers -import * as source from './source'; -export { source }; diff --git a/blocks/api/registration.js b/blocks/api/registration.js index cac698008e5faf..33cc40342dd5e3 100644 --- a/blocks/api/registration.js +++ b/blocks/api/registration.js @@ -3,7 +3,7 @@ /** * External dependencies */ -import { get, isFunction, some, mapValues } from 'lodash'; +import { get, isFunction, some } from 'lodash'; /** * WordPress dependencies @@ -119,17 +119,6 @@ export function registerBlockType( name, settings ) { ...settings, }; - // Resolve deprecated attributes - settings.attributes = mapValues( settings.attributes, ( attribute ) => { - if ( isFunction( attribute.source ) ) { - return { - ...attribute, - ...attribute.source(), - }; - } - return attribute; - } ); - settings = applyFilters( 'registerBlockType', settings, name ); return blocks[ name ] = settings; diff --git a/blocks/hooks/index.js b/blocks/hooks/index.js index e725627bd21c00..13d1c0b06a07b3 100644 --- a/blocks/hooks/index.js +++ b/blocks/hooks/index.js @@ -8,6 +8,7 @@ import createHooks from '@wordpress/hooks'; */ import anchor from './anchor'; import customClassName from './custom-class-name'; +import matchers from './matchers'; const hooks = createHooks(); @@ -47,3 +48,4 @@ export { anchor( hooks ); customClassName( hooks ); +matchers( hooks ); diff --git a/blocks/api/source.js b/blocks/hooks/matchers.js similarity index 67% rename from blocks/api/source.js rename to blocks/hooks/matchers.js index a3f4bc4ec412dd..7aa433cdf9358b 100644 --- a/blocks/api/source.js +++ b/blocks/hooks/matchers.js @@ -1,3 +1,8 @@ +/** + * External dependencies + */ +import { isFunction, mapValues } from 'lodash'; + function warnAboutDeprecatedMatcher() { // eslint-disable-next-line no-console console.warn( @@ -64,3 +69,28 @@ export const node = ( selector ) => () => { selector, }; }; + +/** + * Resolve the matchers attributes for backwards compatibilty + * + * @param {Object} settings Original block settings + * @return {Object} Filtered block settings + */ +export function resolveAttributes( settings ) { + // Resolve deprecated attributes + settings.attributes = mapValues( settings.attributes, ( attribute ) => { + if ( isFunction( attribute.source ) ) { + return { + ...attribute, + ...attribute.source(), + }; + } + return attribute; + } ); + + return settings; +} + +export default function anchor( { addFilter } ) { + addFilter( 'registerBlockType', 'core\matchers', resolveAttributes ); +} diff --git a/blocks/index.js b/blocks/index.js index ef56387023922d..36d647567c635a 100644 --- a/blocks/index.js +++ b/blocks/index.js @@ -27,3 +27,7 @@ export { default as InspectorControls } from './inspector-controls'; export { default as MediaUploadButton } from './media-upload-button'; export { default as UrlInput } from './url-input'; export { default as UrlInputButton } from './url-input/button'; + +// Deprecated matchers +import { attr, prop, text, html, query, node, children } from './hooks/matchers'; +export const source = { attr, prop, text, html, query, node, children };