Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
* @since 1.0.0
*/
function perflab_add_modules_page() {
// Don't add a page if active modules are controlled programmatically.
if ( has_filter( 'perflab_active_modules' ) ) {
return false;
}

$hook_suffix = add_options_page(
__( 'Performance Modules', 'performance-lab' ),
__( 'Performance', 'performance-lab' ),
Expand All @@ -19,7 +24,9 @@ function perflab_add_modules_page() {
'perflab_render_modules_page'
);

add_action( "load-{$hook_suffix}", 'perflab_load_modules_page', 10, 0 );
if ( false !== $hook_suffix ) {
add_action( "load-{$hook_suffix}", 'perflab_load_modules_page', 10, 0 );
}

return $hook_suffix;
}
Expand Down
13 changes: 12 additions & 1 deletion load.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,25 @@ function perflab_get_module_settings() {
* @return array List of active module slugs.
*/
function perflab_get_active_modules() {
return array_keys(
$modules = array_keys(
array_filter(
perflab_get_module_settings(),
function( $module_settings ) {
return isset( $module_settings['enabled'] ) && $module_settings['enabled'];
}
)
);

/**
* Filters active modules to allow programmatically control which modules are active.
*
* @since n.e.x.t

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have the n.e.x.t convention here so far, but it's worth evaluating. Do you mind opening an issue where we can propose/discuss it? It would be important to document this somewhere eventually. We haven't published a release yet, but once we get to that, we'll need to have documentation anyway.

Maybe we could even have a npm script that replaces all n.e.x.t occurrences with a specific version.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated and added a new ticket: #90

*
* @param array An array of the currently active modules.
*/
$modules = apply_filters( 'perflab_active_modules', $modules );

return $modules;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/admin/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public function test_perflab_add_modules_page() {
$hook_suffix = perflab_add_modules_page();
$this->assertSame( get_plugin_page_hookname( PERFLAB_MODULES_SCREEN, 'options-general.php' ), $hook_suffix );
$this->assertFalse( isset( $_wp_submenu_nopriv['options-general.php'][ PERFLAB_MODULES_SCREEN ] ) );

// Reset relevant globals.
$_wp_submenu_nopriv = array();

// Does not register the page if the perflab_active_modules filter is used.
add_filter( 'perflab_active_modules', function() {} );
$hook_suffix = perflab_add_modules_page();
$this->assertFalse( $hook_suffix );
$this->assertFalse( isset( $_wp_submenu_nopriv['options-general.php'][ PERFLAB_MODULES_SCREEN ] ) );
}

public function test_perflab_load_modules_page() {
Expand Down