| @@ -55,9 +55,17 @@ | ||
| 55 | 55 | * @since 1.0.4 |
| 56 | 56 | */ |
| 57 | 57 | private $_logger; |
| 58 | 58 | |
| 59 | - /** | |
| 59 | + /** | |
| 60 | + * @author Leo Fajardo (@leorw) | |
| 61 | + * @since 2.3.0 | |
| 62 | + * | |
| 63 | + * @var string | |
| 64 | + */ | |
| 65 | + private $_sdk_version; | |
| 66 | + | |
| 67 | + /** | |
| 60 | 68 | * @param string $slug |
| 61 | 69 | * @param string $scope 'app', 'developer', 'user' or 'install'. |
| 62 | 70 | * @param number $id Element's id. |
| 63 | 71 | * @param string $public_key Public key. |
| @@ -62,18 +70,27 @@ | ||
| 62 | 70 | * @param number $id Element's id. |
| 63 | 71 | * @param string $public_key Public key. |
| 64 | 72 | * @param bool $is_sandbox |
| 65 | 73 | * @param bool|string $secret_key Element's secret key. |
| 74 | + * @param null|string $sdk_version | |
| 66 | 75 | * |
| 67 | 76 | * @return FS_Api |
| 68 | 77 | */ |
| 69 | - static function instance( $slug, $scope, $id, $public_key, $is_sandbox, $secret_key = false ) { | |
| 78 | + static function instance( | |
| 79 | + $slug, | |
| 80 | + $scope, | |
| 81 | + $id, | |
| 82 | + $public_key, | |
| 83 | + $is_sandbox, | |
| 84 | + $secret_key = false, | |
| 85 | + $sdk_version = null | |
| 86 | + ) { | |
| 70 | 87 | $identifier = md5( $slug . $scope . $id . $public_key . ( is_string( $secret_key ) ? $secret_key : '' ) . json_encode( $is_sandbox ) ); |
| 71 | 88 | |
| 72 | 89 | if ( ! isset( self::$_instances[ $identifier ] ) ) { |
| 73 | 90 | self::_init(); |
| 74 | 91 | |
| 75 | - self::$_instances[ $identifier ] = new FS_Api( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox ); | |
| 92 | + self::$_instances[ $identifier ] = new FS_Api( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox, $sdk_version ); | |
| 76 | 93 | } |
| 77 | 94 | |
| 78 | 95 | return self::$_instances[ $identifier ]; |
| 79 | 96 | } |
| @@ -104,14 +121,24 @@ | ||
| 104 | 121 | * @param number $id Element's id. |
| 105 | 122 | * @param string $public_key Public key. |
| 106 | 123 | * @param bool|string $secret_key Element's secret key. |
| 107 | 124 | * @param bool $is_sandbox |
| 125 | + * @param null|string $sdk_version | |
| 108 | 126 | */ |
| 109 | - private function __construct( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox ) { | |
| 127 | + private function __construct( | |
| 128 | + $slug, | |
| 129 | + $scope, | |
| 130 | + $id, | |
| 131 | + $public_key, | |
| 132 | + $secret_key, | |
| 133 | + $is_sandbox, | |
| 134 | + $sdk_version | |
| 135 | + ) { | |
| 110 | 136 | $this->_api = new Freemius_Api_WordPress( $scope, $id, $public_key, $secret_key, $is_sandbox ); |
| 111 | 137 | |
| 112 | - $this->_slug = $slug; | |
| 113 | - $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug . '_api', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); | |
| 138 | + $this->_slug = $slug; | |
| 139 | + $this->_sdk_version = $sdk_version; | |
| 140 | + $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug . '_api', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); | |
| 114 | 141 | } |
| 115 | 142 | |
| 116 | 143 | /** |
| 117 | 144 | * Find clock diff between server and API server, and store the diff locally. |
| @@ -153,42 +180,54 @@ | ||
| 153 | 180 | * |
| 154 | 181 | * @return array|mixed|string|void |
| 155 | 182 | */ |
| 156 | 183 | private function _call( $path, $method = 'GET', $params = array(), $retry = false ) { |
| 157 | - $this->_logger->entrance( $method . ':' . $path ); | |
| 184 | + $this->_logger->entrance( $method . ':' . $path ); | |
| 158 | 185 | |
| 159 | - if ( self::is_temporary_down() ) { | |
| 160 | - $result = $this->get_temporary_unavailable_error(); | |
| 161 | - } else { | |
| 162 | - $result = $this->_api->Api( $path, $method, $params ); | |
| 186 | + if ( self::is_temporary_down() ) { | |
| 187 | + $result = $this->get_temporary_unavailable_error(); | |
| 188 | + } else { | |
| 189 | + /** | |
| 190 | + * @since 2.3.0 Include the SDK version with all API requests that going through the API manager. IMPORTANT: Only pass the SDK version if the caller didn't include it yet. | |
| 191 | + */ | |
| 192 | + if ( ! empty( $this->_sdk_version ) ) { | |
| 193 | + if ( false === strpos( $path, 'sdk_version=' ) && | |
| 194 | + ! isset( $params['sdk_version'] ) | |
| 195 | + ) { | |
| 196 | + // Always add the sdk_version param in the querystring. DO NOT INCLUDE IT IN THE BODY PARAMS, OTHERWISE, IT MAY LEAD TO AN UNEXPECTED PARAMS PARSING IN CASES WHERE THE $params IS A REGULAR NON-ASSOCIATIVE ARRAY. | |
| 197 | + $path = add_query_arg( 'sdk_version', $this->_sdk_version, $path ); | |
| 198 | + } | |
| 199 | + } | |
| 163 | 200 | |
| 164 | - if ( null !== $result && | |
| 165 | - isset( $result->error ) && | |
| 166 | - isset( $result->error->code ) && | |
| 167 | - 'request_expired' === $result->error->code | |
| 168 | - ) { | |
| 169 | - if ( ! $retry ) { | |
| 170 | - $diff = isset( $result->error->timestamp ) ? | |
| 171 | - ( time() - strtotime( $result->error->timestamp ) ) : | |
| 172 | - false; | |
| 201 | + $result = $this->_api->Api( $path, $method, $params ); | |
| 173 | 202 | |
| 174 | - // Try to sync clock diff. | |
| 175 | - if ( false !== $this->_sync_clock_diff( $diff ) ) { | |
| 176 | - // Retry call with new synced clock. | |
| 177 | - return $this->_call( $path, $method, $params, true ); | |
| 178 | - } | |
| 179 | - } | |
| 180 | - } | |
| 181 | - } | |
| 203 | + if ( null !== $result && | |
| 204 | + isset( $result->error ) && | |
| 205 | + isset( $result->error->code ) && | |
| 206 | + 'request_expired' === $result->error->code | |
| 207 | + ) { | |
| 208 | + if ( ! $retry ) { | |
| 209 | + $diff = isset( $result->error->timestamp ) ? | |
| 210 | + ( time() - strtotime( $result->error->timestamp ) ) : | |
| 211 | + false; | |
| 182 | 212 | |
| 183 | - if ( $this->_logger->is_on() && self::is_api_error( $result ) ) { | |
| 184 | - // Log API errors. | |
| 185 | - $this->_logger->api_error( $result ); | |
| 186 | - } | |
| 213 | + // Try to sync clock diff. | |
| 214 | + if ( false !== $this->_sync_clock_diff( $diff ) ) { | |
| 215 | + // Retry call with new synced clock. | |
| 216 | + return $this->_call( $path, $method, $params, true ); | |
| 217 | + } | |
| 218 | + } | |
| 219 | + } | |
| 220 | + } | |
| 187 | 221 | |
| 188 | - return $result; | |
| 189 | - } | |
| 222 | + if ( $this->_logger->is_on() && self::is_api_error( $result ) ) { | |
| 223 | + // Log API errors. | |
| 224 | + $this->_logger->api_error( $result ); | |
| 225 | + } | |
| 190 | 226 | |
| 227 | + return $result; | |
| 228 | + } | |
| 229 | + | |
| 191 | 230 | /** |
| 192 | 231 | * Override API call to wrap it in servers' clock sync method. |
| 193 | 232 | * |
| 194 | 233 | * @param string $path |
| @@ -252,9 +291,9 @@ | ||
| 252 | 291 | /** |
| 253 | 292 | * If the response code is 404, cache the result for half of the `$expiration`. |
| 254 | 293 | * |
| 255 | 294 | * @author Leo Fajardo (@leorw) |
| 256 | - * @since 2.2.3.1 | |
| 295 | + * @since 2.2.4 | |
| 257 | 296 | */ |
| 258 | 297 | $expiration /= 2; |
| 259 | 298 | } else { |
| 260 | 299 | // If no older data version and the response code is not 404, return result without |