base = trailingslashit( aioseo()->searchStatistics->api->getApiUrl() ) . trailingslashit( aioseo()->searchStatistics->api->getApiVersion() ); $this->route = trailingslashit( $route ); $this->url = trailingslashit( $this->scheme . $this->base . $this->route ); $this->method = $method; $this->token = ! empty( $args['token'] ) ? $args['token'] : aioseo()->searchStatistics->api->auth->getToken(); $this->key = ! empty( $args['key'] ) ? $args['key'] : aioseo()->searchStatistics->api->auth->getKey(); $this->tt = ! empty( $args['tt'] ) ? $args['tt'] : ''; $this->args = ! empty( $args ) ? $args : []; $this->siteurl = site_url(); $this->plugin = 'aioseo-' . strtolower( aioseo()->versionPath ); $this->version = aioseo()->version; $this->sitei = ! empty( $args['sitei'] ) ? $args['sitei'] : ''; } /** * Sends and processes the API request. * * @since 4.3.0 * * @return mixed The response. */ public function request() { // 1. BUILD BODY $body = []; if ( ! empty( $this->args ) ) { foreach ( $this->args as $name => $value ) { $body[ $name ] = $value; } } foreach ( [ 'sitei', 'siteurl', 'version', 'key', 'token', 'tt' ] as $key ) { if ( ! empty( $this->{$key} ) ) { $body[ $key ] = $this->{$key}; } } // If this is a plugin API request, add the data. if ( 'info' === $this->route || 'update' === $this->route ) { $body['aioseoapi-plugin'] = $this->plugin; } // Add in additional data if needed. if ( ! empty( $this->additionalData ) ) { $body['aioseoapi-data'] = maybe_serialize( $this->additionalData ); } if ( 'GET' === $this->method ) { $body['time'] = time(); // Add a timestamp to avoid caching. } $body['timezone'] = gmdate( 'e' ); $body['ip'] = ! empty( $_SERVER['SERVER_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_ADDR'] ) ) : ''; // 2. EXECUTE REQUEST $data = [ 'body' => wp_json_encode( $body ), 'timeout' => 120 ]; if ( 'GET' === $this->method ) { $queryString = http_build_query( $body, '', '&' ); unset( $data['body'] ); $response = aioseo()->helpers->wpRemoteGet( esc_url_raw( $this->url ) . '?' . $queryString, $data ); } else { $response = aioseo()->helpers->wpRemotePost( esc_url_raw( $this->url ), $data ); } // 5. VALIDATE RESPONSE if ( is_wp_error( $response ) ) { return $response; } $responseCode = wp_remote_retrieve_response_code( $response ); $responseBody = json_decode( wp_remote_retrieve_body( $response ), true ); if ( is_wp_error( $responseBody ) ) { return false; } if ( 200 !== $responseCode ) { $type = ! empty( $responseBody['type'] ) ? $responseBody['type'] : 'api-error'; if ( empty( $responseCode ) ) { return new \WP_Error( $type, 'The API was unreachable.' ); } if ( empty( $responseBody ) || ( empty( $responseBody['message'] ) && empty( $responseBody['error'] ) ) ) { return new \WP_Error( $type, sprintf( 'The API returned a %s response', $responseCode ) ); } if ( ! empty( $responseBody['message'] ) ) { return new \WP_Error( $type, sprintf( 'The API returned a %1$d response with this message: %2$s', $responseCode, stripslashes( $responseBody['message'] ) ) ); } if ( ! empty( $responseBody['error'] ) ) { return new \WP_Error( $type, sprintf( 'The API returned a %1$d response with this message: %2$s', $responseCode, stripslashes( $responseBody['error'] ) ) ); } } // Check if the trust token is required. if ( ! empty( $this->tt ) && ( empty( $responseBody['tt'] ) || ! hash_equals( $this->tt, $responseBody['tt'] ) ) ) { return new \WP_Error( 'validation-error', 'Invalid API request.' ); } return $responseBody; } /** * Sets additional data for the request. * * @since 4.3.0 * * @param array $data The additional data. * @return void */ public function setAdditionalData( array $data ) { $this->additionalData = array_merge( $this->additionalData, $data ); } }