class-admin-post-list-column.php
4 days ago
class-dashboard.php
4 days ago
class-main.php
4 days ago
class-notices.php
8 months ago
class-odyssey-assets.php
2 weeks ago
class-odyssey-config-data.php
4 days ago
class-rest-controller.php
1 month ago
class-wp-dashboard-odyssey-widget.php
2 years ago
class-wpcom-client.php
4 days ago
class-wpcom-client.php
154 lines
| 1 | <?php |
| 2 | /** |
| 3 | * A class that wraps `Automattic\Jetpack\Connection\Client` and handles cache and errors. |
| 4 | * |
| 5 | * @package automattic/jetpack-stats-admin |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Stats_Admin; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | use WP_Error; |
| 12 | |
| 13 | /** |
| 14 | * A class that wraps `Automattic\Jetpack\Connection\Client` and handles cache and errors. |
| 15 | * |
| 16 | * @package Automattic\Jetpack\Stats_Admin |
| 17 | */ |
| 18 | class WPCOM_Client { |
| 19 | /** |
| 20 | * Transient prefix for caching REST API responses. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | const CACHE_TRANSIENT_PREFIX = 'STATS_REST_RESP_'; |
| 25 | |
| 26 | /** |
| 27 | * Query the WordPress.com REST API using the blog token cached. |
| 28 | * |
| 29 | * @param String $path The API endpoint relative path. |
| 30 | * @param String $version The API version. |
| 31 | * @param array $args Request arguments. |
| 32 | * @param String $body Request body. |
| 33 | * @param String $base_api_path (optional) the API base path override, defaults to 'rest'. |
| 34 | * @param bool $use_cache (optional) default to true. |
| 35 | * @param string $cache_key (optional) default to null meaning the function auto generates cache key. |
| 36 | * @return array|WP_Error $response Data. |
| 37 | */ |
| 38 | public static function request_as_blog_cached( $path, $version = '1.1', $args = array(), $body = null, $base_api_path = 'rest', $use_cache = true, $cache_key = null ) { |
| 39 | // Only allow caching GET requests. |
| 40 | $use_cache = $use_cache && ! ( isset( $args['method'] ) && strtoupper( $args['method'] ) !== 'GET' ) && ! static::should_bypass_cache(); |
| 41 | |
| 42 | // Arrays are serialized without considering the order of objects, but it's okay atm. |
| 43 | $cache_key = $cache_key ?? self::CACHE_TRANSIENT_PREFIX . md5( implode( '|', array( $path, $version, wp_json_encode( $args, JSON_UNESCAPED_SLASHES ), wp_json_encode( $body, JSON_UNESCAPED_SLASHES ), $base_api_path ) ) ); |
| 44 | |
| 45 | if ( $use_cache ) { |
| 46 | $response_body_content = get_transient( $cache_key ); |
| 47 | if ( false !== $response_body_content ) { |
| 48 | return json_decode( $response_body_content, true ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | $response_body = static::request_as_blog( $path, $version, $args, $body, $base_api_path ); |
| 53 | |
| 54 | if ( is_wp_error( $response_body ) ) { |
| 55 | return $response_body; |
| 56 | } |
| 57 | |
| 58 | // Cache the response for 5 minutes. |
| 59 | set_transient( $cache_key, wp_json_encode( $response_body, JSON_UNESCAPED_SLASHES ), 5 * MINUTE_IN_SECONDS ); |
| 60 | |
| 61 | return $response_body; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Query the WordPress.com REST API using the blog token |
| 66 | * |
| 67 | * @param String $path The API endpoint relative path. |
| 68 | * @param String $version The API version. |
| 69 | * @param array $args Request arguments. |
| 70 | * @param String $body Request body. |
| 71 | * @param String $base_api_path (optional) the API base path override, defaults to 'rest'. |
| 72 | * @return array|WP_Error $response Data. |
| 73 | */ |
| 74 | public static function request_as_blog( $path, $version = '1.1', $args = array(), $body = null, $base_api_path = 'rest' ) { |
| 75 | $response = Client::wpcom_json_api_request_as_blog( |
| 76 | $path, |
| 77 | $version, |
| 78 | $args, |
| 79 | $body, |
| 80 | $base_api_path |
| 81 | ); |
| 82 | |
| 83 | if ( is_wp_error( $response ) ) { |
| 84 | // `Client` fails before sending anything when the site holds no blog token, and that |
| 85 | // error carries no status, which the REST API renders as a 500. Say what actually |
| 86 | // happened so callers can tell an unconnected site from a broken one. Newer connection |
| 87 | // packages name the reason (`no_possible_tokens`); older ones return `missing_token`. |
| 88 | if ( in_array( $response->get_error_code(), array( 'missing_token', 'no_possible_tokens' ), true ) ) { |
| 89 | return new WP_Error( |
| 90 | 'site_not_connected', |
| 91 | __( 'This site is not connected to WordPress.com.', 'jetpack-stats-admin' ), |
| 92 | array( 'status' => 400 ) |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | return $response; |
| 97 | } |
| 98 | |
| 99 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 100 | $response_body_content = wp_remote_retrieve_body( $response ); |
| 101 | $response_body = json_decode( $response_body_content, true ); |
| 102 | |
| 103 | $error = static::get_wp_error( $response_body, (int) $response_code ); |
| 104 | if ( is_wp_error( $error ) ) { |
| 105 | return $error; |
| 106 | } |
| 107 | |
| 108 | return $response_body; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Build error object from remote response body and status code. |
| 113 | * |
| 114 | * @param array $response_body Remote response body. |
| 115 | * @param int $response_code Http response code. |
| 116 | * @return WP_Error |
| 117 | */ |
| 118 | protected static function get_wp_error( $response_body, $response_code = 200 ) { |
| 119 | $error_code = null; |
| 120 | foreach ( array( 'code', 'error' ) as $error_code_key ) { |
| 121 | if ( isset( $response_body[ $error_code_key ] ) ) { |
| 122 | $error_code = $response_body[ $error_code_key ]; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Sometimes the response code could be 200 but the response body still contains an error. |
| 128 | if ( $error_code !== null || $response_code !== 200 ) { |
| 129 | return new WP_Error( |
| 130 | $error_code, |
| 131 | $response_body['message'] ?? 'unknown remote error', |
| 132 | array( 'status' => $response_code ) |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | // No error. |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Check if the cache should be bypassed. |
| 142 | * |
| 143 | * @return bool |
| 144 | */ |
| 145 | protected static function should_bypass_cache() { |
| 146 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 147 | return isset( $_GET['force_refresh'] ) || isset( $_GET['statsPurchaseSuccess'] ) || |
| 148 | // phpcs:ignore WordPress.Arrays.ArrayKeySpacingRestrictions.SpacesAroundArrayKeys, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 149 | ( isset( $_SERVER[ 'HTTP_REFERER' ] ) && false !== strpos( $_SERVER[ 'HTTP_REFERER' ], 'force_refresh' ) ) || |
| 150 | // phpcs:ignore WordPress.Arrays.ArrayKeySpacingRestrictions.SpacesAroundArrayKeys, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 151 | ( isset( $_SERVER[ 'HTTP_REFERER' ] ) && false !== strpos( $_SERVER[ 'HTTP_REFERER' ], 'statsPurchaseSuccess' ) ); |
| 152 | } |
| 153 | } |
| 154 |