| @@ -4,31 +4,13 @@ | ||
| 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 | 14 | */ |
| 33 | 15 | public function __construct( $license = null ) { |
| 34 | 16 | $this->set_license( $license ); |
| @@ -36,10 +18,8 @@ | ||
| 36 | 18 | } |
| 37 | 19 | |
| 38 | 20 | /** |
| 39 | 21 | * @since 3.06 |
| 40 | - * | |
| 41 | - * @return void | |
| 42 | 22 | */ |
| 43 | 23 | private function set_license( $license ) { |
| 44 | 24 | if ( $license === null ) { |
| 45 | 25 | $edd_update = $this->get_pro_updater(); |
| @@ -59,10 +39,8 @@ | ||
| 59 | 39 | } |
| 60 | 40 | |
| 61 | 41 | /** |
| 62 | 42 | * @since 3.06 |
| 63 | - * | |
| 64 | - * @return void | |
| 65 | 43 | */ |
| 66 | 44 | protected function set_cache_key() { |
| 67 | 45 | $this->cache_key = 'frm_addons_l' . ( empty( $this->license ) ? '' : md5( $this->license ) ); |
| 68 | 46 | } |
| @@ -89,15 +67,8 @@ | ||
| 89 | 67 | if ( is_array( $addons ) ) { |
| 90 | 68 | return $addons; |
| 91 | 69 | } |
| 92 | 70 | |
| 93 | - if ( $this->is_running() ) { | |
| 94 | - // If there's no saved cache, we'll need to wait for the current request to finish. | |
| 95 | - return array(); | |
| 96 | - } | |
| 97 | - | |
| 98 | - $this->set_running(); | |
| 99 | - | |
| 100 | 71 | // We need to know the version number to allow different downloads. |
| 101 | 72 | $agent = 'formidable/' . FrmAppHelper::plugin_version(); |
| 102 | 73 | if ( class_exists( 'FrmProDb' ) ) { |
| 103 | 74 | $agent = 'formidable-pro/' . FrmProDb::$plug_version; |
| @@ -118,113 +89,32 @@ | ||
| 118 | 89 | if ( ! is_array( $addons ) ) { |
| 119 | 90 | $addons = array(); |
| 120 | 91 | } |
| 121 | 92 | |
| 122 | - $addons['response_code'] = wp_remote_retrieve_response_code( $response ); | |
| 123 | - | |
| 124 | 93 | foreach ( $addons as $k => $addon ) { |
| 125 | - if ( ! is_array( $addon ) ) { | |
| 94 | + if ( ! isset( $addon['categories'] ) ) { | |
| 126 | 95 | continue; |
| 127 | 96 | } |
| 128 | - | |
| 129 | - if ( isset( $addon['categories'] ) ) { | |
| 130 | - $cats = array_intersect( $this->skip_categories(), $addon['categories'] ); | |
| 131 | - if ( ! empty( $cats ) ) { | |
| 132 | - unset( $addons[ $k ] ); | |
| 133 | - continue; | |
| 134 | - } | |
| 97 | + $cats = array_intersect( $this->skip_categories(), $addon['categories'] ); | |
| 98 | + if ( ! empty( $cats ) ) { | |
| 99 | + unset( $addons[ $k ] ); | |
| 135 | 100 | } |
| 136 | - | |
| 137 | - if ( ! array_key_exists( 'is_new', $addon ) && array_key_exists( 'released', $addon ) ) { | |
| 138 | - $addons[ $k ]['is_new'] = $this->is_new( $addon ); | |
| 139 | - } | |
| 140 | 101 | } |
| 141 | 102 | |
| 142 | 103 | $this->set_cached( $addons ); |
| 143 | - $this->done_running(); | |
| 144 | 104 | |
| 145 | 105 | return $addons; |
| 146 | 106 | } |
| 147 | 107 | |
| 148 | 108 | /** |
| 149 | - * Prevent multiple requests from running at the same time. | |
| 150 | - * | |
| 151 | - * @since 6.8.3 | |
| 152 | - * | |
| 153 | - * @return bool | |
| 154 | - */ | |
| 155 | - protected function is_running() { | |
| 156 | - if ( $this->run_as_multisite() ) { | |
| 157 | - return get_site_transient( $this->transient_key() ); | |
| 158 | - } | |
| 159 | - return get_transient( $this->transient_key() ); | |
| 160 | - } | |
| 161 | - | |
| 162 | - /** | |
| 163 | - * @since 6.8.3 | |
| 164 | - * | |
| 165 | - * @return void | |
| 166 | - */ | |
| 167 | - protected function set_running() { | |
| 168 | - $expires = 2 * MINUTE_IN_SECONDS; | |
| 169 | - if ( $this->run_as_multisite() ) { | |
| 170 | - set_site_transient( $this->transient_key(), true, $expires ); | |
| 171 | - return; | |
| 172 | - } | |
| 173 | - | |
| 174 | - set_transient( $this->transient_key(), true, $expires ); | |
| 175 | - } | |
| 176 | - | |
| 177 | - /** | |
| 178 | - * @since 6.8.3 | |
| 179 | - * | |
| 180 | - * @return void | |
| 181 | - */ | |
| 182 | - protected function done_running() { | |
| 183 | - if ( $this->run_as_multisite() ) { | |
| 184 | - delete_site_transient( $this->transient_key() ); | |
| 185 | - } | |
| 186 | - delete_transient( $this->transient_key() ); | |
| 187 | - } | |
| 188 | - | |
| 189 | - /** | |
| 190 | - * Only allow one site in the network to make the api request at a time. | |
| 191 | - * If there is a license for the request, run individually. | |
| 192 | - * | |
| 193 | - * @since 6.8.3 | |
| 194 | - * | |
| 195 | - * @return bool | |
| 196 | - */ | |
| 197 | - protected function run_as_multisite() { | |
| 198 | - return is_multisite() && empty( $this->license ); | |
| 199 | - } | |
| 200 | - | |
| 201 | - /** | |
| 202 | - * @since 6.8.3 | |
| 203 | - * | |
| 204 | - * @return string | |
| 205 | - */ | |
| 206 | - protected function transient_key() { | |
| 207 | - return strtolower( self::class ) . '_request_lock'; | |
| 208 | - } | |
| 209 | - | |
| 210 | - /** | |
| 211 | 109 | * @since 3.06 |
| 212 | - * | |
| 213 | - * @return string | |
| 214 | 110 | */ |
| 215 | 111 | protected function api_url() { |
| 216 | - if ( empty( $this->license ) ) { | |
| 217 | - // Direct traffic to Cloudflare worker when there is no license. | |
| 218 | - return 'https://plapi.formidableforms.com/list/'; | |
| 219 | - } | |
| 220 | 112 | return 'https://formidableforms.com/wp-json/s11edd/v1/updates/'; |
| 221 | 113 | } |
| 222 | 114 | |
| 223 | 115 | /** |
| 224 | 116 | * @since 3.06 |
| 225 | - * | |
| 226 | - * @return string[] | |
| 227 | 117 | */ |
| 228 | 118 | protected function skip_categories() { |
| 229 | 119 | return array( 'WordPress Form Templates', 'WordPress Form Style Templates' ); |
| 230 | 120 | } |
| @@ -231,10 +121,9 @@ | ||
| 231 | 121 | |
| 232 | 122 | /** |
| 233 | 123 | * @since 3.06 |
| 234 | 124 | * |
| 235 | - * @param object $license_plugin The FrmAddon object. | |
| 236 | - * @param array $addons | |
| 125 | + * @param object $license_plugin The FrmAddon object | |
| 237 | 126 | * |
| 238 | 127 | * @return array |
| 239 | 128 | */ |
| 240 | 129 | public function get_addon_for_license( $license_plugin, $addons = array() ) { |
| @@ -244,9 +133,9 @@ | ||
| 244 | 133 | $download_id = $license_plugin->download_id; |
| 245 | 134 | $plugin = array(); |
| 246 | 135 | if ( empty( $download_id ) && ! empty( $addons ) ) { |
| 247 | 136 | foreach ( $addons as $addon ) { |
| 248 | - 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'] ) ) { | |
| 249 | 138 | return $addon; |
| 250 | 139 | } |
| 251 | 140 | } |
| 252 | 141 | } elseif ( isset( $addons[ $download_id ] ) ) { |
| @@ -271,105 +160,49 @@ | ||
| 271 | 160 | } |
| 272 | 161 | |
| 273 | 162 | /** |
| 274 | 163 | * @since 3.06 |
| 275 | - * | |
| 276 | - * @return array|bool | |
| 164 | + * @return array | |
| 277 | 165 | */ |
| 278 | 166 | protected function get_cached() { |
| 279 | - $cache = $this->get_cached_option(); | |
| 280 | - if ( empty( $cache ) ) { | |
| 281 | - return false; | |
| 282 | - } | |
| 167 | + $cache = get_option( $this->cache_key ); | |
| 283 | 168 | |
| 284 | - // If the api call is running, we can use the expired cache. | |
| 285 | - if ( ! $this->is_running() ) { | |
| 286 | - if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) { | |
| 287 | - // Cache is expired. | |
| 288 | - return false; | |
| 289 | - } | |
| 169 | + FrmAppHelper::filter_gmt_offset(); | |
| 290 | 170 | |
| 291 | - $version = FrmAppHelper::plugin_version(); | |
| 292 | - $for_current = isset( $cache['version'] ) && $cache['version'] == $version; | |
| 293 | - if ( ! $for_current ) { | |
| 294 | - // Force a new check. | |
| 295 | - return false; | |
| 296 | - } | |
| 171 | + if ( empty( $cache ) || empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) { | |
| 172 | + return false; // Cache is expired | |
| 297 | 173 | } |
| 298 | 174 | |
| 299 | - $values = json_decode( $cache['value'], true ); | |
| 300 | - | |
| 301 | - return $values; | |
| 302 | - } | |
| 303 | - | |
| 304 | - /** | |
| 305 | - * Get the cache for the network if multisite. | |
| 306 | - * | |
| 307 | - * @since 6.8.3 | |
| 308 | - * | |
| 309 | - * @return mixed | |
| 310 | - */ | |
| 311 | - protected function get_cached_option() { | |
| 312 | - if ( is_multisite() ) { | |
| 313 | - $cached = get_site_option( $this->cache_key ); | |
| 314 | - if ( $cached ) { | |
| 315 | - return $cached; | |
| 316 | - } | |
| 175 | + $version = FrmAppHelper::plugin_version(); | |
| 176 | + $for_current = isset( $cache['version'] ) && $cache['version'] == $version; | |
| 177 | + if ( ! $for_current ) { | |
| 178 | + // Force a new check. | |
| 179 | + return false; | |
| 317 | 180 | } |
| 318 | 181 | |
| 319 | - return get_option( $this->cache_key ); | |
| 182 | + return json_decode( $cache['value'], true ); | |
| 320 | 183 | } |
| 321 | 184 | |
| 322 | 185 | /** |
| 323 | 186 | * @since 3.06 |
| 324 | - * | |
| 325 | - * @param array $addons | |
| 326 | - * | |
| 327 | - * @return void | |
| 328 | 187 | */ |
| 329 | 188 | protected function set_cached( $addons ) { |
| 189 | + FrmAppHelper::filter_gmt_offset(); | |
| 190 | + | |
| 330 | 191 | $data = array( |
| 331 | - 'timeout' => strtotime( $this->get_cache_timeout( $addons ), time() ), | |
| 332 | - 'value' => wp_json_encode( $addons ), | |
| 192 | + 'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ), | |
| 193 | + 'value' => json_encode( $addons ), | |
| 333 | 194 | 'version' => FrmAppHelper::plugin_version(), |
| 334 | 195 | ); |
| 335 | 196 | |
| 336 | - if ( is_multisite() ) { | |
| 337 | - update_site_option( $this->cache_key, $data ); | |
| 338 | - } else { | |
| 339 | - update_option( $this->cache_key, $data, 'no' ); | |
| 340 | - } | |
| 197 | + update_option( $this->cache_key, $data, 'no' ); | |
| 341 | 198 | } |
| 342 | 199 | |
| 343 | 200 | /** |
| 344 | - * If the last check was a a rate limit, we'll need to check again sooner. | |
| 345 | - * | |
| 346 | - * @since 6.8.3 | |
| 347 | - * | |
| 348 | - * @param array $addons | |
| 349 | - * | |
| 350 | - * @return string | |
| 351 | - */ | |
| 352 | - protected function get_cache_timeout( $addons ) { | |
| 353 | - $timeout = $this->cache_timeout; | |
| 354 | - if ( isset( $addons['response_code'] ) && 429 === $addons['response_code'] ) { | |
| 355 | - $timeout = '+5 minutes'; | |
| 356 | - } | |
| 357 | - return $timeout; | |
| 358 | - } | |
| 359 | - | |
| 360 | - /** | |
| 361 | 201 | * @since 3.06 |
| 362 | - * | |
| 363 | - * @return void | |
| 364 | 202 | */ |
| 365 | 203 | public function reset_cached() { |
| 366 | - if ( is_multisite() ) { | |
| 367 | - delete_site_option( $this->cache_key ); | |
| 368 | - } else { | |
| 369 | - delete_option( $this->cache_key ); | |
| 370 | - } | |
| 371 | - $this->done_running(); | |
| 204 | + delete_option( $this->cache_key ); | |
| 372 | 205 | } |
| 373 | 206 | |
| 374 | 207 | /** |
| 375 | 208 | * @since 3.06 |
| @@ -393,28 +226,11 @@ | ||
| 393 | 226 | $addons = $this->get_api_info(); |
| 394 | 227 | } |
| 395 | 228 | $errors = array(); |
| 396 | 229 | if ( isset( $addons['error'] ) ) { |
| 397 | - if ( is_string( $addons['error'] ) ) { | |
| 398 | - $errors[] = $addons['error']; | |
| 399 | - } elseif ( ! empty( $addons['error']['message'] ) ) { | |
| 400 | - $errors[] = $addons['error']['message']; | |
| 401 | - } | |
| 402 | - | |
| 230 | + $errors[] = $addons['error']['message']; | |
| 403 | 231 | do_action( 'frm_license_error', $addons['error'] ); |
| 404 | 232 | } |
| 405 | 233 | |
| 406 | 234 | return $errors; |
| 407 | - } | |
| 408 | - | |
| 409 | - /** | |
| 410 | - * Check if a template is new. | |
| 411 | - * | |
| 412 | - * @since 6.0 | |
| 413 | - * | |
| 414 | - * @param array $addon | |
| 415 | - * @return bool | |
| 416 | - */ | |
| 417 | - protected function is_new( $addon ) { | |
| 418 | - return strtotime( $addon['released'] ) > strtotime( '-' . $this->new_days . ' days' ); | |
| 419 | 235 | } |
| 420 | 236 | } |