From a5b94123a8842db83e2f95d304ffe272cc52292c Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Tue, 11 Aug 2026 14:44:31 -0500 Subject: [PATCH] fix: guard CHRNX editorial state --- assets/js/editor-podcast.js | 19 ++++- assets/js/editor-taxonomies.js | 19 ++++- inc/editor.php | 17 +++- inc/podcast-meta.php | 9 +-- tests/js/editor-podcast.test.js | 55 ++++++++++++- tests/js/editor-taxonomies.test.js | 58 +++++++++++-- tests/php/EditorAssetsTest.php | 126 +++++++++++++++++++++++++++++ tests/php/PodcastMetaTest.php | 1 + 8 files changed, 285 insertions(+), 19 deletions(-) create mode 100644 tests/php/EditorAssetsTest.php diff --git a/assets/js/editor-podcast.js b/assets/js/editor-podcast.js index 15d14a3..113660b 100644 --- a/assets/js/editor-podcast.js +++ b/assets/js/editor-podcast.js @@ -54,15 +54,30 @@ } const editor = wp.data.select( 'core/editor' ); - if ( ! editor || editor.getCurrentPostType() !== 'post' ) { + if ( + ! editor || + typeof editor.getCurrentPostType !== 'function' || + typeof editor.getEditedPostAttribute !== 'function' || + editor.getCurrentPostType() !== 'post' + ) { return false; } function PodcastPanel() { const editorState = wp.data.useSelect( function ( select ) { + if ( typeof select !== 'function' ) { + return null; + } + const currentEditor = select( 'core/editor' ); const core = select( 'core' ); - if ( ! currentEditor || ! core || typeof core.getEntityRecord !== 'function' ) { + if ( + ! currentEditor || + typeof currentEditor.getCurrentPostType !== 'function' || + typeof currentEditor.getEditedPostAttribute !== 'function' || + ! core || + typeof core.getEntityRecord !== 'function' + ) { return null; } diff --git a/assets/js/editor-taxonomies.js b/assets/js/editor-taxonomies.js index 7484d77..637ab5e 100644 --- a/assets/js/editor-taxonomies.js +++ b/assets/js/editor-taxonomies.js @@ -58,7 +58,12 @@ } const editor = wp.data.select( 'core/editor' ); - if ( ! editor || editor.getCurrentPostType() !== 'post' ) { + if ( + ! editor || + typeof editor.getCurrentPostType !== 'function' || + typeof editor.getEditedPostAttribute !== 'function' || + editor.getCurrentPostType() !== 'post' + ) { return false; } @@ -71,9 +76,19 @@ function EditorialTaxonomyPanels() { const editorState = wp.data.useSelect( function ( select ) { + if ( typeof select !== 'function' ) { + return null; + } + const currentEditor = select( 'core/editor' ); const core = select( 'core' ); - if ( ! currentEditor || ! core || typeof core.getEntityRecords !== 'function' ) { + if ( + ! currentEditor || + typeof currentEditor.getCurrentPostType !== 'function' || + typeof currentEditor.getEditedPostAttribute !== 'function' || + ! core || + typeof core.getEntityRecords !== 'function' + ) { return null; } diff --git a/inc/editor.php b/inc/editor.php index fdabc8c..3a76451 100644 --- a/inc/editor.php +++ b/inc/editor.php @@ -5,12 +5,27 @@ * @package CHRNX */ +/** + * Determine whether a screen is the post block editor. + * + * @param mixed $screen Potential WordPress screen. + * @return bool Whether editor assets belong on the screen. + */ +function chrnx_is_post_block_editor_screen( $screen ) { + return is_object( $screen ) && + isset( $screen->base, $screen->post_type ) && + 'post' === $screen->base && + 'post' === $screen->post_type && + is_callable( array( $screen, 'is_block_editor' ) ) && + $screen->is_block_editor(); +} + /** * Enqueue CHRNX controls only in the post block editor. */ function chrnx_enqueue_editor_assets() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; - if ( ! $screen || 'post' !== $screen->base || 'post' !== $screen->post_type || ! $screen->is_block_editor() ) { + if ( ! chrnx_is_post_block_editor_screen( $screen ) ) { return; } diff --git a/inc/podcast-meta.php b/inc/podcast-meta.php index 2015863..badfd63 100644 --- a/inc/podcast-meta.php +++ b/inc/podcast-meta.php @@ -72,16 +72,11 @@ function chrnx_sanitize_canonical_episode_url( $value ) { return ''; } - $value = trim( $value ); - if ( ! filter_var( $value, FILTER_VALIDATE_URL ) ) { - return ''; - } - - $url = esc_url_raw( $value, array( 'https' ) ); + $url = esc_url_raw( trim( $value ) ); $scheme = wp_parse_url( $url, PHP_URL_SCHEME ); $host = wp_parse_url( $url, PHP_URL_HOST ); - return 'https' === $scheme && ! empty( $host ) ? $url : ''; + return ! empty( $url ) && 'https' === $scheme && ! empty( $host ) ? $url : ''; } /** diff --git a/tests/js/editor-podcast.test.js b/tests/js/editor-podcast.test.js index e71b736..49a4cd1 100644 --- a/tests/js/editor-podcast.test.js +++ b/tests/js/editor-podcast.test.js @@ -93,12 +93,63 @@ describe( 'podcast editor helpers', () => { expect( nonPodcast.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull(); } ); - test( 'gracefully handles missing globals, post data, meta, and unresolved topic terms', () => { + test( 'fails closed when required globals or initial editor selector methods are missing', () => { const helpers = require( modulePath ); expect( helpers.initialize() ).toBe( false ); expect( helpers.initialize( {} ) ).toBe( false ); + + for ( const editorSelector of [ {}, { getCurrentPostType: () => 'post' } ] ) { + const missingEditorMethod = createWp(); + missingEditorMethod.wp.data.select = () => editorSelector; + expect( () => helpers.initialize( missingEditorMethod.wp ) ).not.toThrow(); + expect( helpers.initialize( missingEditorMethod.wp ) ).toBe( false ); + expect( missingEditorMethod.registerPlugin ).not.toHaveBeenCalled(); + expect( missingEditorMethod.editPost ).not.toHaveBeenCalled(); + } + + for ( const key of [ 'components', 'data', 'editPost', 'element', 'plugins' ] ) { + const incomplete = createWp(); + delete incomplete.wp[ key ]; + expect( helpers.initialize( incomplete.wp ) ).toBe( false ); + expect( incomplete.registerPlugin ).not.toHaveBeenCalled(); + expect( incomplete.editPost ).not.toHaveBeenCalled(); + } + } ); + + test( 'reactive render tolerates missing post methods, meta, and terms without dispatching', () => { + const helpers = require( modulePath ); + const incomplete = createWp(); + expect( helpers.initialize( incomplete.wp ) ).toBe( true ); + incomplete.wp.data.useSelect = ( mapper ) => + mapper( ( store ) => ( store === 'core/editor' ? {} : { getEntityRecord: jest.fn() } ) ); + expect( () => incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow(); + expect( incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull(); + expect( incomplete.editPost ).not.toHaveBeenCalled(); + + incomplete.wp.data.useSelect = ( mapper ) => + mapper( ( store ) => + store === 'core/editor' + ? { + getCurrentPostType: () => 'post', + getEditedPostAttribute: jest.fn(), + } + : {}, + ); + expect( () => incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow(); + expect( incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull(); + + incomplete.wp.data.useSelect = createWp().wp.data.useSelect; + incomplete.wp.data.useDispatch = () => ( {} ); + expect( () => incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow(); + expect( incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull(); + expect( incomplete.editPost ).not.toHaveBeenCalled(); + const unresolved = createWp( null ); - expect( () => helpers.initialize( unresolved.wp ) ).not.toThrow(); + unresolved.state.meta = undefined; + unresolved.state.topic = undefined; + expect( helpers.initialize( unresolved.wp ) ).toBe( true ); + expect( () => unresolved.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow(); expect( unresolved.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull(); + expect( unresolved.editPost ).not.toHaveBeenCalled(); } ); } ); diff --git a/tests/js/editor-taxonomies.test.js b/tests/js/editor-taxonomies.test.js index b6ddbc6..ffd2c3a 100644 --- a/tests/js/editor-taxonomies.test.js +++ b/tests/js/editor-taxonomies.test.js @@ -87,16 +87,64 @@ describe( 'editor taxonomy helpers', () => { } ); } ); - test( 'does nothing safely outside post editors or without usable globals or terms', () => { + test( 'fails closed when required globals or initial editor selector methods are missing', () => { const helpers = require( modulePath ); expect( helpers.initialize() ).toBe( false ); expect( helpers.initialize( {} ) ).toBe( false ); - const { wp, registerPlugin, attributes, terms } = createWp(); + + for ( const editorSelector of [ {}, { getCurrentPostType: () => 'post' } ] ) { + const missingEditorMethod = createWp(); + missingEditorMethod.wp.data.select = () => editorSelector; + expect( () => helpers.initialize( missingEditorMethod.wp ) ).not.toThrow(); + expect( helpers.initialize( missingEditorMethod.wp ) ).toBe( false ); + expect( missingEditorMethod.registerPlugin ).not.toHaveBeenCalled(); + expect( missingEditorMethod.editPost ).not.toHaveBeenCalled(); + } + + for ( const key of [ 'components', 'data', 'editPost', 'element', 'plugins' ] ) { + const incomplete = createWp(); + delete incomplete.wp[ key ]; + expect( helpers.initialize( incomplete.wp ) ).toBe( false ); + expect( incomplete.registerPlugin ).not.toHaveBeenCalled(); + expect( incomplete.editPost ).not.toHaveBeenCalled(); + } + } ); + + test( 'reactive render tolerates unresolved editor methods and terms without dispatching', () => { + const helpers = require( modulePath ); + const { wp, registerPlugin, editPost } = createWp(); + expect( helpers.initialize( wp ) ).toBe( true ); + + wp.data.useSelect = ( mapper ) => + mapper( ( store ) => ( store === 'core/editor' ? {} : { getEntityRecords: jest.fn() } ) ); + expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow(); + expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull(); + expect( editPost ).not.toHaveBeenCalled(); + + wp.data.useSelect = ( mapper ) => + mapper( ( store ) => + store === 'core/editor' + ? { + getCurrentPostType: () => 'post', + getEditedPostAttribute: jest.fn(), + } + : {}, + ); + expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow(); + expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull(); + + wp.data.useSelect = createWp().wp.data.useSelect; + wp.data.useDispatch = () => ( {} ); + expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow(); + expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull(); + expect( editPost ).not.toHaveBeenCalled(); + } ); + + test( 'does not register taxonomy panels outside post editors', () => { + const helpers = require( modulePath ); + const { wp, registerPlugin, attributes } = createWp(); attributes.type = 'page'; expect( helpers.initialize( wp ) ).toBe( false ); expect( registerPlugin ).not.toHaveBeenCalled(); - attributes.type = 'post'; - terms.chrnx_topic = undefined; - expect( () => helpers.initialize( wp ) ).not.toThrow(); } ); } ); diff --git a/tests/php/EditorAssetsTest.php b/tests/php/EditorAssetsTest.php new file mode 100644 index 0000000..dca0b39 --- /dev/null +++ b/tests/php/EditorAssetsTest.php @@ -0,0 +1,126 @@ +original_screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; + $this->reset_editor_scripts(); + } + + /** + * Restore screen state and registered scripts. + */ + public function tear_down() { + $this->reset_editor_scripts(); + + if ( $this->original_screen instanceof WP_Screen ) { + set_current_screen( $this->original_screen->id ); + get_current_screen()->is_block_editor( $this->original_screen->is_block_editor() ); + } else { + $GLOBALS['current_screen'] = null; + } + + parent::tear_down(); + } + + /** + * Clear only the CHRNX editor script state. + */ + private function reset_editor_scripts() { + foreach ( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast' ) as $handle ) { + wp_dequeue_script( $handle ); + wp_deregister_script( $handle ); + } + } + + /** + * Set an actual WordPress screen and its editor mode. + * + * @param string $screen_id Screen ID. + * @param bool $is_block_editor Whether block editor mode is active. + * @return WP_Screen Configured screen. + */ + private function set_screen( $screen_id, $is_block_editor ) { + set_current_screen( $screen_id ); + $screen = get_current_screen(); + $screen->is_block_editor( $is_block_editor ); + return $screen; + } + + /** + * Post block editor enqueues exactly the two versioned local scripts. + */ + public function test_post_block_editor_enqueues_exact_editor_scripts() { + $screen = $this->set_screen( 'post', true ); + $this->assertTrue( chrnx_is_post_block_editor_screen( $screen ) ); + + do_action( 'enqueue_block_editor_assets' ); + $scripts = wp_scripts(); + $handles = array_values( + array_filter( + $scripts->queue, + static function ( $handle ) { + return 0 === strpos( $handle, 'chrnx-editor-' ); + } + ) + ); + + $this->assertSame( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast' ), $handles ); + $expected_dependencies = array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-plugins' ); + + foreach ( + array( + 'chrnx-editor-taxonomies' => 'assets/js/editor-taxonomies.js', + 'chrnx-editor-podcast' => 'assets/js/editor-podcast.js', + ) as $handle => $asset + ) { + $this->assertArrayHasKey( $handle, $scripts->registered ); + $this->assertSame( get_theme_file_uri( $asset ), $scripts->registered[ $handle ]->src ); + $this->assertSame( $expected_dependencies, $scripts->registered[ $handle ]->deps ); + $this->assertSame( CHRNX_VERSION, $scripts->registered[ $handle ]->ver ); + } + } + + /** + * Frontend, page, list, and classic-editor contexts enqueue nothing. + */ + public function test_irrelevant_contexts_do_not_enqueue_editor_scripts() { + $contexts = array( + array( null, false ), + array( 'page', true ), + array( 'edit-post', false ), + array( 'post', false ), + ); + + foreach ( $contexts as $context ) { + $this->reset_editor_scripts(); + if ( null === $context[0] ) { + $GLOBALS['current_screen'] = null; + } else { + $this->set_screen( $context[0], $context[1] ); + } + + do_action( 'enqueue_block_editor_assets' ); + $this->assertFalse( wp_script_is( 'chrnx-editor-taxonomies', 'enqueued' ) ); + $this->assertFalse( wp_script_is( 'chrnx-editor-podcast', 'enqueued' ) ); + } + } +} diff --git a/tests/php/PodcastMetaTest.php b/tests/php/PodcastMetaTest.php index e97a7d4..5ab22a1 100644 --- a/tests/php/PodcastMetaTest.php +++ b/tests/php/PodcastMetaTest.php @@ -83,6 +83,7 @@ class PodcastMetaTest extends WP_UnitTestCase { */ public function test_canonical_url_sanitizer_accepts_only_https() { $this->assertSame( 'https://example.test/episode?ref=1', chrnx_sanitize_canonical_episode_url( ' https://example.test/episode?ref=1 ' ) ); + $this->assertSame( 'https://example.test/an%20episode', chrnx_sanitize_canonical_episode_url( 'https://example.test/an episode' ) ); $this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'http://example.test/episode' ) ); $this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'javascript:alert(1)' ) ); $this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'not a url' ) );