diff --git a/compat/block-editor/widget-block.php b/compat/block-editor/widget-block.php index 4fe064a5f..2cda0454c 100644 --- a/compat/block-editor/widget-block.php +++ b/compat/block-editor/widget-block.php @@ -252,9 +252,22 @@ private function prepare_widget_data() : void { $this->so_widgets = array_merge( $so_widgets, $third_party_widgets ); } + /** + * Enqueue block editor assets for SiteOrigin Widget Blocks. + * + * This method enqueues the necessary scripts and styles for the block editor. + * It also localizes widget data for use in the editor. + */ public function enqueue_widget_block_editor_assets() { $current_screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; + if ( + empty( $current_screen ) || + ! in_array( $current_screen->base, array( 'post', 'site-editor', 'widgets' ) ) + ) { + return; + } + wp_enqueue_script( 'sowb-register-widget-blocks', plugins_url( 'register-widget-blocks' . SOW_BUNDLE_JS_SUFFIX . '.js', __FILE__ ), diff --git a/tests/e2e/wb-form-field-site-editor.test.js b/tests/e2e/wb-form-field-site-editor.test.js index 2d6c8ea4e..c29382b0c 100644 --- a/tests/e2e/wb-form-field-site-editor.test.js +++ b/tests/e2e/wb-form-field-site-editor.test.js @@ -652,3 +652,22 @@ test( } } ); + +/** + * Validates that editor scripts aren't being loaded on the frontend. + * + * @param {Object} page The Playwright page object. + */ +test( + 'Test that editor scripts are not loaded on the frontend.', + async ( { page } ) => { + await page.goto( '/' ); + + const hasBlockCss = await page.$( '#sowb-widget-block-css' ) !== null; + const hasBlockJs = await page.$( '#sowb-widget-block-js' ) !== null; + + expect( hasBlockCss ).toBe( false ); + expect( hasBlockJs ).toBe( false ); + + } +);