| @@ -4,22 +4,82 @@ | ||
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | /** @phpstan-consistent-constructor */ |
| 7 | 7 | class FrmAddon { |
| 8 | + | |
| 9 | + /** | |
| 10 | + * @var string | |
| 11 | + */ | |
| 8 | 12 | public $store_url = 'https://formidableforms.com'; |
| 13 | + | |
| 14 | + /** | |
| 15 | + * @var int|string|null | |
| 16 | + */ | |
| 9 | 17 | public $download_id; |
| 18 | + | |
| 19 | + /** | |
| 20 | + * @var string|null | |
| 21 | + */ | |
| 10 | 22 | public $plugin_file; |
| 23 | + | |
| 24 | + /** | |
| 25 | + * @var string | |
| 26 | + */ | |
| 11 | 27 | public $plugin_folder; |
| 28 | + | |
| 29 | + /** | |
| 30 | + * @var string | |
| 31 | + */ | |
| 12 | 32 | public $plugin_name; |
| 33 | + | |
| 34 | + /** | |
| 35 | + * @var string | |
| 36 | + */ | |
| 13 | 37 | public $plugin_slug; |
| 38 | + | |
| 39 | + /** | |
| 40 | + * @var string | |
| 41 | + */ | |
| 14 | 42 | public $option_name; |
| 43 | + | |
| 44 | + /** | |
| 45 | + * @var string|null | |
| 46 | + */ | |
| 15 | 47 | public $version; |
| 16 | - public $author = 'Strategy11'; | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * @var string | |
| 51 | + */ | |
| 52 | + public $author = 'Strategy11'; | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * @var bool | |
| 56 | + */ | |
| 17 | 57 | public $is_parent_licence = false; |
| 18 | - public $needs_license = true; | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * @var bool | |
| 61 | + */ | |
| 62 | + public $needs_license = true; | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * @var bool | |
| 66 | + */ | |
| 19 | 67 | private $is_expired_addon = false; |
| 68 | + | |
| 69 | + /** | |
| 70 | + * @var string|null | |
| 71 | + */ | |
| 20 | 72 | public $license; |
| 73 | + | |
| 74 | + /** | |
| 75 | + * @var bool | |
| 76 | + */ | |
| 21 | 77 | protected $get_beta = false; |
| 78 | + | |
| 79 | + /** | |
| 80 | + * @var array|null | |
| 81 | + */ | |
| 22 | 82 | protected $save_status; |
| 23 | 83 | |
| 24 | 84 | /** |
| 25 | 85 | * This is used to decide whether the license checks should continue. |
| @@ -47,14 +107,41 @@ | ||
| 47 | 107 | * @var bool |
| 48 | 108 | */ |
| 49 | 109 | protected $should_clear_cache = true; |
| 50 | 110 | |
| 111 | + /** | |
| 112 | + * Cached for the request, since every add-on asks for the same values. | |
| 113 | + * | |
| 114 | + * @since 6.35 | |
| 115 | + * | |
| 116 | + * @var array<string,string>|null | |
| 117 | + */ | |
| 118 | + private static $lite_requirements; | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Add-ons that require more than Lite does, keyed by plugin slug. | |
| 122 | + * | |
| 123 | + * Nearly every add-on is plain PHP with no dependency of its own, so it runs anywhere Lite | |
| 124 | + * runs and Lite's own requirements describe it. The exceptions bundle a third party | |
| 125 | + * library, which sets a floor of its own. PDFs bundles Dompdf, which needs PHP 7.1. | |
| 126 | + * | |
| 127 | + * Values here are floors, never ceilings, so raising Lite's minimum still raises every | |
| 128 | + * add-on's. Keys match the names WordPress uses, requires and requires_php. | |
| 129 | + * | |
| 130 | + * @since 6.35 | |
| 131 | + * | |
| 132 | + * @var array<string,array<string,string>> | |
| 133 | + */ | |
| 134 | + private static $addon_requirements = array( | |
| 135 | + 'pdfs' => array( 'requires_php' => '7.1' ), | |
| 136 | + ); | |
| 137 | + | |
| 51 | 138 | public function __construct() { |
| 52 | - | |
| 53 | - if ( empty( $this->plugin_slug ) ) { | |
| 139 | + if ( ! $this->plugin_slug ) { | |
| 54 | 140 | $this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) ); |
| 55 | 141 | } |
| 56 | - if ( empty( $this->option_name ) ) { | |
| 142 | + | |
| 143 | + if ( ! $this->option_name ) { | |
| 57 | 144 | $this->option_name = 'edd_' . $this->plugin_slug . '_license_'; |
| 58 | 145 | } |
| 59 | 146 | |
| 60 | 147 | $this->plugin_folder = plugin_basename( $this->plugin_file ); |
| @@ -63,44 +150,64 @@ | ||
| 63 | 150 | add_filter( 'frm_installed_addons', array( &$this, 'insert_installed_addon' ) ); |
| 64 | 151 | $this->edd_plugin_updater(); |
| 65 | 152 | } |
| 66 | 153 | |
| 154 | + /** | |
| 155 | + * @return void | |
| 156 | + */ | |
| 67 | 157 | public static function load_hooks() { |
| 68 | 158 | add_filter( 'frm_include_addon_page', '__return_true' ); |
| 69 | 159 | new static(); |
| 70 | 160 | } |
| 71 | 161 | |
| 162 | + /** | |
| 163 | + * @param array $plugins | |
| 164 | + * | |
| 165 | + * @return array | |
| 166 | + */ | |
| 72 | 167 | public function insert_installed_addon( $plugins ) { |
| 73 | 168 | $plugins[ $this->plugin_slug ] = $this; |
| 74 | - | |
| 75 | 169 | return $plugins; |
| 76 | 170 | } |
| 77 | 171 | |
| 172 | + /** | |
| 173 | + * @param string $plugin_slug | |
| 174 | + * | |
| 175 | + * @return false|FrmAddon | |
| 176 | + */ | |
| 78 | 177 | public static function get_addon( $plugin_slug ) { |
| 79 | 178 | $plugins = apply_filters( 'frm_installed_addons', array() ); |
| 80 | - $plugin = false; | |
| 81 | - if ( isset( $plugins[ $plugin_slug ] ) ) { | |
| 82 | - $plugin = $plugins[ $plugin_slug ]; | |
| 83 | - } | |
| 84 | - | |
| 85 | - return $plugin; | |
| 179 | + return $plugins[ $plugin_slug ] ?? false; | |
| 86 | 180 | } |
| 87 | 181 | |
| 182 | + /** | |
| 183 | + * @return void | |
| 184 | + */ | |
| 88 | 185 | public function edd_plugin_updater() { |
| 89 | - | |
| 90 | 186 | $this->is_license_revoked(); |
| 91 | 187 | $license = $this->license; |
| 92 | 188 | |
| 93 | 189 | add_action( 'after_plugin_row_' . plugin_basename( $this->plugin_file ), array( $this, 'maybe_show_license_message' ), 10, 2 ); |
| 94 | 190 | |
| 95 | - if ( ! empty( $license ) ) { | |
| 191 | + $is_addon = 'formidable/formidable.php' !== $this->plugin_folder; | |
| 96 | 192 | |
| 97 | - if ( 'formidable/formidable.php' !== $this->plugin_folder ) { | |
| 98 | - add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 ); | |
| 99 | - } | |
| 193 | + if ( $is_addon ) { | |
| 194 | + // Lite gets its tested and required versions from wordpress.org, add-ons have no | |
| 195 | + // such source. Added before the license check, since a site without a license | |
| 196 | + // still needs to know what its add-ons support. | |
| 197 | + add_filter( 'site_transient_update_plugins', array( $this, 'add_version_requirements' ), 20 ); | |
| 198 | + add_filter( 'plugins_api', array( $this, 'add_requirements_to_plugin_info' ), 20, 3 ); | |
| 199 | + } | |
| 100 | 200 | |
| 101 | - add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) ); | |
| 201 | + if ( ! $license ) { | |
| 202 | + return; | |
| 102 | 203 | } |
| 204 | + | |
| 205 | + if ( $is_addon ) { | |
| 206 | + add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 ); | |
| 207 | + } | |
| 208 | + | |
| 209 | + add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) ); | |
| 103 | 210 | } |
| 104 | 211 | |
| 105 | 212 | /** |
| 106 | 213 | * Updates information on the "View version 6.15 details" page with custom data. |
| @@ -106,28 +213,40 @@ | ||
| 106 | 213 | * Updates information on the "View version 6.15 details" page with custom data. |
| 107 | 214 | * |
| 108 | 215 | * @uses api_request() |
| 109 | 216 | * |
| 110 | - * @param mixed $_data | |
| 111 | - * @param string $_action | |
| 112 | - * @param object $_args | |
| 217 | + * @param mixed $_data | |
| 218 | + * @param string $_action | |
| 219 | + * @param object|null $_args | |
| 113 | 220 | * |
| 114 | - * @return object $_data | |
| 221 | + * @return object Data. | |
| 115 | 222 | */ |
| 116 | 223 | public function plugins_api_filter( $_data, $_action = '', $_args = null ) { |
| 117 | - | |
| 118 | - if ( $_action != 'plugin_information' ) { | |
| 224 | + if ( $_action !== 'plugin_information' ) { | |
| 119 | 225 | return $_data; |
| 120 | 226 | } |
| 121 | 227 | |
| 122 | - $slug = basename( $this->plugin_file, '.php' ); | |
| 123 | - $slug2 = str_replace( '/' . $slug . '.php', '', $this->plugin_folder ); | |
| 124 | - if ( empty( $_args->slug ) || ( $_args->slug != $slug && $_args->slug !== $slug2 ) ) { | |
| 228 | + if ( empty( $_args->slug ) || ! $this->is_plugin_slug( $_args->slug ) ) { | |
| 125 | 229 | return $_data; |
| 126 | 230 | } |
| 127 | 231 | |
| 128 | 232 | $item_id = $this->download_id; |
| 129 | - if ( empty( $item_id ) ) { | |
| 233 | + | |
| 234 | + if ( $item_id ) { | |
| 235 | + $api = new FrmFormApi( $this->license ); | |
| 236 | + | |
| 237 | + // Force new API info so we can pull changelog data. | |
| 238 | + // Change log data is intentionally omitted from the cached API response | |
| 239 | + // To help reduce the size of the autoloaded option. | |
| 240 | + $api->force_api_request(); | |
| 241 | + $plugins = $api->get_api_info(); | |
| 242 | + | |
| 243 | + if ( $plugins ) { | |
| 244 | + $_data = $plugins[ $item_id ]; | |
| 245 | + } | |
| 246 | + } | |
| 247 | + | |
| 248 | + if ( empty( $plugins ) ) { | |
| 130 | 249 | $_data = array( |
| 131 | 250 | 'name' => $this->plugin_name, |
| 132 | 251 | 'excerpt' => '', |
| 133 | 252 | 'changelog' => 'See the full changelog at <a href="' . esc_url( $this->store_url . '/changelog/' ) . '"></a>', |
| @@ -135,12 +254,8 @@ | ||
| 135 | 254 | 'high' => '', |
| 136 | 255 | 'low' => 'https://ps.w.org/formidable/assets/banner-1544x500.png', |
| 137 | 256 | ), |
| 138 | 257 | ); |
| 139 | - } else { | |
| 140 | - $api = new FrmFormApi( $this->license ); | |
| 141 | - $plugins = $api->get_api_info(); | |
| 142 | - $_data = $plugins[ $item_id ]; | |
| 143 | 258 | } |
| 144 | 259 | |
| 145 | 260 | $_data['sections'] = array( |
| 146 | 261 | 'description' => $_data['excerpt'], |
| @@ -151,28 +266,295 @@ | ||
| 151 | 266 | |
| 152 | 267 | return (object) $_data; |
| 153 | 268 | } |
| 154 | 269 | |
| 270 | + /** | |
| 271 | + * Checks if a requested plugin_information slug refers to this plugin. | |
| 272 | + * | |
| 273 | + * Either the plugin file name or the plugin folder may be used as an add-on's slug. | |
| 274 | + * | |
| 275 | + * @since 6.35 | |
| 276 | + * | |
| 277 | + * @param string $slug The requested slug. | |
| 278 | + * | |
| 279 | + * @return bool | |
| 280 | + */ | |
| 281 | + private function is_plugin_slug( $slug ) { | |
| 282 | + $file_slug = basename( $this->plugin_file, '.php' ); | |
| 283 | + $folder_slug = str_replace( '/' . $file_slug . '.php', '', $this->plugin_folder ); | |
| 284 | + | |
| 285 | + return $slug === $file_slug || $slug === $folder_slug; | |
| 286 | + } | |
| 287 | + | |
| 288 | + /** | |
| 289 | + * Adds the versions this add-on supports to its update data. | |
| 290 | + * | |
| 291 | + * The API reports no tested or required versions, so anything reading the update transient | |
| 292 | + * has no way to tell whether an add-on has been tried with the WordPress release in use, or | |
| 293 | + * whether the site meets what the add-on needs. | |
| 294 | + * | |
| 295 | + * @since 6.35 | |
| 296 | + * | |
| 297 | + * @param mixed $transient The update_plugins site transient. | |
| 298 | + * | |
| 299 | + * @return mixed | |
| 300 | + */ | |
| 301 | + public function add_version_requirements( $transient ) { | |
| 302 | + if ( ! is_object( $transient ) ) { | |
| 303 | + return $transient; | |
| 304 | + } | |
| 305 | + | |
| 306 | + $requirements = $this->get_version_requirements(); | |
| 307 | + | |
| 308 | + if ( ! $requirements ) { | |
| 309 | + return $transient; | |
| 310 | + } | |
| 311 | + | |
| 312 | + foreach ( array( 'response', 'no_update' ) as $group ) { | |
| 313 | + $plugins = $transient->$group ?? false; | |
| 314 | + | |
| 315 | + if ( ! is_array( $plugins ) || empty( $plugins[ $this->plugin_folder ] ) ) { | |
| 316 | + continue; | |
| 317 | + } | |
| 318 | + | |
| 319 | + // The entries are objects, so this updates the transient in place. | |
| 320 | + $plugin = $plugins[ $this->plugin_folder ]; | |
| 321 | + | |
| 322 | + if ( ! is_object( $plugin ) ) { | |
| 323 | + continue; | |
| 324 | + } | |
| 325 | + | |
| 326 | + foreach ( $requirements as $key => $value ) { | |
| 327 | + if ( empty( $plugin->$key ) ) { | |
| 328 | + $plugin->$key = $value; | |
| 329 | + } | |
| 330 | + } | |
| 331 | + } | |
| 332 | + | |
| 333 | + return $transient; | |
| 334 | + } | |
| 335 | + | |
| 336 | + /** | |
| 337 | + * Adds the versions this add-on supports to its plugin information. | |
| 338 | + * | |
| 339 | + * This is where WordPress itself reads the values, for the "Compatible up to" and | |
| 340 | + * "Requires PHP" lines and the compatibility warnings in the plugin details modal. It is | |
| 341 | + * separate from plugins_api_filter because that filter is only added when a license is set. | |
| 342 | + * | |
| 343 | + * @since 6.35 | |
| 344 | + * | |
| 345 | + * @param mixed $data Plugin information from an earlier filter, or false if none. | |
| 346 | + * @param string $action The requested action. | |
| 347 | + * @param object|null $args Arguments for the request, including the slug. | |
| 348 | + * | |
| 349 | + * @return mixed | |
| 350 | + */ | |
| 351 | + public function add_requirements_to_plugin_info( $data, $action = '', $args = null ) { | |
| 352 | + if ( 'plugin_information' !== $action || empty( $args->slug ) || ! $this->is_plugin_slug( $args->slug ) ) { | |
| 353 | + return $data; | |
| 354 | + } | |
| 355 | + | |
| 356 | + $requirements = $this->get_version_requirements(); | |
| 357 | + | |
| 358 | + if ( ! $requirements ) { | |
| 359 | + return $data; | |
| 360 | + } | |
| 361 | + | |
| 362 | + $is_object = is_object( $data ) && ! is_wp_error( $data ); | |
| 363 | + | |
| 364 | + if ( ! $is_object && ! is_array( $data ) ) { | |
| 365 | + return $data; | |
| 366 | + } | |
| 367 | + | |
| 368 | + foreach ( $requirements as $key => $value ) { | |
| 369 | + if ( ! $is_object ) { | |
| 370 | + if ( empty( $data[ $key ] ) ) { | |
| 371 | + $data[ $key ] = $value; | |
| 372 | + } | |
| 373 | + continue; | |
| 374 | + } | |
| 375 | + | |
| 376 | + if ( empty( $data->$key ) ) { | |
| 377 | + $data->$key = $value; | |
| 378 | + } | |
| 379 | + } | |
| 380 | + | |
| 381 | + return $data; | |
| 382 | + } | |
| 383 | + | |
| 384 | + /** | |
| 385 | + * Gets the versions this add-on reports to WordPress. | |
| 386 | + * | |
| 387 | + * Lite's own values describe every add-on, apart from the few that bundle a library with a | |
| 388 | + * higher floor of its own. | |
| 389 | + * | |
| 390 | + * @since 6.35 | |
| 391 | + * | |
| 392 | + * @return array<string,string> Keyed by the names WordPress uses, tested, requires and requires_php. | |
| 393 | + */ | |
| 394 | + public function get_version_requirements() { | |
| 395 | + $requirements = self::get_lite_requirements(); | |
| 396 | + $addon = self::$addon_requirements[ $this->plugin_slug ] ?? array(); | |
| 397 | + | |
| 398 | + foreach ( $addon as $key => $version ) { | |
| 399 | + $is_higher = empty( $requirements[ $key ] ) || version_compare( $version, $requirements[ $key ], '>' ); | |
| 400 | + | |
| 401 | + if ( $is_higher ) { | |
| 402 | + $requirements[ $key ] = $version; | |
| 403 | + } | |
| 404 | + } | |
| 405 | + | |
| 406 | + /** | |
| 407 | + * Filters the versions a single add-on reports to WordPress. | |
| 408 | + * | |
| 409 | + * @since 6.35 | |
| 410 | + * | |
| 411 | + * @param array<string,string> $requirements Keyed by tested, requires and requires_php. | |
| 412 | + * @param string $plugin_slug The add-on's slug, for example pdfs. | |
| 413 | + */ | |
| 414 | + $requirements = apply_filters( 'frm_addon_version_requirements', $requirements, $this->plugin_slug ); | |
| 415 | + | |
| 416 | + return is_array( $requirements ) ? array_filter( $requirements ) : array(); | |
| 417 | + } | |
| 418 | + | |
| 419 | + /** | |
| 420 | + * Gets the WordPress version the add-ons report as tested. | |
| 421 | + * | |
| 422 | + * @since 6.35 | |
| 423 | + * | |
| 424 | + * @return string The tested version, or an empty string if the readme has none. | |
| 425 | + */ | |
| 426 | + public static function get_tested_wp_version() { | |
| 427 | + return self::get_lite_requirements()['tested']; | |
| 428 | + } | |
| 429 | + | |
| 430 | + /** | |
| 431 | + * Gets the versions Lite declares, which the add-ons report as their own. | |
| 432 | + * | |
| 433 | + * Read from Lite's readme, which is the same place wordpress.org reads them from, so the | |
| 434 | + * add-ons report what Lite reports without anything being added to the API. | |
| 435 | + * | |
| 436 | + * @since 6.35 | |
| 437 | + * | |
| 438 | + * @return array<string,string> Keyed by tested, requires and requires_php. Values may be empty. | |
| 439 | + */ | |
| 440 | + public static function get_lite_requirements() { | |
| 441 | + if ( null !== self::$lite_requirements ) { | |
| 442 | + return self::$lite_requirements; | |
| 443 | + } | |
| 444 | + | |
| 445 | + $headers = array( | |
| 446 | + 'tested' => 'Tested up to', | |
| 447 | + 'requires' => 'Requires at least', | |
| 448 | + 'requires_php' => 'Requires PHP', | |
| 449 | + ); | |
| 450 | + | |
| 451 | + $versions = array( | |
| 452 | + 'tested' => '', | |
| 453 | + 'requires' => '', | |
| 454 | + 'requires_php' => '', | |
| 455 | + ); | |
| 456 | + | |
| 457 | + $readme = FrmAppHelper::plugin_path() . '/readme.txt'; | |
| 458 | + | |
| 459 | + if ( is_readable( $readme ) ) { | |
| 460 | + $data = get_file_data( $readme, $headers ); | |
| 461 | + | |
| 462 | + foreach ( $versions as $key => $version ) { | |
| 463 | + $versions[ $key ] = isset( $data[ $key ] ) ? trim( $data[ $key ] ) : ''; | |
| 464 | + } | |
| 465 | + } | |
| 466 | + | |
| 467 | + /** | |
| 468 | + * Filters the versions read from Lite's readme before the add-ons report them. | |
| 469 | + * | |
| 470 | + * @since 6.35 | |
| 471 | + * | |
| 472 | + * @param array<string,string> $versions Keyed by tested, requires and requires_php. | |
| 473 | + */ | |
| 474 | + $versions = apply_filters( 'frm_lite_version_requirements', $versions ); | |
| 475 | + | |
| 476 | + if ( ! is_array( $versions ) ) { | |
| 477 | + $versions = array(); | |
| 478 | + } | |
| 479 | + | |
| 480 | + $versions['tested'] = self::expand_tested_branch( $versions['tested'] ?? '' ); | |
| 481 | + | |
| 482 | + self::$lite_requirements = $versions; | |
| 483 | + | |
| 484 | + return self::$lite_requirements; | |
| 485 | + } | |
| 486 | + | |
| 487 | + /** | |
| 488 | + * Expands a tested release branch to the release running on this site. | |
| 489 | + * | |
| 490 | + * A readme names a branch, like 7.0, while wordpress.org reports the newest release in | |
| 491 | + * that branch, like 7.0.4. The difference matters because WordPress compares the tested | |
| 492 | + * version to the current one exactly, so a site on 7.0.4 reading 7.0 is warned that the | |
| 493 | + * plugin is untested when it is not. Only versions within the tested branch are expanded, | |
| 494 | + * so a genuinely older tested version is still reported as older. | |
| 495 | + * | |
| 496 | + * @since 6.35 | |
| 497 | + * | |
| 498 | + * @param string $tested The tested version from the readme. | |
| 499 | + * | |
| 500 | + * @return string | |
| 501 | + */ | |
| 502 | + private static function expand_tested_branch( $tested ) { | |
| 503 | + if ( ! $tested ) { | |
| 504 | + return $tested; | |
| 505 | + } | |
| 506 | + | |
| 507 | + // Drop any pre-release suffix, since 7.1-RC4 is not a release to report as tested. | |
| 508 | + $current = preg_replace( '/[^0-9.].*$/', '', get_bloginfo( 'version' ) ); | |
| 509 | + | |
| 510 | + if ( self::get_version_branch( $current ) !== self::get_version_branch( $tested ) ) { | |
| 511 | + return $tested; | |
| 512 | + } | |
| 513 | + | |
| 514 | + return version_compare( $current, $tested, '>' ) ? $current : $tested; | |
| 515 | + } | |
| 516 | + | |
| 517 | + /** | |
| 518 | + * Reduces a version to its major.minor release branch. | |
| 519 | + * | |
| 520 | + * @since 6.35 | |
| 521 | + * | |
| 522 | + * @param string $version A WordPress version. | |
| 523 | + * | |
| 524 | + * @return string The branch, or an empty string if the version cannot be read. | |
| 525 | + */ | |
| 526 | + private static function get_version_branch( $version ) { | |
| 527 | + if ( ! preg_match( '/^(\d+)\.(\d+)/', $version, $matches ) ) { | |
| 528 | + return ''; | |
| 529 | + } | |
| 530 | + | |
| 531 | + return $matches[1] . '.' . $matches[2]; | |
| 532 | + } | |
| 533 | + | |
| 534 | + /** | |
| 535 | + * @return string | |
| 536 | + */ | |
| 155 | 537 | public function get_license() { |
| 156 | 538 | $license = $this->maybe_get_pro_license(); |
| 157 | - if ( ! empty( $license ) ) { | |
| 539 | + | |
| 540 | + if ( $license ) { | |
| 158 | 541 | return $license; |
| 159 | 542 | } |
| 160 | 543 | |
| 161 | 544 | $license = trim( get_option( $this->option_name . 'key' ) ); |
| 162 | - if ( empty( $license ) ) { | |
| 163 | - $license = $this->activate_defined_license(); | |
| 164 | - } | |
| 165 | 545 | |
| 166 | - return $license; | |
| 546 | + return $license ? $license : $this->activate_defined_license(); | |
| 167 | 547 | } |
| 168 | 548 | |
| 169 | 549 | /** |
| 170 | 550 | * @since 3.04.03 |
| 551 | + * | |
| 552 | + * @return false|string | |
| 171 | 553 | */ |
| 172 | 554 | protected function maybe_get_pro_license() { |
| 173 | - // prevent a loop if $this is the pro plugin | |
| 174 | - $get_license = FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) && $this->plugin_name != 'Formidable Pro'; | |
| 555 | + // Prevent a loop if $this is the pro plugin | |
| 556 | + $get_license = FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) && $this->plugin_name !== 'Formidable Pro'; | |
| 175 | 557 | |
| 176 | 558 | if ( ! $get_license ) { |
| 177 | 559 | return false; |
| 178 | 560 | } |
| @@ -179,18 +561,16 @@ | ||
| 179 | 561 | |
| 180 | 562 | $api = new FrmFormApi(); |
| 181 | 563 | $api->get_pro_updater(); |
| 182 | 564 | $license = $api->get_license(); |
| 183 | - if ( empty( $license ) ) { | |
| 565 | + | |
| 566 | + if ( ! $license ) { | |
| 184 | 567 | return false; |
| 185 | 568 | } |
| 186 | 569 | |
| 187 | 570 | $this->get_api_info( $license ); |
| 188 | - if ( ! $this->is_parent_licence ) { | |
| 189 | - $license = false; | |
| 190 | - } | |
| 191 | 571 | |
| 192 | - return $license; | |
| 572 | + return $this->is_parent_licence ? $license : false; | |
| 193 | 573 | } |
| 194 | 574 | |
| 195 | 575 | /** |
| 196 | 576 | * Activate the license in wp-config.php |
| @@ -195,13 +575,17 @@ | ||
| 195 | 575 | /** |
| 196 | 576 | * Activate the license in wp-config.php |
| 197 | 577 | * |
| 198 | 578 | * @since 2.04 |
| 579 | + * | |
| 580 | + * @return string | |
| 199 | 581 | */ |
| 200 | 582 | public function activate_defined_license() { |
| 201 | 583 | $license = $this->get_defined_license(); |
| 202 | - if ( ! empty( $license ) && ! $this->is_active() && ! $this->checked_recently( '1 day' ) ) { | |
| 584 | + | |
| 585 | + if ( $license && ! $this->is_active() && ! $this->checked_recently( '1 day' ) ) { | |
| 203 | 586 | $response = $this->activate_license( $license ); |
| 587 | + | |
| 204 | 588 | if ( ! $response['success'] ) { |
| 205 | 589 | $license = ''; |
| 206 | 590 | } |
| 207 | 591 | } |
| @@ -212,19 +596,28 @@ | ||
| 212 | 596 | /** |
| 213 | 597 | * Check the wp-config.php for the license key |
| 214 | 598 | * |
| 215 | 599 | * @since 2.04 |
| 600 | + * | |
| 601 | + * @return false|string | |
| 216 | 602 | */ |
| 217 | 603 | public function get_defined_license() { |
| 218 | 604 | $constant_name = 'FRM_' . strtoupper( $this->plugin_slug ) . '_LICENSE'; |
| 219 | - | |
| 220 | 605 | return defined( $constant_name ) ? constant( $constant_name ) : false; |
| 221 | 606 | } |
| 222 | 607 | |
| 608 | + /** | |
| 609 | + * @param string $license | |
| 610 | + * | |
| 611 | + * @return void | |
| 612 | + */ | |
| 223 | 613 | public function set_license( $license ) { |
| 224 | 614 | update_option( $this->option_name . 'key', $license ); |
| 225 | 615 | } |
| 226 | 616 | |
| 617 | + /** | |
| 618 | + * @return mixed | |
| 619 | + */ | |
| 227 | 620 | public function is_active() { |
| 228 | 621 | return get_option( $this->option_name . 'active' ); |
| 229 | 622 | } |
| 230 | 623 | |
| @@ -231,8 +624,10 @@ | ||
| 231 | 624 | /** |
| 232 | 625 | * @since 3.04.03 |
| 233 | 626 | * |
| 234 | 627 | * @param array|string $error |
| 628 | + * | |
| 629 | + * @return void | |
| 235 | 630 | */ |
| 236 | 631 | public function maybe_clear_license( $error ) { |
| 237 | 632 | if ( is_array( $error ) && $error['code'] === 'disabled' && $error['license'] === $this->license ) { |
| 238 | 633 | $this->clear_license(); |
| @@ -238,18 +633,23 @@ | ||
| 238 | 633 | $this->clear_license(); |
| 239 | 634 | } |
| 240 | 635 | } |
| 241 | 636 | |
| 637 | + /** | |
| 638 | + * @return void | |
| 639 | + */ | |
| 242 | 640 | public function clear_license() { |
| 243 | 641 | delete_option( $this->option_name . 'active' ); |
| 244 | 642 | delete_option( $this->option_name . 'key' ); |
| 245 | 643 | |
| 246 | - if ( $this->should_clear_cache ) { | |
| 247 | - delete_site_option( $this->transient_key() ); | |
| 248 | - delete_option( $this->transient_key() ); | |
| 249 | - $this->delete_cache(); | |
| 250 | - $this->should_clear_cache = true; | |
| 644 | + if ( ! $this->should_clear_cache ) { | |
| 645 | + return; | |
| 251 | 646 | } |
| 647 | + | |
| 648 | + delete_site_option( $this->transient_key() ); | |
| 649 | + delete_option( $this->transient_key() ); | |
| 650 | + $this->delete_cache(); | |
| 651 | + $this->should_clear_cache = true; | |
| 252 | 652 | } |
| 253 | 653 | |
| 254 | 654 | /** |
| 255 | 655 | * Don't save an invalid license. |
| @@ -255,14 +655,15 @@ | ||
| 255 | 655 | * Don't save an invalid license. |
| 256 | 656 | * |
| 257 | 657 | * @since 6.8.3 |
| 258 | 658 | * |
| 259 | - * @param bool $is_valid If license activation was successful. | |
| 659 | + * @param bool|string $is_valid If license activation was successful. May be a string 'valid'. | |
| 260 | 660 | * |
| 261 | 661 | * @return void |
| 262 | 662 | */ |
| 263 | 663 | protected function maybe_set_active( $is_valid ) { |
| 264 | 664 | update_option( $this->option_name . 'active', $is_valid ); |
| 665 | + | |
| 265 | 666 | if ( $is_valid ) { |
| 266 | 667 | $this->set_active( $is_valid ); |
| 267 | 668 | return; |
| 268 | 669 | } |
| @@ -273,8 +674,13 @@ | ||
| 273 | 674 | delete_option( $this->option_name . 'key' ); |
| 274 | 675 | $this->license = ''; |
| 275 | 676 | } |
| 276 | 677 | |
| 678 | + /** | |
| 679 | + * @param bool|string $is_active Value may include 'valid'. | |
| 680 | + * | |
| 681 | + * @return void | |
| 682 | + */ | |
| 277 | 683 | public function set_active( $is_active ) { |
| 278 | 684 | $this->delete_cache(); |
| 279 | 685 | FrmAppHelper::save_combined_js(); |
| 280 | 686 | $this->update_pro_capabilities(); |
| @@ -283,8 +689,10 @@ | ||
| 283 | 689 | /** |
| 284 | 690 | * Updates roles capabilities after pro license is active. |
| 285 | 691 | * |
| 286 | 692 | * @since 5.0 |
| 693 | + * | |
| 694 | + * @return void | |
| 287 | 695 | */ |
| 288 | 696 | protected function update_pro_capabilities() { |
| 289 | 697 | global $wp_roles; |
| 290 | 698 | |
| @@ -294,8 +702,9 @@ | ||
| 294 | 702 | |
| 295 | 703 | $caps = FrmAppHelper::frm_capabilities( 'pro_only' ); |
| 296 | 704 | $roles = get_editable_roles(); |
| 297 | 705 | $settings = new FrmSettings(); |
| 706 | + | |
| 298 | 707 | foreach ( $caps as $cap => $cap_desc ) { |
| 299 | 708 | $cap_roles = (array) ( $settings->$cap ?? 'administrator' ); |
| 300 | 709 | |
| 301 | 710 | // Make sure administrators always have permissions. |
| @@ -303,9 +712,9 @@ | ||
| 303 | 712 | array_push( $cap_roles, 'administrator' ); |
| 304 | 713 | } |
| 305 | 714 | |
| 306 | 715 | foreach ( $roles as $role => $details ) { |
| 307 | - if ( in_array( $role, $cap_roles ) ) { | |
| 716 | + if ( in_array( $role, $cap_roles, true ) ) { | |
| 308 | 717 | $wp_roles->add_cap( $role, $cap ); |
| 309 | 718 | } else { |
| 310 | 719 | $wp_roles->remove_cap( $role, $cap ); |
| 311 | 720 | } |
| @@ -314,8 +723,10 @@ | ||
| 314 | 723 | } |
| 315 | 724 | |
| 316 | 725 | /** |
| 317 | 726 | * @since 3.04.03 |
| 727 | + * | |
| 728 | + * @return void | |
| 318 | 729 | */ |
| 319 | 730 | protected function delete_cache() { |
| 320 | 731 | delete_transient( 'frm_api_licence' ); |
| 321 | 732 | |
| @@ -330,10 +741,19 @@ | ||
| 330 | 741 | |
| 331 | 742 | $api = new FrmApplicationApi( $this->license ); |
| 332 | 743 | $api->reset_cached(); |
| 333 | 744 | |
| 745 | + // The Sales API tries to pull API data in the constructor. | |
| 746 | + // But we do not need sales data here. | |
| 747 | + FrmSalesApi::prevent_new_sales_requests(); | |
| 748 | + | |
| 334 | 749 | $api = new FrmSalesApi(); |
| 335 | 750 | $api->reset_cached(); |
| 751 | + | |
| 752 | + FrmSalesApi::allow_new_sales_requests(); | |
| 753 | + | |
| 754 | + $api = new FrmStyleApi(); | |
| 755 | + $api->reset_cached(); | |
| 336 | 756 | } |
| 337 | 757 | |
| 338 | 758 | /** |
| 339 | 759 | * The Pro version includes the show_license_message function. |
| @@ -339,12 +759,17 @@ | ||
| 339 | 759 | * The Pro version includes the show_license_message function. |
| 340 | 760 | * We need an extra check before we allow it to show a message. |
| 341 | 761 | * |
| 342 | 762 | * @since 3.04.03 |
| 763 | + * | |
| 764 | + * @param string $file File path of the plugin. | |
| 765 | + * @param array $plugin Plugin data. | |
| 766 | + * | |
| 767 | + * @return void | |
| 343 | 768 | */ |
| 344 | 769 | public function maybe_show_license_message( $file, $plugin ) { |
| 345 | 770 | if ( $this->is_expired_addon || isset( $plugin['package'] ) ) { |
| 346 | - // let's not show a ton of duplicate messages | |
| 771 | + // Let's not show a ton of duplicate messages | |
| 347 | 772 | return; |
| 348 | 773 | } |
| 349 | 774 | |
| 350 | 775 | $this->show_license_message( $file, $plugin ); |
| @@ -349,22 +774,30 @@ | ||
| 349 | 774 | |
| 350 | 775 | $this->show_license_message( $file, $plugin ); |
| 351 | 776 | } |
| 352 | 777 | |
| 778 | + /** | |
| 779 | + * @param string $file File path of the plugin. | |
| 780 | + * @param array $plugin Plugin data. | |
| 781 | + * | |
| 782 | + * @return void | |
| 783 | + */ | |
| 353 | 784 | public function show_license_message( $file, $plugin ) { |
| 354 | 785 | $message = ''; |
| 355 | - if ( empty( $this->license ) ) { | |
| 356 | - /* translators: %1$s: Plugin name, %2$s: Start link HTML, %3$s: end link HTML */ | |
| 357 | - $message = sprintf( esc_html__( 'Your %1$s license key is missing. Please add it on the %2$slicenses page%3$s.', 'formidable' ), esc_html( $this->plugin_name ), '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings' ) ) . '">', '</a>' ); | |
| 358 | - } else { | |
| 786 | + | |
| 787 | + if ( $this->license ) { | |
| 359 | 788 | $api = new FrmFormApi( $this->license ); |
| 360 | 789 | $errors = $api->error_for_license(); |
| 361 | - if ( ! empty( $errors ) ) { | |
| 790 | + | |
| 791 | + if ( $errors ) { | |
| 362 | 792 | $message = reset( $errors ); |
| 363 | 793 | } |
| 794 | + } else { | |
| 795 | + /* translators: %1$s: Plugin name, %2$s: Start link HTML, %3$s: end link HTML */ | |
| 796 | + $message = sprintf( esc_html__( 'Your %1$s license key is missing. Please add it on the %2$slicenses page%3$s.', 'formidable' ), esc_html( $this->plugin_name ), '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings' ) ) . '">', '</a>' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 364 | 797 | } |
| 365 | 798 | |
| 366 | - if ( empty( $message ) ) { | |
| 799 | + if ( ! $message ) { | |
| 367 | 800 | return; |
| 368 | 801 | } |
| 369 | 802 | |
| 370 | 803 | $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' ); |
| @@ -369,14 +802,19 @@ | ||
| 369 | 802 | |
| 370 | 803 | $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' ); |
| 371 | 804 | $id = sanitize_title( $plugin['Name'] ) . '-next'; |
| 372 | 805 | |
| 373 | - echo '<tr class="plugin-update-tr active" id="' . esc_attr( $id ) . '"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message notice error inline notice-error notice-alt"><p>'; | |
| 806 | + echo '<tr class="plugin-update-tr active" id="' . esc_attr( $id ) . '"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message notice error inline notice-error notice-alt"><p>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 374 | 807 | FrmAppHelper::kses_echo( $message, 'a' ); |
| 375 | - echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '").previousSibling;if ( d !== null ){ d.className = d.className + " update"; }</script>'; | |
| 808 | + echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '").previousSibling;if ( d !== null ){ d.className = d.className + " update"; }</script>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 376 | 809 | echo '</p></div></td></tr>'; |
| 377 | 810 | } |
| 378 | 811 | |
| 812 | + /** | |
| 813 | + * @param mixed $transient | |
| 814 | + * | |
| 815 | + * @return mixed | |
| 816 | + */ | |
| 379 | 817 | public function clear_expired_download( $transient ) { |
| 380 | 818 | if ( ! is_object( $transient ) ) { |
| 381 | 819 | return $transient; |
| 382 | 820 | } |
| @@ -388,9 +826,9 @@ | ||
| 388 | 826 | } |
| 389 | 827 | } elseif ( isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) ) { |
| 390 | 828 | $this->prepare_update_details( $transient->response[ $this->plugin_folder ] ); |
| 391 | 829 | |
| 392 | - // if the transient has expired, clear the update and trigger it again | |
| 830 | + // If the transient has expired, clear the update and trigger it again | |
| 393 | 831 | if ( $transient->response[ $this->plugin_folder ] === false ) { |
| 394 | 832 | if ( ! $this->has_been_cleared() ) { |
| 395 | 833 | $this->cleared_plugins(); |
| 396 | 834 | $this->manually_queue_update(); |
| @@ -407,29 +845,37 @@ | ||
| 407 | 845 | * |
| 408 | 846 | * @since 3.04.03 |
| 409 | 847 | * |
| 410 | 848 | * @param object $transient The current plugin info saved for update. |
| 849 | + * | |
| 850 | + * @return void | |
| 411 | 851 | */ |
| 412 | 852 | private function prepare_update_details( &$transient ) { |
| 413 | 853 | $version_info = $transient; |
| 414 | 854 | $has_beta_url = ! empty( $version_info->beta ); |
| 855 | + | |
| 415 | 856 | if ( $this->get_beta && ! $has_beta_url ) { |
| 416 | 857 | $version_info = (object) $this->get_api_info( $this->license ); |
| 417 | 858 | } |
| 418 | 859 | |
| 419 | - if ( ! empty( $version_info->new_version ) ) { | |
| 420 | - $this->clear_old_plugin_version( $version_info ); | |
| 421 | - if ( $version_info === false ) { | |
| 422 | - // Was cleared with timeout. | |
| 423 | - $transient = false; | |
| 424 | - } else { | |
| 425 | - $this->maybe_use_beta_url( $version_info ); | |
| 860 | + if ( empty( $version_info->new_version ) ) { | |
| 861 | + return; | |
| 862 | + } | |
| 426 | 863 | |
| 427 | - if ( version_compare( $version_info->new_version, $this->version, '>' ) ) { | |
| 428 | - $transient = $version_info; | |
| 429 | - } | |
| 430 | - } | |
| 864 | + $this->clear_old_plugin_version( $version_info ); | |
| 865 | + | |
| 866 | + if ( $version_info === false ) { | |
| 867 | + // Was cleared with timeout. | |
| 868 | + $transient = false; | |
| 869 | + | |
| 870 | + return; | |
| 431 | 871 | } |
| 872 | + | |
| 873 | + $this->maybe_use_beta_url( $version_info ); | |
| 874 | + | |
| 875 | + if ( version_compare( $version_info->new_version, $this->version, '>' ) ) { | |
| 876 | + $transient = $version_info; | |
| 877 | + } | |
| 432 | 878 | } |
| 433 | 879 | |
| 434 | 880 | /** |
| 435 | 881 | * Get the API info for this plugin |
| @@ -434,18 +880,22 @@ | ||
| 434 | 880 | /** |
| 435 | 881 | * Get the API info for this plugin |
| 436 | 882 | * |
| 437 | 883 | * @since 3.04.03 |
| 884 | + * | |
| 885 | + * @param string $license The license key. | |
| 886 | + * | |
| 887 | + * @return array The API info for the plugin. | |
| 438 | 888 | */ |
| 439 | 889 | protected function get_api_info( $license ) { |
| 440 | 890 | $api = new FrmFormApi( $license ); |
| 441 | 891 | $addon = $api->get_addon_for_license( $this ); |
| 442 | 892 | |
| 443 | - // if there is no download url, this license does not apply to the addon | |
| 893 | + // If there is no download url, this license does not apply to the addon | |
| 444 | 894 | if ( isset( $addon['package'] ) ) { |
| 445 | 895 | $this->is_parent_licence = true; |
| 446 | 896 | } elseif ( isset( $addon['error'] ) ) { |
| 447 | - // if the license is expired, we must assume all add-ons were packaged | |
| 897 | + // If the license is expired, we must assume all add-ons were packaged | |
| 448 | 898 | $this->is_parent_licence = true; |
| 449 | 899 | $this->is_expired_addon = true; |
| 450 | 900 | } |
| 451 | 901 | |
| @@ -456,17 +906,24 @@ | ||
| 456 | 906 | * Make sure transients don't stick around on sites that |
| 457 | 907 | * don't save the transient expiration |
| 458 | 908 | * |
| 459 | 909 | * @since 2.05.05 |
| 910 | + * | |
| 911 | + * @param object $version_info The version info for the plugin. | |
| 912 | + * | |
| 913 | + * @return void | |
| 460 | 914 | */ |
| 461 | 915 | private function clear_old_plugin_version( &$version_info ) { |
| 462 | 916 | $timeout = ! empty( $version_info->timeout ) ? $version_info->timeout : 0; |
| 463 | - if ( ! empty( $timeout ) && time() > $timeout ) { | |
| 464 | - // Cache is expired. | |
| 465 | - $version_info = false; | |
| 466 | - $api = new FrmFormApi( $this->license ); | |
| 467 | - $api->reset_cached(); | |
| 917 | + | |
| 918 | + if ( ! $timeout || time() <= $timeout ) { | |
| 919 | + return; | |
| 468 | 920 | } |
| 921 | + | |
| 922 | + // Cache is expired. | |
| 923 | + $version_info = false; | |
| 924 | + $api = new FrmFormApi( $this->license ); | |
| 925 | + $api->reset_cached(); | |
| 469 | 926 | } |
| 470 | 927 | |
| 471 | 928 | /** |
| 472 | 929 | * The beta url is always included if the download has a beta. |
| @@ -472,19 +929,31 @@ | ||
| 472 | 929 | * The beta url is always included if the download has a beta. |
| 473 | 930 | * Check if the beta should be downloaded. |
| 474 | 931 | * |
| 475 | 932 | * @since 3.04.03 |
| 933 | + * | |
| 934 | + * @param object $version_info The version info for the plugin. | |
| 935 | + * | |
| 936 | + * @return void | |
| 476 | 937 | */ |
| 477 | 938 | private function maybe_use_beta_url( &$version_info ) { |
| 478 | - if ( $this->get_beta && ! empty( $version_info->beta ) ) { | |
| 479 | - $version_info->new_version = $version_info->beta['version']; | |
| 480 | - $version_info->package = $version_info->beta['package']; | |
| 481 | - if ( ! empty( $version_info->plugin ) ) { | |
| 482 | - $version_info->plugin = $version_info->beta['plugin']; | |
| 483 | - } | |
| 939 | + if ( ! $this->get_beta || empty( $version_info->beta ) ) { | |
| 940 | + return; | |
| 484 | 941 | } |
| 942 | + | |
| 943 | + $version_info->new_version = $version_info->beta['version']; | |
| 944 | + $version_info->package = $version_info->beta['package']; | |
| 945 | + | |
| 946 | + if ( ! empty( $version_info->plugin ) ) { | |
| 947 | + $version_info->plugin = $version_info->beta['plugin']; | |
| 948 | + } | |
| 485 | 949 | } |
| 486 | 950 | |
| 951 | + /** | |
| 952 | + * @param object $transient | |
| 953 | + * | |
| 954 | + * @return bool | |
| 955 | + */ | |
| 487 | 956 | private function is_current_version( $transient ) { |
| 488 | 957 | if ( empty( $transient->checked ) || ! isset( $transient->checked[ $this->plugin_folder ] ) ) { |
| 489 | 958 | return false; |
| 490 | 959 | } |
| @@ -489,13 +958,14 @@ | ||
| 489 | 958 | return false; |
| 490 | 959 | } |
| 491 | 960 | |
| 492 | 961 | $response = empty( $transient->response ); |
| 962 | + | |
| 493 | 963 | if ( $response ) { |
| 494 | 964 | return true; |
| 495 | 965 | } |
| 496 | 966 | |
| 497 | - return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] === $transient->response[ $this->plugin_folder ]->new_version; | |
| 967 | + return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] === $transient->response[ $this->plugin_folder ]->new_version; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 498 | 968 | } |
| 499 | 969 | |
| 500 | 970 | /** |
| 501 | 971 | * @return bool |
| @@ -504,14 +974,20 @@ | ||
| 504 | 974 | $last_cleared = get_option( 'frm_last_cleared' ); |
| 505 | 975 | return $last_cleared && $last_cleared > gmdate( 'Y-m-d H:i:s', strtotime( '-5 minutes' ) ); |
| 506 | 976 | } |
| 507 | 977 | |
| 978 | + /** | |
| 979 | + * @return void | |
| 980 | + */ | |
| 508 | 981 | private function cleared_plugins() { |
| 509 | 982 | update_option( 'frm_last_cleared', gmdate( 'Y-m-d H:i:s' ) ); |
| 510 | 983 | } |
| 511 | 984 | |
| 985 | + /** | |
| 986 | + * @return void | |
| 987 | + */ | |
| 512 | 988 | private function is_license_revoked() { |
| 513 | - if ( empty( $this->license ) || empty( $this->plugin_slug ) || isset( $_POST['license'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 989 | + if ( ! $this->license || ! $this->plugin_slug || isset( $_POST['license'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 514 | 990 | return; |
| 515 | 991 | } |
| 516 | 992 | |
| 517 | 993 | if ( $this->get_defined_license() ) { |
| @@ -519,13 +995,19 @@ | ||
| 519 | 995 | return; |
| 520 | 996 | } |
| 521 | 997 | |
| 522 | 998 | // Only check weekly. |
| 523 | - if ( $this->checked_recently( '7 days', 'valid' ) || $this->is_running() ) { | |
| 999 | + if ( $this->checked_recently( '7 days' ) || $this->is_running() ) { | |
| 524 | 1000 | return; |
| 525 | 1001 | } |
| 526 | 1002 | |
| 527 | 1003 | $response = $this->get_license_status(); |
| 1004 | + | |
| 1005 | + if ( ! empty( $this->save_status['response_code'] ) && 429 === $this->save_status['response_code'] ) { | |
| 1006 | + // If we got a rate limit response, don't clear the license. | |
| 1007 | + return; | |
| 1008 | + } | |
| 1009 | + | |
| 528 | 1010 | if ( 'revoked' === $response['status'] || 'blocked' === $response['status'] || 'disabled' === $response['status'] || 'missing' === $response['status'] ) { |
| 529 | 1011 | $this->clear_license(); |
| 530 | 1012 | } |
| 531 | 1013 | } |
| @@ -532,30 +1014,45 @@ | ||
| 532 | 1014 | |
| 533 | 1015 | /** |
| 534 | 1016 | * Has this been checked too recently? |
| 535 | 1017 | * |
| 536 | - * @param string $time ie. '1 day'. | |
| 537 | - * @param string $required_status Return false if the last check does not match. ie 'valid'. | |
| 1018 | + * @param string $time ie. '1 day'. | |
| 538 | 1019 | * |
| 539 | - * @return bool | |
| 1020 | + * @return bool True if the last check time was recent. | |
| 540 | 1021 | */ |
| 541 | - private function checked_recently( $time, $required_status = '' ) { | |
| 1022 | + private function checked_recently( $time ) { | |
| 542 | 1023 | $last_checked = $this->last_checked(); |
| 543 | - $is_429 = isset( $last_checked['response_code'] ) && 429 === $last_checked['response_code']; | |
| 1024 | + | |
| 1025 | + if ( ! $last_checked ) { | |
| 1026 | + return false; | |
| 1027 | + } | |
| 1028 | + | |
| 1029 | + $is_429 = isset( $last_checked['response_code'] ) && 429 === $last_checked['response_code']; | |
| 1030 | + | |
| 1031 | + // If the last check was a a rate limit error, adjust the check time. | |
| 544 | 1032 | if ( $is_429 ) { |
| 545 | - // If the last check was a a rate limit, we'll need to check again sooner. | |
| 546 | - $time = '5 minutes'; | |
| 547 | - $required_status = ''; | |
| 1033 | + // If the time is 2 minutes, do not adjust it so people | |
| 1034 | + // can still try to activate again in 2 minutes (and not 5). | |
| 1035 | + switch ( $time ) { | |
| 1036 | + case '7 days': | |
| 1037 | + // 7 days is used to check for license revocation. | |
| 1038 | + $time = '30 minutes'; | |
| 1039 | + break; | |
| 1040 | + case '1 day': | |
| 1041 | + // 1 day is used for defined license constants. | |
| 1042 | + // The response won't change often, so we can check less frequently. | |
| 1043 | + $time = '2 hours'; | |
| 1044 | + break; | |
| 1045 | + } | |
| 548 | 1046 | } |
| 549 | 1047 | |
| 550 | - if ( $required_status && ( ! isset( $last_checked['status'] ) || $last_checked['status'] !== $required_status ) ) { | |
| 551 | - // If the last check was invalid, we don't need to check again. | |
| 552 | - return true; | |
| 1048 | + if ( empty( $last_checked['time'] ) ) { | |
| 1049 | + // If we do not have time, data is really old. | |
| 1050 | + return false; | |
| 553 | 1051 | } |
| 554 | 1052 | |
| 555 | - $checked_time = $last_checked['time'] ?? false; | |
| 556 | - $time_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-' . $time ) ); | |
| 557 | - return $checked_time && $checked_time > $time_ago; | |
| 1053 | + $time_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-' . $time ) ); | |
| 1054 | + return $last_checked['time'] > $time_ago; | |
| 558 | 1055 | } |
| 559 | 1056 | |
| 560 | 1057 | /** |
| 561 | 1058 | * @since 6.8.3 Switched to an array to store extra response info. |
| @@ -562,25 +1059,29 @@ | ||
| 562 | 1059 | * |
| 563 | 1060 | * @return array |
| 564 | 1061 | */ |
| 565 | 1062 | private function last_checked() { |
| 566 | - if ( is_multisite() ) { | |
| 567 | - $last_checked = get_site_option( $this->transient_key() ); | |
| 568 | - } else { | |
| 569 | - $last_checked = get_option( $this->transient_key() ); | |
| 570 | - } | |
| 1063 | + $last_checked = is_multisite() ? get_site_option( $this->transient_key() ) : get_option( $this->transient_key() ); | |
| 1064 | + | |
| 571 | 1065 | if ( $last_checked && ! is_array( $last_checked ) ) { |
| 572 | 1066 | // Get string into array for existing values. |
| 573 | 1067 | $last_checked = array( 'time' => $last_checked ); |
| 574 | 1068 | } |
| 575 | - return $last_checked ? (array) $last_checked : array(); | |
| 1069 | + | |
| 1070 | + return $last_checked ? $last_checked : array(); | |
| 576 | 1071 | } |
| 577 | 1072 | |
| 578 | 1073 | /** |
| 1074 | + * @since 6.30 Added the $is_valid param. | |
| 1075 | + * | |
| 1076 | + * @param bool $is_valid | |
| 1077 | + * | |
| 579 | 1078 | * @return void |
| 580 | 1079 | */ |
| 581 | - private function update_last_checked() { | |
| 582 | - $this->save_response['time'] = gmdate( 'Y-m-d H:i:s' ); | |
| 1080 | + private function update_last_checked( $is_valid ) { | |
| 1081 | + $this->save_response['time'] = gmdate( 'Y-m-d H:i:s' ); | |
| 1082 | + $this->save_response['is_valid'] = $is_valid; | |
| 1083 | + | |
| 583 | 1084 | if ( is_multisite() ) { |
| 584 | 1085 | update_site_option( $this->transient_key(), $this->save_response ); |
| 585 | 1086 | } else { |
| 586 | 1087 | update_option( $this->transient_key(), $this->save_response ); |
| @@ -588,19 +1089,25 @@ | ||
| 588 | 1089 | } |
| 589 | 1090 | |
| 590 | 1091 | /** |
| 591 | 1092 | * Use a new cache after the license is changed, or Formidable is updated. |
| 1093 | + * | |
| 1094 | + * @return string | |
| 592 | 1095 | */ |
| 593 | 1096 | private function transient_key() { |
| 594 | 1097 | return 'frm_' . md5( sanitize_key( $this->license . '_' . $this->plugin_slug ) ); |
| 595 | 1098 | } |
| 596 | 1099 | |
| 1100 | + /** | |
| 1101 | + * @return void | |
| 1102 | + */ | |
| 597 | 1103 | public static function activate() { |
| 598 | 1104 | FrmAppHelper::permission_check( 'frm_change_settings' ); |
| 599 | 1105 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 600 | 1106 | |
| 601 | 1107 | $license = stripslashes( FrmAppHelper::get_param( 'license', '', 'post', 'sanitize_text_field' ) ); |
| 602 | - if ( empty( $license ) ) { | |
| 1108 | + | |
| 1109 | + if ( ! $license ) { | |
| 603 | 1110 | wp_send_json( |
| 604 | 1111 | array( |
| 605 | 1112 | 'message' => __( 'Oops! You forgot to enter your license number.', 'formidable' ), |
| 606 | 1113 | 'success' => false, |
| @@ -617,8 +1124,13 @@ | ||
| 617 | 1124 | } |
| 618 | 1125 | |
| 619 | 1126 | /** |
| 620 | 1127 | * @since 4.08 |
| 1128 | + * | |
| 1129 | + * @param string $license The license key. | |
| 1130 | + * @param string $plugin_slug The plugin slug. | |
| 1131 | + * | |
| 1132 | + * @return array The response from the license activation. | |
| 621 | 1133 | */ |
| 622 | 1134 | public static function activate_license_for_plugin( $license, $plugin_slug ) { |
| 623 | 1135 | $this_plugin = self::get_addon( $plugin_slug ); |
| 624 | 1136 | return $this_plugin->activate_license( $license ); |
| @@ -623,8 +1135,13 @@ | ||
| 623 | 1135 | $this_plugin = self::get_addon( $plugin_slug ); |
| 624 | 1136 | return $this_plugin->activate_license( $license ); |
| 625 | 1137 | } |
| 626 | 1138 | |
| 1139 | + /** | |
| 1140 | + * @param string $license The license key. | |
| 1141 | + * | |
| 1142 | + * @return array The response from the license activation. | |
| 1143 | + */ | |
| 627 | 1144 | private function activate_license( $license ) { |
| 628 | 1145 | $this->set_license( $license ); |
| 629 | 1146 | $this->license = $license; |
| 630 | 1147 | |
| @@ -633,12 +1150,15 @@ | ||
| 633 | 1150 | $response = $this->get_license_status(); |
| 634 | 1151 | $response['message'] = ''; |
| 635 | 1152 | $response['success'] = false; |
| 636 | 1153 | |
| 1154 | + $is_valid = false; | |
| 1155 | + | |
| 637 | 1156 | if ( $response['error'] ) { |
| 638 | 1157 | $response['message'] = $response['status']; |
| 639 | 1158 | } else { |
| 640 | 1159 | $messages = $this->get_messages(); |
| 1160 | + | |
| 641 | 1161 | if ( is_string( $response['status'] ) && isset( $messages[ $response['status'] ] ) ) { |
| 642 | 1162 | $response['message'] = $messages[ $response['status'] ]; |
| 643 | 1163 | } else { |
| 644 | 1164 | $response['message'] = FrmAppHelper::kses( $response['status'], array( 'a' ) ); |
| @@ -643,17 +1163,17 @@ | ||
| 643 | 1163 | } else { |
| 644 | 1164 | $response['message'] = FrmAppHelper::kses( $response['status'], array( 'a' ) ); |
| 645 | 1165 | } |
| 646 | 1166 | |
| 647 | - $is_valid = false; | |
| 648 | 1167 | if ( 'valid' === $response['status'] ) { |
| 649 | 1168 | $is_valid = 'valid'; |
| 650 | 1169 | $response['success'] = true; |
| 651 | 1170 | } |
| 1171 | + | |
| 652 | 1172 | $this->maybe_set_active( $is_valid ); |
| 653 | 1173 | } |
| 654 | 1174 | |
| 655 | - $this->update_last_checked(); | |
| 1175 | + $this->update_last_checked( (bool) $is_valid ); | |
| 656 | 1176 | |
| 657 | 1177 | return $response; |
| 658 | 1178 | } |
| 659 | 1179 | |
| @@ -675,8 +1195,11 @@ | ||
| 675 | 1195 | ) |
| 676 | 1196 | ); |
| 677 | 1197 | } |
| 678 | 1198 | |
| 1199 | + /** | |
| 1200 | + * @return array | |
| 1201 | + */ | |
| 679 | 1202 | private function get_license_status() { |
| 680 | 1203 | $this->set_running(); |
| 681 | 1204 | |
| 682 | 1205 | $response = array( |
| @@ -682,14 +1205,16 @@ | ||
| 682 | 1205 | $response = array( |
| 683 | 1206 | 'status' => 'missing', |
| 684 | 1207 | 'error' => true, |
| 685 | 1208 | ); |
| 686 | - if ( empty( $this->license ) ) { | |
| 1209 | + | |
| 1210 | + if ( ! $this->license ) { | |
| 687 | 1211 | $response['error'] = false; |
| 688 | - | |
| 689 | 1212 | return $response; |
| 690 | 1213 | } |
| 691 | 1214 | |
| 1215 | + $is_valid = false; | |
| 1216 | + | |
| 692 | 1217 | try { |
| 693 | 1218 | $response['error'] = false; |
| 694 | 1219 | $license_data = $this->send_mothership_request( 'activate_license' ); |
| 695 | 1220 | |
| @@ -697,8 +1222,9 @@ | ||
| 697 | 1222 | if ( is_array( $license_data ) ) { |
| 698 | 1223 | if ( ! empty( $license_data['license'] ) && in_array( $license_data['license'], array( 'valid', 'invalid' ), true ) ) { |
| 699 | 1224 | $response['status'] = $license_data['license']; |
| 700 | 1225 | $this->save_status['status'] = $license_data['license']; |
| 1226 | + $is_valid = 'valid' === $license_data['license']; | |
| 701 | 1227 | } |
| 702 | 1228 | } else { |
| 703 | 1229 | $response['status'] = $license_data; |
| 704 | 1230 | } |
| @@ -705,13 +1231,16 @@ | ||
| 705 | 1231 | } catch ( Exception $e ) { |
| 706 | 1232 | $response['status'] = $e->getMessage(); |
| 707 | 1233 | } |
| 708 | 1234 | |
| 709 | - $this->update_last_checked(); | |
| 1235 | + $this->update_last_checked( $is_valid ); | |
| 710 | 1236 | $this->done_running(); |
| 711 | 1237 | return $response; |
| 712 | 1238 | } |
| 713 | 1239 | |
| 1240 | + /** | |
| 1241 | + * @return array | |
| 1242 | + */ | |
| 714 | 1243 | private function get_messages() { |
| 715 | 1244 | return array( |
| 716 | 1245 | 'valid' => __( 'Your license has been activated. Enjoy!', 'formidable' ), |
| 717 | 1246 | 'invalid' => __( 'That license key is invalid', 'formidable' ), |
| @@ -724,8 +1253,10 @@ | ||
| 724 | 1253 | } |
| 725 | 1254 | |
| 726 | 1255 | /** |
| 727 | 1256 | * @since 4.03 |
| 1257 | + * | |
| 1258 | + * @return void | |
| 728 | 1259 | */ |
| 729 | 1260 | public static function reset_cache() { |
| 730 | 1261 | FrmAppHelper::permission_check( 'frm_change_settings' ); |
| 731 | 1262 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
| @@ -740,8 +1271,11 @@ | ||
| 740 | 1271 | |
| 741 | 1272 | wp_send_json( $response ); |
| 742 | 1273 | } |
| 743 | 1274 | |
| 1275 | + /** | |
| 1276 | + * @return void | |
| 1277 | + */ | |
| 744 | 1278 | public static function deactivate() { |
| 745 | 1279 | FrmAppHelper::permission_check( 'frm_change_settings' ); |
| 746 | 1280 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 747 | 1281 | |
| @@ -750,11 +1284,13 @@ | ||
| 750 | 1284 | $response = array( |
| 751 | 1285 | 'success' => false, |
| 752 | 1286 | 'message' => '', |
| 753 | 1287 | ); |
| 1288 | + | |
| 754 | 1289 | try { |
| 755 | 1290 | // $license_data->license will be either "deactivated" or "failed" |
| 756 | 1291 | $license_data = $this_plugin->send_mothership_request( 'deactivate_license' ); |
| 1292 | + | |
| 757 | 1293 | if ( is_array( $license_data ) && 'deactivated' === $license_data['license'] ) { |
| 758 | 1294 | $response['success'] = true; |
| 759 | 1295 | $response['message'] = __( 'That license was removed successfully', 'formidable' ); |
| 760 | 1296 | } else { |
| @@ -771,18 +1307,21 @@ | ||
| 771 | 1307 | } |
| 772 | 1308 | |
| 773 | 1309 | /** |
| 774 | 1310 | * @since 4.03 |
| 1311 | + * | |
| 1312 | + * @return FrmAddon | |
| 775 | 1313 | */ |
| 776 | 1314 | private static function set_license_from_post() { |
| 777 | 1315 | $plugin_slug = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' ); |
| 778 | 1316 | $this_plugin = self::get_addon( $plugin_slug ); |
| 779 | - $license = $this_plugin->get_license(); | |
| 780 | - $this_plugin->license = $license; | |
| 1317 | + $this_plugin->license = $this_plugin->get_license(); | |
| 781 | 1318 | return $this_plugin; |
| 782 | 1319 | } |
| 783 | 1320 | |
| 784 | 1321 | /** |
| 1322 | + * @param string $action | |
| 1323 | + * | |
| 785 | 1324 | * @return string |
| 786 | 1325 | */ |
| 787 | 1326 | public function send_mothership_request( $action ) { |
| 788 | 1327 | $api_params = array( |
| @@ -789,8 +1328,9 @@ | ||
| 789 | 1328 | 'edd_action' => $action, |
| 790 | 1329 | 'license' => $this->license, |
| 791 | 1330 | 'url' => home_url(), |
| 792 | 1331 | ); |
| 1332 | + | |
| 793 | 1333 | if ( is_numeric( $this->download_id ) ) { |
| 794 | 1334 | $api_params['item_id'] = absint( $this->download_id ); |
| 795 | 1335 | } else { |
| 796 | 1336 | $api_params['item_name'] = rawurlencode( $this->plugin_name ); |
| @@ -801,31 +1341,31 @@ | ||
| 801 | 1341 | 'timeout' => 25, |
| 802 | 1342 | 'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ), |
| 803 | 1343 | ); |
| 804 | 1344 | |
| 805 | - $resp = wp_remote_post( | |
| 1345 | + $resp = wp_remote_post( | |
| 806 | 1346 | $this->store_url . '?l=' . urlencode( base64_encode( $this->license ) ), |
| 807 | 1347 | $arg_array |
| 808 | 1348 | ); |
| 809 | - $body = wp_remote_retrieve_body( $resp ); | |
| 810 | - $this->save_status = array( 'response_code' => wp_remote_retrieve_response_code( $resp ) ); | |
| 1349 | + $response_code = wp_remote_retrieve_response_code( $resp ); | |
| 1350 | + $body = wp_remote_retrieve_body( $resp ); | |
| 1351 | + $this->save_status = array( 'response_code' => $response_code ); | |
| 1352 | + $this->save_response['response_code'] = $response_code; | |
| 811 | 1353 | |
| 812 | 1354 | $message = __( 'Your License Key was invalid', 'formidable' ); |
| 1355 | + | |
| 813 | 1356 | if ( is_wp_error( $resp ) ) { |
| 814 | 1357 | $link = FrmAppHelper::admin_upgrade_link( 'api', 'knowledgebase/why-cant-i-activate-formidable-pro/' ); |
| 815 | 1358 | /* translators: %1$s: Start link HTML, %2$s: End link HTML */ |
| 816 | - $message = sprintf( __( 'You had an error communicating with the Formidable API. %1$sClick here%2$s for more information.', 'formidable' ), '<a href="' . esc_url( $link ) . '" target="_blank">', '</a>' ); | |
| 1359 | + $message = sprintf( __( 'You had an error communicating with the Formidable API. %1$sClick here%2$s for more information.', 'formidable' ), '<a href="' . esc_url( $link ) . '" target="_blank">', '</a>' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 817 | 1360 | $message .= ' ' . $resp->get_error_message(); |
| 818 | 1361 | } elseif ( 'error' === $body || is_wp_error( $body ) ) { |
| 819 | 1362 | $message = __( 'You had an HTTP error connecting to the Formidable API', 'formidable' ); |
| 820 | 1363 | } else { |
| 821 | 1364 | $json_res = json_decode( $body, true ); |
| 1365 | + | |
| 822 | 1366 | if ( null !== $json_res ) { |
| 823 | - if ( is_array( $json_res ) && isset( $json_res['error'] ) ) { | |
| 824 | - $message = $json_res['error']; | |
| 825 | - } else { | |
| 826 | - $message = $json_res; | |
| 827 | - } | |
| 1367 | + $message = is_array( $json_res ) && isset( $json_res['error'] ) ? $json_res['error'] : $json_res; | |
| 828 | 1368 | } elseif ( ! empty( $resp['response'] ) && ! empty( $resp['response']['code'] ) ) { |
| 829 | 1369 | $resp['body'] = wp_strip_all_tags( $resp['body'] ); |
| 830 | 1370 | |
| 831 | 1371 | $message = sprintf( |
| @@ -839,8 +1379,11 @@ | ||
| 839 | 1379 | |
| 840 | 1380 | return $message; |
| 841 | 1381 | } |
| 842 | 1382 | |
| 1383 | + /** | |
| 1384 | + * @return void | |
| 1385 | + */ | |
| 843 | 1386 | public function manually_queue_update() { |
| 844 | 1387 | $updates = new stdClass(); |
| 845 | 1388 | $updates->last_checked = 0; |
| 846 | 1389 | $updates->response = array(); |