| 1 |
<?php |
| 2 |
/** |
| 3 |
* Which third-party form plugins are installed, and whether their addon is loaded. |
| 4 |
* |
| 5 |
* The setup wizard uses this to name the addon a site actually needs, instead |
| 6 |
* of advertising every addon to everyone. The support diagnosis uses it for |
| 7 |
* the version list. Only presence is detected here; the forms themselves are |
| 8 |
* read by the addons, never by Core. |
| 9 |
* |
| 10 |
* @package Forge12\DoubleOptIn\Setup |
| 11 |
* @since 5.7.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
declare( strict_types=1 ); |
| 15 |
|
| 16 |
namespace Forge12\DoubleOptIn\Setup; |
| 17 |
|
| 18 |
use Forge12\DoubleOptIn\Addon\AddonRegistry; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
class FormPluginDetector { |
| 25 |
|
| 26 |
/** |
| 27 |
* Known form plugins that have a paid addon. |
| 28 |
* |
| 29 |
* @var array<string, array{name: string, addonId: string}> |
| 30 |
*/ |
| 31 |
private const PLUGINS = array( |
| 32 |
'elementor_pro' => array( |
| 33 |
'name' => 'Elementor Pro', |
| 34 |
'addonId' => 'elementor', |
| 35 |
), |
| 36 |
'wpforms' => array( |
| 37 |
'name' => 'WPForms', |
| 38 |
'addonId' => 'wpforms', |
| 39 |
), |
| 40 |
'gravityforms' => array( |
| 41 |
'name' => 'Gravity Forms', |
| 42 |
'addonId' => 'gravity-forms', |
| 43 |
), |
| 44 |
'avada' => array( |
| 45 |
'name' => 'Avada Forms', |
| 46 |
'addonId' => 'avada', |
| 47 |
), |
| 48 |
); |
| 49 |
|
| 50 |
/** @var callable(string):?string */ |
| 51 |
private $versionOf; |
| 52 |
|
| 53 |
/** @var callable():string[] */ |
| 54 |
private $loadedAddons; |
| 55 |
|
| 56 |
/** |
| 57 |
* @param callable|null $versionOf Returns the version of a plugin id, or null |
| 58 |
* when it is not installed (test seam). |
| 59 |
* @param callable|null $loadedAddons Returns the ids of loaded addons (test seam). |
| 60 |
*/ |
| 61 |
public function __construct( ?callable $versionOf = null, ?callable $loadedAddons = null ) { |
| 62 |
$this->versionOf = $versionOf ?? array( self::class, 'detectVersion' ); |
| 63 |
$this->loadedAddons = $loadedAddons ?? static function (): array { |
| 64 |
if ( ! class_exists( AddonRegistry::class ) ) { |
| 65 |
return array(); |
| 66 |
} |
| 67 |
return array_map( 'strval', array_keys( AddonRegistry::getInstance()->available() ) ); |
| 68 |
}; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Installed form plugins. |
| 73 |
* |
| 74 |
* @return array<int, array{id: string, name: string, version: string, addonId: string, addonLoaded: bool}> |
| 75 |
*/ |
| 76 |
public function installed(): array { |
| 77 |
$loaded = (array) call_user_func( $this->loadedAddons ); |
| 78 |
$found = array(); |
| 79 |
|
| 80 |
foreach ( self::PLUGINS as $id => $plugin ) { |
| 81 |
$version = call_user_func( $this->versionOf, $id ); |
| 82 |
if ( $version === null ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
$found[] = array( |
| 86 |
'id' => $id, |
| 87 |
'name' => $plugin['name'], |
| 88 |
'version' => (string) $version, |
| 89 |
'addonId' => $plugin['addonId'], |
| 90 |
'addonLoaded' => in_array( $plugin['addonId'], $loaded, true ), |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Filter the detected form plugins. |
| 96 |
* |
| 97 |
* @param array $found Detected plugins. |
| 98 |
* |
| 99 |
* @since 5.7.0 |
| 100 |
*/ |
| 101 |
return (array) apply_filters( 'f12_doi_setup_form_plugins', $found ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Installed form plugins whose addon is not loaded — the ones worth mentioning. |
| 106 |
* |
| 107 |
* @return array<int, array{id: string, name: string, version: string, addonId: string, addonLoaded: bool}> |
| 108 |
*/ |
| 109 |
public function withoutAddon(): array { |
| 110 |
return array_values( |
| 111 |
array_filter( |
| 112 |
$this->installed(), |
| 113 |
static function ( array $plugin ): bool { |
| 114 |
return empty( $plugin['addonLoaded'] ); |
| 115 |
} |
| 116 |
) |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Version of a known form plugin, or null when it is not active. |
| 122 |
*/ |
| 123 |
public static function detectVersion( string $id ): ?string { |
| 124 |
switch ( $id ) { |
| 125 |
case 'elementor_pro': |
| 126 |
return defined( 'ELEMENTOR_PRO_VERSION' ) ? (string) constant( 'ELEMENTOR_PRO_VERSION' ) : null; |
| 127 |
case 'wpforms': |
| 128 |
return defined( 'WPFORMS_VERSION' ) ? (string) constant( 'WPFORMS_VERSION' ) : null; |
| 129 |
case 'gravityforms': |
| 130 |
if ( class_exists( 'GFForms' ) ) { |
| 131 |
return isset( \GFForms::$version ) ? (string) \GFForms::$version : ''; |
| 132 |
} |
| 133 |
return null; |
| 134 |
case 'avada': |
| 135 |
if ( defined( 'FUSION_BUILDER_VERSION' ) ) { |
| 136 |
return (string) constant( 'FUSION_BUILDER_VERSION' ); |
| 137 |
} |
| 138 |
return class_exists( 'Fusion_Form_Builder' ) ? '' : null; |
| 139 |
} |
| 140 |
return null; |
| 141 |
} |
| 142 |
} |
| 143 |
|