From 99f9cf9f1c5e20c184402543f77c4aa48243b3de Mon Sep 17 00:00:00 2001 From: badasswp Date: Mon, 27 Oct 2025 16:45:14 +0100 Subject: [PATCH 1/3] refactor: use webpack generated file to load JS assets --- search-replace-for-block-editor.php | 41 ++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/search-replace-for-block-editor.php b/search-replace-for-block-editor.php index 4575535..6303a09 100644 --- a/search-replace-for-block-editor.php +++ b/search-replace-for-block-editor.php @@ -26,27 +26,20 @@ * @since 1.0.0 * @since 1.0.2 Load asset via plugin directory URL. * @since 1.2.2 Localise WP version. + * @since 1.7.0 Use webpack generated PHP asset file. * * @wp-hook 'enqueue_block_editor_assets' */ add_action( 'enqueue_block_editor_assets', function() { global $wp_version; + $assets = get_assets( plugin_dir_path( __FILE__ ) . './dist/app.asset.php' ); + wp_enqueue_script( 'search-replace-for-block-editor', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'dist/app.js', - [ - 'wp-i18n', - 'wp-element', - 'wp-blocks', - 'wp-components', - 'wp-editor', - 'wp-hooks', - 'wp-compose', - 'wp-plugins', - 'wp-edit-post', - ], - '1.6.0', + $assets['dependencies'], + $assets['version'], false, ); @@ -79,3 +72,27 @@ dirname( plugin_basename( __FILE__ ) ) . '/languages' ); } ); + +/** + * Get Asset dependencies. + * + * @since 1.7.0 + * + * @param string $path Path to webpack generated PHP asset file. + * @return array + */ +function get_assets( string $path ): array { + $assets = [ + 'version' => strval( time() ), + 'dependencies' => [], + ]; + + if ( ! file_exists( $path ) ) { + return $assets; + } + + // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable + $assets = require_once $path; + + return $assets; +} From 10e55a0b1990806f465560976b817a9fed2ca49f Mon Sep 17 00:00:00 2001 From: badasswp Date: Mon, 27 Oct 2025 16:45:48 +0100 Subject: [PATCH 2/3] chore: use defined constant --- search-replace-for-block-editor.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/search-replace-for-block-editor.php b/search-replace-for-block-editor.php index 6303a09..30244fc 100644 --- a/search-replace-for-block-editor.php +++ b/search-replace-for-block-editor.php @@ -20,6 +20,8 @@ die; } +define( 'SRFBE', 'search-replace-for-block-editor' ); + /** * Load Search & Replace Script for Block Editor. * @@ -36,7 +38,7 @@ $assets = get_assets( plugin_dir_path( __FILE__ ) . './dist/app.asset.php' ); wp_enqueue_script( - 'search-replace-for-block-editor', + SRFBE, trailingslashit( plugin_dir_url( __FILE__ ) ) . 'dist/app.js', $assets['dependencies'], $assets['version'], @@ -44,13 +46,13 @@ ); wp_set_script_translations( - 'search-replace-for-block-editor', - 'search-replace-for-block-editor', + SRFBE, + SRFBE, plugin_dir_path( __FILE__ ) . 'languages' ); wp_localize_script( - 'search-replace-for-block-editor', + SRFBE, 'srfbe', [ 'wpVersion' => $wp_version, @@ -67,7 +69,7 @@ */ add_action( 'init', function() { load_plugin_textdomain( - 'search-replace-for-block-editor', + SRFBE, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); From b392b2da2f5b5f6f1f023d7081178d9102a35dd9 Mon Sep 17 00:00:00 2001 From: badasswp Date: Mon, 27 Oct 2025 16:46:14 +0100 Subject: [PATCH 3/3] feat: ensure count is shown for text selection on modal open --- src/core/app.tsx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/core/app.tsx b/src/core/app.tsx index 91ce729..1de741f 100644 --- a/src/core/app.tsx +++ b/src/core/app.tsx @@ -49,7 +49,17 @@ const SearchReplaceForBlockEditor = (): JSX.Element => { */ const openModal = (): void => { setIsModalVisible( true ); - setReplacements( 0 ); + + // Get selected text, if any. + const selectedText: string = getBlockEditorIframe() + .getSelection() + .toString(); + + // By default, reset count and search input. + if ( ! selectedText ) { + setReplacements( 0 ); + setSearchInput( '' ); + } }; /** @@ -61,7 +71,6 @@ const SearchReplaceForBlockEditor = (): JSX.Element => { */ const closeModal = (): void => { setIsModalVisible( false ); - setReplacements( 0 ); }; /**