get_params(); $providers = empty( $params['providers'] ) || ! is_array( $params['providers'] ) ? array() : $params['providers']; $api_successful = array( 'success' => true ); // If success is false, the whole Cloud CSS generation process failed. if ( empty( $params['success'] ) ) { if ( empty( $params['message'] ) || ! is_string( $params['message'] ) ) { $error = __( 'An unknown error occurred', 'jetpack-boost' ); } else { $error = $params['message']; } $state->set_error( $error ); $state->save(); return $api_successful; } // Update each provider. foreach ( $providers as $provider_key => $result ) { if ( ! isset( $result['data'] ) ) { return new \WP_Error( 'invalid_data', __( 'Invalid request; missing data element', 'jetpack-boost' ) ); } $data = $result['data']; // Success if ( ! empty( $result['success'] ) && ! empty( $data['css'] ) && is_string( $data['css'] ) ) { $storage->store_css( $provider_key, $data['css'] ); $state->set_provider_success( $provider_key ); continue; } // Failures must have an array of urls. if ( empty( $data['urls'] ) || ! is_array( $data['urls'] ) ) { return new \WP_Error( 'invalid_data', __( 'Invalid request; missing urls element', 'jetpack-boost' ) ); } $state->set_provider_errors( $provider_key, $this->flatten_url_errors( $data['urls'] ) ); } // Save the state changes. $state->save(); return $api_successful; } /** * Errors arrive from Shield in an associative array with the URL as the key. * This function flattens the array into a list of assoc arrays with the URL in each member. */ private function flatten_url_errors( $errors ) { $flat_errors = array(); foreach ( $errors as $url => $error ) { $flat_errors[] = array_merge( array( 'url' => $url ), $error ); } return $flat_errors; } public function permissions() { return array( new Signed_With_Blog_Token(), ); } }