-
Notifications
You must be signed in to change notification settings - Fork 98
Create AJAX runtime environment setup/cleanup #101
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
Changes from all commits
e1da095
fcd4f07
eef17eb
c6fe064
2ad3ce5
7544e1b
a17d716
7f122a0
9719698
dccdae6
86fa8d2
eb9d97c
7f8abd0
2157c4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,9 @@ | |
| use Exception; | ||
| use WordPress\Plugin_Check\Checker\AJAX_Runner; | ||
| use WordPress\Plugin_Check\Checker\Runtime_Check; | ||
| use WordPress\Plugin_Check\Checker\Runtime_Environment_Setup; | ||
| use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; | ||
|
|
||
| /** | ||
| * Class to handle the Admin AJAX requests. | ||
| * | ||
|
|
@@ -33,6 +35,8 @@ class Admin_AJAX { | |
| * @since n.e.x.t | ||
| */ | ||
| public function add_hooks() { | ||
| add_action( 'wp_ajax_plugin_check_clean_up_environment', array( $this, 'clean_up_environment' ) ); | ||
| add_action( 'wp_ajax_plugin_check_set_up_environment', array( $this, 'set_up_environment' ) ); | ||
| add_action( 'wp_ajax_plugin_check_get_checks_to_run', array( $this, 'get_checks_to_run' ) ); | ||
| add_action( 'wp_ajax_plugin_check_run_checks', array( $this, 'run_checks' ) ); | ||
| } | ||
|
|
@@ -46,6 +50,100 @@ public function get_nonce() { | |
| return wp_create_nonce( self::NONCE_KEY ); | ||
| } | ||
|
|
||
| /** | ||
| * Handles the AJAX request to setup the runtime environment if needed. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| public function set_up_environment() { | ||
| // Verify the nonce before continuing. | ||
| $valid_nonce = $this->verify_nonce( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING ) ); | ||
|
joemcgill marked this conversation as resolved.
|
||
|
|
||
| if ( is_wp_error( $valid_nonce ) ) { | ||
| 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 ); | ||
| $plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_STRING ); | ||
|
|
||
| try { | ||
| $runner->set_check_slugs( $checks ); | ||
| $runner->set_plugin( $plugin ); | ||
| $checks_to_run = $runner->get_checks_to_run(); | ||
| } catch ( Exception $error ) { | ||
| wp_send_json_error( | ||
| new WP_Error( 'invalid-request', $error->getMessage() ), | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| $message = __( 'No runtime checks, runtime environment was not setup.', 'plugin-check' ); | ||
|
|
||
| if ( $this->has_runtime_check( $checks_to_run ) ) { | ||
| $runtime = new Runtime_Environment_Setup(); | ||
| $runtime->setup(); | ||
| $message = __( 'Runtime environment setup successful.', 'plugin-check' ); | ||
| } | ||
|
|
||
| wp_send_json_success( | ||
| array( | ||
| 'message' => $message, | ||
| 'plugin' => $plugin, | ||
| 'checks' => $checks, | ||
|
Comment on lines
+104
to
+105
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you're returning all these values from every AJAX endpoint just to be able to chain the AJAX requests in the JS file. I think that makes for a confusing architecture and requires us to include unnecessary data in the AJAX responses. No need to fix that here since it's already happening in existing code, but I think that will be something we should resolve as part of the architecture review.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @felixarntz I agree and let's tackle this as part of the architectural review. |
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Handles the AJAX request to cleanup the runtime environment. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| public function clean_up_environment() { | ||
| global $wpdb; | ||
|
|
||
| // Verify the nonce before continuing. | ||
| $valid_nonce = $this->verify_nonce( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING ) ); | ||
|
|
||
| if ( is_wp_error( $valid_nonce ) ) { | ||
| wp_send_json_error( $valid_nonce, 403 ); | ||
| } | ||
|
|
||
| // Set the new prefix. | ||
| $old_prefix = $wpdb->set_prefix( 'wppc_' ); | ||
|
|
||
| $message = __( 'Runtime environment was not prepared, cleanup was not run.', 'plugin-check' ); | ||
|
|
||
| // Test if the runtime environment tables exist. | ||
| if ( 'wppc_posts' === $wpdb->get_var( "SHOW TABLES LIKE 'wppc_posts'" ) || defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) ) { | ||
| $runtime = new Runtime_Environment_Setup(); | ||
| $runtime->cleanup(); | ||
| $message = __( 'Runtime environment cleanup successful.', 'plugin-check' ); | ||
| } | ||
|
|
||
| // Restore the old prefix. | ||
| $wpdb->set_prefix( $old_prefix ); | ||
|
|
||
| wp_send_json_success( | ||
| array( | ||
| 'message' => $message, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Handles the AJAX request that returns the checks to run. | ||
| * | ||
|
|
@@ -59,9 +157,9 @@ public function get_checks_to_run() { | |
| wp_send_json_error( $valid_nonce, 403 ); | ||
| } | ||
|
|
||
| $checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); | ||
| $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 ); | ||
|
|
||
| $runner = Plugin_Request_Utility::get_runner(); | ||
|
|
||
| if ( is_null( $runner ) ) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,14 @@ public function prepare() { | |
|
|
||
| $cleanup_functions = array(); | ||
|
|
||
| $use_minimal_theme_preparation = new Use_Minimal_Theme_Preparation( 'wp-empty-theme', WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . '/test-content/themes' ); | ||
| if ( ! defined( 'WP_PLUGIN_CHECK_PLUGIN_DIR_PATH' ) ) { | ||
| $plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins'; | ||
| $theme_folder = $plugins_dir . '/plugin-check/test-content/themes'; | ||
| } else { | ||
| $theme_folder = WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'test-content/themes'; | ||
| } | ||
|
Comment on lines
+57
to
+62
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is inverted compared to https://github.com/10up/plugin-check/pull/101/files#diff-35a6c4633cd89c9ad66535db741d064ff3f730cf69ea87d347d8e250544fff1eR85 |
||
|
|
||
| $use_minimal_theme_preparation = new Use_Minimal_Theme_Preparation( 'wp-empty-theme', $theme_folder ); | ||
| $cleanup_functions[] = $use_minimal_theme_preparation->prepare(); | ||
|
|
||
| $force_single_plugin_preparation = new Force_Single_Plugin_Preparation( $this->check_context->basename() ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.