admin
3 weeks ago
alerts
9 months ago
blocks
4 weeks ago
front-end
3 months ago
third-party
3 months ago
watchers
3 months ago
abstract-exclude-post-type.php
1 year ago
academy-integration.php
3 weeks ago
breadcrumbs-integration.php
2 years ago
cleanup-integration.php
3 months ago
estimated-reading-time.php
5 years ago
exclude-attachment-post-type.php
3 years ago
exclude-oembed-cache-post-type.php
1 year ago
feature-flag-integration.php
4 years ago
front-end-integration.php
1 month ago
integration-interface.php
5 years ago
primary-category.php
5 years ago
settings-integration.php
2 months ago
support-integration.php
6 months ago
uninstall-integration.php
4 years ago
woocommerce-product-category-permalink-integration.php
3 months ago
xmlrpc.php
4 years ago
feature-flag-integration.php
107 lines
| 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 |