Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 27 additions & 3 deletions admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ function perflab_add_modules_page() {
'perflab_render_modules_page'
);

// Add the following hooks only if the screen was successfully added.
if ( false !== $hook_suffix ) {
add_action( "load-{$hook_suffix}", 'perflab_load_modules_page', 10, 0 );
add_action( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ), 'perflab_plugin_action_links_add_settings' );
}

return $hook_suffix;
Expand Down Expand Up @@ -338,7 +340,30 @@ function perflab_get_module_data( $module_file ) {
}

/**
* Enable all non-experimental modules on plugin activation.
* Adds a link to the modules page to the plugin's entry in the plugins list table.
*
* This function is only used if the modules page exists and is accessible.
*
* @since 1.0.0
* @see perflab_add_modules_page()
*
* @param array $links List of plugin action links HTML.
* @return array Modified list of plugin action links HTML.
*/
function perflab_plugin_action_links_add_settings( $links ) {
// Add link as the first plugin action link.
$settings_link = sprintf(
'<a href="%s">%s</a>',
esc_url( add_query_arg( 'page', PERFLAB_MODULES_SCREEN, admin_url( 'options-general.php' ) ) ),
esc_html__( 'Settings', 'performance-lab' )
);
array_unshift( $links, $settings_link );

return $links;
}

/**
* Enables all non-experimental modules on plugin activation.
*
* @since 1.0.0
*/
Expand All @@ -359,5 +384,4 @@ function perflab_activation_hook() {

update_option( PERFLAB_MODULES_SETTING, $modules_settings );
}

register_activation_hook( dirname( __DIR__ ) . '/load.php', 'perflab_activation_hook' );
register_activation_hook( PERFLAB_MAIN_FILE, 'perflab_activation_hook' );
1 change: 1 addition & 0 deletions load.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

define( 'PERFLAB_VERSION', '1.0.0-beta.1' );
define( 'PERFLAB_MAIN_FILE', __FILE__ );
define( 'PERFLAB_MODULES_SETTING', 'perflab_modules_settings' );
define( 'PERFLAB_MODULES_SCREEN', 'perflab-modules' );

Expand Down
26 changes: 23 additions & 3 deletions tests/admin/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,41 @@ class Admin_Load_Tests extends WP_UnitTestCase {
public function test_perflab_add_modules_page() {
global $_wp_submenu_nopriv;

// Reset relevant globals.
// Reset relevant globals and filters.
$_wp_submenu_nopriv = array();
remove_all_filters( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ) );

// The default user does not have the 'manage_options' capability.
$hook_suffix = perflab_add_modules_page();
$this->assertFalse( $hook_suffix );
$this->assertTrue( isset( $_wp_submenu_nopriv['options-general.php'][ PERFLAB_MODULES_SCREEN ] ) );
// Ensure plugin action link is not added.
$this->assertFalse( (bool) has_action( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ), 'perflab_plugin_action_links_add_settings' ) );

// Reset relevant globals.
// Reset relevant globals and filters.
$_wp_submenu_nopriv = array();
remove_all_filters( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ) );

// Rely on current user to be an administrator (with 'manage_options' capability).
$user_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $user_id );
$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 ] ) );
// Ensure plugin action link is added.
$this->assertTrue( (bool) has_action( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ), 'perflab_plugin_action_links_add_settings' ) );

// Reset relevant globals.
// Reset relevant globals and filters.
$_wp_submenu_nopriv = array();
remove_all_filters( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ) );

// 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 ] ) );
// Ensure plugin action link is not added.
$this->assertFalse( (bool) has_action( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ), 'perflab_plugin_action_links_add_settings' ) );
}

public function test_perflab_load_modules_page() {
Expand Down Expand Up @@ -199,4 +208,15 @@ public function test_perflab_get_module_data() {
$this->assertSame( $expected_module_data, $module_data );
}
}

public function test_perflab_plugin_action_links_add_settings() {
$original_links = array( '<a href="https://wordpress.org">wordpress.org</a>' );
$expected_links = array(
'<a href="' . admin_url( '/' ) . 'options-general.php?page=' . PERFLAB_MODULES_SCREEN . '">Settings</a>',
$original_links[0],
);

$actual_links = perflab_plugin_action_links_add_settings( $original_links );
$this->assertSame( $expected_links, $actual_links );
}
}