Skip to content

Commit

Permalink
Remove abstraction layers around block compat
Browse files Browse the repository at this point in the history
  • Loading branch information
WPprodigy committed Jan 16, 2020
1 parent 86bd66c commit dde1d2d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 189 deletions.
23 changes: 2 additions & 21 deletions common/php/class-module.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ class EF_Module {
'private',
);

/**
* Associative array of hook_name => callback_name
* This is used for Gutenberg-compat initialization
* [
* 'init' => 'init_callback_on_module_instance'
* ]
* @var array
*/
protected $compat_hooks = [];

function __construct() {}

/**
Expand Down Expand Up @@ -490,7 +480,7 @@ function users_select_form( $selected = null, $args = null ) {

<?php if( !empty($users) ) : ?>
<ul class="<?php echo esc_attr( $list_class ) ?>">
<?php foreach( $users as $user ) :
<?php foreach( $users as $user ) :
$checked = ( in_array($user->ID, $selected) ) ? 'checked="checked"' : '';
// Add a class to checkbox of current user so we know not to add them in notified list during notifiedMessage() js function
$current_user_class = ( get_current_user_id() == $user->ID ) ? 'class="post_following_list-current_user" ' : '';
Expand Down Expand Up @@ -598,15 +588,6 @@ function upgrade_074_term_descriptions( $taxonomy ) {
wp_update_term( $term->term_id, $taxonomy, array( 'description' => $new_description ) );
}
}

/**
* Return compatibility hooks for the current instance
*
* @return array
*/
function get_compat_hooks() {
return isset( $this->compat_hooks ) && is_array( $this->compat_hooks ) ? $this->compat_hooks : [];
}

}

}
117 changes: 7 additions & 110 deletions common/php/trait-block-editor-compatible.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,129 +6,26 @@
// phpcs:disable WordPressVIPMinimum.Variables.VariableAnalysis.SelfOutsideClass

