Skip to content
74 changes: 74 additions & 0 deletions includes/Admin/Admin_Page.php
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() {
Comment thread
jjgrainger marked this conversation as resolved.
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';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function prepare() {
add_filter( 'pre_option_template_root', array( $this, 'get_theme_root' ) );
add_filter( 'pre_option_stylesheet_root', array( $this, 'get_theme_root' ) );

// Register the custom themes directory if relevant.
// Registers the custom themes directory if relevant.
if ( ! empty( $this->themes_dir ) ) {
register_theme_directory( $this->themes_dir );

Expand Down
5 changes: 5 additions & 0 deletions includes/Plugin_Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace WordPress\Plugin_Check;

use WordPress\Plugin_Check\Admin\Admin_Page;

/**
* Main class for the plugin.
*
Expand Down Expand Up @@ -52,5 +54,8 @@ public function context() {
*/
public function add_hooks() {
// @TODO: Update to register CLI command to WordPress as part of issue #30

$admin_page = new Admin_Page();
$admin_page->add_hooks();
}
}
44 changes: 44 additions & 0 deletions templates/admin-page.php
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>
121 changes: 121 additions & 0 deletions tests/Admin/Admin_Page_Tests.php
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 );
Comment thread
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() {
Comment thread
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 );
}
}