From 64bc7ae6982ba1c637ea2990b476369bf58c0712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Fri, 22 Oct 2021 12:19:33 +0200 Subject: [PATCH 1/4] Add filter around fetch link suggestions --- package-lock.json | 1 + packages/core-data/package.json | 1 + .../src/fetch/__experimental-fetch-link-suggestions.js | 8 +++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index de9be8e893e60..eb7426ed24ba6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18929,6 +18929,7 @@ "@wordpress/data": "file:packages/data", "@wordpress/deprecated": "file:packages/deprecated", "@wordpress/element": "file:packages/element", + "@wordpress/hooks": "file:packages/hooks", "@wordpress/html-entities": "file:packages/html-entities", "@wordpress/i18n": "file:packages/i18n", "@wordpress/is-shallow-equal": "file:packages/is-shallow-equal", diff --git a/packages/core-data/package.json b/packages/core-data/package.json index 3f657d7c32cf8..0fa4adfe77976 100644 --- a/packages/core-data/package.json +++ b/packages/core-data/package.json @@ -35,6 +35,7 @@ "@wordpress/data": "file:../data", "@wordpress/deprecated": "file:../deprecated", "@wordpress/element": "file:../element", + "@wordpress/hooks": "file:../hooks", "@wordpress/html-entities": "file:../html-entities", "@wordpress/i18n": "file:../i18n", "@wordpress/is-shallow-equal": "file:../is-shallow-equal", diff --git a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js index add566050d4b3..aa892a8c4352b 100644 --- a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js +++ b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js @@ -3,6 +3,7 @@ */ import apiFetch from '@wordpress/api-fetch'; import { addQueryArgs } from '@wordpress/url'; +import { applyFilters } from '@wordpress/hooks'; import { decodeEntities } from '@wordpress/html-entities'; import { __ } from '@wordpress/i18n'; @@ -154,7 +155,12 @@ const fetchLinkSuggestions = async ( } return Promise.all( queries ).then( ( results ) => { - return results + const filteredResults = applyFilters( + 'editor.fetchLink.suggestions', + results, + search + ); + return filteredResults .reduce( ( accumulator, current ) => accumulator.concat( current ), //flatten list [] From 1baedc2aa97d8a827c98c0123e800197ba04bd00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Mon, 25 Oct 2021 12:09:25 +0200 Subject: [PATCH 2/4] Wrap fetch link suggestions filter in a Promise --- .../src/fetch/__experimental-fetch-link-suggestions.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js index aa892a8c4352b..0914608f8a50c 100644 --- a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js +++ b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js @@ -154,11 +154,9 @@ const fetchLinkSuggestions = async ( ); } - return Promise.all( queries ).then( ( results ) => { - const filteredResults = applyFilters( - 'editor.fetchLink.suggestions', - results, - search + return Promise.all( queries ).then( async ( results ) => { + const filteredResults = await Promise.resolve( + applyFilters( 'editor.fetchLink.suggestions', results, search ) ); return filteredResults .reduce( From 06f306a94f1a8f36d3ba56355766423e8a534415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Mon, 25 Oct 2021 19:49:26 +0200 Subject: [PATCH 3/4] Add tests --- .../__experimental-fetch-link-suggestions.js | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js index b4a2a4ec0d3fe..13a800e6a8d68 100644 --- a/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js +++ b/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js @@ -1,3 +1,8 @@ +/** + * WordPress dependencies + */ +import { addFilter, removeFilter } from '@wordpress/hooks'; + /** * Internal dependencies */ @@ -229,4 +234,67 @@ describe( 'fetchLinkSuggestions', () => { ] ) ); } ); + it( 'returns results added by hooks', () => { + addFilter( + 'editor.fetchLink.suggestions', + 'plugin_link_suggestions', + ( results ) => { + return results.concat( [ + { + id: 'plugin_case', + url: 'https://www.example.com/', + title: 'Plugin Case', + type: 'custom_link', + }, + ] ); + } + ); + + return fetchLinkSuggestions( '', {} ).then( ( suggestions ) => { + expect( suggestions[ suggestions.length - 1 ] ).toEqual( { + id: 'plugin_case', + title: 'Plugin Case', + url: 'https://www.example.com/', + type: 'custom_link', + } ); + removeFilter( + 'editor.fetchLink.suggestions', + 'plugin_link_suggestions' + ); + } ); + } ); + it( 'returns results added by async hooks', () => { + addFilter( + 'editor.fetchLink.suggestions', + 'plugin_link_suggestions', + ( results ) => { + return new Promise( ( resolve ) => { + resolve( + results.concat( [ + { + id: 'async_plugin_case', + url: 'https://www.example.com/', + title: 'Async Plugin Case', + type: 'custom_link', + }, + ] ) + ); + } ); + } + ); + + return fetchLinkSuggestions( '', {} ).then( ( suggestions ) => { + expect( suggestions[ suggestions.length - 1 ] ).toEqual( { + id: 'async_plugin_case', + title: 'Async Plugin Case', + url: 'https://www.example.com/', + type: 'custom_link', + } ); + + removeFilter( + 'editor.fetchLink.suggestions', + 'plugin_link_suggestions' + ); + } ); + } ); } ); From 59ad6fc422370eddfcc1ffbbdd765f28cf34a3b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Mon, 25 Oct 2021 19:49:58 +0200 Subject: [PATCH 4/4] Add experimental prefix to the filter name --- .../src/fetch/__experimental-fetch-link-suggestions.js | 6 +++++- .../fetch/test/__experimental-fetch-link-suggestions.js | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js index 0914608f8a50c..946c0083a53ad 100644 --- a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js +++ b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js @@ -156,7 +156,11 @@ const fetchLinkSuggestions = async ( return Promise.all( queries ).then( async ( results ) => { const filteredResults = await Promise.resolve( - applyFilters( 'editor.fetchLink.suggestions', results, search ) + applyFilters( + 'experimentalEditor.fetchLink.suggestions', + results, + search + ) ); return filteredResults .reduce( diff --git a/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js index 13a800e6a8d68..9853229a3f9bd 100644 --- a/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js +++ b/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js @@ -236,7 +236,7 @@ describe( 'fetchLinkSuggestions', () => { } ); it( 'returns results added by hooks', () => { addFilter( - 'editor.fetchLink.suggestions', + 'experimentalEditor.fetchLink.suggestions', 'plugin_link_suggestions', ( results ) => { return results.concat( [ @@ -258,14 +258,14 @@ describe( 'fetchLinkSuggestions', () => { type: 'custom_link', } ); removeFilter( - 'editor.fetchLink.suggestions', + 'experimentalEditor.fetchLink.suggestions', 'plugin_link_suggestions' ); } ); } ); it( 'returns results added by async hooks', () => { addFilter( - 'editor.fetchLink.suggestions', + 'experimentalEditor.fetchLink.suggestions', 'plugin_link_suggestions', ( results ) => { return new Promise( ( resolve ) => { @@ -292,7 +292,7 @@ describe( 'fetchLinkSuggestions', () => { } ); removeFilter( - 'editor.fetchLink.suggestions', + 'experimentalEditor.fetchLink.suggestions', 'plugin_link_suggestions' ); } );