| @@ -55,17 +55,9 @@ | ||
| 55 | 55 | * @since 1.0.4 |
| 56 | 56 | */ |
| 57 | 57 | private $_logger; |
| 58 | 58 | |
| 59 | - /** | |
| 60 | - * @author Leo Fajardo (@leorw) | |
| 61 | - * @since 2.3.0 | |
| 62 | - * | |
| 63 | - * @var string | |
| 64 | - */ | |
| 65 | - private $_sdk_version; | |
| 66 | - | |
| 67 | - /** | |
| 59 | + /** | |
| 68 | 60 | * @param string $slug |
| 69 | 61 | * @param string $scope 'app', 'developer', 'user' or 'install'. |
| 70 | 62 | * @param number $id Element's id. |
| 71 | 63 | * @param string $public_key Public key. |
| @@ -70,27 +62,18 @@ | ||
| 70 | 62 | * @param number $id Element's id. |
| 71 | 63 | * @param string $public_key Public key. |
| 72 | 64 | * @param bool $is_sandbox |
| 73 | 65 | * @param bool|string $secret_key Element's secret key. |
| 74 | - * @param null|string $sdk_version | |
| 75 | 66 | * |
| 76 | 67 | * @return FS_Api |
| 77 | 68 | */ |
| 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 | - ) { | |
| 69 | + static function instance( $slug, $scope, $id, $public_key, $is_sandbox, $secret_key = false ) { | |
| 87 | 70 | $identifier = md5( $slug . $scope . $id . $public_key . ( is_string( $secret_key ) ? $secret_key : '' ) . json_encode( $is_sandbox ) ); |
| 88 | 71 | |
| 89 | 72 | if ( ! isset( self::$_instances[ $identifier ] ) ) { |
| 90 | 73 | self::_init(); |
| 91 | 74 | |
| 92 | - self::$_instances[ $identifier ] = new FS_Api( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox, $sdk_version ); | |
| 75 | + self::$_instances[ $identifier ] = new FS_Api( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox ); | |
| 93 | 76 | } |
| 94 | 77 | |
| 95 | 78 | return self::$_instances[ $identifier ]; |
| 96 | 79 | } |
| @@ -121,24 +104,14 @@ | ||
| 121 | 104 | * @param number $id Element's id. |
| 122 | 105 | * @param string $public_key Public key. |
| 123 | 106 | * @param bool|string $secret_key Element's secret key. |
| 124 | 107 | * @param bool $is_sandbox |
| 125 | - * @param null|string $sdk_version | |
| 126 | 108 | */ |
| 127 | - private function __construct( | |
| 128 | - $slug, | |
| 129 | - $scope, | |
| 130 | - $id, | |
| 131 | - $public_key, | |
| 132 | - $secret_key, | |
| 133 | - $is_sandbox, | |
| 134 | - $sdk_version | |
| 135 | - ) { | |
| 109 | + private function __construct( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox ) { | |
| 136 | 110 | $this->_api = new Freemius_Api_WordPress( $scope, $id, $public_key, $secret_key, $is_sandbox ); |
| 137 | 111 | |
| 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 ); | |
| 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 ); | |
| 141 | 114 | } |
| 142 | 115 | |
| 143 | 116 | /** |
| 144 | 117 | * Find clock diff between server and API server, and store the diff locally. |
| @@ -180,54 +153,42 @@ | ||
| 180 | 153 | * |
| 181 | 154 | * @return array|mixed|string|void |
| 182 | 155 | */ |
| 183 | 156 | private function _call( $path, $method = 'GET', $params = array(), $retry = false ) { |
| 184 | - $this->_logger->entrance( $method . ':' . $path ); | |
| 157 | + $this->_logger->entrance( $method . ':' . $path ); | |
| 185 | 158 | |
| 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 | - } | |
| 159 | + if ( self::is_temporary_down() ) { | |
| 160 | + $result = $this->get_temporary_unavailable_error(); | |
| 161 | + } else { | |
| 162 | + $result = $this->_api->Api( $path, $method, $params ); | |
| 200 | 163 | |
| 201 | - $result = $this->_api->Api( $path, $method, $params ); | |
| 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; | |
| 202 | 173 | |
| 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; | |
| 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 | + } | |
| 212 | 182 | |
| 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 | - } | |
| 183 | + if ( $this->_logger->is_on() && self::is_api_error( $result ) ) { | |
| 184 | + // Log API errors. | |
| 185 | + $this->_logger->api_error( $result ); | |
| 186 | + } | |
| 221 | 187 | |
| 222 | - if ( $this->_logger->is_on() && self::is_api_error( $result ) ) { | |
| 223 | - // Log API errors. | |
| 224 | - $this->_logger->api_error( $result ); | |
| 225 | - } | |
| 188 | + return $result; | |
| 189 | + } | |
| 226 | 190 | |
| 227 | - return $result; | |
| 228 | - } | |
| 229 | - | |
| 230 | 191 | /** |
| 231 | 192 | * Override API call to wrap it in servers' clock sync method. |
| 232 | 193 | * |
| 233 | 194 | * @param string $path |
| @@ -291,9 +252,9 @@ | ||
| 291 | 252 | /** |
| 292 | 253 | * If the response code is 404, cache the result for half of the `$expiration`. |
| 293 | 254 | * |
| 294 | 255 | * @author Leo Fajardo (@leorw) |
| 295 | - * @since 2.2.4 | |
| 256 | + * @since 2.2.3.1 | |
| 296 | 257 | */ |
| 297 | 258 | $expiration /= 2; |
| 298 | 259 | } else { |
| 299 | 260 | // If no older data version and the response code is not 404, return result without |