| @@ -107,8 +107,35 @@ | ||
| 107 | 107 | * @var bool |
| 108 | 108 | */ |
| 109 | 109 | protected $should_clear_cache = true; |
| 110 | 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 | + | |
| 111 | 138 | public function __construct() { |
| 112 | 139 | if ( ! $this->plugin_slug ) { |
| 113 | 140 | $this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) ); |
| 114 | 141 | } |
| @@ -160,13 +187,23 @@ | ||
| 160 | 187 | $license = $this->license; |
| 161 | 188 | |
| 162 | 189 | add_action( 'after_plugin_row_' . plugin_basename( $this->plugin_file ), array( $this, 'maybe_show_license_message' ), 10, 2 ); |
| 163 | 190 | |
| 191 | + $is_addon = 'formidable/formidable.php' !== $this->plugin_folder; | |
| 192 | + | |
| 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 | + } | |
| 200 | + | |
| 164 | 201 | if ( ! $license ) { |
| 165 | 202 | return; |
| 166 | 203 | } |
| 167 | 204 | |
| 168 | - if ( 'formidable/formidable.php' !== $this->plugin_folder ) { | |
| 205 | + if ( $is_addon ) { | |
| 169 | 206 | add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 ); |
| 170 | 207 | } |
| 171 | 208 | |
| 172 | 209 | add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) ); |
| @@ -187,12 +224,9 @@ | ||
| 187 | 224 | if ( $_action !== 'plugin_information' ) { |
| 188 | 225 | return $_data; |
| 189 | 226 | } |
| 190 | 227 | |
| 191 | - $slug = basename( $this->plugin_file, '.php' ); | |
| 192 | - $slug2 = str_replace( '/' . $slug . '.php', '', $this->plugin_folder ); | |
| 193 | - | |
| 194 | - if ( empty( $_args->slug ) || ( $_args->slug !== $slug && $_args->slug !== $slug2 ) ) { | |
| 228 | + if ( empty( $_args->slug ) || ! $this->is_plugin_slug( $_args->slug ) ) { | |
| 195 | 229 | return $_data; |
| 196 | 230 | } |
| 197 | 231 | |
| 198 | 232 | $item_id = $this->download_id; |
| @@ -233,10 +267,274 @@ | ||
| 233 | 267 | return (object) $_data; |
| 234 | 268 | } |
| 235 | 269 | |
| 236 | 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 | + * | |
| 237 | 500 | * @return string |
| 238 | 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 | + */ | |
| 239 | 537 | public function get_license() { |
| 240 | 538 | $license = $this->maybe_get_pro_license(); |
| 241 | 539 | |
| 242 | 540 | if ( $license ) { |
| @@ -443,10 +741,16 @@ | ||
| 443 | 741 | |
| 444 | 742 | $api = new FrmApplicationApi( $this->license ); |
| 445 | 743 | $api->reset_cached(); |
| 446 | 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 | + | |
| 447 | 749 | $api = new FrmSalesApi(); |
| 448 | 750 | $api->reset_cached(); |
| 751 | + | |
| 752 | + FrmSalesApi::allow_new_sales_requests(); | |
| 449 | 753 | |
| 450 | 754 | $api = new FrmStyleApi(); |
| 451 | 755 | $api->reset_cached(); |
| 452 | 756 | } |