From cdd9afe945a7afe1b1cb3e269796bff978f79c41 Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Thu, 23 Feb 2023 17:48:15 +0530 Subject: [PATCH 1/5] Add 'check this plugin' link on the plugin list table --- includes/Admin/Admin_Page.php | 24 ++++++++++++++++++++++++ templates/admin-page.php | 2 +- tests/Admin/Admin_Page_Tests.php | 21 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index 3d84b822b..cb0c12eb0 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -19,6 +19,7 @@ class Admin_Page { */ public function add_hooks() { add_action( 'admin_menu', array( $this, 'add_page' ) ); + add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 2 ); } /** @@ -63,6 +64,29 @@ public function render_page() { $available_plugins = $this->get_available_plugins(); + $selected_plugin_basename = filter_input( INPUT_GET, 'plugin', FILTER_SANITIZE_STRING ); + require_once WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . '/templates/admin-page.php'; } + + /** + * Add "check this plugin" link in the plugins list table. + * + * @param array $actions List of actions. + * @param string $plugin_file Plugin main file. + * @return array + */ + public function filter_plugin_action_links( $actions, $plugin_file ) { + + if ( is_plugin_active( $plugin_file ) ) { + + $actions[] = sprintf( + '%2$s', + esc_url( admin_url() . 'tools.php?page=plugin-check&plugin=' . $plugin_file ), + esc_html__( 'Check this plugin', 'plugin-check' ) + ); + } + + return $actions; + } } diff --git a/templates/admin-page.php b/templates/admin-page.php index 009312801..755e42128 100644 --- a/templates/admin-page.php +++ b/templates/admin-page.php @@ -25,7 +25,7 @@ $available_plugin ) { ?> - diff --git a/tests/Admin/Admin_Page_Tests.php b/tests/Admin/Admin_Page_Tests.php index 1945d1d9d..507f90013 100644 --- a/tests/Admin/Admin_Page_Tests.php +++ b/tests/Admin/Admin_Page_Tests.php @@ -67,9 +67,17 @@ public function test_render_page() { public function test_filter_plugin_action_links() { - $action_links = $this->admin_page->filter_plugin_action_links( array(), 'test-plugin/test-plugin.php' ); + $current_screen = get_current_screen(); + + $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) ); + + if ( is_multisite() ) { + grant_super_admin( $admin_user ); + } - $this->assertEmpty( $action_links ); + wp_set_current_user( $admin_user ); + + $this->admin_page->add_page(); $base_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); From 1002b940a14b83438e24eec7f59f81aa4b08c3fa Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Fri, 24 Feb 2023 18:32:11 +0530 Subject: [PATCH 3/5] Address feedbacks --- tests/Admin/Admin_Page_Tests.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/Admin/Admin_Page_Tests.php b/tests/Admin/Admin_Page_Tests.php index 507f90013..adcd8cad3 100644 --- a/tests/Admin/Admin_Page_Tests.php +++ b/tests/Admin/Admin_Page_Tests.php @@ -67,20 +67,21 @@ public function test_render_page() { public function test_filter_plugin_action_links() { - $current_screen = get_current_screen(); + $base_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); + /** Editor check */ + $editor = self::factory()->user->create( array( 'role' => 'editor' ) ); + wp_set_current_user( $editor ); + $action_links = $this->admin_page->filter_plugin_action_links( array(), $base_file ); + $this->assertEmpty( $action_links ); + + /** Administrator check */ $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) ); if ( is_multisite() ) { grant_super_admin( $admin_user ); } - wp_set_current_user( $admin_user ); - - $this->admin_page->add_page(); - - $base_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); - $action_links = $this->admin_page->filter_plugin_action_links( array(), $base_file ); $this->assertEquals( From d04bf75304c169346c13f27454dfe06537efe625 Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Fri, 24 Feb 2023 18:38:56 +0530 Subject: [PATCH 4/5] Add since in docblock --- includes/Admin/Admin_Page.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index c1b89c55f..e5f1d75e1 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -72,6 +72,8 @@ public function render_page() { /** * Add "check this plugin" link in the plugins list table. * + * @since n.e.x.t + * * @param array $actions List of actions. * @param string $plugin_file Plugin main file. * @return array From dac021c25fa580a25ec8a4dc343ccb336afcf70c Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Sat, 25 Feb 2023 11:01:20 +0530 Subject: [PATCH 5/5] Address feedbacks --- includes/Admin/Admin_Page.php | 4 ++-- tests/Admin/Admin_Page_Tests.php | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index f7e2f72a1..c2b40c008 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -76,13 +76,13 @@ public function render_page() { } /** - * Add "check this plugin" link in the plugins list table. + * Adds "check this plugin" link in the plugins list table. * * @since n.e.x.t * * @param array $actions List of actions. * @param string $plugin_file Plugin main file. - * @return array + * @return array The modified list of actions. */ public function filter_plugin_action_links( $actions, $plugin_file ) { diff --git a/tests/Admin/Admin_Page_Tests.php b/tests/Admin/Admin_Page_Tests.php index cefc69d1a..79279503b 100644 --- a/tests/Admin/Admin_Page_Tests.php +++ b/tests/Admin/Admin_Page_Tests.php @@ -124,9 +124,6 @@ public function test_filter_plugin_action_links() { $base_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); - /** Editor check */ - $editor = self::factory()->user->create( array( 'role' => 'editor' ) ); - wp_set_current_user( $editor ); $action_links = $this->admin_page->filter_plugin_action_links( array(), $base_file ); $this->assertEmpty( $action_links );