-
Notifications
You must be signed in to change notification settings - Fork 98
Add admin tools page #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
72f15c4
Add admin tools page
e047148
Add admin tools page
3ec4dd6
Merge branch 'trunk' into feature/add-admin-tools-page
6afe441
Address feedbacks
d2ab70f
Address feedbacks
9d56f40
Address feedbacks
30fec5e
Fix unit test
89de72c
Address feedbacks
4f89ded
Merge branch 'trunk' into feature/add-admin-tools-page
felixarntz 76108a5
add missing since annotations
jjgrainger 0737742
update tests
jjgrainger 40d63ff
fix lint issues
jjgrainger 3c28256
Merge branch 'trunk' into feature/add-admin-tools-page
jjgrainger 18f7240
update wp_object_cache usage in tests
jjgrainger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?php | ||
| /** | ||
| * Class WordPress\Plugin_Check\Admin\Admin_Page | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| namespace WordPress\Plugin_Check\Admin; | ||
|
|
||
| /** | ||
| * Class is handling admin tools page functionality. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| class Admin_Page { | ||
|
|
||
| /** | ||
| * Initializes hooks. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| public function add_hooks() { | ||
| add_action( 'admin_menu', array( $this, 'add_page' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Registers the admin page under the tools menu. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| public function add_page() { | ||
| add_management_page( | ||
| __( 'Plugin Check', 'plugin-check' ), | ||
| __( 'Plugin Check', 'plugin-check' ), | ||
| 'activate_plugins', | ||
| 'plugin-check', | ||
| array( $this, 'render_page' ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the list of plugins. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return array List of available plugins. | ||
| */ | ||
| private function get_available_plugins() { | ||
| $available_plugins = get_plugins(); | ||
|
|
||
| if ( empty( $available_plugins ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $plugin_check_base_name = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); | ||
|
|
||
| if ( isset( $available_plugins[ $plugin_check_base_name ] ) ) { | ||
| unset( $available_plugins[ $plugin_check_base_name ] ); | ||
| } | ||
|
|
||
| return $available_plugins; | ||
| } | ||
|
|
||
| /** | ||
| * Render the "Plugin Check" page. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| public function render_page() { | ||
| $available_plugins = $this->get_available_plugins(); | ||
|
|
||
| require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . '/templates/admin-page.php'; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
| /** | ||
| * Template for the Admin page. | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| ?> | ||
|
|
||
| <div class="wrap"> | ||
|
|
||
| <h1><?php esc_html_e( 'Plugin Check', 'plugin-check' ); ?></h1> | ||
|
|
||
| <div class="plugin-check-content"> | ||
|
|
||
| <?php if ( ! empty( $available_plugins ) ) { ?> | ||
|
|
||
| <form> | ||
| <h2> | ||
| <label class="title" for="plugin-check__plugins"> | ||
| <?php esc_html_e( 'Check the Plugin', 'plugin-check' ); ?> | ||
| </label> | ||
| </h2> | ||
|
|
||
| <select id="plugin-check__plugins" name="plugin_check_plugins"> | ||
| <option><?php esc_html_e( 'Select Plugin', 'plugin-check' ); ?></option> | ||
| <?php foreach ( $available_plugins as $plugin_basename => $available_plugin ) { ?> | ||
| <option value="<?php echo esc_attr( $plugin_basename ); ?>"> | ||
| <?php echo esc_html( $available_plugin['Name'] ); ?> | ||
| </option> | ||
| <?php } ?> | ||
| </select> | ||
|
|
||
| <input type="submit" value="<?php esc_attr_e( 'Check it!', 'plugin-check' ); ?>" /> | ||
| </form> | ||
|
|
||
| <?php } else { ?> | ||
|
|
||
| <h2><?php esc_html_e( 'No plugins available.', 'plugin-check' ); ?></h2> | ||
|
|
||
| <?php } ?> | ||
| </div> | ||
|
|
||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| <?php | ||
| /** | ||
| * Tests for the Admin_Page class. | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| namespace Admin; | ||
|
|
||
| use WordPress\Plugin_Check\Admin\Admin_Page; | ||
| use WP_Object_Cache; | ||
| use WP_UnitTestCase; | ||
|
|
||
| class Admin_Page_Tests extends WP_UnitTestCase { | ||
|
|
||
| protected $admin_page; | ||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
| $this->admin_page = new Admin_Page(); | ||
| } | ||
|
|
||
| public function test_add_hooks() { | ||
| $this->admin_page->add_hooks(); | ||
| $this->assertEquals( 10, has_action( 'admin_menu', array( $this->admin_page, 'add_page' ) ) ); | ||
| } | ||
|
|
||
| public function test_add_page() { | ||
| global $_parent_pages; | ||
|
|
||
| $current_screen = get_current_screen(); | ||
|
|
||
| $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) ); | ||
|
|
||
| if ( is_multisite() ) { | ||
| grant_super_admin( $admin_user ); | ||
| } | ||
|
|
||
| wp_set_current_user( $admin_user ); | ||
| set_current_screen( 'dashboard' ); | ||
|
|
||
| $this->admin_page->add_page(); | ||
|
|
||
| $parent_pages = $_parent_pages; | ||
|
|
||
| set_current_screen( $current_screen ); | ||
|
felixarntz marked this conversation as resolved.
|
||
|
|
||
| $this->assertArrayHasKey( 'plugin-check', $parent_pages ); | ||
| $this->assertEquals( 'tools.php', $parent_pages['plugin-check'] ); | ||
| } | ||
|
|
||
| public function test_render_page() { | ||
|
jjgrainger marked this conversation as resolved.
|
||
| global $wp_object_cache; | ||
|
|
||
| // Backup original plugins in the object cache. | ||
| $original_plugins = $wp_object_cache->get( 'plugins', 'plugins' ); | ||
|
|
||
| // Create the basic information required get_available_plugins. | ||
| $expected_plugins = array( | ||
| 'hello.php' => array( | ||
| 'Name' => 'Hello Dolly', | ||
| ), | ||
| 'akismet/akismet.php' => array( | ||
| 'Name' => 'Akistmet', | ||
| ), | ||
| 'Fake-plugin/load.php' => array( | ||
| 'Name' => 'Fake Plugin', | ||
| ), | ||
| ); | ||
|
|
||
| // Include the Plugin Checker plugin. | ||
| $plugin_basename = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); | ||
| $expected_plugins[ $plugin_basename ] = array( 'Name' => 'Plugin Checker' ); | ||
|
|
||
| // Set the expected plugins in the cache. | ||
| $wp_object_cache->set( 'plugins', array( '' => $expected_plugins ), 'plugins' ); | ||
|
|
||
| // Render the admin page. | ||
| ob_start(); | ||
| $this->admin_page->render_page(); | ||
| $output = ob_get_contents(); | ||
| ob_end_clean(); | ||
|
|
||
| // Restore the original cache. | ||
| $wp_object_cache->set( 'plugins', $original_plugins, 'plugins' ); | ||
|
|
||
| // Remove the plugin checker from exptected plugins for testing. | ||
| unset( $expected_plugins[ $plugin_basename ] ); | ||
|
|
||
| // Assert the Plugin Checker does not appear in the select dropdown. | ||
| $this->assertStringNotContainsString( $plugin_basename, $output ); | ||
|
|
||
| // Assert the expected plugins appear in the select dropdown. | ||
| foreach ( $expected_plugins as $plugin => $data ) { | ||
| $this->assertStringContainsString( '<option value="' . $plugin . '">', $output ); | ||
| $this->assertStringContainsString( $data['Name'], $output ); | ||
| } | ||
| } | ||
|
|
||
| public function test_render_page_with_no_plugins() { | ||
| global $wp_object_cache; | ||
|
|
||
| // Backup original plugins in the object cache. | ||
| $original_plugins = $wp_object_cache->get( 'plugins', 'plugins' ); | ||
|
|
||
| // Set the expected plugins to be empty in the cache. | ||
| $wp_object_cache->add( 'plugins', array( '' => array() ), 'plugins' ); | ||
|
|
||
| // Render the admin page. | ||
| ob_start(); | ||
| $this->admin_page->render_page(); | ||
| $output = ob_get_contents(); | ||
| ob_end_clean(); | ||
|
|
||
| // Restore the original cache. | ||
| $wp_object_cache->set( 'plugins', $original_plugins, 'plugins' ); | ||
|
|
||
| $this->assertStringContainsString( 'No plugins available.', $output ); | ||
| $this->assertStringNotContainsString( '<select id="plugin-check__plugins"', $output ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.