|
6 | 6 | // phpcs:disable WordPressVIPMinimum.Variables.VariableAnalysis.SelfOutsideClass |
7 | 7 |
|
8 | 8 | 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 | | - } |
76 | 9 |
|
77 | 10 | /** |
78 | 11 | * Helper function to determine whether we're running WP 5.0. |
79 | 12 | * |
80 | 13 | * @return boolean |
81 | 14 | */ |
82 | | - public static function is_at_least_50() { |
| 15 | + private function is_at_least_wp_50() { |
83 | 16 | return version_compare( get_bloginfo( 'version' ), '5.0', '>=' ); |
84 | 17 | } |
85 | 18 |
|
86 | 19 | /** |
87 | | - * Helper to determine whether either Gutenberg plugin or Classic Editor plugin is loaded. |
| 20 | + * Whether or not we are in the block editor. |
88 | 21 | * |
89 | | - * @param string $slug |
90 | 22 | * @return boolean |
91 | 23 | */ |
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 | + } |
131 | 28 |
|
132 | | - return count( array_filter( $conditions, function( $c ) { return (bool) $c; } ) ) > 0; |
| 29 | + return false; |
133 | 30 | } |
134 | 31 | } |
0 commit comments