Skip to content

Commit 0348c3e

Browse files
Connor Jenningscojennin
authored andcommitted
Scope module assets for custom status
1 parent a17323a commit 0348c3e

File tree

4 files changed

+292
-16
lines changed

4 files changed

+292
-16
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
trait EF_Module_With_View {
4+
5+
/**
6+
* Whether or not the current page is an Edit Flow settings view (either main or module)
7+
* Determination is based on $pagenow, $_GET['page'], and the module's $settings_slug
8+
* If there's no module name specified, it will return true against all Edit Flow settings views
9+
*
10+
* @since 0.8.3
11+
*
12+
* @param string $slug (Optional) Module name to check against
13+
* @return bool true if is module settings view
14+
*/
15+
public function is_module_settings_view( $slug = false ) {
16+
global $pagenow, $edit_flow;
17+
18+
// All of the settings views are based on admin.php and a $_GET['page'] parameter
19+
if ( 'admin.php' !== $pagenow || ! isset( $_GET['page'] ) )
20+
return false;
21+
22+
$settings_view_slugs = array();
23+
// Load all of the modules that have a settings slug/ callback for the settings page
24+
foreach ( $edit_flow->modules as $mod_name => $mod_data ) {
25+
if ( isset( $mod_data->options->enabled )
26+
&& 'on' === $mod_data->options->enabled
27+
&& $mod_data->configure_page_cb )
28+
$settings_view_slugs[] = $mod_data->settings_slug;
29+
}
30+
31+
// The current page better be in the array of registered settings view slugs
32+
if ( empty( $settings_view_slugs ) || ! in_array( $_GET['page'], $settings_view_slugs ) ) {
33+
return false;
34+
}
35+
36+
if ( $slug && $edit_flow->modules->{$slug}->settings_slug !== $_GET['page'] ) {
37+
return false;
38+
}
39+
40+
return true;
41+
}
42+
43+
/**
44+
* Check whether if we're at module settings view
45+
* for the current module based on `is_module_settings_view` method
46+
*
47+
* @return bool
48+
*/
49+
public function is_current_module_settings_view() {
50+
return $this->is_module_settings_view( $this->module->name );
51+
}
52+
53+
/**
54+
* Check for admin page
55+
* @param array $allowed_pages
56+
*
57+
* @return bool
58+
*/
59+
public function is_active_view( $allowed_pages = array( 'edit.php', 'post.php', 'post-new.php' ) ) {
60+
return ( $this->is_admin_page( $allowed_pages ) );
61+
}
62+
63+
/**
64+
* Check whether the current post type is supported for $this module
65+
*
66+
* @return bool
67+
*/
68+
public function is_supported_post_type( ) {
69+
$post_type = $this->get_current_post_type();
70+
return (
71+
$post_type
72+
&&
73+
in_array( $post_type, $this->get_post_types_for_module( $this->module ), true )
74+
);
75+
}
76+
77+
/**
78+
* Check whether currently viewing the desired admin page
79+
*
80+
* @param array $allowed_pages
81+
*
82+
* @return bool
83+
*/
84+
public function is_admin_page( $allowed_pages = array( 'edit.php', 'post.php', 'post-new.php' ) ) {
85+
global $pagenow;
86+
87+
return ( $pagenow && in_array( $pagenow, $allowed_pages, true ) );
88+
}
89+
90+
/**
91+
* Shorthand for `is_active_view` to check for list type views ( list of posts pages, custom post types )
92+
*
93+
* @see is_active_view
94+
* @return bool
95+
*/
96+
public function is_active_list_view() {
97+
return $this->is_active_view( array( 'edit.php' ) );
98+
}
99+
100+
/**
101+
* Shorthand for `is_active_view` to check for editor mode
102+
*
103+
* @see is_active_view
104+
* @return bool
105+
*/
106+
public function is_active_editor_view() {
107+
return $this->is_active_view( array( 'post.php', 'posts-new.php' ) );
108+
}
109+
}

