Merge pull request woocommerce/woocommerce-admin#2423 from woocommerce/fix/2288-unhandled-rest-requests-wp-error

Performance indicators - bail whenever an internal API request fails.
This commit is contained in:
Jeff Stieler 2019-06-14 09:20:02 -06:00 committed by GitHub
commit 4c62701b86
2 changed files with 26 additions and 6 deletions

View File

@ -117,19 +117,30 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
return true;
}
$request = new WP_REST_Request( 'GET', '/wc/v4/reports' );
$response = rest_do_request( $request );
$endpoints = $response->get_data();
$allowed_stats = array();
$request = new WP_REST_Request( 'GET', '/wc/v4/reports' );
$response = rest_do_request( $request );
if ( is_wp_error( $response ) ) {
return $response;
}
if ( 200 !== $response->get_status() ) {
return new WP_Error( 'woocommerce_reports_performance_indicators_result_failed', __( 'Sorry, fetching performance indicators failed.', 'woocommerce-admin' ) );
}
$endpoints = $response->get_data();
$allowed_stats = array();
foreach ( $endpoints as $endpoint ) {
if ( '/stats' === substr( $endpoint['slug'], -6 ) ) {
$request = new WP_REST_Request( 'OPTIONS', $endpoint['path'] );
$response = rest_do_request( $request );
$data = $response->get_data();
if ( is_wp_error( $response ) ) {
return $response;
}
$data = $response->get_data();
$prefix = substr( $endpoint['slug'], 0, -6 );
@ -283,6 +294,10 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
$response = rest_do_request( $request );
if ( is_wp_error( $response ) ) {
return $response;
}
$data = $response->get_data();
$format = $this->formats[ $stat ];
$label = $this->labels[ $stat ];

View File

@ -530,7 +530,12 @@ class WC_Admin_Loader {
if ( ! empty( $preload_data_endpoints ) ) {
foreach ( $preload_data_endpoints as $key => $endpoint ) {
$settings['dataEndpoints'][ $key ] = $preload_data[ $endpoint ]['body'];
// Handle error case: rest_do_request() doesn't guarantee success.
if ( empty( $preload_data[ $endpoint ] ) ) {
$settings['dataEndpoints'][ $key ] = array();
} else {
$settings['dataEndpoints'][ $key ] = $preload_data[ $endpoint ]['body'];
}
}
}
$settings = self::get_custom_settings( $settings );