Skip to content

Commit

Permalink
Scope module assets for custom status
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Jennings committed Dec 9, 2019
1 parent 3eb2acd commit 52882ba
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 42 deletions.
107 changes: 107 additions & 0 deletions common/php/class-module-with-view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

class EF_Module_With_View extends EF_Module {

/**
* Whether or not the current page is an Edit Flow settings view (either main or module)
* Determination is based on $pagenow, $_GET['page'], and the module's $settings_slug
* If there's no module name specified, it will return true against all Edit Flow settings views
*
* @since 0.8.3
*
* @param string $slug (Optional) Module name to check against
* @return bool true if is module settings view
*/
public function is_module_settings_view( $slug = false ) {
global $pagenow, $edit_flow;

// All of the settings views are based on admin.php and a $_GET['page'] parameter
if ( 'admin.php' !== $pagenow || ! isset( $_GET['page'] ) )
return false;

$settings_view_slugs = array();
// Load all of the modules that have a settings slug/ callback for the settings page
foreach ( $edit_flow->modules as $mod_name => $mod_data ) {
if ( isset( $mod_data->options->enabled ) && $mod_data->options->enabled == 'on' && $mod_data->configure_page_cb )
$settings_view_slugs[] = $mod_data->settings_slug;
}

// The current page better be in the array of registered settings view slugs
if ( empty( $settings_view_slugs ) || ! in_array( $_GET['page'], $settings_view_slugs ) ) {
return false;
}

if ( $slug && $edit_flow->modules->{$slug}->settings_slug !== $_GET['page'] ) {
return false;
}

return true;
}

/**
* Check whether if we're at module settings view
* for the current module based on `is_module_settings_view` method
*
* @return bool
*/
public function is_current_module_settings_view() {
return $this->is_module_settings_view( $this->module->name );
}

/**
* Check for admin page and whether the current post type is supported
* @param array $allowed_pages
*
* @return bool
*/
function is_active_view( $allowed_pages = array( 'edit.php', 'post.php', 'post-new.php' ) ) {
return ( $this->is_admin_page( $allowed_pages ) && $this->is_supported_post_type() );
}

/**
* Check whether the current post type is supported for $this module
*
* @return bool
*/
public function is_supported_post_type() {
$post_type = $this->get_current_post_type();
return (
$post_type
&&
in_array( $post_type, $this->get_post_types_for_module( $this->module ), true )
);
}

/**
* Check whether currently viewing the desired admin page
*
* @param array $allowed_pages
*
* @return bool
*/
public function is_admin_page( $allowed_pages = array( 'edit.php', 'post.php', 'post-new.php' ) ) {
global $pagenow;

return ( $pagenow && in_array( $pagenow, $allowed_pages, true ) );
}

/**
* Shorthand for `is_active_view` to check for list type views ( list of posts pages, custom post types )
*
* @see is_active_view
* @return bool
*/
public function is_active_list_view() {
return $this->is_active_view( array( 'edit.php' ) );
}

/**
* Shorthand for `is_active_view` to check for editor mode
*
* @see is_active_view
* @return bool
*/
public function is_active_editor_view() {
return $this->is_active_view( array( 'post.php', 'posts-new.php' ) );
}
}
16 changes: 13 additions & 3 deletions common/php/trait-block-editor-compatible.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,24 @@ function action_init_for_admin() {

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 ) );
if ( is_array( $callback ) ) {
foreach ( $callback as $cb ) {
$this->replace_hook( $hook, $cb );
}
} else {
$this->replace_hook( $hook, $callback );
}
}
}
}

