$score_request->execute(); } remove_filter( 'jetpack_boost_excluded_query_parameters', array( $this, 'allow_jb_disable_module' ) ); return $score_request; } /** * Get Speed_Score_Request instance by url. * * @param string $url Url to get the Speed_Score_Request instance for. * * @return Speed_Score_Request */ private function get_score_request_by_url( $url ) { return Speed_Score_Request::get( Speed_Score_Request::generate_cache_id_from_url( $url ) ); } /** * Add query parameters to the url that would disable all boost modules. * * @param string $url The original URL we are measuring for score. * * @return string */ private function get_boost_modules_disabled_url( $url ) { return add_query_arg( 'jb-disable-modules', 'all', $url ); } /** * Can the user access speed scores? * * @return bool */ public function can_access_speed_scores() { return current_user_can( 'manage_options' ); } /** * Clear speed score request cache on jetpack_boost_deactivate action. */ public function clear_speed_score_request_cache() { Speed_Score_Request::clear_cache(); } /** * Prepare the speed score response. * * @param string $url URL of the speed is requested for. * @param Speed_Score_Request $score_request Speed score request. * @param Speed_Score_Request $score_request_no_boost Speed score request without boost enabled. * * @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response */ private function prepare_speed_score_response( $url, $score_request, $score_request_no_boost ) { $history = new Speed_Score_History( $url ); $url_no_boost = $this->get_boost_modules_disabled_url( $url ); $history_no_boost = new Speed_Score_History( $url_no_boost ); $response = array(); if ( ( ! $score_request || $score_request->is_success() ) && ( ! $score_request_no_boost || $score_request_no_boost->is_success() ) ) { $response['status'] = 'success'; $response['scores'] = array( 'current' => $history->latest_scores(), 'noBoost' => null, ); // Only include noBoost scores if at least one module is enabled. if ( $this->modules->have_enabled_modules() ) { $response['scores']['noBoost'] = $history_no_boost->latest_scores(); } $response['scores']['isStale'] = $history->is_stale(); } elseif ( ( $score_request && $score_request->is_error() ) || ( $score_request_no_boost && $score_request_no_boost->is_error() ) ) { // If either request ended up in error, we can just return the one with error so front-end can take action. The relevent url is available on the serialized object. if ( $score_request->is_error() ) { // Serialized version of score request contains the status property and error description if any. $response = $score_request->jsonSerialize(); } else { $response = $score_request_no_boost->jsonSerialize(); } } else { // If no request ended up in error/success as previous conditions dictate, it means that either of them are in pending state. $response['status'] = 'pending'; } return rest_ensure_response( $response ); } }