diff --git a/common/php/class-module-with-view.php b/common/php/class-module-with-view.php new file mode 100644 index 000000000..5e14d2a9b --- /dev/null +++ b/common/php/class-module-with-view.php @@ -0,0 +1,181 @@ +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 ) + ); + } + + /** + * Checks for the current post type + * + * @since 0.7 + * @return string|null $post_type The post type we've found, or null if no post type + */ + function get_current_post_type() { + global $post, $typenow, $pagenow, $current_screen; + //get_post() needs a variable + $post_id = isset( $_REQUEST['post'] ) ? (int) $_REQUEST['post'] : false; + + if ( $post && $post->post_type ) { + $post_type = $post->post_type; + } elseif ( $typenow ) { + $post_type = $typenow; + } elseif ( $current_screen && ! empty( $current_screen->post_type ) ) { + $post_type = $current_screen->post_type; + } elseif ( isset( $_REQUEST['post_type'] ) ) { + $post_type = sanitize_key( $_REQUEST['post_type'] ); + } elseif ( 'post.php' == $pagenow + && $post_id + && ! empty( get_post( $post_id )->post_type ) ) { + $post_type = get_post( $post_id )->post_type; + } elseif ( in_array( $pagenow, array('edit.php', 'post-new.php' ) ) && empty( $_REQUEST['post_type'] ) ) { + $post_type = 'post'; + } else { + $post_type = null; + } + + return $post_type; + } + + /** + * 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' ) ); + } + + + /** + * This method was supposed to check whether or not the current page is a user-facing Edit Flow View + * But it was never implemented + * + * It is now deprecated in favor of `$this->is_active_view` + * @see EF_Module::is_active_view() + * @since 0.7 + * @deprecated 0.8.3 + * + * @param string $module_name (Optional) Module name to check against + */ + public function is_whitelisted_functional_view( $module_name = null ) { + _deprecated_function( __FUNCTION__, '0.8.3', 'is_active_view' ); + + return true; + } + + /** + * 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.7 + * @deprecated 0.8.3 + * + * @param string $module_name (Optional) Module name to check against + * @return bool $is_settings_view Return true if it is + */ + public 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( $module_name ); + } + +} \ No newline at end of file diff --git a/common/php/class-module.php b/common/php/class-module.php index 8c6ac5527..7824f7ff9 100755 --- a/common/php/class-module.php +++ b/common/php/class-module.php @@ -208,38 +208,7 @@ function enqueue_datepicker_resources() { wp_enqueue_style( 'jquery-ui-theme', EDIT_FLOW_URL . 'common/css/jquery.ui.theme.css', false, EDIT_FLOW_VERSION, 'screen' ); } - /** - * Checks for the current post type - * - * @since 0.7 - * @return string|null $post_type The post type we've found, or null if no post type - */ - function get_current_post_type() { - global $post, $typenow, $pagenow, $current_screen; - //get_post() needs a variable - $post_id = isset( $_REQUEST['post'] ) ? (int)$_REQUEST['post'] : false; - - if ( $post && $post->post_type ) { - $post_type = $post->post_type; - } elseif ( $typenow ) { - $post_type = $typenow; - } elseif ( $current_screen && !empty( $current_screen->post_type ) ) { - $post_type = $current_screen->post_type; - } elseif ( isset( $_REQUEST['post_type'] ) ) { - $post_type = sanitize_key( $_REQUEST['post_type'] ); - } elseif ( 'post.php' == $pagenow - && $post_id - && !empty( get_post( $post_id )->post_type ) ) { - $post_type = get_post( $post_id )->post_type; - } elseif ( 'edit.php' == $pagenow && empty( $_REQUEST['post_type'] ) ) { - $post_type = 'post'; - } else { - $post_type = null; - } - return $post_type; - } - /** * Wrapper for the get_user_meta() function so we can replace it if we need to * @@ -296,55 +265,6 @@ function print_ajax_response( $status, $message = '' ) { exit; } - /** - * Whether or not the current page is a user-facing Edit Flow View - * @todo Think of a creative way to make this work - * - * @since 0.7 - * - * @param string $module_name (Optional) Module name to check against - */ - function is_whitelisted_functional_view( $module_name = null ) { - - // @todo complete this method - - return true; - } - - /** - * 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.7 - * - * @param string $module_name (Optional) Module name to check against - * @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; - } - - /** * This is a hack, Hack, HACK!!! * Encode all of the given arguments as a serialized array, and then base64_encode @@ -573,6 +493,7 @@ function upgrade_074_term_descriptions( $taxonomy ) { wp_update_term( $term->term_id, $taxonomy, array( 'description' => $new_description ) ); } } - + + } } diff --git a/edit_flow.php b/edit_flow.php index 6e6024e32..d8ed8c996 100644 --- a/edit_flow.php +++ b/edit_flow.php @@ -93,7 +93,14 @@ 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/EF_Script_Interface.php' ); + require_once( EDIT_FLOW_ROOT . '/interfaces/EF_Style_Interface.php' ); + require_once( EDIT_FLOW_ROOT . '/common/php/class-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/EF_Script_Interface.php b/interfaces/EF_Script_Interface.php new file mode 100644 index 000000000..0f9218be4 --- /dev/null +++ b/interfaces/EF_Script_Interface.php @@ -0,0 +1,9 @@ +is_calendar_view() ) { + return; + } + wp_enqueue_style( 'edit-flow-calendar-css', $this->module_url . 'lib/calendar.css', false, EDIT_FLOW_VERSION ); } @@ -188,9 +189,12 @@ function add_admin_styles() { */ function enqueue_admin_scripts() { + if ( ! $this->is_calendar_view() ) { + return; + } + $this->enqueue_datepicker_resources(); - if ( $this->is_whitelisted_functional_view() ) { $js_libraries = array( 'jquery', 'jquery-ui-core', @@ -198,6 +202,7 @@ function enqueue_admin_scripts() { 'jquery-ui-draggable', 'jquery-ui-droppable', ); + foreach( $js_libraries as $js_library ) { wp_enqueue_script( $js_library ); } @@ -205,7 +210,7 @@ function enqueue_admin_scripts() { $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 ); - } + } @@ -1851,6 +1856,12 @@ public function fix_post_date_on_update_part_two( $post_ID, $post_after, $post_b clean_post_cache( $post_ID ); } + + public function is_calendar_view() { + global $pagenow; + + return ( 'index.php' === $pagenow && isset( $_GET['page'] ) && $_GET['page'] === 'calendar' ); + } } // EF_Calendar } // class_exists('EF_Calendar') diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index ed20665d2..4979f2ee3 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_With_View implements EF_Script_Interface, EF_Style_Interface { 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' ) ); @@ -117,6 +112,12 @@ function init() { // Filter through Post States and run a function to check if they are also a Status add_filter( 'display_post_states', array( $this, 'check_if_post_state_is_status' ), 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' ) ); } /** @@ -217,7 +218,7 @@ function upgrade( $previous_version ) { function register_custom_statuses() { global $wp_post_statuses; - if ( $this->disable_custom_statuses_for_post_type() ) + if ( ! $this->is_custom_status_view() ) return; // Register new taxonomy so that we can store all our fancy new custom statuses (or is it stati?) @@ -256,70 +257,13 @@ function register_custom_statuses() { } } - /** - * Whether custom post statuses should be disabled for this post type. - * Used to stop custom statuses from being registered for post types that don't support them. - * - * @since 0.7.5 - * - * @return bool - */ - function disable_custom_statuses_for_post_type( $post_type = null ) { - global $pagenow; - - // Only allow deregistering on 'edit.php' and 'post.php' - if ( ! in_array( $pagenow, array( 'edit.php', 'post.php', 'post-new.php' ) ) ) - return false; - - if ( is_null( $post_type ) ) - $post_type = $this->get_current_post_type(); - - if ( $post_type && ! in_array( $post_type, $this->get_post_types_for_module( $this->module ) ) ) - return true; - - 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_custom_status_view() ) : ?>