blic static function disable( $feature ) { wc_deprecated_function( __METHOD__, '11.1.0', 'FeaturesUtil::feature_is_enabled()' ); if ( 'analytics' === $feature ) { update_option( Analytics::TOGGLE_OPTION_NAME, 'no' ); return true; } if ( 'remote-inbox-notifications' === $feature ) { update_option( RemoteInboxNotifications::TOGGLE_OPTION_NAME, 'no' ); return true; } return false; } /** * Adds the Features section to the advanced tab of WooCommerce Settings * * @deprecated 7.0 The WooCommerce Admin features are now handled by the WooCommerce features engine (see the FeaturesController class). * * @param array $sections Sections. * @return array */ public static function add_features_section( $sections ) { return $sections; } /** * Adds the Features settings. * * @deprecated 7.0 The WooCommerce Admin features are now handled by the WooCommerce features engine (see the FeaturesController class). * * @param array $settings Settings. * @param string $current_section Current section slug. * @return array */ public static function add_features_settings( $settings, $current_section ) { return $settings; } /** * Loads the required scripts on the correct pages. */ public static function load_scripts() { if ( ! PageController::is_admin_or_embed_page() ) { return; } $available_features = self::get_available_features(); $enabled_features = array(); foreach ( self::get_features() as $key ) { $enabled_features[ $key ] = in_array( $key, $available_features, true ); } $enabled_features = array_merge( $enabled_features, self::get_legacy_feature_compatibility_values() ); wp_add_inline_script( WC_ADMIN_APP, 'window.wcAdminFeatures = ' . wp_json_encode( $enabled_features, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ), 'before' ); } /** * Adds body classes to the main wp-admin wrapper, allowing us to better target elements in specific scenarios. * * @param string $admin_body_class Body class to add. */ public static function add_admin_body_classes( $admin_body_class = '' ) { if ( ! PageController::is_admin_or_embed_page() ) { return $admin_body_class; } $classes = explode( ' ', trim( $admin_body_class ) ); foreach ( self::get_available_features() as $feature_key ) { $classes[] = sanitize_html_class( 'woocommerce-feature-enabled-' . $feature_key ); } $admin_body_class = implode( ' ', array_unique( $classes ) ); return " $admin_body_class "; } /** * Gets legacy feature flag compatibility values. * * This method is intended for passive compatibility paths, such as script globals * and filtering shared settings, where emitting deprecation notices would warn on * every admin page load. * * @since 11.1.0 * @return array */ public static function get_legacy_feature_compatibility_values() { $compatibility_values = array_merge( array_fill_keys( array_keys( self::$retired_feature_compatibility_versions ), true ), array( 'analytics' => self::is_analytics_enabled(), 'remote-inbox-notifications' => 'yes' === get_option( RemoteInboxNotifications::TOGGLE_OPTION_NAME, 'yes' ), ) ); return array_intersect_key( $compatibility_values, array_flip( self::get_features_with_legacy_compatibility_defaults() ) ); } /** * Gets default legacy feature flag compatibility values before public filtering. * * @return array */ private static function get_legacy_feature_compatibility_defaults() { return array_merge( array_fill_keys( array_keys( self::$retired_feature_compatibility_versions ), true ), array( 'analytics' => true, 'remote-inbox-notifications' => true, ) ); } /** * Gets WooCommerce Admin features with legacy compatibility defaults before public filtering. * * @return array Enabled WooCommerce Admin features/sections. */ private static function get_features_with_legacy_compatibility_defaults() { /** * Filter allowing WooCommerce Admin features to be changed after legacy compatibility defaults are seeded. * * @since 11.1.0 * * @param array $features Array of feature slugs. */ return apply_filters( 'woocommerce_admin_features', array_keys( self::get_legacy_feature_compatibility_defaults() ) ); } /** * Checks if Analytics was disabled by legacy WooCommerce Admin filters. * * @return bool True if Analytics was disabled by legacy filters. */ public static function is_analytics_disabled_by_legacy_filters(): bool { /** * Filter allowing WooCommerce Admin optional features to be disabled. * * @param bool $disabled False. */ if ( apply_filters( 'woocommerce_admin_disabled', false ) ) { // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingSinceComment return true; } return ! in_array( 'analytics', self::get_features_with_legacy_compatibility_defaults(), true ); } /** * Checks if analytics is enabled, without going through FeaturesController. * * @return bool */ private static function is_analytics_enabled() { return ! self::is_analytics_disabled_by_legacy_filters() && 'yes' === get_option( Analytics::TOGGLE_OPTION_NAME, 'yes' ); } /** * Checks if a feature slug is supported only by the legacy compatibility shim. * * @param string $feature Feature slug. * @return bool */ private static function is_legacy_compatibility_feature( $feature ) { return array_key_exists( $feature, self::get_legacy_feature_compatibility_defaults() ); } /** * Gets version metadata for a legacy feature flag shim. * * @param string $feature Feature slug. * @return array{deprecated_since: string, removed_in: ?string}|null */ private static function get_legacy_feature_compatibility_versions( $feature ) { return self::$retired_feature_compatibility_versions[ $feature ] ?? null; } /** * Gets the WooCommerce version where a legacy feature flag shim was deprecated. * * @param string $feature Feature slug. * @return string|null */ private static function get_legacy_feature_compatibility_deprecation_version( $feature ) { $versions = self::get_legacy_feature_compatibility_versions( $feature ); return $versions['deprecated_since'] ?? null; } /** * Gets the WooCommerce version where a legacy feature flag shim will be removed. * * @param string $feature Feature slug. * @return string|null */ private static function get_legacy_feature_compatibility_removal_version( $feature ) { $versions = self::get_legacy_feature_compatibility_versions( $feature ); return $versions['removed_in'] ?? null; } /** * Emits a deprecation notice for a direct legacy feature flag shim lookup. * * @param string $method Method name. * @param string $feature Feature slug. */ private static function warn_legacy_feature_compatibility_usage( $method, $feature ): void { $deprecation_version = self::get_legacy_feature_compatibility_deprecation_version( $feature ); $removal_version = self::get_legacy_feature_compatibility_removal_version( $feature ); if ( ! $deprecation_version ) { return; } wc_deprecated_function( sprintf( "%s( '%s' )", $method, $feature ), $deprecation_version, sprintf( 'direct feature behavior checks. The %1$s WC Admin feature flag shim will be removed in %2$s.', $feature, $removal_version ? 'WooCommerce ' . $removal_version : 'a future version of WooCommerce' ) ); } /** * Alias internal features classes to make them backward compatible. * We've moved our feature classes to src-internal as part of merging this * repository with WooCommerce Core to form a monorepo. * See https://wp.me/p90Yrv-2HY for details. */ private function register_internal_class_aliases() { $aliases = array( // new class => original class (this will be aliased). 'Automattic\WooCommerce\Internal\Admin\WCPayPromotion\Init' => 'Automattic\WooCommerce\Admin\Features\WcPayPromotion\Init', 'Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\Init' => 'Automattic\WooCommerce\Admin\Features\RemoteFreeExtensions\Init', 'Automattic\WooCommerce\Internal\Admin\ActivityPanels' => 'Automattic\WooCommerce\Admin\Features\ActivityPanels', 'Automattic\WooCommerce\Internal\Admin\Analytics' => 'Automattic\WooCommerce\Admin\Features\Analytics', 'Automattic\WooCommerce\Internal\Admin\Coupons' => 'Automattic\WooCommerce\Admin\Features\Coupons', 'Automattic\WooCommerce\Internal\Admin\CouponsMovedTrait' => 'Automattic\WooCommerce\Admin\Features\CouponsMovedTrait', 'Automattic\WooCommerce\Internal\Admin\CustomerEffortScoreTracks' => 'Automattic\WooCommerce\Admin\Features\CustomerEffortScoreTracks', 'Automattic\WooCommerce\Internal\Admin\Homescreen' => 'Automattic\WooCommerce\Admin\Features\Homescreen', 'Automattic\WooCommerce\Internal\Admin\Marketing' => 'Automattic\WooCommerce\Admin\Features\Marketing', 'Automattic\WooCommerce\Internal\Admin\MobileAppBanner' => 'Automattic\WooCommerce\Admin\Features\MobileAppBanner', 'Automattic\WooCommerce\Internal\Admin\RemoteInboxNotifications' => 'Automattic\WooCommerce\Admin\Features\RemoteInboxNotifications', 'Automattic\WooCommerce\Internal\Admin\ShippingLabelBanner' => 'Automattic\WooCommerce\Admin\Features\ShippingLabelBanner', 'Automattic\WooCommerce\Internal\Admin\ShippingLabelBannerDisplayRules' => 'Automattic\WooCommerce\Admin\Features\ShippingLabelBannerDisplayRules', 'Automattic\WooCommerce\Internal\Admin\WcPayWelcomePage' => 'Automattic\WooCommerce\Admin\Features\WcPayWelcomePage', ); foreach ( $aliases as $new_class => $orig_class ) { class_alias( $new_class, $orig_class ); } } /** * Check if we're in an admin context where features should be loaded. * * @return boolean */ private static function should_load_features() { $should_load = ( is_admin() || wp_doing_ajax() || wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) || ( WC()->is_rest_api_request() && ! WC()->is_store_api_request() ) || // Allow features to be loaded in frontend for admin users. This is needed for the use case such as the coming soon footer banner. current_user_can( 'manage_woocommerce' ) ); /** * Filter to determine if admin features should be loaded. * * @since 9.6.0 * @param boolean $should_load Whether admin features should be loaded. It defaults to true when the current request is in an admin context. */ return apply_filters( 'woocommerce_admin_should_load_features', $should_load ); } }