| @@ -4,36 +4,21 @@ | ||
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | class FrmFormApi { |
| 7 | 7 | |
| 8 | - /** | |
| 9 | - * @var string | |
| 10 | - */ | |
| 11 | 8 | protected $license = ''; |
| 12 | - | |
| 13 | - /** | |
| 14 | - * @var string | |
| 15 | - */ | |
| 16 | 9 | protected $cache_key = ''; |
| 17 | - | |
| 18 | - /** | |
| 19 | - * @var string | |
| 20 | - */ | |
| 21 | 10 | protected $cache_timeout = '+6 hours'; |
| 22 | 11 | |
| 23 | 12 | /** |
| 24 | 13 | * The number of days an add-on is new. |
| 25 | 14 | * |
| 26 | - * @var int | |
| 15 | + * @var int $new_days | |
| 27 | 16 | */ |
| 28 | 17 | protected $new_days = 90; |
| 29 | 18 | |
| 30 | 19 | /** |
| 31 | 20 | * @since 3.06 |
| 32 | - * | |
| 33 | - * @param string|null $license The license key. | |
| 34 | - * | |
| 35 | - * @return void | |
| 36 | 21 | */ |
| 37 | 22 | public function __construct( $license = null ) { |
| 38 | 23 | $this->set_license( $license ); |
| 39 | 24 | $this->set_cache_key(); |
| @@ -41,17 +26,14 @@ | ||
| 41 | 26 | |
| 42 | 27 | /** |
| 43 | 28 | * @since 3.06 |
| 44 | 29 | * |
| 45 | - * @param string|null $license The license key. | |
| 46 | - * | |
| 47 | 30 | * @return void |
| 48 | 31 | */ |
| 49 | 32 | private function set_license( $license ) { |
| 50 | 33 | if ( $license === null ) { |
| 51 | 34 | $edd_update = $this->get_pro_updater(); |
| 52 | - | |
| 53 | - if ( $edd_update ) { | |
| 35 | + if ( ! empty( $edd_update ) ) { | |
| 54 | 36 | $license = $edd_update->license; |
| 55 | 37 | } |
| 56 | 38 | } |
| 57 | 39 | $this->license = $license; |
| @@ -58,9 +40,8 @@ | ||
| 58 | 40 | } |
| 59 | 41 | |
| 60 | 42 | /** |
| 61 | 43 | * @since 3.06 |
| 62 | - * | |
| 63 | 44 | * @return string |
| 64 | 45 | */ |
| 65 | 46 | public function get_license() { |
| 66 | 47 | return $this->license; |
| @@ -76,9 +57,8 @@ | ||
| 76 | 57 | } |
| 77 | 58 | |
| 78 | 59 | /** |
| 79 | 60 | * @since 3.06 |
| 80 | - * | |
| 81 | 61 | * @return string |
| 82 | 62 | */ |
| 83 | 63 | public function get_cache_key() { |
| 84 | 64 | return $this->cache_key; |
| @@ -85,34 +65,23 @@ | ||
| 85 | 65 | } |
| 86 | 66 | |
| 87 | 67 | /** |
| 88 | 68 | * @since 3.06 |
| 89 | - * | |
| 90 | 69 | * @return array |
| 91 | 70 | */ |
| 92 | 71 | public function get_api_info() { |
| 93 | 72 | $url = $this->api_url(); |
| 94 | - | |
| 95 | 73 | if ( ! empty( $this->license ) ) { |
| 96 | 74 | $url .= '?l=' . urlencode( base64_encode( $this->license ) ); |
| 97 | 75 | } |
| 98 | 76 | |
| 99 | 77 | $addons = $this->get_cached(); |
| 100 | - | |
| 101 | 78 | if ( is_array( $addons ) ) { |
| 102 | 79 | return $addons; |
| 103 | 80 | } |
| 104 | 81 | |
| 105 | - if ( $this->is_running() ) { | |
| 106 | - // If there's no saved cache, we'll need to wait for the current request to finish. | |
| 107 | - return array(); | |
| 108 | - } | |
| 109 | - | |
| 110 | - $this->set_running(); | |
| 111 | - | |
| 112 | 82 | // We need to know the version number to allow different downloads. |
| 113 | 83 | $agent = 'formidable/' . FrmAppHelper::plugin_version(); |
| 114 | - | |
| 115 | 84 | if ( class_exists( 'FrmProDb' ) ) { |
| 116 | 85 | $agent = 'formidable-pro/' . FrmProDb::$plug_version; |
| 117 | 86 | } |
| 118 | 87 | |
| @@ -131,10 +100,8 @@ | ||
| 131 | 100 | if ( ! is_array( $addons ) ) { |
| 132 | 101 | $addons = array(); |
| 133 | 102 | } |
| 134 | 103 | |
| 135 | - $addons['response_code'] = wp_remote_retrieve_response_code( $response ); | |
| 136 | - | |
| 137 | 104 | foreach ( $addons as $k => $addon ) { |
| 138 | 105 | if ( ! is_array( $addon ) ) { |
| 139 | 106 | continue; |
| 140 | 107 | } |
| @@ -140,10 +107,9 @@ | ||
| 140 | 107 | } |
| 141 | 108 | |
| 142 | 109 | if ( isset( $addon['categories'] ) ) { |
| 143 | 110 | $cats = array_intersect( $this->skip_categories(), $addon['categories'] ); |
| 144 | - | |
| 145 | - if ( $cats ) { | |
| 111 | + if ( ! empty( $cats ) ) { | |
| 146 | 112 | unset( $addons[ $k ] ); |
| 147 | 113 | continue; |
| 148 | 114 | } |
| 149 | 115 | } |
| @@ -153,86 +119,18 @@ | ||
| 153 | 119 | } |
| 154 | 120 | } |
| 155 | 121 | |
| 156 | 122 | $this->set_cached( $addons ); |
| 157 | - $this->done_running(); | |
| 158 | 123 | |
| 159 | 124 | return $addons; |
| 160 | 125 | } |
| 161 | 126 | |
| 162 | 127 | /** |
| 163 | - * Prevent multiple requests from running at the same time. | |
| 164 | - * | |
| 165 | - * @since 6.8.3 | |
| 166 | - * | |
| 167 | - * @return bool | |
| 168 | - */ | |
| 169 | - protected function is_running() { | |
| 170 | - if ( $this->run_as_multisite() ) { | |
| 171 | - return get_site_transient( $this->transient_key() ); | |
| 172 | - } | |
| 173 | - return get_transient( $this->transient_key() ); | |
| 174 | - } | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * @since 6.8.3 | |
| 178 | - * | |
| 179 | - * @return void | |
| 180 | - */ | |
| 181 | - protected function set_running() { | |
| 182 | - $expires = 2 * MINUTE_IN_SECONDS; | |
| 183 | - | |
| 184 | - if ( $this->run_as_multisite() ) { | |
| 185 | - set_site_transient( $this->transient_key(), true, $expires ); | |
| 186 | - return; | |
| 187 | - } | |
| 188 | - | |
| 189 | - set_transient( $this->transient_key(), true, $expires ); | |
| 190 | - } | |
| 191 | - | |
| 192 | - /** | |
| 193 | - * @since 6.8.3 | |
| 194 | - * | |
| 195 | - * @return void | |
| 196 | - */ | |
| 197 | - protected function done_running() { | |
| 198 | - if ( $this->run_as_multisite() ) { | |
| 199 | - delete_site_transient( $this->transient_key() ); | |
| 200 | - } | |
| 201 | - delete_transient( $this->transient_key() ); | |
| 202 | - } | |
| 203 | - | |
| 204 | - /** | |
| 205 | - * Only allow one site in the network to make the api request at a time. | |
| 206 | - * If there is a license for the request, run individually. | |
| 207 | - * | |
| 208 | - * @since 6.8.3 | |
| 209 | - * | |
| 210 | - * @return bool | |
| 211 | - */ | |
| 212 | - protected function run_as_multisite() { | |
| 213 | - return is_multisite() && empty( $this->license ); | |
| 214 | - } | |
| 215 | - | |
| 216 | - /** | |
| 217 | - * @since 6.8.3 | |
| 218 | - * | |
| 219 | - * @return string | |
| 220 | - */ | |
| 221 | - protected function transient_key() { | |
| 222 | - return strtolower( self::class ) . '_request_lock'; | |
| 223 | - } | |
| 224 | - | |
| 225 | - /** | |
| 226 | 128 | * @since 3.06 |
| 227 | 129 | * |
| 228 | 130 | * @return string |
| 229 | 131 | */ |
| 230 | 132 | protected function api_url() { |
| 231 | - if ( empty( $this->license ) ) { | |
| 232 | - // Direct traffic to Cloudflare worker when there is no license. | |
| 233 | - return 'https://plapi.formidableforms.com/list/'; | |
| 234 | - } | |
| 235 | 133 | return 'https://formidableforms.com/wp-json/s11edd/v1/updates/'; |
| 236 | 134 | } |
| 237 | 135 | |
| 238 | 136 | /** |
| @@ -246,24 +144,22 @@ | ||
| 246 | 144 | |
| 247 | 145 | /** |
| 248 | 146 | * @since 3.06 |
| 249 | 147 | * |
| 250 | - * @param object $license_plugin The FrmAddon object. | |
| 251 | - * @param array $addons | |
| 148 | + * @param object $license_plugin The FrmAddon object | |
| 149 | + * @param array $addons | |
| 252 | 150 | * |
| 253 | 151 | * @return array |
| 254 | 152 | */ |
| 255 | 153 | public function get_addon_for_license( $license_plugin, $addons = array() ) { |
| 256 | - if ( ! $addons ) { | |
| 154 | + if ( empty( $addons ) ) { | |
| 257 | 155 | $addons = $this->get_api_info(); |
| 258 | 156 | } |
| 259 | - | |
| 260 | 157 | $download_id = $license_plugin->download_id; |
| 261 | 158 | $plugin = array(); |
| 262 | - | |
| 263 | - if ( ! $download_id && ! empty( $addons ) ) { | |
| 159 | + if ( empty( $download_id ) && ! empty( $addons ) ) { | |
| 264 | 160 | foreach ( $addons as $addon ) { |
| 265 | - if ( is_array( $addon ) && ! empty( $addon['title'] ) && strtolower( $license_plugin->plugin_name ) === strtolower( $addon['title'] ) ) { | |
| 161 | + if ( strtolower( $license_plugin->plugin_name ) == strtolower( $addon['title'] ) ) { | |
| 266 | 162 | return $addon; |
| 267 | 163 | } |
| 268 | 164 | } |
| 269 | 165 | } elseif ( isset( $addons[ $download_id ] ) ) { |
| @@ -274,10 +170,8 @@ | ||
| 274 | 170 | } |
| 275 | 171 | |
| 276 | 172 | /** |
| 277 | 173 | * @since 3.06 |
| 278 | - * | |
| 279 | - * @return false|object | |
| 280 | 174 | */ |
| 281 | 175 | public function get_pro_updater() { |
| 282 | 176 | if ( FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) ) { |
| 283 | 177 | $updater = FrmProAppHelper::get_updater(); |
| @@ -290,28 +184,23 @@ | ||
| 290 | 184 | } |
| 291 | 185 | |
| 292 | 186 | /** |
| 293 | 187 | * @since 3.06 |
| 294 | - * | |
| 295 | - * @return array|bool | |
| 188 | + * @return array | |
| 296 | 189 | */ |
| 297 | 190 | protected function get_cached() { |
| 298 | - $cache = $this->get_cached_option(); | |
| 191 | + $cache = get_option( $this->cache_key ); | |
| 299 | 192 | |
| 300 | - if ( ! $cache ) { | |
| 301 | - return false; | |
| 302 | - } | |
| 193 | + FrmAppHelper::filter_gmt_offset(); | |
| 303 | 194 | |
| 304 | - $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout']; | |
| 305 | - | |
| 306 | - if ( ! $is_expired && isset( $cache['version'] ) && $cache['version'] !== FrmAppHelper::plugin_version() ) { | |
| 307 | - $is_expired = true; | |
| 195 | + if ( empty( $cache ) || empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) { | |
| 196 | + return false; // Cache is expired | |
| 308 | 197 | } |
| 309 | 198 | |
| 310 | - // Avoid old cached data, unless we're currently trying to query for new data. | |
| 311 | - // The call to $this->is_running likely triggers a database query, so only call if if we're expired. | |
| 312 | - // (Rather than the other way around, which is less efficient). | |
| 313 | - if ( $is_expired && ! $this->is_running() ) { | |
| 199 | + $version = FrmAppHelper::plugin_version(); | |
| 200 | + $for_current = isset( $cache['version'] ) && $cache['version'] == $version; | |
| 201 | + if ( ! $for_current ) { | |
| 202 | + // Force a new check. | |
| 314 | 203 | return false; |
| 315 | 204 | } |
| 316 | 205 | |
| 317 | 206 | return json_decode( $cache['value'], true ); |
| @@ -317,27 +206,8 @@ | ||
| 317 | 206 | return json_decode( $cache['value'], true ); |
| 318 | 207 | } |
| 319 | 208 | |
| 320 | 209 | /** |
| 321 | - * Get the cache for the network if multisite. | |
| 322 | - * | |
| 323 | - * @since 6.8.3 | |
| 324 | - * | |
| 325 | - * @return mixed | |
| 326 | - */ | |
| 327 | - protected function get_cached_option() { | |
| 328 | - if ( is_multisite() ) { | |
| 329 | - $cached = get_site_option( $this->cache_key ); | |
| 330 | - | |
| 331 | - if ( $cached ) { | |
| 332 | - return $cached; | |
| 333 | - } | |
| 334 | - } | |
| 335 | - | |
| 336 | - return get_option( $this->cache_key ); | |
| 337 | - } | |
| 338 | - | |
| 339 | - /** | |
| 340 | 210 | * @since 3.06 |
| 341 | 211 | * |
| 342 | 212 | * @param array $addons |
| 343 | 213 | * |
| @@ -343,86 +213,52 @@ | ||
| 343 | 213 | * |
| 344 | 214 | * @return void |
| 345 | 215 | */ |
| 346 | 216 | protected function set_cached( $addons ) { |
| 217 | + FrmAppHelper::filter_gmt_offset(); | |
| 218 | + | |
| 347 | 219 | $data = array( |
| 348 | - 'timeout' => strtotime( $this->get_cache_timeout( $addons ), time() ), | |
| 349 | - 'value' => wp_json_encode( $addons ), | |
| 220 | + 'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ), | |
| 221 | + 'value' => json_encode( $addons ), | |
| 350 | 222 | 'version' => FrmAppHelper::plugin_version(), |
| 351 | 223 | ); |
| 352 | 224 | |
| 353 | - if ( is_multisite() ) { | |
| 354 | - update_site_option( $this->cache_key, $data ); | |
| 355 | - } else { | |
| 356 | - // Autoload the license cache because it gets called everywhere. | |
| 357 | - $autoload = str_starts_with( $this->cache_key, 'frm_addons_l' ); | |
| 358 | - update_option( $this->cache_key, $data, $autoload ); | |
| 359 | - } | |
| 225 | + update_option( $this->cache_key, $data, 'no' ); | |
| 360 | 226 | } |
| 361 | 227 | |
| 362 | 228 | /** |
| 363 | - * If the last check was a a rate limit, we'll need to check again sooner. | |
| 364 | - * | |
| 365 | - * @since 6.8.3 | |
| 366 | - * | |
| 367 | - * @param array $addons | |
| 368 | - * | |
| 369 | - * @return string | |
| 370 | - */ | |
| 371 | - protected function get_cache_timeout( $addons ) { | |
| 372 | - if ( isset( $addons['response_code'] ) && 429 === $addons['response_code'] ) { | |
| 373 | - return '+5 minutes'; | |
| 374 | - } | |
| 375 | - return $this->cache_timeout; | |
| 376 | - } | |
| 377 | - | |
| 378 | - /** | |
| 379 | 229 | * @since 3.06 |
| 380 | 230 | * |
| 381 | 231 | * @return void |
| 382 | 232 | */ |
| 383 | 233 | public function reset_cached() { |
| 384 | - if ( is_multisite() ) { | |
| 385 | - delete_site_option( $this->cache_key ); | |
| 386 | - } else { | |
| 387 | - delete_option( $this->cache_key ); | |
| 388 | - } | |
| 389 | - $this->done_running(); | |
| 234 | + delete_option( $this->cache_key ); | |
| 390 | 235 | } |
| 391 | 236 | |
| 392 | 237 | /** |
| 393 | 238 | * @since 3.06 |
| 394 | - * | |
| 395 | 239 | * @return array |
| 396 | 240 | */ |
| 397 | 241 | public function error_for_license() { |
| 242 | + $errors = array(); | |
| 398 | 243 | if ( ! empty( $this->license ) ) { |
| 399 | - return $this->get_error_from_response(); | |
| 244 | + $errors = $this->get_error_from_response(); | |
| 400 | 245 | } |
| 401 | - return array(); | |
| 246 | + | |
| 247 | + return $errors; | |
| 402 | 248 | } |
| 403 | 249 | |
| 404 | 250 | /** |
| 405 | 251 | * @since 3.06 |
| 406 | - * | |
| 407 | - * @param array $addons | |
| 408 | - * | |
| 409 | 252 | * @return array |
| 410 | 253 | */ |
| 411 | 254 | public function get_error_from_response( $addons = array() ) { |
| 412 | - if ( ! $addons ) { | |
| 255 | + if ( empty( $addons ) ) { | |
| 413 | 256 | $addons = $this->get_api_info(); |
| 414 | 257 | } |
| 415 | - | |
| 416 | 258 | $errors = array(); |
| 417 | - | |
| 418 | 259 | if ( isset( $addons['error'] ) ) { |
| 419 | - if ( is_string( $addons['error'] ) ) { | |
| 420 | - $errors[] = $addons['error']; | |
| 421 | - } elseif ( ! empty( $addons['error']['message'] ) ) { | |
| 422 | - $errors[] = $addons['error']['message']; | |
| 423 | - } | |
| 424 | - | |
| 260 | + $errors[] = $addons['error']['message']; | |
| 425 | 261 | do_action( 'frm_license_error', $addons['error'] ); |
| 426 | 262 | } |
| 427 | 263 | |
| 428 | 264 | return $errors; |
| @@ -433,9 +269,8 @@ | ||
| 433 | 269 | * |
| 434 | 270 | * @since 6.0 |
| 435 | 271 | * |
| 436 | 272 | * @param array $addon |
| 437 | - * | |
| 438 | 273 | * @return bool |
| 439 | 274 | */ |
| 440 | 275 | protected function is_new( $addon ) { |
| 441 | 276 | return strtotime( $addon['released'] ) > strtotime( '-' . $this->new_days . ' days' ); |