diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js new file mode 100644 index 000000000..2b76327a1 --- /dev/null +++ b/assets/js/plugin-check-admin.js @@ -0,0 +1,65 @@ +( function ( data ) { + const checkItButton = document.getElementById( 'plugin-check__submit' ); + const pluginsList = document.getElementById( 'plugin-check__plugins-dropdown' ); + + // Return early if the elements cannot be found on the page. + if ( ! checkItButton || ! pluginsList ) { + console.error( 'Missing form elements on page' ); + return; + } + + checkItButton.addEventListener( 'click', (e) => { + e.preventDefault(); + + const pluginCheckData = new FormData(); + + // Collect the data to pass along for generating a check results. + pluginCheckData.append( 'action', 'plugin_check_run_checks' ); + pluginCheckData.append( 'nonce', data.nonce ); + pluginCheckData.append( 'plugin', pluginsList.value ); + + fetch( + ajaxurl, + { + method: 'POST', + credentials: 'same-origin', + body: pluginCheckData + } + ) + .then( + ( response ) => { + return response.json(); + } + ) + .then( + ( data ) => { + if ( ! data ) { + throw new Error( 'Response contains no data' ); + } + + if ( ! data.success ) { + // // If not successful and no message in the response. + if ( ! data.data || ! data.data[0].message ) { + throw new Error( 'Response contains no data' ); + } + + // If not successful and there is a message in the response. + throw new Error( data.data[0].message ); + } + + // If the response is successful and there is no message in the response. + if ( ! data.data || ! data.data.message ) { + throw new Error( 'Response contains no data' ); + } + + // If the response is successful and there is a message in the response. + console.log( data.data.message ); + } + ) + .catch( + ( error ) => { console.error( error ); } + ); + + } ); + +} )( PLUGIN_CHECK ); /* global PLUGIN_CHECK */ diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php new file mode 100644 index 000000000..2fa7200ce --- /dev/null +++ b/includes/Admin/Admin_AJAX.php @@ -0,0 +1,66 @@ + __( 'Verified!', 'plugin-check' ), + ) + ); + } +} diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index c2b40c008..c16a3e6cc 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -15,28 +15,87 @@ class Admin_Page { /** - * Initializes hooks. + * Admin AJAX class instance. + * + * @since n.e.x.t + * @var Admin_AJAX + */ + protected $admin_ajax; + + /** + * Constructor. + * + * @since n.e.x.t + */ + public function __construct() { + $this->admin_ajax = new Admin_AJAX(); + } + + /** + * Registers WordPress hooks for the admin page. * * @since n.e.x.t */ 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 ); + + $this->admin_ajax->add_hooks(); } /** * Registers the admin page under the tools menu. * * @since n.e.x.t + * + * @return string The hook identifier for the admin page. */ public function add_page() { - add_management_page( + $hook = add_management_page( __( 'Plugin Check', 'plugin-check' ), __( 'Plugin Check', 'plugin-check' ), 'activate_plugins', 'plugin-check', array( $this, 'render_page' ) ); + + add_action( "load-{$hook}", array( $this, 'initialize_page' ) ); + + return $hook; + } + + /** + * Initializes page hooks. + * + * @since n.e.x.t + */ + public function initialize_page() { + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + } + + /** + * Loads the check's script. + * + * @since n.e.x.t + */ + public function enqueue_scripts() { + wp_enqueue_script( + 'plugin-check-admin', + WP_PLUGIN_CHECK_PLUGIN_DIR_URL . 'assets/js/plugin-check-admin.js', + array(), + WP_PLUGIN_CHECK_VERSION, + true + ); + + wp_add_inline_script( + 'plugin-check-admin', + 'const PLUGIN_CHECK = ' . json_encode( + array( + 'nonce' => $this->admin_ajax->get_nonce(), + ) + ), + 'before' + ); } /** @@ -63,7 +122,7 @@ private function get_available_plugins() { } /** - * Render the "Plugin Check" page. + * Renders the "Plugin Check" page. * * @since n.e.x.t */ @@ -85,9 +144,7 @@ public function render_page() { * @return array The modified list of actions. */ public function filter_plugin_action_links( $actions, $plugin_file ) { - if ( current_user_can( 'activate_plugins' ) ) { - $actions[] = sprintf( '%2$s', esc_url( admin_url() . 'tools.php?page=plugin-check&plugin=' . $plugin_file ), diff --git a/plugin-check.php b/plugin-check.php index 5c31b12b2..c92a78b00 100644 --- a/plugin-check.php +++ b/plugin-check.php @@ -19,6 +19,7 @@ define( 'WP_PLUGIN_CHECK_MINIMUM_PHP', '5.6' ); define( 'WP_PLUGIN_CHECK_MAIN_FILE', __FILE__ ); define( 'WP_PLUGIN_CHECK_PLUGIN_DIR_PATH', plugin_dir_path( WP_PLUGIN_CHECK_MAIN_FILE ) ); +define( 'WP_PLUGIN_CHECK_PLUGIN_DIR_URL', plugin_dir_url( WP_PLUGIN_CHECK_MAIN_FILE ) ); /** * Checks basic requirements and loads the plugin. diff --git a/templates/admin-page.php b/templates/admin-page.php index 268c0eeaf..f57a090a6 100644 --- a/templates/admin-page.php +++ b/templates/admin-page.php @@ -17,12 +17,12 @@
diff --git a/tests/Admin/Admin_AJAX_Tests.php b/tests/Admin/Admin_AJAX_Tests.php new file mode 100644 index 000000000..8772369af --- /dev/null +++ b/tests/Admin/Admin_AJAX_Tests.php @@ -0,0 +1,32 @@ +admin_ajax = new Admin_AJAX(); + } + + public function test_add_hooks() { + $this->admin_ajax->add_hooks(); + $this->assertEquals( 10, has_action( 'wp_ajax_plugin_check_run_checks', array( $this->admin_ajax, 'run_checks' ) ) ); + } + + public function test_get_nonce() { + $this->assertNotFalse( + wp_verify_nonce( $this->admin_ajax->get_nonce(), Admin_AJAX::NONCE_KEY ) + ); + } +} diff --git a/tests/Admin/Admin_Page_Tests.php b/tests/Admin/Admin_Page_Tests.php index 79279503b..f452d0346 100644 --- a/tests/Admin/Admin_Page_Tests.php +++ b/tests/Admin/Admin_Page_Tests.php @@ -8,7 +8,6 @@ namespace Admin; use WordPress\Plugin_Check\Admin\Admin_Page; -use WP_Object_Cache; use WP_UnitTestCase; class Admin_Page_Tests extends WP_UnitTestCase { @@ -40,14 +39,19 @@ public function test_add_page() { wp_set_current_user( $admin_user ); set_current_screen( 'dashboard' ); - $this->admin_page->add_page(); - + $page_hook = $this->admin_page->add_page(); $parent_pages = $_parent_pages; set_current_screen( $current_screen ); $this->assertArrayHasKey( 'plugin-check', $parent_pages ); $this->assertEquals( 'tools.php', $parent_pages['plugin-check'] ); + $this->assertNotFalse( has_action( "load-{$page_hook}", array( $this->admin_page, 'initialize_page' ) ) ); + } + + public function test_initialize_page() { + $this->admin_page->initialize_page(); + $this->assertEquals( 10, has_action( 'admin_enqueue_scripts', array( $this->admin_page, 'enqueue_scripts' ) ) ); } public function test_render_page() {