From 435447b9f8d7a436c05954652d85f29e4691c2f7 Mon Sep 17 00:00:00 2001 From: Norris Date: Mon, 29 Jan 2018 13:43:22 +0200 Subject: [PATCH 01/37] Add method `is_module_view` --- common/php/class-module.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/common/php/class-module.php b/common/php/class-module.php index c19034913..ac324a875 100755 --- a/common/php/class-module.php +++ b/common/php/class-module.php @@ -604,6 +604,22 @@ function upgrade_074_term_descriptions( $taxonomy ) { wp_update_term( $term->term_id, $taxonomy, array( 'description' => $new_description ) ); } } + + /** + * Check if currently viewing a module + * + * @param $slug - the slug of the module + * + * @return bool + */ + public function is_module_view( $slug ) { + + if ( ! is_admin() ) { + return false; + } + + return ( isset( $_GET['page'] ) && $_GET['page'] === $slug ); + } } } From 970c89ecbc7eed5ad3fd73bb5e77f49d511d59b5 Mon Sep 17 00:00:00 2001 From: Norris Date: Mon, 29 Jan 2018 13:52:54 +0200 Subject: [PATCH 02/37] Deprecate `is_whitelisted_settings_view()` in favor of `is_module_settings_view()` --- common/php/class-module.php | 59 ++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/common/php/class-module.php b/common/php/class-module.php index ac324a875..a235cde32 100755 --- a/common/php/class-module.php +++ b/common/php/class-module.php @@ -357,26 +357,8 @@ function is_whitelisted_functional_view( $module_name = null ) { * @return bool $is_settings_view Return true if it is */ function is_whitelisted_settings_view( $module_name = null ) { - global $pagenow, $edit_flow; - - // All of the settings views are based on admin.php and a $_GET['page'] parameter - if ( $pagenow != 'admin.php' || !isset( $_GET['page'] ) ) - return false; - - // 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 ( !in_array( $_GET['page'], $settings_view_slugs ) ) - return false; - - if ( $module_name && $edit_flow->modules->$module_name->settings_slug != $_GET['page'] ) - return false; - - return true; + _deprecated_function('is_whitelisted_settings_view', '0.8.3', 'is_module_settings_view'); + return $this->is_module_settings_view(); } @@ -605,6 +587,43 @@ function upgrade_074_term_descriptions( $taxonomy ) { } } + + /** + * 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 ( $pagenow !== 'admin.php' || ! 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 if currently viewing a module * From 89206dcd06053dc1c9dfb47ff1130815245f8642 Mon Sep 17 00:00:00 2001 From: Norris Date: Mon, 29 Jan 2018 14:10:53 +0200 Subject: [PATCH 03/37] Add style & script interfaces to improve method name consistency --- edit_flow.php | 7 ++++++- interfaces/Edit_Flow_Scripts.php | 6 ++++++ interfaces/Edit_Flow_Styles.php | 6 ++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 interfaces/Edit_Flow_Scripts.php create mode 100644 interfaces/Edit_Flow_Styles.php diff --git a/edit_flow.php b/edit_flow.php index 57eb5639f..d1fdb0713 100644 --- a/edit_flow.php +++ b/edit_flow.php @@ -93,7 +93,12 @@ private function load_modules() { // Edit Flow base module require_once( EDIT_FLOW_ROOT . '/common/php/class-module.php' ); - + + /* + * Include interfaces: + */ + require_once( EDIT_FLOW_ROOT . '/interfaces/Edit_Flow_Scripts.php' ); + require_once( EDIT_FLOW_ROOT . '/interfaces/Edit_Flow_Styles.php' ); // Scan the modules directory and include any modules that exist there $module_dirs = scandir( EDIT_FLOW_ROOT . '/modules/' ); $class_names = array(); diff --git a/interfaces/Edit_Flow_Scripts.php b/interfaces/Edit_Flow_Scripts.php new file mode 100644 index 000000000..81fabc505 --- /dev/null +++ b/interfaces/Edit_Flow_Scripts.php @@ -0,0 +1,6 @@ + Date: Mon, 29 Jan 2018 14:12:48 +0200 Subject: [PATCH 04/37] Add `Edit_Flow_Module_With_View` interface --- edit_flow.php | 2 ++ interfaces/Edit_Flow_Module_With_View.php | 6 ++++++ 2 files changed, 8 insertions(+) create mode 100644 interfaces/Edit_Flow_Module_With_View.php diff --git a/edit_flow.php b/edit_flow.php index d1fdb0713..2434b8a06 100644 --- a/edit_flow.php +++ b/edit_flow.php @@ -99,6 +99,8 @@ private function load_modules() { */ require_once( EDIT_FLOW_ROOT . '/interfaces/Edit_Flow_Scripts.php' ); require_once( EDIT_FLOW_ROOT . '/interfaces/Edit_Flow_Styles.php' ); + require_once( EDIT_FLOW_ROOT . '/interfaces/Edit_Flow_Module_With_View.php' ); + // Scan the modules directory and include any modules that exist there $module_dirs = scandir( EDIT_FLOW_ROOT . '/modules/' ); $class_names = array(); diff --git a/interfaces/Edit_Flow_Module_With_View.php b/interfaces/Edit_Flow_Module_With_View.php new file mode 100644 index 000000000..7ec800001 --- /dev/null +++ b/interfaces/Edit_Flow_Module_With_View.php @@ -0,0 +1,6 @@ + Date: Mon, 29 Jan 2018 15:54:38 +0200 Subject: [PATCH 05/37] Accept $module_name in `is_module_settings_view` --- common/php/class-module.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/php/class-module.php b/common/php/class-module.php index a235cde32..fc3dbda72 100755 --- a/common/php/class-module.php +++ b/common/php/class-module.php @@ -358,7 +358,7 @@ function is_whitelisted_functional_view( $module_name = null ) { */ function is_whitelisted_settings_view( $module_name = null ) { _deprecated_function('is_whitelisted_settings_view', '0.8.3', 'is_module_settings_view'); - return $this->is_module_settings_view(); + return $this->is_module_settings_view( $module_name ); } From 9b2698af7ce687cf7d54395db1d2abcbb772b7d5 Mon Sep 17 00:00:00 2001 From: Norris Date: Mon, 29 Jan 2018 15:55:39 +0200 Subject: [PATCH 06/37] Refactor calendar asset enqueuing --- modules/calendar/calendar.php | 61 +++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/modules/calendar/calendar.php b/modules/calendar/calendar.php index cff44e745..63306acf2 100644 --- a/modules/calendar/calendar.php +++ b/modules/calendar/calendar.php @@ -7,7 +7,7 @@ */ if ( !class_exists('EF_Calendar') ) { -class EF_Calendar extends EF_Module { +class EF_Calendar extends EF_Module implements Edit_Flow_Styles, Edit_Flow_Scripts, Edit_Flow_Module_With_View { const usermeta_key_prefix = 'ef_calendar_'; const screen_id = 'dashboard_page_calendar'; @@ -92,7 +92,7 @@ function init() { add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_menu', array( $this, 'action_admin_menu' ) ); - add_action( 'admin_print_styles', array( $this, 'add_admin_styles' ) ); + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); // Ajax manipulation for the calendar @@ -168,17 +168,18 @@ function upgrade( $previous_version ) { function action_admin_menu() { add_submenu_page('index.php', __('Calendar', 'edit-flow'), __('Calendar', 'edit-flow'), apply_filters( 'ef_view_calendar_cap', 'ef_view_calendar' ), $this->module->slug, array( $this, 'view_calendar' ) ); } - + /** * Add any necessary CSS to the WordPress admin * * @uses wp_enqueue_style() */ - function add_admin_styles() { - global $pagenow; - // Only load calendar styles on the calendar page - if ( $pagenow == 'index.php' && isset( $_GET['page'] ) && $_GET['page'] == 'calendar' ) - wp_enqueue_style( 'edit-flow-calendar-css', $this->module_url . 'lib/calendar.css', false, EDIT_FLOW_VERSION ); + public function enqueue_admin_styles() { + if ( ! $this->is_current_module_view() ) { + return; + } + + wp_enqueue_style( 'edit-flow-calendar-css', $this->module_url . 'lib/calendar.css', false, EDIT_FLOW_VERSION ); } /** @@ -189,24 +190,28 @@ function add_admin_styles() { */ function enqueue_admin_scripts() { + if ( ! $this->is_current_module_view() ) { + return; + } + $this->enqueue_datepicker_resources(); - - if ( $this->is_whitelisted_functional_view() ) { - $js_libraries = array( - 'jquery', - 'jquery-ui-core', - 'jquery-ui-sortable', - 'jquery-ui-draggable', - 'jquery-ui-droppable', - ); - foreach( $js_libraries as $js_library ) { - wp_enqueue_script( $js_library ); - } - wp_enqueue_script( 'edit-flow-calendar-js', $this->module_url . 'lib/calendar.js', $js_libraries, EDIT_FLOW_VERSION, true ); - - $ef_cal_js_params = array( 'can_add_posts' => current_user_can( $this->create_post_cap ) ? 'true' : 'false' ); - wp_localize_script( 'edit-flow-calendar-js', 'ef_calendar_params', $ef_cal_js_params ); + + $js_libraries = array( + 'jquery', + 'jquery-ui-core', + 'jquery-ui-sortable', + 'jquery-ui-draggable', + 'jquery-ui-droppable', + ); + + foreach( $js_libraries as $js_library ) { + wp_enqueue_script( $js_library ); } + wp_enqueue_script( 'edit-flow-calendar-js', $this->module_url . 'lib/calendar.js', $js_libraries, EDIT_FLOW_VERSION, true ); + + $ef_cal_js_params = array( 'can_add_posts' => current_user_can( $this->create_post_cap ) ? 'true' : 'false' ); + wp_localize_script( 'edit-flow-calendar-js', 'ef_calendar_params', $ef_cal_js_params ); + } @@ -1836,7 +1841,13 @@ public function fix_post_date_on_update_part_two( $post_ID, $post_after, $post_b $wpdb->update( $wpdb->posts, array( 'post_date' => $post_date ), array( 'ID' => $post_ID ) ); clean_post_cache( $post_ID ); } - + + + public function is_current_module_view() { + global $pagenow; + + return ( $pagenow == 'index.php' && $this->is_module_view( 'calendar' ) ); + } } // EF_Calendar } // class_exists('EF_Calendar') From a46ed47754a843a09934aa7902fe6e2babe2a4a7 Mon Sep 17 00:00:00 2001 From: Norris Date: Mon, 29 Jan 2018 15:55:52 +0200 Subject: [PATCH 07/37] Refactor custom-status asset enqueuing --- modules/custom-status/custom-status.php | 143 +++++++++++++----------- 1 file changed, 75 insertions(+), 68 deletions(-) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 853d703e4..8200dbf0e 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -10,7 +10,7 @@ */ if ( !class_exists( 'EF_Custom_Status' ) ) { -class EF_Custom_Status extends EF_Module { +class EF_Custom_Status extends EF_Module implements Edit_Flow_Scripts, Edit_Flow_Styles, Edit_Flow_Module_With_View { var $module; @@ -77,11 +77,6 @@ function init() { // Register our settings 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_notices', array( $this, 'no_js_notice' ) ); - add_action( 'admin_print_scripts', array( $this, 'post_admin_header' ) ); - // Methods for handling the actions of creating, making default, and deleting post stati add_action( 'admin_init', array( $this, 'handle_add_custom_status' ) ); add_action( 'admin_init', array( $this, 'handle_edit_custom_status' ) ); @@ -115,6 +110,13 @@ function init() { // Pagination for custom post statuses when previewing posts add_filter( 'wp_link_pages_link', array( $this, 'modify_preview_link_pagination_url' ), 10, 2 ); + + // Load CSS and JS resources that we probably need + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); + + add_action( 'admin_notices', array( $this, 'no_js_notice' ) ); + add_action( 'admin_print_scripts', array( $this, 'post_admin_header' ) ); } /** @@ -278,46 +280,13 @@ function disable_custom_statuses_for_post_type( $post_type = null ) { return false; } - /** - * Enqueue Javascript resources that we need in the admin: - * - Primary use of Javascript is to manipulate the post status dropdown on Edit Post and Manage Posts - * - 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; - - // 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' ); - 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() ) { - 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__( "— No Change —", 'edit-flow' ), - 'published' => esc_html__( 'Published', 'edit-flow' ), - 'save_as' => esc_html__( 'Save as', 'edit-flow' ), - 'save' => esc_html__( 'Save', 'edit-flow' ), - 'edit' => esc_html__( 'Edit', 'edit-flow' ), - 'ok' => esc_html__( 'OK', 'edit-flow' ), - 'cancel' => esc_html__( 'Cancel', 'edit-flow' ), - )); - } - } /** * 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_current_module_view() ) : ?>