Skip to content
24 changes: 12 additions & 12 deletions src/wp-includes/class-wp-icons-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
)
);
}
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand Down Expand Up @@ -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 );

Expand Down
72 changes: 71 additions & 1 deletion tests/phpunit/tests/icons/wpIconsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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, '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"></svg>' );

$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( '<svg', $registered_icons[0]['content'] );

unlink( $file_path );
}

/**
* Should fail to register an icon that provides both `content` and `file_path`.
*
* @ticket 64847
*
* @covers ::register
*
* @expectedIncorrectUsage WP_Icons_Registry::register
*/
public function test_register_icon_with_content_and_file_path() {
$name = 'test-plugin/content-and-file-path';
$settings = array(
'label' => 'Icon',
'content' => '<svg></svg>',
'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 ) );
}
}
Loading