From 0348c3eff4eee4ee86ade135d84f7b4598bc4919 Mon Sep 17 00:00:00 2001 From: Connor Jennings Date: Mon, 9 Dec 2019 17:38:44 -0500 Subject: [PATCH] Scope module assets for custom status --- common/php/trait-module-with-view.php | 109 ++++++++++++++++++ edit_flow.php | 2 + modules/custom-status/custom-status.php | 51 +++++++-- tests/test-edit-flow-custom-status.php | 146 ++++++++++++++++++++++-- 4 files changed, 292 insertions(+), 16 deletions(-) create mode 100644 common/php/trait-module-with-view.php diff --git a/common/php/trait-module-with-view.php b/common/php/trait-module-with-view.php new file mode 100644 index 000000000..776b179e3 --- /dev/null +++ b/common/php/trait-module-with-view.php @@ -0,0 +1,109 @@ +modules as $mod_name => $mod_data ) { + if ( isset( $mod_data->options->enabled ) + && 'on' === $mod_data->options->enabled + && $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 + * @param array $allowed_pages + * + * @return bool + */ + public function is_active_view( $allowed_pages = array( 'edit.php', 'post.php', 'post-new.php' ) ) { + return ( $this->is_admin_page( $allowed_pages ) ); + } + + /** + * 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' ) ); + } +} diff --git a/edit_flow.php b/edit_flow.php index 2148b6983..9b10a64fe 100644 --- a/edit_flow.php +++ b/edit_flow.php @@ -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/trait-module-with-view.php' ); + // Edit Flow Block Editor Compat trait require_once( EDIT_FLOW_ROOT . '/common/php/trait-block-editor-compatible.php' ); diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 0086c70a1..93832a2d5 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -14,6 +14,7 @@ class EF_Custom_Status extends EF_Module { use Block_Editor_Compatible; + use EF_Module_With_View; var $module; @@ -80,7 +81,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' ) ); @@ -282,7 +284,7 @@ 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() { + function enqueue_admin_scripts() { if ( $this->disable_custom_statuses_for_post_type() ) { return; } @@ -301,13 +303,13 @@ function action_admin_enqueue_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_localize_script('edit_flow-custom_status', '__ef_localize_custom_status', array( 'no_change' => esc_html__( "— No Change —", 'edit-flow' ), @@ -319,8 +321,12 @@ function action_admin_enqueue_scripts() { 'cancel' => esc_html__( 'Cancel', 'edit-flow' ), )); } + } - + public 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' ); + } } /** @@ -328,7 +334,7 @@ function action_admin_enqueue_scripts() { * 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() ) : ?>