| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations; |
| 4 |
|
| 5 |
use WPSEO_Admin_Asset_Manager; |
| 6 |
use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 7 |
use Yoast\WP\SEO\Conditionals\Feature_Flag_Conditional; |
| 8 |
|
| 9 |
/** |
| 10 |
* Gathers all feature flags and surfaces them to the JavaScript side of the plugin. |
| 11 |
*/ |
| 12 |
class Feature_Flag_Integration implements Integration_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* The admin asset manager. |
| 16 |
* |
| 17 |
* @var WPSEO_Admin_Asset_Manager |
| 18 |
*/ |
| 19 |
protected $asset_manager; |
| 20 |
|
| 21 |
/** |
| 22 |
* All of the feature flag conditionals. |
| 23 |
* |
| 24 |
* @var Feature_Flag_Conditional[] |
| 25 |
*/ |
| 26 |
protected $feature_flags; |
| 27 |
|
| 28 |
/** |
| 29 |
* Feature_Flag_Integration constructor. |
| 30 |
* |
| 31 |
* @param WPSEO_Admin_Asset_Manager $asset_manager The admin asset manager. |
| 32 |
* @param Feature_Flag_Conditional ...$feature_flags All of the known feature flag conditionals. |
| 33 |
*/ |
| 34 |
public function __construct( WPSEO_Admin_Asset_Manager $asset_manager, Feature_Flag_Conditional ...$feature_flags ) { |
| 35 |
$this->asset_manager = $asset_manager; |
| 36 |
$this->feature_flags = $feature_flags; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the conditionals based on which this loadable should be active. |
| 41 |
* |
| 42 |
* @return string[] The conditionals based on which this loadable should be active. |
| 43 |
*/ |
| 44 |
public static function get_conditionals() { |
| 45 |
return [ Admin_Conditional::class ]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Initializes the integration. |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function register_hooks() { |
| 54 |
\add_action( 'admin_init', [ $this, 'add_feature_flags' ] ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Gathers all the feature flags and injects them into the JavaScript. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function add_feature_flags() { |
| 63 |
$enabled_features = $this->get_enabled_features(); |
| 64 |
// Localize under both names for BC. |
| 65 |
$this->asset_manager->localize_script( 'feature-flag-package', 'wpseoFeatureFlags', $enabled_features ); |
| 66 |
$this->asset_manager->localize_script( 'feature-flag-package', 'wpseoFeaturesL10n', $enabled_features ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns an array of all enabled feature flags. |
| 71 |
* |
| 72 |
* @return string[] The array of enabled features. |
| 73 |
*/ |
| 74 |
public function get_enabled_features() { |
| 75 |
$enabled_features = []; |
| 76 |
foreach ( $this->feature_flags as $feature_flag ) { |
| 77 |
if ( $feature_flag->is_met() ) { |
| 78 |
$enabled_features[] = $feature_flag->get_feature_name(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return $this->filter_enabled_features( $enabled_features ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Runs the list of enabled feature flags through a filter. |
| 87 |
* |
| 88 |
* @param string[] $enabled_features The list of currently enabled feature flags. |
| 89 |
* |
| 90 |
* @return string[] The (possibly adapted) list of enabled features. |
| 91 |
*/ |
| 92 |
protected function filter_enabled_features( $enabled_features ) { |
| 93 |
/** |
| 94 |
* Filters the list of currently enabled feature flags. |
| 95 |
* |
| 96 |
* @param string[] $enabled_features The current list of enabled feature flags. |
| 97 |
*/ |
| 98 |
$filtered_enabled_features = \apply_filters( 'wpseo_enable_feature', $enabled_features ); |
| 99 |
|
| 100 |
if ( ! \is_array( $filtered_enabled_features ) ) { |
| 101 |
$filtered_enabled_features = $enabled_features; |
| 102 |
} |
| 103 |
|
| 104 |
return $filtered_enabled_features; |
| 105 |
} |
| 106 |
} |
| 107 |
|