trait Block_Editor_Compatible {
/**
* Holds the reference to the Module's object
*
* @var EF_Module
*/
protected $ef_module;
/**
* Holds associative array of hooks and their respective callbacks
*
* @var array
*/
protected $hooks = [];
protected static $active_plugins;

/**
* This method handles init of Module Compat
*
* @param EF_Module $module_instance
* @param array $hooks associative array of hooks and their respective callbacks
*
*
* @return void
*/
function __construct( $module_instance, $hooks = [] ) {
$this->ef_module = $module_instance;
$this->hooks = $hooks;

if ( is_admin() ) {

add_action( 'init', [ $this, 'action_init_for_admin' ], 15 );
}
}

/**
* Unhook the module's hooks and use the module's Compat hooks instead.
*
* This is currently run on init action, but only when `is_admin` is true.
*
* @since 0.9
*
* @return void
*/
function action_init_for_admin() {
if ( ! $this->ef_module->is_enabled() ) {
return;
}

$this->check_active_plugins();

if ( $this->should_apply_compat() ) {
foreach ( $this->hooks as $hook => $callback ) {
if ( is_callable( [ $this, $callback ] ) ) {
remove_action( $hook, array( $this->ef_module, $callback ) );
add_action( $hook, array( $this, $callback ) );
}
}
}
}

function check_active_plugins() {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

self::$active_plugins = [
'classic-editor' => is_plugin_active( 'classic-editor' ),
'gutenberg' => is_plugin_active( 'gutenberg' ),
];
}

/**
* Helper function to determine whether we're running WP 5.0.
*
* @return boolean
*/
public static function is_at_least_50() {
private function is_at_least_wp_50() {
return version_compare( get_bloginfo( 'version' ), '5.0', '>=' );
}

/**
* Helper to determine whether either Gutenberg plugin or Classic Editor plugin is loaded.
* Whether or not we are in the block editor.
*
* @param string $slug
* @return boolean
*/
public static function is_plugin_active( $slug = '' ) {
return isset( self::$active_plugins[ $slug ] ) && self::$active_plugins[ $slug ];
}

/**
* Detect whether we should load compatability for the module.
* 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.
*
* However, Block Editor can be enabled/disabled on a granular basis via the filters for the core and the plugin versions.
*
* use_block_editor_for_post
* use_block_editor_for_post_type
* gutenberg_can_edit_post_type
* gutenberg_can_edit_post
*
*
* This needs to be handled in the compat hook callback.
*
* If any of $conditions evaluates to TRUE, we should apply compat hooks.
*
* @return boolean
*/
protected function should_apply_compat() {
$conditions = [
/**
* 5.0:
*
* Classic editor either disabled or enabled (either via an option or with GET argument).
* It's a hairy conditional :(
*/
// phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.NoNonceVerification
self::is_at_least_50() && ! self::is_plugin_active( 'classic-editor' ),
self::is_at_least_50() && self::is_plugin_active( 'classic-editor' ) && ( get_option( 'classic-editor-replace' ) === 'block' && ! isset( $_GET[ 'classic-editor__forget' ] ) ),
self::is_at_least_50() && self::is_plugin_active( 'classic-editor' ) && ( get_option( 'classic-editor-replace' ) === 'classic' && isset( $_GET[ 'classic-editor__forget' ] ) ),
/**
* < 5.0 but Gutenberg plugin is active.
*/
! self::is_at_least_50() && self::is_plugin_active( 'gutenberg' ),
];
public function is_block_editor() {
if ( self::is_at_least_wp_50() && function_exists( 'get_current_screen' ) ) {
return get_current_screen()->is_block_editor();
}

return count( array_filter( $conditions, function( $c ) { return (bool) $c; } ) ) > 0;
return false;
}
}
8 changes: 0 additions & 8 deletions edit_flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ private function load_modules() {
if ( file_exists( EDIT_FLOW_ROOT . "/modules/{$module_dir}/$module_dir.php" ) ) {
include_once( EDIT_FLOW_ROOT . "/modules/{$module_dir}/$module_dir.php" );

// Try to load Gutenberg compat files
if ( file_exists( EDIT_FLOW_ROOT . "/modules/{$module_dir}/compat/block-editor.php" ) ) {
include_once( EDIT_FLOW_ROOT . "/modules/{$module_dir}/compat/block-editor.php" );
}
// Prepare the class name because it should be standardized
$tmp = explode( '-', $module_dir );
$class_name = '';
Expand All @@ -150,10 +146,6 @@ private function load_modules() {
foreach( $class_names as $slug => $class_name ) {
if ( class_exists( $class_name ) ) {
$this->$slug = new $class_name();
$compat_class_name = "{$class_name}_Block_Editor_Compat";
if ( class_exists( $compat_class_name ) ) {
$this->$slug->compat = new $compat_class_name( $this->$slug, $this->$slug->get_compat_hooks() );
}
}
}

Expand Down
37 changes: 0 additions & 37 deletions modules/custom-status/compat/block-editor.php

This file was deleted.

24 changes: 11 additions & 13 deletions modules/custom-status/custom-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@
if ( !class_exists( 'EF_Custom_Status' ) ) {

class EF_Custom_Status extends EF_Module {
use Block_Editor_Compatible;

var $module;

private $custom_statuses_cache = array();

/**
* Define the hooks that need to be unhooked/rehooked to make the module Gutenberg-ready.
*
* @var array
*/
protected $compat_hooks = [
'admin_enqueue_scripts' => 'action_admin_enqueue_scripts',
];

// This is taxonomy name used to store all our custom statuses
const taxonomy_key = 'post_status';

Expand Down Expand Up @@ -300,12 +292,18 @@ function disable_custom_statuses_for_post_type( $post_type = null ) {
* - We have other custom code for Quick Edit and JS niceties
*/
function action_admin_enqueue_scripts() {
global $pagenow;

if ( $this->disable_custom_statuses_for_post_type() ) {
return;
}

// Load block editor assets and return early.
if ( $this->is_block_editor() ) {
wp_enqueue_style( 'edit-flow-block-custom-status-styles', EDIT_FLOW_URL . 'blocks/dist/custom-status.editor.build.css', false, EDIT_FLOW_VERSION );
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 );
wp_localize_script( 'edit-flow-block-custom-status-script', 'EditFlowCustomStatuses', array_values( $this->get_custom_statuses() ) );
return;
}

// Load Javascript we need to use on the configuration views (jQuery Sortable and Quick Edit)
if ( $this->is_whitelisted_settings_view( $this->module->name ) ) {
wp_enqueue_script( 'jquery-ui-sortable' );
Expand Down Expand Up @@ -757,7 +755,7 @@ function check_if_post_state_is_status( $post_states, $post ) {
echo '<span class="show"></span>';
}
}

return $post_states;
}

Expand Down Expand Up @@ -1520,7 +1518,7 @@ public function fix_preview_link_part_three( $preview_link, $query_args ) {
}
}
}
return remove_query_arg( [ 'preview_nonce' ], $preview_link );
return remove_query_arg( [ 'preview_nonce' ], $preview_link );
}

/**
Expand Down

0 comments on commit dde1d2d

Please sign in to comment.