Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose before filter hook in useSettings for injecting block settings in the editor #45089

Merged
merged 35 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e42f6f6
Add blockEditor.useSetting.before hook to useSetting
alecgeatches Sep 20, 2022
6064007
Fix the merge conflicts from the latest refactor within gutenberg
ingeniumed Sep 23, 2022
bb57e5e
Merge pull request #1 from ingeniumed/add/use-setting-hooks
ingeniumed Sep 23, 2022
63789cb
Merge branch 'WordPress:trunk' into trunk
alecgeatches Sep 26, 2022
3426c5b
Merge branch 'WordPress:trunk' into trunk
ingeniumed Sep 27, 2022
e4d7ed0
Add a new filter for sanitizing on the php side
ingeniumed Sep 27, 2022
c5a4c1a
Add in a filter for the get_Settings_nodes for CSS override
ingeniumed Sep 28, 2022
711589c
Merge branch 'WordPress:trunk' into trunk
ingeniumed Sep 28, 2022
46eb108
Fix merge conflicts
ingeniumed Sep 28, 2022
36e7d37
Add newline back for package-lock
ingeniumed Sep 28, 2022
3da1bac
Merge pull request #2 from ingeniumed/add/filter-sanitize-php-side
ingeniumed Sep 28, 2022
b6dc84f
Add blockEditor.useSetting.before hook to useSetting
alecgeatches Sep 20, 2022
f9e0eae
Fix useSetting merge mistake
alecgeatches Sep 29, 2022
6313e65
Make get_blocks_metadata public for selector access
alecgeatches Sep 30, 2022
7e2779a
Merge pull request #3 from ingeniumed/add/public-block-metadata
ingeniumed Oct 4, 2022
194d313
Merge branch 'trunk' of github.com:ingeniumed/gutenberg into add/nest…
ingeniumed Oct 4, 2022
ca77c46
Remove the previous way of doing the nesting via filters in sanitize …
ingeniumed Oct 4, 2022
0d24f78
Merge pull request #4 from ingeniumed/remove/old-approach-using-filters
ingeniumed Oct 4, 2022
08e6ca4
Add useSetting tests for hook override
alecgeatches Oct 18, 2022
a345c49
Simplify block context mocking
alecgeatches Oct 18, 2022
e736f6e
Remove debug code, add test description
alecgeatches Oct 18, 2022
7a74ee6
Merge pull request #9 from ingeniumed/add/use-setting-tests
alecgeatches Oct 18, 2022
c6379aa
Merge branch 'trunk' of github.com:ingeniumed/gutenberg into add/nest…
ingeniumed Oct 19, 2022
c45932d
Merge branch 'add/nested-block-using-filters' of github.com:ingeniume…
ingeniumed Oct 19, 2022
a8f5a04
Merge branch 'trunk' of github.com:ingeniumed/gutenberg into add/nest…
ingeniumed Oct 19, 2022
a2260f3
Merge branch 'WordPress:trunk' into add/nested-block-using-filters
ingeniumed Oct 31, 2022
1c97bc3
Merge branch 'WordPress:trunk' into add/nested-block-using-filters
alecgeatches Nov 1, 2022
d76d296
Remove the previously exposed method for metadata of a block, do an e…
ingeniumed Nov 3, 2022
6c7858d
Fix the failing test by adding in all the parameters expected
ingeniumed Nov 3, 2022
f48c536
Merge branch 'WordPress:trunk' into add/nested-block-using-filters
ingeniumed Nov 3, 2022
a7e895d
Merge branch 'WordPress:trunk' into add/nested-block-using-filters
ingeniumed Nov 24, 2022
d00aab8
Rerun tests
alecgeatches Nov 24, 2022
8cc0e27
Reorder so it returns early if it found a match
oandregal Dec 2, 2022
dc80804
Change the wording for what the filter allows in the comment
ingeniumed Dec 5, 2022
4bab3ff
Merge branch 'WordPress:trunk' into add/nested-block-using-filters
ingeniumed Dec 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/block-editor/src/components/use-setting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
__EXPERIMENTAL_PATHS_WITH_MERGE as PATHS_WITH_MERGE,
hasBlockSupport,
} from '@wordpress/blocks';
import { applyFilters } from '@wordpress/hooks';

/**
* Internal dependencies
Expand Down Expand Up @@ -122,7 +123,18 @@ export default function useSetting( path ) {
return undefined;
}

let result;
// 0. Allow third parties to filter the block's settings at runtime.
let result = applyFilters(
'blockEditor.useSetting.before',
undefined,
path,
clientId,
blockName
);

if ( undefined !== result ) {
return result;
}

const normalizedPath = removeCustomPrefixes( path );

Expand Down
99 changes: 99 additions & 0 deletions packages/block-editor/src/components/use-setting/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* WordPress dependencies
*/
import { addFilter, removeFilter } from '@wordpress/hooks';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import useSetting from '..';
import * as BlockEditContext from '../../block-edit/context';

// Mock useSelect() functions used by useSetting()
jest.mock( '@wordpress/data/src/components/use-select' );

let selectMock = {};
const setupSelectMock = () => {
selectMock = {
getSettings: () => ( {} ),
getBlockParents: () => [],
getBlockName: () => '',
};
};

useSelect.mockImplementation( ( callback ) => callback( () => selectMock ) );

const mockSettings = ( settings ) => {
selectMock.getSettings = () => ( {
__experimentalFeatures: settings,
} );
};

const mockCurrentBlockContext = (
blockContext = { name: '', isSelected: false }
) => {
jest.spyOn( BlockEditContext, 'useBlockEditContext' ).mockReturnValue(
blockContext
);
};

describe( 'useSetting', () => {
beforeEach( () => {
setupSelectMock();
mockCurrentBlockContext();
} );

it( 'uses block setting', () => {
mockSettings( {
blocks: {
'core/test-block': {
layout: {
contentSize: '840px',
},
},
},
} );

mockCurrentBlockContext( {
name: 'core/test-block',
} );

expect( useSetting( 'layout.contentSize' ) ).toBe( '840px' );
} );

it( 'uses blockEditor.useSetting.before hook override', () => {
mockSettings( {
blocks: {
'core/test-block': {
layout: {
contentSize: '840px',
},
},
},
} );

mockCurrentBlockContext( {
name: 'core/test-block',
} );

addFilter(
'blockEditor.useSetting.before',
'test/useSetting.before',
( result, path, clientId, blockName ) => {
if ( blockName === 'core/test-block' ) {
return '960px';
}

return result;
}
);

expect( useSetting( 'layout.contentSize' ) ).toBe( '960px' );

removeFilter(
'blockEditor.useSetting.before',
'test/useSetting.before'
);
} );
} );