edit_flow.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ private function load_modules() {
111111
// Edit Flow base module
112112
require_once( EDIT_FLOW_ROOT . '/common/php/class-module.php' );
113113

114+
require_once( EDIT_FLOW_ROOT . '/common/php/trait-module-with-view.php' );
115+
114116
// Edit Flow Block Editor Compat trait
115117
require_once( EDIT_FLOW_ROOT . '/common/php/trait-block-editor-compatible.php' );
116118

modules/custom-status/custom-status.php

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
class EF_Custom_Status extends EF_Module {
1616
use Block_Editor_Compatible;
17+
use EF_Module_With_View;
1718

1819
var $module;
1920

@@ -80,7 +81,8 @@ function init() {
8081
add_action( 'admin_init', array( $this, 'register_settings' ) );
8182

8283
// Load CSS and JS resources that we probably need
83-
add_action( 'admin_enqueue_scripts', array( $this, 'action_admin_enqueue_scripts' ) );
84+
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
85+
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
8486
add_action( 'admin_notices', array( $this, 'no_js_notice' ) );
8587
add_action( 'admin_print_scripts', array( $this, 'post_admin_header' ) );
8688

@@ -282,7 +284,7 @@ function disable_custom_statuses_for_post_type( $post_type = null ) {
282284
* - jQuery Sortable plugin is used for drag and dropping custom statuses
283285
* - We have other custom code for Quick Edit and JS niceties
284286
*/
285-
function action_admin_enqueue_scripts() {
287+
function enqueue_admin_scripts() {
286288
if ( $this->disable_custom_statuses_for_post_type() ) {
287289
return;
288290
}
@@ -301,13 +303,13 @@ function action_admin_enqueue_scripts() {
301303
}
302304

303305
// Load Javascript we need to use on the configuration views (jQuery Sortable and Quick Edit)
304-
if ( $this->is_whitelisted_settings_view( $this->module->name ) ) {
306+
if ( $this->is_current_module_settings_view() ) {
305307
wp_enqueue_script( 'jquery-ui-sortable' );
306308
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 );
307309
}
308310

309311
// Custom javascript to modify the post status dropdown where it shows up
310-
if ( $this->is_whitelisted_page() ) {
312+
if ( $this->is_custom_status_view() ) {
311313
wp_enqueue_script( 'edit_flow-custom_status', $this->module_url . 'lib/custom-status.js', array( 'jquery','post' ), EDIT_FLOW_VERSION, true );
312314
wp_localize_script('edit_flow-custom_status', '__ef_localize_custom_status', array(
313315
'no_change' => esc_html__( "&mdash; No Change &mdash;", 'edit-flow' ),
@@ -319,16 +321,20 @@ function action_admin_enqueue_scripts() {
319321
'cancel' => esc_html__( 'Cancel', 'edit-flow' ),
320322
));
321323
}
324+
}
322325

323-
326+
public function enqueue_admin_styles() {
327+
if ( $this->is_custom_status_view() ) {
328+
wp_enqueue_style( 'edit_flow-custom_status', $this->module_url . 'lib/custom-status.css', false, EDIT_FLOW_VERSION, 'all' );
329+
}
324330
}
325331

326332
/**
327333
* Displays a notice to users if they have JS disabled
328334
* Javascript is needed for custom statuses to be fully functional
329335
*/
330336
function no_js_notice() {
331-
if( $this->is_whitelisted_page() ) :
337+
if ( $this->is_custom_status_view() ) :
332338
?>
333339
<style type="text/css">
334340
/* Hide post status dropdown by default in case of JS issues **/
@@ -346,12 +352,14 @@ function no_js_notice() {
346352
endif;
347353
}
348354

349-
/**
355+
/**
350356
* Check whether custom status stuff should be loaded on this page
351357
*
352358
* @todo migrate this to the base module class
353359
*/
354360
function is_whitelisted_page() {
361+
_deprecated_function( 'is_whitelisted_page', '0.9.4', 'is_custom_status_view' );
362+
355363
global $pagenow;
356364

357365
if ( !in_array( $this->get_current_post_type(), $this->get_post_types_for_module( $this->module ) ) )
@@ -381,7 +389,7 @@ function post_admin_header() {
381389
wp_get_current_user() ;
382390

383391
// 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
384-
if ( $this->is_whitelisted_page() ) {
392+
if ( $this->is_custom_status_view() ) {
385393

386394
$custom_statuses = $this->get_custom_statuses();
387395

@@ -1747,6 +1755,33 @@ public function fix_post_row_actions( $actions, $post ) {
17471755
$actions['view'] = '<a href="' . esc_url( $preview_link ) . '" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $post->post_title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';
17481756
return $actions;
17491757
}
1758+
1759+
/**
1760+
* Check whether current view is relavant to this module and whether the user has access to it
1761+
*
1762+
* @return bool
1763+
*/
1764+
public function is_custom_status_view() {
1765+
1766+
if ( $this->is_current_module_settings_view() ) {
1767+
return true;
1768+
}
1769+
1770+
if ( ! $this->is_active_view( array( 'post.php', 'edit.php', 'post-new.php', 'page.php', 'edit-pages.php', 'page-new.php' ) ) ) {
1771+
return false;
1772+
}
1773+
1774+
$post_type_obj = get_post_type_object( $this->get_current_post_type() );
1775+
1776+
if ( $post_type_obj
1777+
&& ( ! current_user_can( $post_type_obj->cap->edit_posts )
1778+
|| ! $this->is_supported_post_type() ) ) {
1779+
return false;
1780+
}
1781+
1782+
return true;
1783+
}
1784+
17501785
}
17511786

17521787
}

0 commit comments

Comments
 (0)