Skip to content

Commit ef85c27

Browse files
authored
Merge pull request #83 from badasswp/feat/add-custom-filter-to-restrict-post-types
Feat: Add custom filter `excludedPostTypes` to restrict specific Post types
2 parents ff63c38 + a12c14e commit ef85c27

File tree

10 files changed

+97
-2
lines changed

10 files changed

+97
-2
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ jobs:
2020
with:
2121
node-version: 20.x
2222

23+
- name: Clear Yarn cache
24+
run: yarn cache clean
25+
2326
- name: Install NPM
2427
run: |
2528
yarn install

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Feat: Add toggle for Regex expression matching.
55
* Feat: Add Plugin options page.
66
* Feat: Add Shortcut command (CMD + F).
7-
* Feat: Add custom hooks: `afterSearchReplace`.
7+
* Feat: Add custom hooks: `afterSearchReplace`, `excludedPostTypes`.
88
* Test: Add e2e tests for plugin codebase.
99
* Tested up to WP 6.9.
1010

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ addFilter(
7777
- allowedBlocks _`{string[]}`_ List of Allowed Blocks.
7878
<br/>
7979

80+
#### `search-replace-for-block-editor.excludedPostTypes`
81+
82+
This custom hook (filter) provides the ability to prevent the loading of the Search & Replace app for specific post types like so:
83+
84+
```js
85+
import { addFilter } from '@wordpress/hooks';
86+
87+
addFilter(
88+
'search-replace-for-block-editor.excludedPostTypes',
89+
'yourNamespace',
90+
( excludedPostTypes ) => {
91+
if ( ! excludedPostTypes.includes( 'page' ) ) {
92+
excludedPostTypes.push( 'page' );
93+
}
94+
95+
return excludedPostTypes;
96+
}
97+
);
98+
```
99+
100+
**Parameters**
101+
102+
- excludedPostTypes _`{string[]}`_ List of Allowed Blocks.
103+
<br/>
104+
80105
#### `search-replace-for-block-editor.replaceBlockAttribute`
81106

82107
This custom hook (action) provides the ability to include search and replace functionality for custom blocks with custom properties:

inc/Services/Boot.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function register_assets(): void {
7979
'srfbe',
8080
[
8181
'wpVersion' => $wp_version,
82+
'postType' => get_post_type() ?? null,
8283
'isShortcutEnabled' => $srfbe['use_shortcut'] ?? null,
8384
'isCaseMatchingEnabled' => $srfbe['case_matching'] ?? null,
8485
'isRegexMatchingEnabled' => $srfbe['regex_matching'] ?? null,

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Want to add your personal touch? All of our documentation can be found [here](ht
6767
* Feat: Add toggle for Regex expression matching.
6868
* Feat: Add Plugin options page.
6969
* Feat: Add Shortcut command (CMD + F).
70-
* Feat: Add custom hooks: `afterSearchReplace`.
70+
* Feat: Add custom hooks: `afterSearchReplace`, `excludedPostTypes`.
7171
* Test: Add e2e tests for plugin codebase.
7272
* Tested up to WP 6.9.
7373

src/core/utils.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,43 @@ export const getShortcutEvent = (): KeyboardEvent => {
357357
bubbles: true,
358358
} );
359359
};
360+
361+
/**
362+
* Is Allowed for Post type.
363+
*
364+
* This function checkes to see that the plugin
365+
* is allowed for a specific post type.
366+
*
367+
* @since 1.10.0
368+
*
369+
* @return {boolean} True/false.
370+
*/
371+
export const isAllowedForPostType = (): boolean => {
372+
const { postType } = srfbe;
373+
374+
// Bail out, if undefined.
375+
if ( ! postType ) {
376+
return false;
377+
}
378+
379+
/**
380+
* Filter if a Post has access to
381+
* Search & Replace app.
382+
*
383+
* @since 1.10.0
384+
*
385+
* @param {string[]} excludedPostTypes Excluded Post types.
386+
* @return {string[]}
387+
*/
388+
const excludedPostTypes = applyFilters(
389+
'search-replace-for-block-editor.excludedPostTypes',
390+
[]
391+
) as string[];
392+
393+
// Bail out, if excluded.
394+
if ( excludedPostTypes.includes( postType ) ) {
395+
return false;
396+
}
397+
398+
return true;
399+
};

src/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ import SearchReplaceForBlockEditor from './core/app';
1111
import {
1212
getAppRoot,
1313
getEditorRoot,
14+
isAllowedForPostType,
1415
isWpVersionGreaterThanOrEqualTo,
1516
} from './core/utils';
1617

1718
( async () => {
1819
try {
20+
// Bail out, if not allowed.
21+
if ( ! isAllowedForPostType() ) {
22+
return;
23+
}
24+
1925
const app = getAppRoot( ( await getEditorRoot() ) as HTMLElement );
26+
2027
if ( ! isWpVersionGreaterThanOrEqualTo( '6.2.0' ) ) {
2128
ReactDOM.render( <SearchReplaceForBlockEditor />, app );
2229
} else {

src/typings/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
declare global {
22
var srfbe: {
33
wpVersion: string;
4+
postType: string;
45
isShortcutEnabled: boolean;
56
isCaseMatchingEnabled: boolean;
67
isRegexMatchingEnabled: boolean;

tests/unit/js/utils.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ describe( 'getBlockEditorIframe', () => {
224224
} );
225225
} );
226226

227+
const localizedValues = {
228+
postType: 'post',
229+
isShortcutEnabled: true,
230+
isCaseMatchingEnabled: false,
231+
isRegexMatchingEnabled: false,
232+
isCloseModalEnabled: false,
233+
isSavePostEnabled: false,
234+
};
235+
227236
describe( 'isWpVersionGreaterThanOrEqualTo', () => {
228237
beforeEach( () => {
229238
jest.resetModules();
@@ -232,6 +241,7 @@ describe( 'isWpVersionGreaterThanOrEqualTo', () => {
232241
it( 'isWpVersionGreaterThanOrEqualTo returns true if WP version is up to or above passed in arg version', () => {
233242
window.srfbe = {
234243
wpVersion: '6.7.2',
244+
...localizedValues,
235245
};
236246

237247
const {
@@ -247,6 +257,7 @@ describe( 'isWpVersionGreaterThanOrEqualTo', () => {
247257
it( 'isWpVersionGreaterThanOrEqualTo returns false if WP version is not up to passed in arg version', () => {
248258
window.srfbe = {
249259
wpVersion: '6.7.0',
260+
...localizedValues,
250261
};
251262

252263
const {
@@ -262,6 +273,7 @@ describe( 'isWpVersionGreaterThanOrEqualTo', () => {
262273
it( 'isWpVersionGreaterThanOrEqualTo returns false if param version number is not valid', () => {
263274
window.srfbe = {
264275
wpVersion: '2.3',
276+
...localizedValues,
265277
};
266278

267279
const {
@@ -277,6 +289,7 @@ describe( 'isWpVersionGreaterThanOrEqualTo', () => {
277289
it( 'isWpVersionGreaterThanOrEqualTo returns true if one of params has version number without dot notation but is equal to or greater than version number', () => {
278290
window.srfbe = {
279291
wpVersion: '67',
292+
...localizedValues,
280293
};
281294

282295
const {
@@ -292,6 +305,7 @@ describe( 'isWpVersionGreaterThanOrEqualTo', () => {
292305
it( 'isWpVersionGreaterThanOrEqualTo returns true if one of params has version number with single dot notation but is equal to or greater than version number', () => {
293306
window.srfbe = {
294307
wpVersion: '6.8',
308+
...localizedValues,
295309
};
296310

297311
const {

tests/unit/php/Services/BootTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,16 @@ public function test_register_assets() {
111111
->with( 'search_replace_for_block_editor', [] )
112112
->andReturn( null );
113113

114+
WP_Mock::userFunction( 'get_post_type' )
115+
->andReturn( 'post' );
116+
114117
WP_Mock::userFunction( 'wp_localize_script' )
115118
->with(
116119
'search-replace-for-block-editor',
117120
'srfbe',
118121
[
119122
'wpVersion' => '6.9.1',
123+
'postType' => 'post',
120124
'isShortcutEnabled' => null,
121125
'isCaseMatchingEnabled' => null,
122126
'isRegexMatchingEnabled' => null,

0 commit comments

Comments
 (0)