function replace_hook( $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' );

Expand Down
2 changes: 2 additions & 0 deletions edit_flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ private function load_modules() {
// Edit Flow base module
require_once( EDIT_FLOW_ROOT . '/common/php/class-module.php' );

require_once( EDIT_FLOW_ROOT . '/common/php/class-module-with-view.php' );

// Edit Flow Block Editor Compat trait
require_once( EDIT_FLOW_ROOT . '/common/php/trait-block-editor-compatible.php' );

Expand Down
17 changes: 14 additions & 3 deletions modules/custom-status/compat/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class EF_Custom_Status_Block_Editor_Compat {
*
* @return void
*/
function action_admin_enqueue_scripts() {
function enqueue_admin_scripts() {
if ( $this->ef_module->disable_custom_statuses_for_post_type() ) {
return;
}
Expand All @@ -18,15 +18,26 @@ function action_admin_enqueue_scripts() {
* WP_Screen::is_block_editor only available in 5.0. If it's available and is false it's safe to say we should only pass through to the module.
*/
if ( Block_Editor_Compatible::is_at_least_50() && ! get_current_screen()->is_block_editor() ) {
return $this->ef_module->action_admin_enqueue_scripts();
return $this->ef_module->enqueue_admin_scripts();
}

wp_enqueue_style( 'edit-flow-block-custom-status', EDIT_FLOW_URL . 'blocks/dist/custom-status.editor.build.css', false, EDIT_FLOW_VERSION );
wp_enqueue_script( 'edit-flow-block-custom-status', 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', 'EditFlowCustomStatuses', $this->get_custom_statuses() );
}

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

if ( Block_Editor_Compatible::is_at_least_50() && ! get_current_screen()->is_block_editor() ) {
return $this->ef_module->enqueue_admin_styles();
}

wp_enqueue_style( 'edit-flow-block-custom-status', EDIT_FLOW_URL . 'blocks/dist/custom-status.editor.build.css', false, EDIT_FLOW_VERSION );
}

/**
* Just a wrapper to make sure we're have simple array instead of associative one.
*
Expand Down
71 changes: 35 additions & 36 deletions modules/custom-status/custom-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

if ( !class_exists( 'EF_Custom_Status' ) ) {

class EF_Custom_Status extends EF_Module {
class EF_Custom_Status extends EF_Module_With_View {

var $module;

Expand All @@ -24,7 +24,7 @@ class EF_Custom_Status extends EF_Module {
* @var array
*/
protected $compat_hooks = [
'admin_enqueue_scripts' => 'action_admin_enqueue_scripts',
'admin_enqueue_scripts' => [ 'enqueue_admin_styles', 'enqueue_admin_scripts' ]
];

// This is taxonomy name used to store all our custom statuses
Expand Down Expand Up @@ -88,7 +88,8 @@ function init() {
add_action( 'admin_init', array( $this, 'register_settings' ) );

// Load CSS and JS resources that we probably need
add_action( 'admin_enqueue_scripts', array( $this, 'action_admin_enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
add_action( 'admin_notices', array( $this, 'no_js_notice' ) );
add_action( 'admin_print_scripts', array( $this, 'post_admin_header' ) );

Expand Down Expand Up @@ -299,23 +300,17 @@ function disable_custom_statuses_for_post_type( $post_type = null ) {
* - jQuery Sortable plugin is used for drag and dropping custom statuses
* - 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;
}

function enqueue_admin_scripts() {

// Load Javascript we need to use on the configuration views (jQuery Sortable and Quick Edit)
if ( $this->is_whitelisted_settings_view( $this->module->name ) ) {
if ( $this->is_current_module_settings_view() ) {
wp_enqueue_script( 'jquery-ui-sortable' );
wp_enqueue_script( 'edit-flow-custom-status-configure', $this->module_url . 'lib/custom-status-configure.js', array( 'jquery', 'jquery-ui-sortable', 'edit-flow-settings-js' ), EDIT_FLOW_VERSION, true );
}

// Custom javascript to modify the post status dropdown where it shows up
if ( $this->is_whitelisted_page() ) {
if ( $this->is_custom_status_view() ) {
wp_enqueue_script( 'edit_flow-custom_status', $this->module_url . 'lib/custom-status.js', array( 'jquery','post' ), EDIT_FLOW_VERSION, true );
wp_enqueue_style( 'edit_flow-custom_status', $this->module_url . 'lib/custom-status.css', false, EDIT_FLOW_VERSION, 'all' );
wp_localize_script('edit_flow-custom_status', '__ef_localize_custom_status', array(
'no_change' => esc_html__( "&mdash; No Change &mdash;", 'edit-flow' ),
'published' => esc_html__( 'Published', 'edit-flow' ),
Expand All @@ -326,16 +321,20 @@ function action_admin_enqueue_scripts() {
'cancel' => esc_html__( 'Cancel', 'edit-flow' ),
));
}
}


function enqueue_admin_styles() {
if ( $this->is_custom_status_view() ) {
wp_enqueue_style( 'edit_flow-custom_status', $this->module_url . 'lib/custom-status.css', false, EDIT_FLOW_VERSION, 'all' );
}
}

/**
* Displays a notice to users if they have JS disabled
* Javascript is needed for custom statuses to be fully functional
*/
function no_js_notice() {
if( $this->is_whitelisted_page() ) :
if ( $this->is_custom_status_view() ) :
?>
<style type="text/css">
/* Hide post status dropdown by default in case of JS issues **/
Expand All @@ -353,26 +352,6 @@ function no_js_notice() {
endif;
}

/**
* Check whether custom status stuff should be loaded on this page
*
* @todo migrate this to the base module class
*/
function is_whitelisted_page() {
global $pagenow;

if ( !in_array( $this->get_current_post_type(), $this->get_post_types_for_module( $this->module ) ) )
return false;

$post_type_obj = get_post_type_object( $this->get_current_post_type() );

if( ! current_user_can( $post_type_obj->cap->edit_posts ) )
return false;

// Only add the script to Edit Post and Edit Page pages -- don't want to bog down the rest of the admin with unnecessary javascript
return in_array( $pagenow, array( 'post.php', 'edit.php', 'post-new.php', 'page.php', 'edit-pages.php', 'page-new.php' ) );
}

/**
* Adds all necessary javascripts to make custom statuses work
*
Expand All @@ -388,7 +367,7 @@ function post_admin_header() {
wp_get_current_user() ;

// Only add the script to Edit Post and Edit Page pages -- don't want to bog down the rest of the admin with unnecessary javascript
if ( $this->is_whitelisted_page() ) {
if ( $this->is_custom_status_view() ) {

$custom_statuses = $this->get_custom_statuses();

Expand Down Expand Up @@ -1777,6 +1756,26 @@ public function fix_post_row_actions( $actions, $post ) {
$actions['view'] = '<a href="' . esc_url( $preview_link ) . '" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $post->post_title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';
return $actions;
}

/**
* Check whether current view is relavant to this module and whether the user has access to it
*
* @return bool
*/
public function is_custom_status_view() {
if( $this->is_current_module_settings_view() ) {
return true;
}
if( ! $this->is_active_view( array( 'post.php', 'edit.php', 'post-new.php', 'page.php', 'edit-pages.php', 'page-new.php' ) ) ) {
return false;
}
$post_type_obj = get_post_type_object( $this->get_current_post_type() );
if( $post_type_obj && ! current_user_can( $post_type_obj->cap->edit_posts ) ) {
return false;
}
return true;
}

}

}
Expand Down

0 comments on commit 52882ba

Please sign in to comment.