From 1a0bee1d97bda1e016bdff55534135d9bce591f0 Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Mon, 27 Feb 2023 15:29:22 +0530 Subject: [PATCH 01/18] Working on ajax fetch api --- assets/js/plugin-check-admin.js | 34 ++++++++++++++++++ includes/Admin/Admin_AJAX.php | 63 +++++++++++++++++++++++++++++++++ includes/Admin/Admin_Page.php | 54 +++++++++++++++++++++++++++- plugin-check.php | 1 + templates/admin-page.php | 6 ++-- 5 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 assets/js/plugin-check-admin.js create mode 100644 includes/Admin/Admin_AJAX.php diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js new file mode 100644 index 000000000..80a4f2f8f --- /dev/null +++ b/assets/js/plugin-check-admin.js @@ -0,0 +1,34 @@ +( function ( data ) { + const checkItButton = document.getElementById( 'pc_check_it' ); + const pluginsList = document.getElementById( 'pc_plugins' ); + + checkItButton.addEventListener( 'click', () => { + + const pluginCheckData = new FormData(); + + // Collect the data to pass along for generating a check results. + pluginCheckData.append( 'action', 'plugin_check_run_check' ); + pluginCheckData.append( 'nonce', data.nonce ); + pluginCheckData.append( 'plugin', pluginsList.value ); + + fetch( + data.ajaxUrl, + { + method: 'POST', + credentials: 'same-origin', + body: pluginCheckData + } + ) + .then( + ( response ) => { console.log( response ); } + ) + .then( + ( data ) => { console.log( data ); } + ) + .catch( + ( error ) => { console.log( 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..1fe65670b --- /dev/null +++ b/includes/Admin/Admin_AJAX.php @@ -0,0 +1,63 @@ +nonce_key ); + } + + /** + * Run check. + * + * @since n.e.x.t + */ + public function run_check() { + + $nonce = filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING ); + + if ( ! wp_verify_nonce( $nonce, $this->nonce_key ) ) { + + wp_send_json_error(); + } + + wp_send_json_success( + array( + 'message' => 'Verified', + ) + ); + } +} diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index 8af87b65b..8609f9f9d 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -14,6 +14,23 @@ */ class Admin_Page { + /** + * 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(); + } + /** * Initializes hooks. * @@ -21,6 +38,8 @@ class Admin_Page { */ public function add_hooks() { add_action( 'admin_menu', array( $this, 'add_page' ) ); + + $this->admin_ajax->add_hooks(); } /** @@ -29,13 +48,46 @@ public function add_hooks() { * @since n.e.x.t */ 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' ) ); + } + + /** + * Initializes page hooks. + * + * @since n.e.x.t + */ + public function initialize_page() { + + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + } + + /** + * Load 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(), '1.0.0', true ); + + wp_add_inline_script( + 'plugin-check-admin', + 'const PLUGIN_CHECK = ' . json_encode( + array( + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), + 'nonce' => $this->admin_ajax->get_nonce(), + ) + ), + 'before' + ); } /** 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 009312801..d696f0700 100644 --- a/templates/admin-page.php +++ b/templates/admin-page.php @@ -17,12 +17,12 @@

-

- $available_plugin ) { ?>
From 6e3698bec79deaa7e5eea2450a507db0d5ab594c Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Mon, 27 Feb 2023 22:51:45 +0530 Subject: [PATCH 02/18] Add plugin check ajax check request --- assets/js/plugin-check-admin.js | 13 +++- includes/Admin/Admin_AJAX.php | 2 +- includes/Admin/Admin_Page.php | 2 - tests/Admin/Admin_AJAX_Tests.php | 101 +++++++++++++++++++++++++++++++ tests/Admin/Admin_Page_Tests.php | 24 +++++++- 5 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 tests/Admin/Admin_AJAX_Tests.php diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index 80a4f2f8f..5b8ed0794 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -20,10 +20,19 @@ } ) .then( - ( response ) => { console.log( response ); } + ( response ) => { + if ( ! response.ok ) { + + throw new Error(`[${response.message}]`); + } + + return response.json(); + } ) .then( - ( data ) => { console.log( data ); } + ( data ) => { + console.log( data.data.message ); + } ) .catch( ( error ) => { console.log( error ); } diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index 1fe65670b..45efc6bff 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -56,7 +56,7 @@ public function run_check() { wp_send_json_success( array( - 'message' => 'Verified', + 'message' => 'Verified!', ) ); } diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index 8609f9f9d..c9336f77e 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -65,7 +65,6 @@ public function add_page() { * @since n.e.x.t */ public function initialize_page() { - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); } @@ -75,7 +74,6 @@ public function initialize_page() { * @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(), '1.0.0', true ); wp_add_inline_script( diff --git a/tests/Admin/Admin_AJAX_Tests.php b/tests/Admin/Admin_AJAX_Tests.php new file mode 100644 index 000000000..0e186cda3 --- /dev/null +++ b/tests/Admin/Admin_AJAX_Tests.php @@ -0,0 +1,101 @@ +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_check', array( $this->admin_ajax, 'run_check' ) ) ); + } + + public function test_get_nonce() { + $this->assertNotEmpty( $this->admin_ajax->get_nonce() ); + } + +// public function test_render_page() { +// 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( '