AsyncProductEditorCategoryField
3 years ago
Blueprint
11 months ago
Fulfillments
4 weeks ago
MarketingRecommendations
3 months ago
Navigation
1 year ago
OnboardingTasks
4 weeks ago
PaymentGatewaySuggestions
1 year ago
ProductBlockEditor
4 weeks ago
ProductDataViews
4 weeks ago
ShippingPartnerSuggestions
1 month ago
Features.php
4 weeks ago
LaunchYourStore.php
4 weeks ago
Onboarding.php
1 year ago
ProductVariationsClassicRedesign.php
4 weeks ago
TransientNotices.php
3 years ago
Features.php
354 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Features loader for features developed in WooCommerce Admin. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\Features; |
| 7 | |
| 8 | use Automattic\WooCommerce\Admin\PageController; |
| 9 | use Automattic\WooCommerce\Internal\Admin\Loader; |
| 10 | use Automattic\WooCommerce\Internal\Admin\WCAdminAssets; |
| 11 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 12 | |
| 13 | /** |
| 14 | * Features Class. |
| 15 | */ |
| 16 | class Features { |
| 17 | /** |
| 18 | * Class instance. |
| 19 | * |
| 20 | * @var Loader instance |
| 21 | */ |
| 22 | protected static $instance = null; |
| 23 | |
| 24 | /** |
| 25 | * Optional features |
| 26 | * |
| 27 | * @var array |
| 28 | */ |
| 29 | protected static $optional_features = array( |
| 30 | 'analytics' => array( 'default' => 'yes' ), |
| 31 | 'remote-inbox-notifications' => array( 'default' => 'yes' ), |
| 32 | ); |
| 33 | |
| 34 | /** |
| 35 | * Get class instance. |
| 36 | */ |
| 37 | public static function get_instance() { |
| 38 | if ( ! self::$instance ) { |
| 39 | self::$instance = new self(); |
| 40 | } |
| 41 | return self::$instance; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Constructor. |
| 46 | */ |
| 47 | public function __construct() { |
| 48 | |
| 49 | $this->register_internal_class_aliases(); |
| 50 | |
| 51 | if ( ! self::should_load_features() ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Load feature before WooCommerce update hooks. |
| 56 | add_action( 'init', array( __CLASS__, 'load_features' ), 4 ); |
| 57 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_scripts' ), 15 ); |
| 58 | add_filter( 'admin_body_class', array( __CLASS__, 'add_admin_body_classes' ) ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Gets a build configured array of enabled WooCommerce Admin features/sections, but does not respect optionally disabled features. |
| 63 | * |
| 64 | * @return array Enabled Woocommerce Admin features/sections. |
| 65 | */ |
| 66 | public static function get_features() { |
| 67 | return apply_filters( 'woocommerce_admin_features', array() ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets the optional feature options as an associative array that can be toggled on or off. |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | public static function get_optional_feature_options() { |
| 76 | $features = array(); |
| 77 | |
| 78 | foreach ( array_keys( self::$optional_features ) as $optional_feature_key ) { |
| 79 | $feature_class = self::get_feature_class( $optional_feature_key ); |
| 80 | |
| 81 | if ( $feature_class ) { |
| 82 | $features[ $optional_feature_key ] = $feature_class::TOGGLE_OPTION_NAME; |
| 83 | } |
| 84 | } |
| 85 | return $features; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns if a specific wc-admin feature exists in the current environment. |
| 90 | * |
| 91 | * @param string $feature Feature slug. |
| 92 | * @return bool Returns true if the feature exists. |
| 93 | */ |
| 94 | public static function exists( $feature ) { |
| 95 | $features = self::get_features(); |
| 96 | return in_array( $feature, $features, true ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the feature class as a string. |
| 101 | * |
| 102 | * @param string $feature Feature name. |
| 103 | * @return string|null |
| 104 | */ |
| 105 | public static function get_feature_class( $feature ) { |
| 106 | $feature = str_replace( '-', '', ucwords( strtolower( $feature ), '-' ) ); |
| 107 | $feature_class = 'Automattic\\WooCommerce\\Admin\\Features\\' . $feature; |
| 108 | |
| 109 | $should_autoload_class = self::should_load_features(); |
| 110 | |
| 111 | if ( class_exists( $feature_class, $should_autoload_class ) ) { |
| 112 | return $feature_class; |
| 113 | } |
| 114 | |
| 115 | // Handle features contained in subdirectory. |
| 116 | if ( class_exists( $feature_class . '\\Init', $should_autoload_class ) ) { |
| 117 | return $feature_class . '\\Init'; |
| 118 | } |
| 119 | |
| 120 | return null; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Class loader for enabled WooCommerce Admin features/sections. |
| 125 | */ |
| 126 | public static function load_features() { |
| 127 | if ( ! self::should_load_features() ) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | $features = self::get_features(); |
| 132 | foreach ( $features as $feature ) { |
| 133 | $feature_class = self::get_feature_class( $feature ); |
| 134 | |
| 135 | if ( $feature_class ) { |
| 136 | new $feature_class(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if ( FeaturesUtil::feature_is_enabled( 'blueprint' ) ) { |
| 141 | new \Automattic\WooCommerce\Admin\Features\Blueprint\Init(); |
| 142 | } |
| 143 | |
| 144 | if ( FeaturesUtil::feature_is_enabled( 'order-detail-redesign' ) ) { |
| 145 | new \Automattic\WooCommerce\Internal\Features\OrderDetailRedesign\Init(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Gets a build configured array of enabled WooCommerce Admin respecting optionally disabled features. |
| 151 | * |
| 152 | * @return array Enabled Woocommerce Admin features/sections. |
| 153 | */ |
| 154 | public static function get_available_features() { |
| 155 | $features = self::get_features(); |
| 156 | $optional_feature_keys = array_keys( self::$optional_features ); |
| 157 | $optional_features_unavailable = array(); |
| 158 | |
| 159 | /** |
| 160 | * Filter allowing WooCommerce Admin optional features to be disabled. |
| 161 | * |
| 162 | * @param bool $disabled False. |
| 163 | */ |
| 164 | if ( apply_filters( 'woocommerce_admin_disabled', false ) ) { |
| 165 | return array_values( array_diff( $features, $optional_feature_keys ) ); |
| 166 | } |
| 167 | |
| 168 | foreach ( $optional_feature_keys as $optional_feature_key ) { |
| 169 | $feature_class = self::get_feature_class( $optional_feature_key ); |
| 170 | |
| 171 | if ( $feature_class ) { |
| 172 | $default = isset( self::$optional_features[ $optional_feature_key ]['default'] ) ? |
| 173 | self::$optional_features[ $optional_feature_key ]['default'] : |
| 174 | 'no'; |
| 175 | |
| 176 | // Check if the feature is currently being enabled, if it is continue. |
| 177 | /* phpcs:disable WordPress.Security.NonceVerification */ |
| 178 | $feature_option = $feature_class::TOGGLE_OPTION_NAME; |
| 179 | if ( isset( $_POST[ $feature_option ] ) && '1' === $_POST[ $feature_option ] ) { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | if ( 'yes' !== get_option( $feature_class::TOGGLE_OPTION_NAME, $default ) ) { |
| 184 | $optional_features_unavailable[] = $optional_feature_key; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return array_values( array_diff( $features, $optional_features_unavailable ) ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Check if a feature is enabled. |
| 194 | * |
| 195 | * @param string $feature Feature slug. |
| 196 | * @return bool |
| 197 | */ |
| 198 | public static function is_enabled( $feature ) { |
| 199 | $available_features = self::get_available_features(); |
| 200 | return in_array( $feature, $available_features, true ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Enable a toggleable optional feature. |
| 205 | * |
| 206 | * @param string $feature Feature name. |
| 207 | * @return bool |
| 208 | */ |
| 209 | public static function enable( $feature ) { |
| 210 | $features = self::get_optional_feature_options(); |
| 211 | |
| 212 | if ( isset( $features[ $feature ] ) ) { |
| 213 | update_option( $features[ $feature ], 'yes' ); |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Disable a toggleable optional feature. |
| 222 | * |
| 223 | * @param string $feature Feature name. |
| 224 | * @return bool |
| 225 | */ |
| 226 | public static function disable( $feature ) { |
| 227 | $features = self::get_optional_feature_options(); |
| 228 | |
| 229 | if ( isset( $features[ $feature ] ) ) { |
| 230 | update_option( $features[ $feature ], 'no' ); |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Adds the Features section to the advanced tab of WooCommerce Settings |
| 239 | * |
| 240 | * @deprecated 7.0 The WooCommerce Admin features are now handled by the WooCommerce features engine (see the FeaturesController class). |
| 241 | * |
| 242 | * @param array $sections Sections. |
| 243 | * @return array |
| 244 | */ |
| 245 | public static function add_features_section( $sections ) { |
| 246 | return $sections; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Adds the Features settings. |
| 251 | * |
| 252 | * @deprecated 7.0 The WooCommerce Admin features are now handled by the WooCommerce features engine (see the FeaturesController class). |
| 253 | * |
| 254 | * @param array $settings Settings. |
| 255 | * @param string $current_section Current section slug. |
| 256 | * @return array |
| 257 | */ |
| 258 | public static function add_features_settings( $settings, $current_section ) { |
| 259 | return $settings; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Loads the required scripts on the correct pages. |
| 264 | */ |
| 265 | public static function load_scripts() { |
| 266 | if ( ! PageController::is_admin_or_embed_page() ) { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | $features = self::get_features(); |
| 271 | $enabled_features = array(); |
| 272 | foreach ( $features as $key ) { |
| 273 | $enabled_features[ $key ] = self::is_enabled( $key ); |
| 274 | } |
| 275 | wp_add_inline_script( WC_ADMIN_APP, 'window.wcAdminFeatures = ' . wp_json_encode( $enabled_features, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ), 'before' ); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /** |
| 280 | * Adds body classes to the main wp-admin wrapper, allowing us to better target elements in specific scenarios. |
| 281 | * |
| 282 | * @param string $admin_body_class Body class to add. |
| 283 | */ |
| 284 | public static function add_admin_body_classes( $admin_body_class = '' ) { |
| 285 | if ( ! PageController::is_admin_or_embed_page() ) { |
| 286 | return $admin_body_class; |
| 287 | } |
| 288 | |
| 289 | $classes = explode( ' ', trim( $admin_body_class ) ); |
| 290 | |
| 291 | $features = self::get_features(); |
| 292 | foreach ( $features as $feature_key ) { |
| 293 | $classes[] = sanitize_html_class( 'woocommerce-feature-enabled-' . $feature_key ); |
| 294 | } |
| 295 | |
| 296 | $admin_body_class = implode( ' ', array_unique( $classes ) ); |
| 297 | return " $admin_body_class "; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Alias internal features classes to make them backward compatible. |
| 302 | * We've moved our feature classes to src-internal as part of merging this |
| 303 | * repository with WooCommerce Core to form a monorepo. |
| 304 | * See https://wp.me/p90Yrv-2HY for details. |
| 305 | */ |
| 306 | private function register_internal_class_aliases() { |
| 307 | $aliases = array( |
| 308 | // new class => original class (this will be aliased). |
| 309 | 'Automattic\WooCommerce\Internal\Admin\WCPayPromotion\Init' => 'Automattic\WooCommerce\Admin\Features\WcPayPromotion\Init', |
| 310 | 'Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\Init' => 'Automattic\WooCommerce\Admin\Features\RemoteFreeExtensions\Init', |
| 311 | 'Automattic\WooCommerce\Internal\Admin\ActivityPanels' => 'Automattic\WooCommerce\Admin\Features\ActivityPanels', |
| 312 | 'Automattic\WooCommerce\Internal\Admin\Analytics' => 'Automattic\WooCommerce\Admin\Features\Analytics', |
| 313 | 'Automattic\WooCommerce\Internal\Admin\Coupons' => 'Automattic\WooCommerce\Admin\Features\Coupons', |
| 314 | 'Automattic\WooCommerce\Internal\Admin\CouponsMovedTrait' => 'Automattic\WooCommerce\Admin\Features\CouponsMovedTrait', |
| 315 | 'Automattic\WooCommerce\Internal\Admin\CustomerEffortScoreTracks' => 'Automattic\WooCommerce\Admin\Features\CustomerEffortScoreTracks', |
| 316 | 'Automattic\WooCommerce\Internal\Admin\Homescreen' => 'Automattic\WooCommerce\Admin\Features\Homescreen', |
| 317 | 'Automattic\WooCommerce\Internal\Admin\Marketing' => 'Automattic\WooCommerce\Admin\Features\Marketing', |
| 318 | 'Automattic\WooCommerce\Internal\Admin\MobileAppBanner' => 'Automattic\WooCommerce\Admin\Features\MobileAppBanner', |
| 319 | 'Automattic\WooCommerce\Internal\Admin\RemoteInboxNotifications' => 'Automattic\WooCommerce\Admin\Features\RemoteInboxNotifications', |
| 320 | 'Automattic\WooCommerce\Internal\Admin\ShippingLabelBanner' => 'Automattic\WooCommerce\Admin\Features\ShippingLabelBanner', |
| 321 | 'Automattic\WooCommerce\Internal\Admin\ShippingLabelBannerDisplayRules' => 'Automattic\WooCommerce\Admin\Features\ShippingLabelBannerDisplayRules', |
| 322 | 'Automattic\WooCommerce\Internal\Admin\WcPayWelcomePage' => 'Automattic\WooCommerce\Admin\Features\WcPayWelcomePage', |
| 323 | ); |
| 324 | foreach ( $aliases as $new_class => $orig_class ) { |
| 325 | class_alias( $new_class, $orig_class ); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Check if we're in an admin context where features should be loaded. |
| 331 | * |
| 332 | * @return boolean |
| 333 | */ |
| 334 | private static function should_load_features() { |
| 335 | $should_load = ( |
| 336 | is_admin() || |
| 337 | wp_doing_ajax() || |
| 338 | wp_doing_cron() || |
| 339 | ( defined( 'WP_CLI' ) && WP_CLI ) || |
| 340 | ( WC()->is_rest_api_request() && ! WC()->is_store_api_request() ) || |
| 341 | // Allow features to be loaded in frontend for admin users. This is needed for the use case such as the coming soon footer banner. |
| 342 | current_user_can( 'manage_woocommerce' ) |
| 343 | ); |
| 344 | |
| 345 | /** |
| 346 | * Filter to determine if admin features should be loaded. |
| 347 | * |
| 348 | * @since 9.6.0 |
| 349 | * @param boolean $should_load Whether admin features should be loaded. It defaults to true when the current request is in an admin context. |
| 350 | */ |
| 351 | return apply_filters( 'woocommerce_admin_should_load_features', $should_load ); |
| 352 | } |
| 353 | } |
| 354 |