Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove any other editor modifications #8

Merged
merged 4 commits into from
Apr 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions includes/class-newspack-newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static function instance() {
* Constructor.
*/
public function __construct() {
add_action( 'the_post', [ __CLASS__, 'remove_other_editor_modifications' ] );
add_action( 'init', [ __CLASS__, 'register_cpt' ] );
add_action( 'init', [ __CLASS__, 'register_meta' ] );
add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'enqueue_block_editor_assets' ] );
Expand All @@ -52,6 +53,30 @@ public function __construct() {
include_once dirname( __FILE__ ) . '/class-newspack-newsletters-renderer.php';
}

/**
* Remove all editor enqueued assets besides this plugins'.
* This is to prevent theme styles being loaded in the editor.
* Remove editor color palette theme supports - the MJML parser uses a static list of default editor colors.
*/
public static function remove_other_editor_modifications() {
if ( self::NEWSPACK_NEWSLETTERS_CPT != get_post_type() ) {
return;
}

$enqueue_block_editor_assets_filters = $GLOBALS['wp_filter']['enqueue_block_editor_assets']->callbacks;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little scary! We could be removing enqueued assets that are needed. If all we really want to do is remove assets enqueued by the theme what if we only remove actions whose name begins with the theme slug? E.G.:

global $wp_filter;
$theme_slug = get_option( 'stylesheet' );

$enqueue_block_editor_assets_filters = $wp_filter['enqueue_block_editor_assets']->callbacks;
foreach ( $enqueue_block_editor_assets_filters as $priority => $filters ) {
	foreach ( $filters as $tag => $handlers ) {
		if ( 0 === strpos( $tag, $theme_slug ) ) {
			foreach ( $handlers as $handler ) {
				remove_action( 'enqueue_block_editor_assets', $handler, $priority );
			}
		}
	}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how strongly these conventions are enforced - even Newspack theme registers a newspack_enqueue_editor_override_assets callback while having newspack-theme slug.
Also, I wouldn't worry about unregistering these other scripts and styles – by definition these are additional and the idea here is to have a stripped-down version of the editor.

foreach ( $enqueue_block_editor_assets_filters as $index => $filter ) {
$action_handlers = array_keys( $filter );
foreach ( $action_handlers as $handler ) {
if ( __CLASS__ . '::enqueue_block_editor_assets' != $handler ) {
remove_action( 'enqueue_block_editor_assets', $handler, $index );
}
}
}

remove_editor_styles();
remove_theme_support( 'editor-color-palette' );
}

/**
* Register the custom post type.
*/
Expand Down