Skip to content

Commit e025dd0

Browse files
GaryJonesclaude
andcommitted
fix: scope module asset loading to post management pages
Module assets (CSS/JS) were loading on every admin page because is_whitelisted_functional_view() always returned true. This was wasteful and caused unnecessary resource loading. Implement is_post_management_page() to properly check if we're on a post management page (post.php, post-new.php, edit.php) before loading module assets. Optionally validates the current post type is supported by the module. Closes #351 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 88befb7 commit e025dd0

File tree

5 files changed

+246
-9
lines changed

5 files changed

+246
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
88

99
### Fixed
1010

11+
* fix: scope module asset loading to relevant pages only by @GaryJones in [#858](https://github.com/Automattic/Edit-Flow/pull/858)
1112
* fix: allow text selection in calendar overlay without triggering drag by @GaryJones in [#857](https://github.com/Automattic/Edit-Flow/pull/857)
1213
* fix: update post date to current time when publishing from custom status by @GaryJones in [#856](https://github.com/Automattic/Edit-Flow/pull/856)
1314
* fix: resolve calendar drag-and-drop not persisting post date changes by @GaryJones in [#854](https://github.com/Automattic/Edit-Flow/pull/854)

common/php/class-module.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,16 +372,44 @@ protected function print_ajax_response( $status, $message = '', $http_code = 200
372372
}
373373

374374
/**
375-
* Whether or not the current page is a user-facing Edit Flow View
376-
* @todo Think of a creative way to make this work
375+
* Whether or not the current page is a post management page.
376+
*
377+
* A post management page is where the module's functionality is actually
378+
* needed, such as post editing pages (post.php, post-new.php) or post listing
379+
* pages (edit.php) for supported post types.
377380
*
378381
* @since 0.7
382+
* @since 0.10.0 Actually implemented instead of returning true. Renamed from
383+
* is_whitelisted_functional_view().
379384
*
380-
* @param string $module_name (Optional) Module name to check against
385+
* @see https://github.com/Automattic/Edit-Flow/issues/351
386+
*
387+
* @param string $module_name (Optional) Module name to check against.
388+
* @return bool Whether the current page is a post management page for the module.
381389
*/
382-
public function is_whitelisted_functional_view( $module_name = null ) {
390+
public function is_post_management_page( $module_name = null ) {
391+
global $pagenow, $edit_flow;
392+
393+
// Only load on post editing and listing pages.
394+
$functional_pages = [ 'post.php', 'post-new.php', 'edit.php' ];
395+
if ( ! in_array( $pagenow, $functional_pages, true ) ) {
396+
return false;
397+
}
398+
399+
// Get the current post type.
400+
$current_post_type = $this->get_current_post_type();
401+
if ( ! $current_post_type ) {
402+
return false;
403+
}
383404

384-
// @todo complete this method
405+
// If a module name is specified, check if this post type is supported by that module.
406+
if ( $module_name && isset( $edit_flow->modules->$module_name ) ) {
407+
$module = $edit_flow->modules->$module_name;
408+
$supported_post_types = $this->get_post_types_for_module( $module );
409+
if ( ! in_array( $current_post_type, $supported_post_types, true ) ) {
410+
return false;
411+
}
412+
}
385413

386414
return true;
387415
}

modules/notifications/notifications.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function register_taxonomies() {
194194
public function enqueue_admin_scripts() {
195195
global $post;
196196

197-
if ( $this->is_whitelisted_functional_view() ) {
197+
if ( $this->is_post_management_page( $this->module->name ) ) {
198198
wp_enqueue_script( 'jquery-listfilterizer' );
199199
wp_enqueue_script( 'edit-flow-notifications-js', $this->module_url . 'lib/notifications.js', [ 'jquery', 'jquery-listfilterizer' ], EDIT_FLOW_VERSION, true );
200200

@@ -232,7 +232,7 @@ public function enqueue_admin_scripts() {
232232
*/
233233
public function enqueue_admin_styles() {
234234

235-
if ( $this->is_whitelisted_functional_view() || $this->is_whitelisted_settings_view() ) {
235+
if ( $this->is_post_management_page( $this->module->name ) || $this->is_whitelisted_settings_view( $this->module->name ) ) {
236236
wp_enqueue_style( 'jquery-listfilterizer' );
237237
wp_enqueue_style( 'edit-flow-notifications-css', $this->module->module_url . 'lib/notifications.css', false, EDIT_FLOW_VERSION );
238238
}

modules/user-groups/user-groups.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function register_usergroup_objects() {
229229
*/
230230
public function enqueue_admin_scripts() {
231231

232-
if ( $this->is_whitelisted_functional_view() || $this->is_whitelisted_settings_view( $this->module->name ) ) {
232+
if ( $this->is_post_management_page( $this->module->name ) || $this->is_whitelisted_settings_view( $this->module->name ) ) {
233233
wp_enqueue_script( 'jquery-listfilterizer' );
234234
wp_enqueue_script( 'edit-flow-user-groups-js', $this->module_url . 'lib/user-groups.js', array( 'jquery', 'jquery-listfilterizer' ), EDIT_FLOW_VERSION, true );
235235
}
@@ -249,7 +249,7 @@ public function enqueue_admin_scripts() {
249249
public function enqueue_admin_styles() {
250250

251251

252-
if ( $this->is_whitelisted_functional_view() || $this->is_whitelisted_settings_view() ) {
252+
if ( $this->is_post_management_page( $this->module->name ) || $this->is_whitelisted_settings_view( $this->module->name ) ) {
253253
wp_enqueue_style( 'jquery-listfilterizer' );
254254
wp_enqueue_style( 'edit-flow-user-groups-css', $this->module_url . 'lib/user-groups.css', false, EDIT_FLOW_VERSION );
255255
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<?php
2+
/**
3+
* Tests for scoped module asset loading.
4+
*
5+
* @package Automattic\EditFlow\Tests\Integration
6+
* @see https://github.com/Automattic/edit-flow/issues/351
7+
*/
8+
9+
declare( strict_types=1 );
10+
11+
namespace Automattic\EditFlow\Tests\Integration;
12+
13+
use EF_Module;
14+
use Yoast\WPTestUtils\WPIntegration\TestCase;
15+
16+
/**
17+
* Test that is_post_management_page() properly scopes asset loading.
18+
*
19+
* Module assets should only load on pages where the module functionality is needed,
20+
* not on every admin page.
21+
*/
22+
class ModuleAssetScopingTest extends TestCase {
23+
24+
protected static $admin_user_id;
25+
26+
public static function wpSetUpBeforeClass( $factory ) {
27+
self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );
28+
}
29+
30+
public static function wpTearDownAfterClass() {
31+
self::delete_user( self::$admin_user_id );
32+
}
33+
34+
protected function setUp(): void {
35+
parent::setUp();
36+
wp_set_current_user( self::$admin_user_id );
37+
}
38+
39+
/**
40+
* Get a module instance for testing.
41+
*
42+
* @return EF_Module
43+
*/
44+
protected function get_test_module() {
45+
global $edit_flow;
46+
// Use notifications module as test subject since it uses is_post_management_page().
47+
return $edit_flow->notifications;
48+
}
49+
50+
/**
51+
* Test that is_post_management_page returns false on dashboard.
52+
*/
53+
public function test_is_post_management_page_returns_false_on_dashboard() {
54+
global $pagenow;
55+
$pagenow = 'index.php';
56+
57+
$module = $this->get_test_module();
58+
$this->assertFalse(
59+
$module->is_post_management_page(),
60+
'is_post_management_page() should return false on dashboard'
61+
);
62+
}
63+
64+
/**
65+
* Test that is_post_management_page returns false on options page.
66+
*/
67+
public function test_is_post_management_page_returns_false_on_options() {
68+
global $pagenow;
69+
$pagenow = 'options-general.php';
70+
71+
$module = $this->get_test_module();
72+
$this->assertFalse(
73+
$module->is_post_management_page(),
74+
'is_post_management_page() should return false on options page'
75+
);
76+
}
77+
78+
/**
79+
* Test that is_post_management_page returns false on plugins page.
80+
*/
81+
public function test_is_post_management_page_returns_false_on_plugins() {
82+
global $pagenow;
83+
$pagenow = 'plugins.php';
84+
85+
$module = $this->get_test_module();
86+
$this->assertFalse(
87+
$module->is_post_management_page(),
88+
'is_post_management_page() should return false on plugins page'
89+
);
90+
}
91+
92+
/**
93+
* Test that is_post_management_page returns false on users page.
94+
*/
95+
public function test_is_post_management_page_returns_false_on_users() {
96+
global $pagenow;
97+
$pagenow = 'users.php';
98+
99+
$module = $this->get_test_module();
100+
$this->assertFalse(
101+
$module->is_post_management_page(),
102+
'is_post_management_page() should return false on users page'
103+
);
104+
}
105+
106+
/**
107+
* Test that is_post_management_page returns true on post edit page.
108+
*/
109+
public function test_is_post_management_page_returns_true_on_post_edit() {
110+
global $pagenow, $typenow;
111+
$pagenow = 'post.php';
112+
$typenow = 'post';
113+
114+
$module = $this->get_test_module();
115+
$this->assertTrue(
116+
$module->is_post_management_page(),
117+
'is_post_management_page() should return true on post.php'
118+
);
119+
}
120+
121+
/**
122+
* Test that is_post_management_page returns true on new post page.
123+
*/
124+
public function test_is_post_management_page_returns_true_on_post_new() {
125+
global $pagenow, $typenow;
126+
$pagenow = 'post-new.php';
127+
$typenow = 'post';
128+
129+
$module = $this->get_test_module();
130+
$this->assertTrue(
131+
$module->is_post_management_page(),
132+
'is_post_management_page() should return true on post-new.php'
133+
);
134+
}
135+
136+
/**
137+
* Test that is_post_management_page returns true on posts list page.
138+
*/
139+
public function test_is_post_management_page_returns_true_on_edit() {
140+
global $pagenow, $typenow;
141+
$pagenow = 'edit.php';
142+
$typenow = 'post';
143+
144+
$module = $this->get_test_module();
145+
$this->assertTrue(
146+
$module->is_post_management_page(),
147+
'is_post_management_page() should return true on edit.php'
148+
);
149+
}
150+
151+
/**
152+
* Test that is_post_management_page returns true on page edit page.
153+
*/
154+
public function test_is_post_management_page_returns_true_on_page_edit() {
155+
global $pagenow, $typenow;
156+
$pagenow = 'post.php';
157+
$typenow = 'page';
158+
159+
$module = $this->get_test_module();
160+
$this->assertTrue(
161+
$module->is_post_management_page(),
162+
'is_post_management_page() should return true on post.php for pages'
163+
);
164+
}
165+
166+
/**
167+
* Test that is_post_management_page respects module-specific post types.
168+
*/
169+
public function test_is_post_management_page_respects_module_post_types() {
170+
global $pagenow, $typenow, $edit_flow;
171+
172+
// Register a custom post type that's NOT supported by the module.
173+
register_post_type( 'unsupported_cpt', array( 'public' => true ) );
174+
175+
$pagenow = 'post.php';
176+
$typenow = 'unsupported_cpt';
177+
178+
$module = $this->get_test_module();
179+
180+
// When passing module name, it should check if post type is supported.
181+
$result = $module->is_post_management_page( $module->module->name );
182+
183+
// Clean up.
184+
unregister_post_type( 'unsupported_cpt' );
185+
186+
$this->assertFalse(
187+
$result,
188+
'is_post_management_page() should return false for unsupported post types when module name is passed'
189+
);
190+
}
191+
192+
/**
193+
* Test that is_post_management_page without module name still works on supported post types.
194+
*/
195+
public function test_is_post_management_page_without_module_name() {
196+
global $pagenow, $typenow;
197+
$pagenow = 'post.php';
198+
$typenow = 'post';
199+
200+
$module = $this->get_test_module();
201+
202+
// Without module name, it should just check the page type.
203+
$this->assertTrue(
204+
$module->is_post_management_page(),
205+
'is_post_management_page() without module name should return true on post edit pages'
206+
);
207+
}
208+
}

0 commit comments

Comments
 (0)