Changeset 65895
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/abilities-api.php
r65370 r65895 823 823 * @param string $slug The slug of the ability category to check. 824 824 * @return bool `true` if the ability category is registered, `false` otherwise. 825 * 826 * @phpstan-assert-if-true =lowercase-string&non-empty-string $slug 825 827 */ 826 828 function wp_has_ability_category( string $slug ): bool { … … 884 886 * @see wp_get_ability_category() 885 887 * 886 * @return WP_Ability_Category[] An array of registered ability category instances, 887 * keyed by category slug. Returns an empty array if no ability categories 888 * are registered or if the registry is unavailable. 888 * @return array<string|int, WP_Ability_Category> An array of registered ability category instances, 889 * keyed by category slug. A numeric slug, such as `'123'`, 890 * is keyed by an integer, since PHP casts numeric string 891 * array keys to integers; use 892 * {@see WP_Ability_Category::get_slug()} when a string slug is 893 * needed. Returns an empty array if no ability categories 894 * are registered or if the registry is unavailable. 895 * 896 * @phpstan-return array<(lowercase-string&non-empty-string)|non-negative-int, WP_Ability_Category> 889 897 */ 890 898 function wp_get_ability_categories(): array { -
trunk/src/wp-includes/abilities-api/class-wp-ability-categories-registry.php
r64235 r65895 31 31 * 32 32 * @since 6.9.0 33 * @var WP_Ability_Category[] 33 * @var array<string|int, WP_Ability_Category> 34 * 35 * @phpstan-var array<(lowercase-string&non-empty-string)|non-negative-int, WP_Ability_Category> 34 36 */ 35 37 private $registered_categories = array(); … … 122 124 */ 123 125 public function unregister( string $slug ): ?WP_Ability_Category { 124 if ( ! $this->is_registered( $slug) ) {126 if ( ! isset( $this->registered_categories[ $slug ] ) ) { 125 127 _doing_it_wrong( 126 128 __METHOD__, … … 147 149 * @see wp_get_ability_categories() 148 150 * 149 * @return array<string, WP_Ability_Category> The array of registered ability categories. 151 * @return array<string|int, WP_Ability_Category> The array of registered ability categories. 152 * 153 * @phpstan-return array<(lowercase-string&non-empty-string)|non-negative-int, WP_Ability_Category> 150 154 */ 151 155 public function get_all_registered(): array { … … 164 168 * @param string $slug The slug of the ability category. 165 169 * @return bool True if the ability category is registered, false otherwise. 170 * 171 * @phpstan-assert-if-true =lowercase-string&non-empty-string $slug 166 172 */ 167 173 public function is_registered( string $slug ): bool { … … 182 188 */ 183 189 public function get_registered( string $slug ): ?WP_Ability_Category { 184 if ( ! $this->is_registered( $slug) ) {190 if ( ! isset( $this->registered_categories[ $slug ] ) ) { 185 191 _doing_it_wrong( 186 192 __METHOD__, -
trunk/tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php
r64088 r65895 350 350 $this->assertArrayHasKey( 'test-math', $categories ); 351 351 $this->assertArrayHasKey( 'test-system', $categories ); 352 } 353 354 /** 355 * Tests that a category with a numeric slug is stored under an integer key. 356 * 357 * PHP casts a numeric string array key to an integer, so the registry 358 * must still find the category by its string slug. 359 * 360 * @ticket 66739 361 */ 362 public function test_numeric_category_slug(): void { 363 $category = $this->registry->register( 364 '123', 365 array( 366 'label' => 'Numeric', 367 'description' => 'A category with a numeric slug.', 368 ) 369 ); 370 371 $this->assertInstanceOf( WP_Ability_Category::class, $category ); 372 $this->assertSame( '123', $category->get_slug() ); 373 $this->assertTrue( $this->registry->is_registered( '123' ) ); 374 $this->assertSame( $category, $this->registry->get_registered( '123' ) ); 375 $this->assertSame( array( 123 ), array_keys( $this->registry->get_all_registered() ), 'Expected the numeric slug to be an integer key.' ); 376 377 $this->assertSame( $category, $this->registry->unregister( '123' ) ); 378 $this->assertFalse( $this->registry->is_registered( '123' ) ); 379 $this->assertSame( array(), $this->registry->get_all_registered() ); 380 $this->assertSame( array(), $this->doing_it_wrong_log, 'Expected no incorrect usage notices.' ); 352 381 } 353 382
Note:
See TracChangeset
for help on using the changeset viewer.