PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / feature-flag-integration.php

feature-flag-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at src/integrations/feature-flag-integration.php

107 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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