Skip to content

Commit 9412e98

Browse files
committed
Remove abstraction layers around block compat
1 parent 68649af commit 9412e98

File tree

5 files changed

+18
-187
lines changed

5 files changed

+18
-187
lines changed

common/php/class-module.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ class EF_Module {
1515
'private',
1616
);
1717

18-
/**
19-
* Associative array of hook_name => callback_name
20-
* This is used for Gutenberg-compat initialization
21-
* [
22-
* 'init' => 'init_callback_on_module_instance'
23-
* ]
24-
* @var array
25-
*/
26-
protected $compat_hooks = [];
27-
2818
function __construct() {}
2919

3020
/**
@@ -490,7 +480,7 @@ function users_select_form( $selected = null, $args = null ) {
490480

491481
<?php if( !empty($users) ) : ?>
492482
<ul class="<?php echo esc_attr( $list_class ) ?>">
493-
<?php foreach( $users as $user ) :
483+
<?php foreach( $users as $user ) :
494484
$checked = ( in_array($user->ID, $selected) ) ? 'checked="checked"' : '';
495485
// Add a class to checkbox of current user so we know not to add them in notified list during notifiedMessage() js function
496486
$current_user_class = ( get_current_user_id() == $user->ID ) ? 'class="post_following_list-current_user" ' : '';
@@ -598,15 +588,6 @@ function upgrade_074_term_descriptions( $taxonomy ) {
598588
wp_update_term( $term->term_id, $taxonomy, array( 'description' => $new_description ) );
599589
}
600590
}
601-
602-
/**
603-
* Return compatibility hooks for the current instance
604-
*
605-
* @return array
606-
*/
607-
function get_compat_hooks() {
608-
return isset( $this->compat_hooks ) && is_array( $this->compat_hooks ) ? $this->compat_hooks : [];
609-
}
610-
611591
}
592+
612593
}

common/php/trait-block-editor-compatible.php

Lines changed: 7 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -6,129 +6,26 @@
66
// phpcs:disable WordPressVIPMinimum.Variables.VariableAnalysis.SelfOutsideClass
77

