| @@ -95,10 +95,14 @@ | ||
| 95 | 95 | foreach ( $abilities as $ability ) { |
| 96 | 96 | $this->register( $ability ); |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - // Mirror into the WordPress Abilities API when it exists. | |
| 99 | + // Mirror into the WordPress Abilities API when it exists. The category | |
| 100 | + // must exist before any ability may claim it, and core fires its | |
| 101 | + // `wp_abilities_api_categories_init` action first, so both hooks are | |
| 102 | + // registered together here. | |
| 100 | 103 | if ( function_exists( 'wp_register_ability' ) ) { |
| 104 | + add_action( 'wp_abilities_api_categories_init', array( $this, 'register_ability_category' ) ); | |
| 101 | 105 | add_action( 'wp_abilities_api_init', array( $this, 'register_with_wp_abilities' ) ); |
| 102 | 106 | } |
| 103 | 107 | } |
| 104 | 108 | |
| @@ -148,13 +152,47 @@ | ||
| 148 | 152 | return $this->abilities; |
| 149 | 153 | } |
| 150 | 154 | |
| 151 | 155 | /** |
| 156 | + * Register our ability category with the WordPress Abilities API. | |
| 157 | + * | |
| 158 | + * Core rejects (and emits a `_doing_it_wrong()` notice for) any ability | |
| 159 | + * whose category is unknown, so this must run on | |
| 160 | + * `wp_abilities_api_categories_init` — the only action on which | |
| 161 | + * `wp_register_ability_category()` accepts a registration. | |
| 162 | + * | |
| 163 | + * @return void | |
| 164 | + */ | |
| 165 | + public function register_ability_category() { | |
| 166 | + if ( ! function_exists( 'wp_register_ability_category' ) ) { | |
| 167 | + return; | |
| 168 | + } | |
| 169 | + if ( function_exists( 'wp_has_ability_category' ) && wp_has_ability_category( self::CATEGORY ) ) { | |
| 170 | + return; | |
| 171 | + } | |
| 172 | + wp_register_ability_category( | |
| 173 | + self::CATEGORY, | |
| 174 | + array( | |
| 175 | + 'label' => __( 'NotificationX', 'notificationx' ), | |
| 176 | + 'description' => __( 'Read and manage NotificationX notifications, form entries, analytics and settings.', 'notificationx' ), | |
| 177 | + ) | |
| 178 | + ); | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 152 | 182 | * Bridge our abilities into the WordPress Abilities API when available. |
| 153 | 183 | * |
| 184 | + * Bails when the category is missing — that only happens if the module | |
| 185 | + * booted after `wp_abilities_api_categories_init` had already fired, and | |
| 186 | + * registering anyway would fail per ability with a `_doing_it_wrong()` | |
| 187 | + * notice. NotificationX's own registry is unaffected either way. | |
| 188 | + * | |
| 154 | 189 | * @return void |
| 155 | 190 | */ |
| 156 | 191 | public function register_with_wp_abilities() { |
| 192 | + if ( function_exists( 'wp_has_ability_category' ) && ! wp_has_ability_category( self::CATEGORY ) ) { | |
| 193 | + return; | |
| 194 | + } | |
| 157 | 195 | foreach ( $this->abilities as $ability ) { |
| 158 | 196 | if ( function_exists( 'wp_has_ability' ) && wp_has_ability( $ability->get_id() ) ) { |
| 159 | 197 | continue; |
| 160 | 198 | } |