fix: guard CHRNX editorial state
This commit is contained in:
8 files changed
+285
-19
No files matched your search
@@ -54,15 +54,30 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const editor = wp.data.select( 'core/editor' );
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function PodcastPanel() {
|
function PodcastPanel() {
|
||||||
const editorState = wp.data.useSelect( function ( select ) {
|
const editorState = wp.data.useSelect( function ( select ) {
|
||||||
|
if ( typeof select !== 'function' ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const currentEditor = select( 'core/editor' );
|
const currentEditor = select( 'core/editor' );
|
||||||
const core = select( 'core' );
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,12 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const editor = wp.data.select( 'core/editor' );
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,9 +76,19 @@
|
|||||||
|
|
||||||
function EditorialTaxonomyPanels() {
|
function EditorialTaxonomyPanels() {
|
||||||
const editorState = wp.data.useSelect( function ( select ) {
|
const editorState = wp.data.useSelect( function ( select ) {
|
||||||
|
if ( typeof select !== 'function' ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const currentEditor = select( 'core/editor' );
|
const currentEditor = select( 'core/editor' );
|
||||||
const core = select( 'core' );
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-1
@@ -5,12 +5,27 @@
|
|||||||
* @package CHRNX
|
* @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.
|
* Enqueue CHRNX controls only in the post block editor.
|
||||||
*/
|
*/
|
||||||
function chrnx_enqueue_editor_assets() {
|
function chrnx_enqueue_editor_assets() {
|
||||||
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
|
$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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,16 +72,11 @@ function chrnx_sanitize_canonical_episode_url( $value ) {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = trim( $value );
|
$url = esc_url_raw( trim( $value ) );
|
||||||
if ( ! filter_var( $value, FILTER_VALIDATE_URL ) ) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
$url = esc_url_raw( $value, array( 'https' ) );
|
|
||||||
$scheme = wp_parse_url( $url, PHP_URL_SCHEME );
|
$scheme = wp_parse_url( $url, PHP_URL_SCHEME );
|
||||||
$host = wp_parse_url( $url, PHP_URL_HOST );
|
$host = wp_parse_url( $url, PHP_URL_HOST );
|
||||||
|
|
||||||
return 'https' === $scheme && ! empty( $host ) ? $url : '';
|
return ! empty( $url ) && 'https' === $scheme && ! empty( $host ) ? $url : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -93,12 +93,63 @@ describe( 'podcast editor helpers', () => {
|
|||||||
expect( nonPodcast.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
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 );
|
const helpers = require( modulePath );
|
||||||
expect( helpers.initialize() ).toBe( false );
|
expect( helpers.initialize() ).toBe( false );
|
||||||
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 );
|
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.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
||||||
|
expect( unresolved.editPost ).not.toHaveBeenCalled();
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
@@ -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 );
|
const helpers = require( modulePath );
|
||||||
expect( helpers.initialize() ).toBe( false );
|
expect( helpers.initialize() ).toBe( false );
|
||||||
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';
|
attributes.type = 'page';
|
||||||
expect( helpers.initialize( wp ) ).toBe( false );
|
expect( helpers.initialize( wp ) ).toBe( false );
|
||||||
expect( registerPlugin ).not.toHaveBeenCalled();
|
expect( registerPlugin ).not.toHaveBeenCalled();
|
||||||
attributes.type = 'post';
|
|
||||||
terms.chrnx_topic = undefined;
|
|
||||||
expect( () => helpers.initialize( wp ) ).not.toThrow();
|
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Tests for block editor asset loading.
|
||||||
|
*
|
||||||
|
* @package CHRNX
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify editor scripts are tightly scoped to the post block editor.
|
||||||
|
*/
|
||||||
|
class EditorAssetsTest extends WP_UnitTestCase {
|
||||||
|
/**
|
||||||
|
* Original current screen.
|
||||||
|
*
|
||||||
|
* @var WP_Screen|null
|
||||||
|
*/
|
||||||
|
private $original_screen;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Preserve screen state and reset script handles.
|
||||||
|
*/
|
||||||
|
public function set_up() {
|
||||||
|
parent::set_up();
|
||||||
|
$this->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' ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -83,6 +83,7 @@ class PodcastMetaTest extends WP_UnitTestCase {
|
|||||||
*/
|
*/
|
||||||
public function test_canonical_url_sanitizer_accepts_only_https() {
|
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/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( 'http://example.test/episode' ) );
|
||||||
$this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'javascript:alert(1)' ) );
|
$this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'javascript:alert(1)' ) );
|
||||||
$this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'not a url' ) );
|
$this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'not a url' ) );
|
||||||
|
|||||||
Reference in new issue
Block a user