88
trait Block_Editor_Compatible {
9-
/**
10-
* Holds the reference to the Module's object
11-
*
12-
* @var EF_Module
13-
*/
14-
protected $ef_module;
15-
/**
16-
* Holds associative array of hooks and their respective callbacks
17-
*
18-
* @var array
19-
*/
20-
protected $hooks = [];
21-
protected static $active_plugins;
22-
23-
/**
24-
* This method handles init of Module Compat
25-
*
26-
* @param EF_Module $module_instance
27-
* @param array $hooks associative array of hooks and their respective callbacks
28-
*
29-
*
30-
* @return void
31-
*/
32-
function __construct( $module_instance, $hooks = [] ) {
33-
$this->ef_module = $module_instance;
34-
$this->hooks = $hooks;
35-
36-
if ( is_admin() ) {
37-
38-
add_action( 'init', [ $this, 'action_init_for_admin' ], 15 );
39-
}
40-
}
41-
42-
/**
43-
* Unhook the module's hooks and use the module's Compat hooks instead.
44-
*
45-
* This is currently run on init action, but only when `is_admin` is true.
46-
*
47-
* @since 0.9
48-
*
49-
* @return void
50-
*/
51-
function action_init_for_admin() {
52-
if ( ! $this->ef_module->is_enabled() ) {
53-
return;
54-
}
55-
56-
$this->check_active_plugins();
57-
58-
if ( $this->should_apply_compat() ) {
59-
foreach ( $this->hooks as $hook => $callback ) {
60-
if ( is_callable( [ $this, $callback ] ) ) {
61-
remove_action( $hook, array( $this->ef_module, $callback ) );
62-
add_action( $hook, array( $this, $callback ) );
63-
}
64-
}
65-
}
66-
}
67-
68-
function check_active_plugins() {
69-
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
70-
71-
self::$active_plugins = [
72-
'classic-editor' => is_plugin_active( 'classic-editor' ),
73-
'gutenberg' => is_plugin_active( 'gutenberg' ),
74-
];
75-
}
769

7710
/**
7811
* Helper function to determine whether we're running WP 5.0.
7912
*
8013
* @return boolean
8114
*/
82-
public static function is_at_least_50() {
15+
private function is_at_least_wp_50() {
8316
return version_compare( get_bloginfo( 'version' ), '5.0', '>=' );
8417
}
8518

8619
/**
87-
* Helper to determine whether either Gutenberg plugin or Classic Editor plugin is loaded.
20+
* Whether or not we are in the block editor.
8821
*
89-
* @param string $slug
9022
* @return boolean
9123
*/
92-
public static function is_plugin_active( $slug = '' ) {
93-
return isset( self::$active_plugins[ $slug ] ) && self::$active_plugins[ $slug ];
94-
}
95-
96-
/**
97-
* Detect whether we should load compatability for the module.
98-
* This runs very early during request lifecycle and may not be precise. It's better to use `get_current_screen()->is_block_editor()` if it's available.
99-
*
100-
* However, Block Editor can be enabled/disabled on a granular basis via the filters for the core and the plugin versions.
101-
*
102-
* use_block_editor_for_post
103-
* use_block_editor_for_post_type
104-
* gutenberg_can_edit_post_type
105-
* gutenberg_can_edit_post
106-
*
107-
*
108-
* This needs to be handled in the compat hook callback.
109-
*
110-
* If any of $conditions evaluates to TRUE, we should apply compat hooks.
111-
*
112-
* @return boolean
113-
*/
114-
protected function should_apply_compat() {
115-
$conditions = [
116-
/**
117-
* 5.0:
118-
*
119-
* Classic editor either disabled or enabled (either via an option or with GET argument).
120-
* It's a hairy conditional :(
121-
*/
122-
// phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.NoNonceVerification
123-
self::is_at_least_50() && ! self::is_plugin_active( 'classic-editor' ),
124-
self::is_at_least_50() && self::is_plugin_active( 'classic-editor' ) && ( get_option( 'classic-editor-replace' ) === 'block' && ! isset( $_GET[ 'classic-editor__forget' ] ) ),
125-
self::is_at_least_50() && self::is_plugin_active( 'classic-editor' ) && ( get_option( 'classic-editor-replace' ) === 'classic' && isset( $_GET[ 'classic-editor__forget' ] ) ),
126-
/**
127-
* < 5.0 but Gutenberg plugin is active.
128-
*/
129-
! self::is_at_least_50() && self::is_plugin_active( 'gutenberg' ),
130-
];
24+
public function is_block_editor() {
25+
if ( self::is_at_least_wp_50() && function_exists( 'get_current_screen' ) ) {
26+
return get_current_screen()->is_block_editor();
27+
}
13128

132-
return count( array_filter( $conditions, function( $c ) { return (bool) $c; } ) ) > 0;
29+
return false;
13330
}
13431
}

edit_flow.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ private function load_modules() {
121121
if ( file_exists( EDIT_FLOW_ROOT . "/modules/{$module_dir}/$module_dir.php" ) ) {
122122
include_once( EDIT_FLOW_ROOT . "/modules/{$module_dir}/$module_dir.php" );
123123

124-
// Try to load Gutenberg compat files
125-
if ( file_exists( EDIT_FLOW_ROOT . "/modules/{$module_dir}/compat/block-editor.php" ) ) {
126-
include_once( EDIT_FLOW_ROOT . "/modules/{$module_dir}/compat/block-editor.php" );
127-
}
128124
// Prepare the class name because it should be standardized
129125
$tmp = explode( '-', $module_dir );
130126
$class_name = '';
@@ -150,10 +146,6 @@ private function load_modules() {
150146
foreach( $class_names as $slug => $class_name ) {
151147
if ( class_exists( $class_name ) ) {
152148
$this->$slug = new $class_name();
153-
$compat_class_name = "{$class_name}_Block_Editor_Compat";
154-
if ( class_exists( $compat_class_name ) ) {
155-
$this->$slug->compat = new $compat_class_name( $this->$slug, $this->$slug->get_compat_hooks() );
156-
}
157149
}
158150
}
159151

modules/custom-status/compat/block-editor.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

modules/custom-status/custom-status.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,12 @@
1313
if ( !class_exists( 'EF_Custom_Status' ) ) {
1414

1515
class EF_Custom_Status extends EF_Module {
16+
use Block_Editor_Compatible;
1617

1718
var $module;
1819

1920
private $custom_statuses_cache = array();
2021

21-
/**
22-
* Define the hooks that need to be unhooked/rehooked to make the module Gutenberg-ready.
23-
*
24-
* @var array
25-
*/
26-
protected $compat_hooks = [
27-
'admin_enqueue_scripts' => 'action_admin_enqueue_scripts',
28-
];
29-
3022
// This is taxonomy name used to store all our custom statuses
3123
const taxonomy_key = 'post_status';
3224

@@ -291,12 +283,18 @@ function disable_custom_statuses_for_post_type( $post_type = null ) {
291283
* - We have other custom code for Quick Edit and JS niceties
292284
*/
293285
function action_admin_enqueue_scripts() {
294-
global $pagenow;
295-
296286
if ( $this->disable_custom_statuses_for_post_type() ) {
297287
return;
298288
}
299289

290+
// Load block editor assets and return early.
291+
if ( $this->is_block_editor() ) {
292+
wp_enqueue_style( 'edit-flow-block-custom-status-styles', EDIT_FLOW_URL . 'blocks/dist/custom-status.editor.build.css', false, EDIT_FLOW_VERSION );
293+
wp_enqueue_script( 'edit-flow-block-custom-status-script', EDIT_FLOW_URL . 'blocks/dist/custom-status.build.js', array( 'wp-blocks', 'wp-element', 'wp-edit-post', 'wp-plugins', 'wp-components' ), EDIT_FLOW_VERSION );
294+
wp_localize_script( 'edit-flow-block-custom-status-script', 'EditFlowCustomStatuses', array_values( $this->get_custom_statuses() ) );
295+
return;
296+
}
297+
300298
// Load Javascript we need to use on the configuration views (jQuery Sortable and Quick Edit)
301299
if ( $this->is_whitelisted_settings_view( $this->module->name ) ) {
302300
wp_enqueue_script( 'jquery-ui-sortable' );

0 commit comments

Comments
 (0)