Skip to content

Commit 6b88e56

Browse files
authored
Cleanup block editor compatibility logic (#580)
1 parent 8c2f3f5 commit 6b88e56

File tree

13 files changed

+78
-321
lines changed

13 files changed

+78
-321
lines changed

BLOCKS.md

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

blocks/BLOCKS.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Block Editor Compatibility
2+
3+
This file describes steps to start developing Blocks for Edit Flow.
4+
Currently, the following Edit Flow modules are compatible with Gutenberg Block Editor:
5+
6+
* Custom Statuses
7+
8+
### Setup
9+
10+
*Note:* This document assumes you have a working knowledge of modern JavaScript and its tooling. Including npm, Webpack, and React.
11+
12+
Prerequisites: `npm`.
13+
14+
From the plugin's folder run:
15+
16+
```
17+
npm i
18+
```
19+
20+
This should leave you with everything you need for development, including a local copy of Webpack and webpack-cli.
21+
22+
## Anatomy of an Edit Flow block.
23+
24+
There are two parts for adding Block Editor compatibility to modules.
25+
26+
#### PHP
27+
28+
On the PHP side, we mostly just need to make sure the block assets are enqueued when they are needed. There is a [Block_Editor_Compatible](common/php/trait-block-editor-compatible.php) trait that gives access to helpful methods related to the block editor when used within modules.
29+
30+
#### JavaScript
31+
32+
##### Development
33+
34+
To start the Webpack in watch mode:
35+
36+
```npm run dev```
37+
38+
##### Build for production
39+
40+
To generate optimized/minified production-ready files:
41+
42+
```npm run build```
43+
44+
##### File Structure
45+
46+
```
47+
blocks/
48+
# Source files:
49+
src/
50+
module-slug/
51+
block.js # Gutenberg Block code for the module
52+
editor.scss # Editor styles
53+
style.scss # Front-end styles
54+
# Build
55+
dist/
56+
module-slug.build.js # Built block js
57+
module-slug.editor.build.css # Built editor CSS
58+
module-slug.style.build.css # Built front-end CSS
59+
```
60+
61+
The files from `dist/` should be enqueued in the compat class for the module.

blocks/dist/calendar.build.js

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

blocks/dist/calendar.editor.build.css

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

blocks/dist/calendar.style.build.css

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

blocks/src/calendar/block.js

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

blocks/src/calendar/editor.scss

Whitespace-only changes.

blocks/src/calendar/style.scss

Whitespace-only changes.

common/php/class-module.php

Lines changed: 1 addition & 20 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
/**
@@ -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
}

0 commit comments

Comments
 (0)