diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index dc685f318..3c3a98322 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -28,8 +28,9 @@ /** * Setup the runtime environment if needed. * - * @param {Object} data Data object with props passed to form data. * @since n.e.x.t + * + * @param {Object} data Data object with props passed to form data. */ function setUpEnvironment( data ) { const pluginCheckData = new FormData(); @@ -65,6 +66,8 @@ * Cleanup the runtime environment. * * @since n.e.x.t + * + * @return {Object} The response data. */ function cleanUpEnvironment() { const pluginCheckData = new FormData(); @@ -129,19 +132,37 @@ /** * Run Checks. * + * @since n.e.x.t + * * @param {Object} data The response data. + */ + async function runChecks( data ) { + for ( let i = 0; i < data.checks.length; i++ ) { + try { + const result = await runCheck( data.plugin, data.checks[ i ] ); + console.log( result ); + } catch ( e ) { + // Ignore for now. + } + } + } + + /** + * Run a single check. + * * @since n.e.x.t + * + * @param {string} plugin The plugin to check. + * @param {string} check The check to run. + * @return {Object} The check results. */ - function runChecks( data ) { + function runCheck( plugin, check ) { const pluginCheckData = new FormData(); pluginCheckData.append( 'nonce', pluginCheck.nonce ); - pluginCheckData.append( 'plugin', data.plugin ); + pluginCheckData.append( 'plugin', plugin ); + pluginCheckData.append( 'checks[]', check ); pluginCheckData.append( 'action', 'plugin_check_run_checks' ); - for ( let i = 0; i < data.checks.length; i++ ) { - pluginCheckData.append( 'checks[]', data.checks[ i ] ); - } - return fetch( ajaxurl, { method: 'POST', credentials: 'same-origin', diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index 1299c6973..07c9aa304 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -10,6 +10,7 @@ use WP_Error; use Exception; use WordPress\Plugin_Check\Checker\AJAX_Runner; +use WordPress\Plugin_Check\Checker\Check_Result; use WordPress\Plugin_Check\Checker\Runtime_Check; use WordPress\Plugin_Check\Checker\Runtime_Environment_Setup; use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; @@ -208,9 +209,40 @@ public function run_checks() { wp_send_json_error( $valid_nonce, 403 ); } + $runner = Plugin_Request_Utility::get_runner(); + + if ( is_null( $runner ) ) { + $runner = new AJAX_Runner(); + } + + // Make sure we are using the correct runner instance. + if ( ! ( $runner instanceof AJAX_Runner ) ) { + wp_send_json_error( + new WP_Error( 'invalid-runner', __( 'AJAX Runner was not initialized correctly.', 'plugin-check' ) ), + 500 + ); + } + + $checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_FORCE_ARRAY ); + $checks = is_null( $checks ) ? array() : $checks; + $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); + + try { + $runner->set_check_slugs( $checks ); + $runner->set_plugin( $plugin ); + $results = $runner->run(); + } catch ( Exception $error ) { + wp_send_json_error( + new WP_Error( 'invalid-request', $error->getMessage() ), + 400 + ); + } + wp_send_json_success( array( - 'message' => __( 'Verified!', 'plugin-check' ), + 'message' => __( 'Checks run successfully', 'plugin-check' ), + 'errors' => $results->get_errors(), + 'warnings' => $results->get_warnings(), ) ); } diff --git a/package-lock.json b/package-lock.json index 2ce2893a0..8a8a8ea3c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12237,4 +12237,4 @@ "dev": true } } -} +} \ No newline at end of file