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