From c347a98b31a64b726cae0ed74733def0c203a6cb Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Tue, 20 Jul 2021 12:19:40 -0500 Subject: [PATCH 1/3] Prevent PHP notice on Widgets page This doesn't fix the deeper issue of using , though. --- php/Blocks/Loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/Blocks/Loader.php b/php/Blocks/Loader.php index 192b8fe5..2c4c6d91 100644 --- a/php/Blocks/Loader.php +++ b/php/Blocks/Loader.php @@ -126,7 +126,7 @@ public function editor_assets() { wp_enqueue_script( $js_handle, $this->assets['url']['entry'], - $js_config['dependencies'], + [], $js_config['version'], true ); From 80bdc6b2cdb1294fc83e4274c5bc41cb19018a1e Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Tue, 20 Jul 2021 12:39:52 -0500 Subject: [PATCH 2/3] Use mediaUpload() from 'core/block-editor' settings To prevent an error in the new Widgets page. --- js/src/block-editor/controls/test/file.js | 4 ++++ js/src/block-editor/controls/test/image.js | 4 ++++ js/src/block-editor/hooks/useMedia.js | 10 +++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/js/src/block-editor/controls/test/file.js b/js/src/block-editor/controls/test/file.js index 9c711f30..76f2822f 100644 --- a/js/src/block-editor/controls/test/file.js +++ b/js/src/block-editor/controls/test/file.js @@ -16,6 +16,10 @@ jest.mock( '@wordpress/api-fetch', () => { } ); } ); +jest.mock( '@wordpress/data/build/components/use-select', () => + jest.fn( () => false ) +); + // @todo: remove this when the console warning no longer appears. // Expected mock function not to be called but it was called with: // ["wp.components.DropZoneProvider is deprecated. Note: wp.component.DropZone no longer needs a provider. wp.components.DropZoneProvider is safe to remove from your code."] diff --git a/js/src/block-editor/controls/test/image.js b/js/src/block-editor/controls/test/image.js index b14df690..c47b5d6f 100644 --- a/js/src/block-editor/controls/test/image.js +++ b/js/src/block-editor/controls/test/image.js @@ -16,6 +16,10 @@ jest.mock( '@wordpress/api-fetch', () => { } ); } ); +jest.mock( '@wordpress/data/build/components/use-select', () => + jest.fn( () => false ) +); + // @todo: remove this when the console warning no longer appears. // Expected mock function not to be called but it was called with: // ["wp.components.DropZoneProvider is deprecated. Note: wp.component.DropZone no longer needs a provider. wp.components.DropZoneProvider is safe to remove from your code."] diff --git a/js/src/block-editor/hooks/useMedia.js b/js/src/block-editor/hooks/useMedia.js index 288f274a..b293f2f3 100644 --- a/js/src/block-editor/hooks/useMedia.js +++ b/js/src/block-editor/hooks/useMedia.js @@ -1,8 +1,9 @@ /** * WordPress dependencies */ +import { store as blockEditorStore } from '@wordpress/block-editor'; import { useSelect } from '@wordpress/data'; -import { mediaUpload } from '@wordpress/editor'; +import { mediaUpload as legacyMediaUpload } from '@wordpress/editor'; import { useEffect, useState } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; @@ -58,6 +59,13 @@ const useMedia = ( fieldValue, onChange, allowedTypes ) => { return select( 'core' ).getMedia( fieldValue ); } ); + /* @type {function|undefined} */ + const mediaUpload = useSelect( ( select ) => { + // @ts-ignore The function isn't in the declaration file. + const { getSettings } = select( blockEditorStore ); + return getSettings().mediaUpload || legacyMediaUpload; + } ); + useEffect( () => { if ( media?.source_url ) { setMediaSrc( media.source_url ); From 8a871601dfc17c14eddaab2e1bc84b6c30118255 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Tue, 20 Jul 2021 12:42:14 -0500 Subject: [PATCH 3/3] Add the ? operator before object access Just to be sure there isn't an error from it being undefined. --- js/src/block-editor/hooks/useMedia.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/src/block-editor/hooks/useMedia.js b/js/src/block-editor/hooks/useMedia.js index b293f2f3..7764f8c9 100644 --- a/js/src/block-editor/hooks/useMedia.js +++ b/js/src/block-editor/hooks/useMedia.js @@ -63,7 +63,7 @@ const useMedia = ( fieldValue, onChange, allowedTypes ) => { const mediaUpload = useSelect( ( select ) => { // @ts-ignore The function isn't in the declaration file. const { getSettings } = select( blockEditorStore ); - return getSettings().mediaUpload || legacyMediaUpload; + return getSettings()?.mediaUpload || legacyMediaUpload; } ); useEffect( () => {