Skip to content

Commit a1ca4ff

Browse files
authored
Merge pull request #87 from WordPress/feature/modules-control-filter
Allow programmatic control of active modules via filter
2 parents 1eeb19d + ea34db7 commit a1ca4ff

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎admin/load.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
* @since 1.0.0
1212
*/
1313
function perflab_add_modules_page() {
14+
// Don't add a page if active modules are controlled programmatically.
15+
if ( has_filter( 'perflab_active_modules' ) ) {
16+
return false;
17+
}
18+
1419
$hook_suffix = add_options_page(
1520
__( 'Performance Modules', 'performance-lab' ),
1621
__( 'Performance', 'performance-lab' ),
@@ -19,7 +24,9 @@ function perflab_add_modules_page() {
1924
'perflab_render_modules_page'
2025
);
2126

22-
add_action( "load-{$hook_suffix}", 'perflab_load_modules_page', 10, 0 );
27+
if ( false !== $hook_suffix ) {
28+
add_action( "load-{$hook_suffix}", 'perflab_load_modules_page', 10, 0 );
29+
}
2330

2431
return $hook_suffix;
2532
}

‎load.php‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,25 @@ function perflab_get_module_settings() {
8383
* @return array List of active module slugs.
8484
*/
8585
function perflab_get_active_modules() {
86-
return array_keys(
86+
$modules = array_keys(
8787
array_filter(
8888
perflab_get_module_settings(),
8989
function( $module_settings ) {
9090
return isset( $module_settings['enabled'] ) && $module_settings['enabled'];
9191
}
9292
)
9393
);
94+
95+
/**
96+
* Filters active modules to allow programmatically control which modules are active.
97+
*
98+
* @since 1.0.0
99+
*
100+
* @param array An array of the currently active modules.
101+
*/
102+
$modules = apply_filters( 'perflab_active_modules', $modules );
103+
104+
return $modules;
94105
}
95106

96107
/**

‎tests/admin/load-tests.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public function test_perflab_add_modules_page() {
6969
$hook_suffix = perflab_add_modules_page();
7070
$this->assertSame( get_plugin_page_hookname( PERFLAB_MODULES_SCREEN, 'options-general.php' ), $hook_suffix );
7171
$this->assertFalse( isset( $_wp_submenu_nopriv['options-general.php'][ PERFLAB_MODULES_SCREEN ] ) );
72+
73+
// Reset relevant globals.
74+
$_wp_submenu_nopriv = array();
75+
76+
// Does not register the page if the perflab_active_modules filter is used.
77+
add_filter( 'perflab_active_modules', function() {} );
78+
$hook_suffix = perflab_add_modules_page();
79+
$this->assertFalse( $hook_suffix );
80+
$this->assertFalse( isset( $_wp_submenu_nopriv['options-general.php'][ PERFLAB_MODULES_SCREEN ] ) );
7281
}
7382

7483
public function test_perflab_load_modules_page() {

0 commit comments

Comments
 (0)