Skip to content
Merged
Prev Previous commit
Next Next commit
Update page slug and delete settings option
  • Loading branch information
mukeshpanchal27 committed Mar 21, 2024
commit 59c96e58a7770fa623fbf0ebb95d1ca1bd48a5c3
6 changes: 3 additions & 3 deletions admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function perflab_add_features_page() {
__( 'Performance Features', 'performance-lab' ),
__( 'Performance', 'performance-lab' ),
'manage_options',
PERFLAB_MODULES_SCREEN,
PERFLAB_SCREEN,
'perflab_render_settings_page'
);

Expand Down Expand Up @@ -117,7 +117,7 @@ function perflab_render_pointer( $pointer_id = 'perflab-admin-pointer', $args =
$args['content'] = sprintf(
/* translators: %s: settings page link */
esc_html__( 'You can now test upcoming WordPress performance features. Open %s to individually toggle the performance features.', 'performance-lab' ),
'<a href="' . esc_url( add_query_arg( 'page', PERFLAB_MODULES_SCREEN, admin_url( 'options-general.php' ) ) ) . '">' . esc_html__( 'Settings > Performance', 'performance-lab' ) . '</a>'
'<a href="' . esc_url( add_query_arg( 'page', PERFLAB_SCREEN, admin_url( 'options-general.php' ) ) ) . '">' . esc_html__( 'Settings > Performance', 'performance-lab' ) . '</a>'
);
}

Expand Down Expand Up @@ -173,7 +173,7 @@ 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_url( add_query_arg( 'page', PERFLAB_SCREEN, admin_url( 'options-general.php' ) ) ),
esc_html__( 'Settings', 'performance-lab' )
);
array_unshift( $links, $settings_link );
Expand Down
37 changes: 36 additions & 1 deletion load.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
define( 'PERFLAB_VERSION', '2.9.0' );
define( 'PERFLAB_MAIN_FILE', __FILE__ );
define( 'PERFLAB_PLUGIN_DIR_PATH', plugin_dir_path( PERFLAB_MAIN_FILE ) );
define( 'PERFLAB_MODULES_SCREEN', 'perflab-modules' );
define( 'PERFLAB_SCREEN', 'performance-lab' );

// If the constant isn't defined yet, it means the Performance Lab object cache file is not loaded.
if ( ! defined( 'PERFLAB_OBJECT_CACHE_DROPIN_VERSION' ) ) {
Expand Down Expand Up @@ -261,6 +261,41 @@ function perflab_maybe_remove_object_cache_dropin() {
}
register_deactivation_hook( __FILE__, 'perflab_maybe_remove_object_cache_dropin' );

/**
* Redirects module pages to the peformance feature page.
*
* @since n.e.x.t
*
* @global $plugin_page
*/
function perflab_no_access_redirect_module_to_peformance_feature_page() {
global $plugin_page;

if ( 'perflab-modules' !== $plugin_page ) {
return;
}

if ( current_user_can( 'manage_options' ) ) {
wp_safe_redirect(
add_query_arg( 'page', PERFLAB_SCREEN )
);
exit;
}
}
add_action( 'admin_page_access_denied', 'perflab_no_access_redirect_module_to_peformance_feature_page' );

/**
* Cleanup function to delete 'perflab_modules_settings' option if present.
*
* @since n.e.x.t
*/
function perflab_cleanup_option() {
if ( current_user_can( 'manage_options' ) && get_option( 'perflab_modules_settings' ) ) {
delete_option( 'perflab_modules_settings' );
}
}
add_action( 'admin_init', 'perflab_cleanup_option' );

// Only load admin integration when in admin.
if ( is_admin() ) {
require_once PERFLAB_PLUGIN_DIR_PATH . 'admin/load.php';
Expand Down
10 changes: 5 additions & 5 deletions tests/admin/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function test_perflab_add_features_page() {
// The default user does not have the 'manage_options' capability.
$hook_suffix = perflab_add_features_page();
$this->assertFalse( $hook_suffix );
$this->assertTrue( isset( $_wp_submenu_nopriv['options-general.php'][ PERFLAB_MODULES_SCREEN ] ) );
$this->assertTrue( isset( $_wp_submenu_nopriv['options-general.php'][ PERFLAB_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' ) );

Expand All @@ -32,8 +32,8 @@ public function test_perflab_add_features_page() {
$user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $user_id );
$hook_suffix = perflab_add_features_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 ] ) );
$this->assertSame( get_plugin_page_hookname( PERFLAB_SCREEN, 'options-general.php' ), $hook_suffix );
$this->assertFalse( isset( $_wp_submenu_nopriv['options-general.php'][ PERFLAB_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' ) );

Expand All @@ -47,13 +47,13 @@ public function test_perflab_render_settings_page() {
perflab_render_settings_page();
$output = ob_get_clean();
$this->assertStringContainsString( '<div class="wrap">', $output );
$this->assertStringNotContainsString( "<input type='hidden' name='option_page' value='" . PERFLAB_MODULES_SCREEN . "' />", $output );
$this->assertStringNotContainsString( "<input type='hidden' name='option_page' value='" . PERFLAB_SCREEN . "' />", $output );
}

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>',
'<a href="' . admin_url( '/' ) . 'options-general.php?page=' . PERFLAB_SCREEN . '">Settings</a>',
$original_links[0],
);

Expand Down