Prevent wp_remote_post or wp_remote_get to trigger when not necessary

This commit is contained in:
Claudio Sanches 2020-01-07 15:51:16 -03:00
parent b4125269a3
commit 4342ae2b6f
1 changed files with 52 additions and 36 deletions

View File

@ -72,16 +72,11 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
*/
public function get_items( $request ) {
$schema = $this->get_item_schema();
$mappings = $this->get_item_mappings();
$fields = $this->get_fields_for_response( $request );
$mappings = $this->get_item_mappings( $fields );
$response = array();
foreach ( $mappings as $section => $values ) {
foreach ( $values as $key => $value ) {
if ( isset( $schema['properties'][ $section ]['properties'][ $key ]['type'] ) ) {
settype( $values[ $key ], $schema['properties'][ $section ]['properties'][ $key ]['type'] );
}
}
settype( $values, $schema['properties'][ $section ]['type'] );
$response[ $section ] = $values;
}
@ -571,11 +566,12 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
/**
* Return an array of sections and the data associated with each.
*
* @param array $fields List of fields to be included on the response.
* @return array
*/
public function get_item_mappings() {
public function get_item_mappings( $fields ) {
return array(
'environment' => $this->get_environment_info(),
'environment' => $this->get_environment_info( $fields ),
'database' => $this->get_database_info(),
'active_plugins' => $this->get_active_plugins(),
'inactive_plugins' => $this->get_inactive_plugins(),
@ -592,11 +588,23 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
* Get array of environment information. Includes thing like software
* versions, and various server settings.
*
* @param array $fields List of fields to be included on the response.
* @return array
*/
public function get_environment_info() {
public function get_environment_info( $fields ) {
global $wpdb;
$exclude = array();
foreach ( $fields as $field ) {
$values = explode( '.', $field );
if ( 'environment' !== $values[0] || empty( $values[1] ) ) {
continue;
}
$exclude[] = $values[1];
}
// Figure out cURL version, if installed.
$curl_version = '';
if ( function_exists( 'curl_version' ) ) {
@ -613,6 +621,9 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
}
// Test POST requests.
$post_response_successful = null;
$post_response_code = null;
if ( empty( $exclude ) || 0 < count( array_intersect( array( 'remote_post_successful', 'remote_post_response' ), $exclude ) ) ) {
$post_response_code = get_transient( 'woocommerce_test_remote_post' );
if ( false === $post_response_code || is_wp_error( $post_response_code ) ) {
@ -634,8 +645,12 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
}
$post_response_successful = ! is_wp_error( $post_response_code ) && $post_response_code >= 200 && $post_response_code < 300;
}
// Test GET requests.
$get_response_successful = null;
$get_response_code = null;
if ( empty( $exclude ) || 0 < count( array_intersect( array( 'remote_get_successful', 'remote_get_response' ), $exclude ) ) ) {
$get_response_code = get_transient( 'woocommerce_test_remote_get' );
if ( false === $get_response_code || is_wp_error( $get_response_code ) ) {
@ -647,6 +662,7 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
}
$get_response_successful = ! is_wp_error( $get_response_code ) && $get_response_code >= 200 && $get_response_code < 300;
}
$database_version = wc_get_server_database_version();