From 5212be0003c1f86f66304fec0c96c4537fa98a84 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Wed, 2 Oct 2024 18:41:11 +0200 Subject: [PATCH 1/5] first attempt to multisite --- includes/Admin/Admin_Page.php | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index 24736828b..28b7b4c79 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -54,8 +54,11 @@ public function __construct( Admin_AJAX $admin_ajax ) { * @since 1.0.0 */ public function add_hooks() { - add_action( 'admin_menu', array( $this, 'add_and_initialize_page' ) ); - add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 4 ); + $admin_menu_hook = is_multisite() ? 'network_admin_menu' : 'admin_menu'; + $plugin_action_link_hook = is_multisite() ? 'network_admin_plugin_action_links' : 'plugin_action_links'; + + add_action( $admin_menu_hook, array( $this, 'add_and_initialize_page' ) ); + add_filter( $plugin_action_link_hook, array( $this, 'filter_plugin_action_links' ), 10, 4 ); add_action( 'admin_enqueue_scripts', array( $this, 'add_jump_to_line_code_editor' ) ); $this->admin_ajax->add_hooks(); @@ -64,16 +67,28 @@ public function add_hooks() { /** * Adds the admin page under the tools menu. * + * @since n.e.x.t Added multisite support. * @since 1.0.0 */ public function add_page() { - $this->hook_suffix = add_management_page( - __( 'Plugin Check', 'plugin-check' ), - __( 'Plugin Check', 'plugin-check' ), - 'activate_plugins', - 'plugin-check', - array( $this, 'render_page' ) - ); + if ( is_multisite() ) { + $this->hook_suffix = add_submenu_page( + 'settings.php', + __( 'Plugin Check', 'plugin-check' ), + __( 'Plugin Check', 'plugin-check' ), + 'manage_network_plugins', + 'plugin-check', + array( $this, 'render_page' ) + ); + } else { + $this->hook_suffix = add_management_page( + __( 'Plugin Check', 'plugin-check' ), + __( 'Plugin Check', 'plugin-check' ), + 'activate_plugins', + 'plugin-check', + array( $this, 'render_page' ) + ); + } } /** @@ -307,7 +322,7 @@ public function filter_plugin_action_links( $actions, $plugin_file, $plugin_data return $actions; } - if ( current_user_can( 'activate_plugins' ) ) { + if ( ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) || current_user_can( 'activate_plugins' ) ) { $actions[] = sprintf( '%2$s', esc_url( admin_url( "tools.php?page=plugin-check&plugin={$plugin_file}" ) ), From ae9f28094607a6fa95f753a317f3e782c2dad4df Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Wed, 2 Oct 2024 20:27:03 +0200 Subject: [PATCH 2/5] fix link in network admin --- includes/Admin/Admin_Page.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index 28b7b4c79..77501a5e5 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -54,7 +54,7 @@ public function __construct( Admin_AJAX $admin_ajax ) { * @since 1.0.0 */ public function add_hooks() { - $admin_menu_hook = is_multisite() ? 'network_admin_menu' : 'admin_menu'; + $admin_menu_hook = is_multisite() ? 'network_admin_menu' : 'admin_menu'; $plugin_action_link_hook = is_multisite() ? 'network_admin_plugin_action_links' : 'plugin_action_links'; add_action( $admin_menu_hook, array( $this, 'add_and_initialize_page' ) ); @@ -322,7 +322,13 @@ public function filter_plugin_action_links( $actions, $plugin_file, $plugin_data return $actions; } - if ( ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) || current_user_can( 'activate_plugins' ) ) { + if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) { + $actions[] = sprintf( + '%2$s', + esc_url( network_admin_url( "settings.php?page=plugin-check&plugin={$plugin_file}" ) ), + esc_html__( 'Check this plugin', 'plugin-check' ) + ); + } elseif ( current_user_can( 'activate_plugins' ) ) { $actions[] = sprintf( '%2$s', esc_url( admin_url( "tools.php?page=plugin-check&plugin={$plugin_file}" ) ), From e8c8f821e17038af1f9ebf1f8ca0c62685799e24 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Wed, 2 Oct 2024 20:31:19 +0200 Subject: [PATCH 3/5] less strings --- includes/Admin/Admin_Page.php | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index 77501a5e5..668de860a 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -71,24 +71,17 @@ public function add_hooks() { * @since 1.0.0 */ public function add_page() { - if ( is_multisite() ) { - $this->hook_suffix = add_submenu_page( - 'settings.php', - __( 'Plugin Check', 'plugin-check' ), - __( 'Plugin Check', 'plugin-check' ), - 'manage_network_plugins', - 'plugin-check', - array( $this, 'render_page' ) - ); - } else { - $this->hook_suffix = add_management_page( - __( 'Plugin Check', 'plugin-check' ), - __( 'Plugin Check', 'plugin-check' ), - 'activate_plugins', - 'plugin-check', - array( $this, 'render_page' ) - ); - } + $admin_parent_page = is_multisite() ? 'settings.php' : 'tools.php'; + $capabilities = is_multisite() ? 'manage_network_plugins' : 'activate_plugins'; + + $this->hook_suffix = add_submenu_page( + $admin_parent_page, + __( 'Plugin Check', 'plugin-check' ), + __( 'Plugin Check', 'plugin-check' ), + $capabilities, + 'plugin-check', + array( $this, 'render_page' ) + ); } /** From 21a8df17c9b1fddeb386e0d384593815012780f0 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Wed, 2 Oct 2024 20:39:31 +0200 Subject: [PATCH 4/5] fixed tests --- tests/phpunit/tests/Admin/Admin_Page_Tests.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/Admin/Admin_Page_Tests.php b/tests/phpunit/tests/Admin/Admin_Page_Tests.php index d7c4e011d..260495210 100644 --- a/tests/phpunit/tests/Admin/Admin_Page_Tests.php +++ b/tests/phpunit/tests/Admin/Admin_Page_Tests.php @@ -35,8 +35,10 @@ public function test_add_and_initialize_page() { $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $expected_parent_page = 'tools.php'; if ( is_multisite() ) { grant_super_admin( $admin_user ); + $expected_parent_page = 'settings.php'; } wp_set_current_user( $admin_user ); @@ -50,7 +52,7 @@ public function test_add_and_initialize_page() { set_current_screen( $current_screen ); $this->assertArrayHasKey( 'plugin-check', $parent_pages ); - $this->assertEquals( 'tools.php', $parent_pages['plugin-check'] ); + $this->assertEquals( $expected_parent_page, $parent_pages['plugin-check'] ); $this->assertNotFalse( has_action( "load-{$page_hook}", array( $this->admin_page, 'initialize_page' ) ) ); } From 8fbf6705632eb53c664bca6d5d8d438811c8c9cb Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Wed, 2 Oct 2024 20:43:59 +0200 Subject: [PATCH 5/5] fix tests hooks --- tests/phpunit/tests/Admin/Admin_Page_Tests.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/Admin/Admin_Page_Tests.php b/tests/phpunit/tests/Admin/Admin_Page_Tests.php index 260495210..5777694ef 100644 --- a/tests/phpunit/tests/Admin/Admin_Page_Tests.php +++ b/tests/phpunit/tests/Admin/Admin_Page_Tests.php @@ -23,8 +23,11 @@ public function set_up() { public function test_add_hooks() { $this->admin_page->add_hooks(); - $this->assertEquals( 10, has_action( 'admin_menu', array( $this->admin_page, 'add_and_initialize_page' ) ) ); - $this->assertEquals( 10, has_filter( 'plugin_action_links', array( $this->admin_page, 'filter_plugin_action_links' ) ) ); + $admin_menu_hook = is_multisite() ? 'network_admin_menu' : 'admin_menu'; + $plugin_action_link_hook = is_multisite() ? 'network_admin_plugin_action_links' : 'plugin_action_links'; + + $this->assertEquals( 10, has_action( $admin_menu_hook, array( $this->admin_page, 'add_and_initialize_page' ) ) ); + $this->assertEquals( 10, has_filter( $plugin_action_link_hook, array( $this->admin_page, 'filter_plugin_action_links' ) ) ); } public function test_add_and_initialize_page() {