diff --git a/src/wp-includes/class-wp-icons-registry.php b/src/wp-includes/class-wp-icons-registry.php index b096610c1b04f..2e2c2ca2956b4 100644 --- a/src/wp-includes/class-wp-icons-registry.php +++ b/src/wp-includes/class-wp-icons-registry.php @@ -81,8 +81,8 @@ protected function __construct() { $this->register( 'core/' . $icon_name, array( - 'label' => $icon_data['label'], - 'filePath' => $icons_directory . $icon_data['filePath'], + 'label' => $icon_data['label'], + 'file_path' => $icons_directory . $icon_data['filePath'], ) ); } @@ -97,11 +97,11 @@ protected function __construct() { * @param array $icon_properties { * List of properties for the icon. * - * @type string $label Required. A human-readable label for the icon. - * @type string $content Optional. SVG markup for the icon. - * If not provided, the content will be retrieved from the `filePath` if set. - * If both `content` and `filePath` are not set, the icon will not be registered. - * @type string $filePath Optional. The full path to the file containing the icon content. + * @type string $label Required. A human-readable label for the icon. + * @type string $content Optional. SVG markup for the icon. + * If not provided, the content will be retrieved from the `file_path` if set. + * If both `content` and `file_path` are not set, the icon will not be registered. + * @type string $file_path Optional. The full path to the file containing the icon content. * } * @return bool True if the icon was registered with success and false otherwise. */ @@ -143,7 +143,7 @@ protected function register( $icon_name, $icon_properties ) { return false; } - $allowed_keys = array_fill_keys( array( 'label', 'content', 'filePath' ), 1 ); + $allowed_keys = array_fill_keys( array( 'label', 'content', 'file_path' ), 1 ); foreach ( array_keys( $icon_properties ) as $key ) { if ( ! array_key_exists( $key, $allowed_keys ) ) { _doing_it_wrong( @@ -169,12 +169,12 @@ protected function register( $icon_name, $icon_properties ) { } if ( - ( ! isset( $icon_properties['content'] ) && ! isset( $icon_properties['filePath'] ) ) || - ( isset( $icon_properties['content'] ) && isset( $icon_properties['filePath'] ) ) + ( ! isset( $icon_properties['content'] ) && ! isset( $icon_properties['file_path'] ) ) || + ( isset( $icon_properties['content'] ) && isset( $icon_properties['file_path'] ) ) ) { _doing_it_wrong( __METHOD__, - __( 'Icons must provide either `content` or `filePath`.' ), + __( 'Icons must provide either `content` or `file_path`.' ), '7.0.0' ); return false; @@ -262,7 +262,7 @@ protected function sanitize_icon_content( $icon_content ) { protected function get_content( $icon_name ) { if ( ! isset( $this->registered_icons[ $icon_name ]['content'] ) ) { $content = file_get_contents( - $this->registered_icons[ $icon_name ]['filePath'] + $this->registered_icons[ $icon_name ]['file_path'] ); $content = $this->sanitize_icon_content( $content ); diff --git a/tests/phpunit/tests/icons/wpIconsRegistry.php b/tests/phpunit/tests/icons/wpIconsRegistry.php index fba2eacde43f5..23352964db0f5 100644 --- a/tests/phpunit/tests/icons/wpIconsRegistry.php +++ b/tests/phpunit/tests/icons/wpIconsRegistry.php @@ -40,7 +40,7 @@ public function tear_down() { * Invokes WP_Icons_Registry::register despite it being private * * @param string $icon_name Icon name including namespace. - * @param array $icon_properties Icon properties (label, content, filePath). + * @param array $icon_properties Icon properties (label, content, file_path). * @return bool True if the icon was registered successfully. */ private function register( $icon_name, $icon_properties ) { @@ -107,4 +107,74 @@ public function test_register_invalid_name( $icon_name ) { $result = $this->register( $icon_name, $settings ); $this->assertFalse( $result ); } + + /** + * Should register an icon that provides its content through `file_path`. + * + * @ticket 64847 + * + * @covers ::register + */ + public function test_register_icon_with_file_path() { + $file_path = tempnam( get_temp_dir(), 'wp-icon-' ); + file_put_contents( $file_path, '' ); + + $name = 'test-plugin/file-path-icon'; + $settings = array( + 'label' => 'Icon', + 'file_path' => $file_path, + ); + + $result = $this->register( $name, $settings ); + $this->assertTrue( $result ); + $this->assertTrue( $this->registry->is_registered( $name ) ); + + $registered_icons = $this->registry->get_registered_icons( $name ); + $this->assertCount( 1, $registered_icons ); + $this->assertStringContainsString( ' 'Icon', + 'content' => '', + 'file_path' => '/path/to/icon.svg', + ); + + $result = $this->register( $name, $settings ); + $this->assertFalse( $result ); + $this->assertFalse( $this->registry->is_registered( $name ) ); + } + + /** + * Should fail to register an icon that provides neither `content` nor `file_path`. + * + * @ticket 64847 + * + * @covers ::register + * + * @expectedIncorrectUsage WP_Icons_Registry::register + */ + public function test_register_icon_without_content_or_file_path() { + $name = 'test-plugin/no-content'; + $settings = array( + 'label' => 'Icon', + ); + + $result = $this->register( $name, $settings ); + $this->assertFalse( $result ); + $this->assertFalse( $this->registry->is_registered( $name ) ); + } }