Domain
4 weeks ago
REST
4 weeks ago
AbilitiesCategories.php
4 weeks ago
AbilitiesLoader.php
4 weeks ago
AbilitiesRegistry.php
4 weeks ago
AbilitiesRestBridge.php
4 weeks ago
AbilitiesLoader.php
224 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Abilities loader class file. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Abilities; |
| 9 | |
| 10 | use Automattic\WooCommerce\Abilities\AbilityDefinition; |
| 11 | use Automattic\WooCommerce\Internal\Abilities\Domain\OrderAddNote; |
| 12 | use Automattic\WooCommerce\Internal\Abilities\Domain\OrderUpdateStatus; |
| 13 | use Automattic\WooCommerce\Internal\Abilities\Domain\OrdersQuery; |
| 14 | use Automattic\WooCommerce\Internal\Abilities\Domain\ProductCreate; |
| 15 | use Automattic\WooCommerce\Internal\Abilities\Domain\ProductDelete; |
| 16 | use Automattic\WooCommerce\Internal\Abilities\Domain\ProductUpdate; |
| 17 | use Automattic\WooCommerce\Internal\Abilities\Domain\ProductsQuery; |
| 18 | |
| 19 | defined( 'ABSPATH' ) || exit; |
| 20 | |
| 21 | /** |
| 22 | * Hooks WooCommerce ability definitions into the WordPress Abilities API. |
| 23 | */ |
| 24 | class AbilitiesLoader { |
| 25 | |
| 26 | /** |
| 27 | * Whether the loader hooks have been registered. |
| 28 | * |
| 29 | * @var bool |
| 30 | */ |
| 31 | private static bool $initialized = false; |
| 32 | |
| 33 | /** |
| 34 | * Canonical WooCommerce domain ability definition classes. |
| 35 | * |
| 36 | * @var array<int, class-string> |
| 37 | */ |
| 38 | private const CORE_ABILITY_DEFINITION_CLASSES = array( |
| 39 | OrdersQuery::class, |
| 40 | OrderAddNote::class, |
| 41 | OrderUpdateStatus::class, |
| 42 | ProductsQuery::class, |
| 43 | ProductCreate::class, |
| 44 | ProductDelete::class, |
| 45 | ProductUpdate::class, |
| 46 | ); |
| 47 | |
| 48 | /** |
| 49 | * Log source for ability registration notices. |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | private const LOG_SOURCE = 'woocommerce-abilities'; |
| 54 | |
| 55 | /** |
| 56 | * Core ability instances registered by this loader in the current request. |
| 57 | * |
| 58 | * @var array<string, object> |
| 59 | */ |
| 60 | private static array $registered_core_abilities = array(); |
| 61 | |
| 62 | /** |
| 63 | * Initialize ability registration hooks. |
| 64 | * |
| 65 | * @internal |
| 66 | * |
| 67 | * @since 10.9.0 |
| 68 | */ |
| 69 | final public static function init(): void { |
| 70 | if ( self::$initialized ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Register abilities when Abilities API is ready. |
| 76 | * Support both old (pre-6.9) and new (6.9+) action names. |
| 77 | */ |
| 78 | AbilitiesCategories::init(); |
| 79 | add_action( 'abilities_api_init', array( __CLASS__, 'register_abilities' ) ); |
| 80 | add_action( 'wp_abilities_api_init', array( __CLASS__, 'register_abilities' ) ); |
| 81 | |
| 82 | AbilitiesRestBridge::init(); |
| 83 | |
| 84 | self::$initialized = true; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Register all configured ability definitions. |
| 89 | * |
| 90 | * @since 10.9.0 |
| 91 | */ |
| 92 | public static function register_abilities(): void { |
| 93 | if ( ! function_exists( 'wp_register_ability' ) ) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | foreach ( self::get_ability_definition_classes() as $class_name ) { |
| 98 | if ( ! is_string( $class_name ) || ! class_exists( $class_name ) ) { |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | if ( ! is_a( $class_name, AbilityDefinition::class, true ) ) { |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | $ability_name = $class_name::get_name(); |
| 107 | |
| 108 | if ( '' === $ability_name ) { |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | $is_core_ability = self::is_core_ability_definition_class( $class_name ); |
| 113 | |
| 114 | if ( self::is_reserved_woocommerce_ability_name( $ability_name ) && ! $is_core_ability ) { |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | if ( function_exists( 'wp_has_ability' ) && wp_has_ability( $ability_name ) ) { |
| 119 | $existing_ability = function_exists( 'wp_get_ability' ) ? wp_get_ability( $ability_name ) : null; |
| 120 | |
| 121 | if ( |
| 122 | $is_core_ability |
| 123 | && isset( self::$registered_core_abilities[ $ability_name ] ) |
| 124 | && $existing_ability === self::$registered_core_abilities[ $ability_name ] |
| 125 | ) { |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | if ( ! $is_core_ability || ! function_exists( 'wp_unregister_ability' ) ) { |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | // Drop stale instance tracking before replacing a shadowed registration. |
| 134 | unset( self::$registered_core_abilities[ $ability_name ] ); |
| 135 | wp_unregister_ability( $ability_name ); |
| 136 | self::log_replaced_reserved_ability( $ability_name, $class_name ); |
| 137 | } |
| 138 | |
| 139 | $registered_ability = wp_register_ability( $ability_name, $class_name::get_registration_args() ); |
| 140 | |
| 141 | if ( $is_core_ability && null !== $registered_ability ) { |
| 142 | self::$registered_core_abilities[ $ability_name ] = $registered_ability; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Log when WooCommerce replaces a pre-existing registration in its reserved namespace. |
| 149 | * |
| 150 | * @param string $ability_name Ability name. |
| 151 | * @param class-string $class_name Ability definition class name. |
| 152 | */ |
| 153 | private static function log_replaced_reserved_ability( string $ability_name, string $class_name ): void { |
| 154 | if ( ! function_exists( 'wc_get_logger' ) ) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | wc_get_logger()->warning( |
| 159 | 'WooCommerce unregistered a previously registered ability before registering its canonical definition.', |
| 160 | array( |
| 161 | 'source' => self::LOG_SOURCE, |
| 162 | 'ability_name' => $ability_name, |
| 163 | 'definition_class' => $class_name, |
| 164 | 'reserved_prefix' => 'woocommerce/', |
| 165 | ) |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Check whether an ability definition class is provided by WooCommerce core. |
| 171 | * |
| 172 | * @param class-string $class_name Ability definition class name. |
| 173 | * @return bool |
| 174 | */ |
| 175 | private static function is_core_ability_definition_class( string $class_name ): bool { |
| 176 | return in_array( $class_name, self::CORE_ABILITY_DEFINITION_CLASSES, true ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Check whether an ability name uses WooCommerce's reserved namespace. |
| 181 | * |
| 182 | * @param string $ability_name Ability name. |
| 183 | * @return bool |
| 184 | */ |
| 185 | private static function is_reserved_woocommerce_ability_name( string $ability_name ): bool { |
| 186 | return 0 === strpos( $ability_name, 'woocommerce/' ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Get all ability definition classes that should be loaded. |
| 191 | * |
| 192 | * @return array<int, class-string> |
| 193 | */ |
| 194 | private static function get_ability_definition_classes(): array { |
| 195 | /** |
| 196 | * Filter WooCommerce ability definition classes. |
| 197 | * |
| 198 | * Extensions can append autoloadable classes that implement |
| 199 | * {@see AbilityDefinition}. The loader will call get_name() and |
| 200 | * get_registration_args() on each definition class and register the ability on the |
| 201 | * Abilities API init hook. Returning a subset will not unregister core abilities; |
| 202 | * core classes are always retained. |
| 203 | * |
| 204 | * @since 10.9.0 |
| 205 | * |
| 206 | * @param array<int, class-string> $classes Ability definition class names. |
| 207 | */ |
| 208 | $classes = apply_filters( 'woocommerce_ability_definition_classes', self::CORE_ABILITY_DEFINITION_CLASSES ); |
| 209 | |
| 210 | if ( ! is_array( $classes ) ) { |
| 211 | $classes = array(); |
| 212 | } |
| 213 | |
| 214 | return array_values( |
| 215 | array_unique( |
| 216 | array_filter( |
| 217 | array_merge( self::CORE_ABILITY_DEFINITION_CLASSES, $classes ), |
| 218 | 'is_string' |
| 219 | ) |
| 220 | ) |
| 221 | ); |
| 222 | } |
| 223 | } |
| 224 |