From a30e87392e2bd4546b83f0ab499b222e31ca33d1 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 28 Apr 2023 12:37:45 +0100 Subject: [PATCH 1/3] Refactor to remove repeated code. --- includes/Admin/Admin_AJAX.php | 145 ++++++++++++++++++---------------- 1 file changed, 77 insertions(+), 68 deletions(-) diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index 8110f15d9..b22a538dc 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -89,27 +89,11 @@ public function get_nonce() { */ public function set_up_environment() { // Verify the nonce before continuing. - $valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); + $this->validate_request(); - if ( is_wp_error( $valid_request ) ) { - wp_send_json_error( $valid_request, 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_FULL_SPECIAL_CHARS ); + $runner = $this->get_runner(); + $checks = $this->get_checks(); + $plugin = $this->get_plugin(); try { $runner->set_check_slugs( $checks ); @@ -148,11 +132,7 @@ public function clean_up_environment() { global $wpdb, $table_prefix; // Verify the nonce before continuing. - $valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); - - if ( is_wp_error( $valid_request ) ) { - wp_send_json_error( $valid_request, 403 ); - } + $this->validate_request(); // Set the new prefix. $old_prefix = $wpdb->set_prefix( $table_prefix . 'pc_' ); @@ -183,28 +163,11 @@ public function clean_up_environment() { */ public function get_checks_to_run() { // Verify the nonce before continuing. - $valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); - - if ( is_wp_error( $valid_request ) ) { - wp_send_json_error( $valid_request, 403 ); - } - - $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_FULL_SPECIAL_CHARS ); - $runner = Plugin_Request_Utility::get_runner(); - - if ( is_null( $runner ) ) { - $runner = new AJAX_Runner(); - } + $this->validate_request(); - // 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' ) ), - 403 - ); - } + $runner = $this->get_runner(); + $checks = $this->get_checks(); + $plugin = $this->get_plugin(); try { $runner->set_check_slugs( $checks ); @@ -234,29 +197,11 @@ public function get_checks_to_run() { */ public function run_checks() { // Verify the nonce before continuing. - $valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); - - if ( is_wp_error( $valid_request ) ) { - wp_send_json_error( $valid_request, 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 - ); - } + $this->validate_request(); - $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_FULL_SPECIAL_CHARS ); + $runner = $this->get_runner(); + $checks = $this->get_checks(); + $plugin = $this->get_plugin(); try { $runner->set_check_slugs( $checks ); @@ -278,6 +223,70 @@ public function run_checks() { ); } + + /** + * Get the AJAX_Runner runner. + * + * @since n.e.x.t + * + * @return AJAX_Runner + */ + protected function get_runner() { + $runner = Plugin_Request_Utility::get_runner(); + + if ( is_null( $runner ) ) { + Plugin_Request_Utility::initialize_runner(); + $runner = Plugin_Request_Utility::get_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 + ); + } + + return $runner; + } + + /** + * Get array of checks to run. + * + * @since n.e.x.t + * + * @return array + */ + protected function get_checks() { + $checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_FORCE_ARRAY ); + + return is_null( $checks ) ? array() : (array) $checks; + } + + /** + * Get requested plugin. + * + * @since n.e.x.t + * + * @return string + */ + protected function get_plugin() { + return filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); + } + + /** + * Validate request by checking nonce. + * + * @since n.e.x.t + */ + protected function validate_request() { + $valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); + + if ( is_wp_error( $valid_request ) ) { + wp_send_json_error( $valid_request, 403 ); + } + } + /** * Verify the request. * From 9778ae2f406ad72f877bd8cfda369f7a53c6de78 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 4 May 2023 11:40:26 +0100 Subject: [PATCH 2/3] Just create instance. --- includes/Admin/Admin_AJAX.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index b22a538dc..7c4506595 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -235,8 +235,7 @@ protected function get_runner() { $runner = Plugin_Request_Utility::get_runner(); if ( is_null( $runner ) ) { - Plugin_Request_Utility::initialize_runner(); - $runner = Plugin_Request_Utility::get_runner(); + $runner = new AJAX_Runner(); } // Make sure we are using the correct runner instance. From f48c9df7a975e005745f0df0632760cd44fb4c41 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 4 May 2023 11:54:26 +0100 Subject: [PATCH 3/3] Feedback. --- includes/Admin/Admin_AJAX.php | 47 ++++++++++++++++------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/includes/Admin/Admin_AJAX.php b/includes/Admin/Admin_AJAX.php index 7c4506595..790b88b90 100644 --- a/includes/Admin/Admin_AJAX.php +++ b/includes/Admin/Admin_AJAX.php @@ -89,7 +89,11 @@ public function get_nonce() { */ public function set_up_environment() { // Verify the nonce before continuing. - $this->validate_request(); + $valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); + + if ( is_wp_error( $valid_request ) ) { + wp_send_json_error( $valid_request, 403 ); + } $runner = $this->get_runner(); $checks = $this->get_checks(); @@ -132,7 +136,11 @@ public function clean_up_environment() { global $wpdb, $table_prefix; // Verify the nonce before continuing. - $this->validate_request(); + $valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); + + if ( is_wp_error( $valid_request ) ) { + wp_send_json_error( $valid_request, 403 ); + } // Set the new prefix. $old_prefix = $wpdb->set_prefix( $table_prefix . 'pc_' ); @@ -163,7 +171,11 @@ public function clean_up_environment() { */ public function get_checks_to_run() { // Verify the nonce before continuing. - $this->validate_request(); + $valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); + + if ( is_wp_error( $valid_request ) ) { + wp_send_json_error( $valid_request, 403 ); + } $runner = $this->get_runner(); $checks = $this->get_checks(); @@ -197,7 +209,11 @@ public function get_checks_to_run() { */ public function run_checks() { // Verify the nonce before continuing. - $this->validate_request(); + $valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); + + if ( is_wp_error( $valid_request ) ) { + wp_send_json_error( $valid_request, 403 ); + } $runner = $this->get_runner(); $checks = $this->get_checks(); @@ -234,16 +250,8 @@ public function run_checks() { protected function get_runner() { $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 - ); + $runner = new AJAX_Runner(); } return $runner; @@ -273,19 +281,6 @@ protected function get_plugin() { return filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); } - /** - * Validate request by checking nonce. - * - * @since n.e.x.t - */ - protected function validate_request() { - $valid_request = $this->verify_request( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); - - if ( is_wp_error( $valid_request ) ) { - wp_send_json_error( $valid_request, 403 ); - } - } - /** * Verify the request. *