Skip to content

Commit c091bf3

Browse files
committed
chore: Move register regex checks to 7.1 and renamed tests class/file_name
1 parent bac73b4 commit c091bf3

3 files changed

Lines changed: 220 additions & 38 deletions

File tree

‎lib/compat/wordpress-7.0/class-wp-icons-registry.php‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,34 +93,6 @@ private function register( $icon_name, $icon_properties ) {
9393
return false;
9494
}
9595

96-
if ( preg_match( '/[A-Z]+/', $icon_name ) ) {
97-
_doing_it_wrong(
98-
__METHOD__,
99-
__( 'Icon names must not contain uppercase characters.' ),
100-
'7.0.0'
101-
);
102-
return false;
103-
}
104-
105-
$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
106-
if ( ! preg_match( $name_matcher, $icon_name ) ) {
107-
_doing_it_wrong(
108-
__METHOD__,
109-
__( 'Icon names must contain a namespace prefix. Example: my-plugin/my-custom-icon' ),
110-
'7.0.0'
111-
);
112-
return false;
113-
}
114-
115-
if ( $this->is_registered( $icon_name ) ) {
116-
_doing_it_wrong(
117-
__METHOD__,
118-
__( 'Icon is already registered.' ),
119-
'7.0.0'
120-
);
121-
return false;
122-
}
123-
12496
$allowed_keys = array_fill_keys( array( 'label', 'content', 'filePath' ), 1 );
12597
foreach ( array_keys( $icon_properties ) as $key ) {
12698
if ( ! array_key_exists( $key, $allowed_keys ) ) {
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
3+
class Gutenberg_Icons_Registry_7_1 extends WP_Icons_Registry {
4+
/**
5+
* Modified to point $manifest_path to Gutenberg packages
6+
*/
7+
protected function __construct() {
8+
$icons_directory = gutenberg_dir_path() . 'packages/icons/src';
9+
$icons_directory = trailingslashit( $icons_directory );
10+
$manifest_path = $icons_directory . 'manifest.php';
11+
12+
if ( ! is_readable( $manifest_path ) ) {
13+
wp_trigger_error(
14+
__METHOD__,
15+
__( 'Core icon collection manifest is missing or unreadable.', 'gutenberg' )
16+
);
17+
return;
18+
}
19+
20+
$collection = include $manifest_path;
21+
22+
if ( empty( $collection ) ) {
23+
wp_trigger_error(
24+
__METHOD__,
25+
__( 'Core icon collection manifest is empty or invalid.', 'gutenberg' )
26+
);
27+
return;
28+
}
29+
30+
foreach ( $collection as $icon_name => $icon_data ) {
31+
if (
32+
empty( $icon_data['filePath'] )
33+
|| ! is_string( $icon_data['filePath'] )
34+
) {
35+
_doing_it_wrong(
36+
__METHOD__,
37+
__( 'Core icon collection manifest must provide valid a "filePath" for each icon.', 'gutenberg' ),
38+
'7.0.0'
39+
);
40+
return;
41+
}
42+
43+
$this->register(
44+
'core/' . $icon_name,
45+
array(
46+
'label' => $icon_data['label'],
47+
'filePath' => $icons_directory . $icon_data['filePath'],
48+
)
49+
);
50+
}
51+
}
52+
53+
/**
54+
* Registers an icon.
55+
*
56+
* @param string $icon_name Icon name including namespace.
57+
* @param array $icon_properties {
58+
* List of properties for the icon.
59+
*
60+
* @type string $label Required. A human-readable label for the icon.
61+
* @type string $content Optional. SVG markup for the icon.
62+
* If not provided, the content will be retrieved from the `filePath` if set.
63+
* If both `content` and `filePath` are not set, the icon will not be registered.
64+
* @type string $filePath Optional. The full path to the file containing the icon content.
65+
* }
66+
* @return bool True if the icon was registered with success and false otherwise.
67+
*/
68+
protected function register( $icon_name, $icon_properties ) {
69+
if ( ! isset( $icon_name ) || ! is_string( $icon_name ) ) {
70+
_doing_it_wrong(
71+
__METHOD__,
72+
__( 'Icon name must be a string.', 'gutenberg' ),
73+
'7.0.0'
74+
);
75+
return false;
76+
}
77+
78+
if ( preg_match( '/[A-Z]+/', $icon_name ) ) {
79+
_doing_it_wrong(
80+
__METHOD__,
81+
__( 'Icon names must not contain uppercase characters.' ),
82+
'7.1.0'
83+
);
84+
return false;
85+
}
86+
87+
$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
88+
if ( ! preg_match( $name_matcher, $icon_name ) ) {
89+
_doing_it_wrong(
90+
__METHOD__,
91+
__( 'Icon names must contain a namespace prefix. Example: my-plugin/my-custom-icon' ),
92+
'7.1.0'
93+
);
94+
return false;
95+
}
96+
97+
if ( $this->is_registered( $icon_name ) ) {
98+
_doing_it_wrong(
99+
__METHOD__,
100+
__( 'Icon is already registered.' ),
101+
'7.1.0'
102+
);
103+
return false;
104+
}
105+
106+
$allowed_keys = array_fill_keys( array( 'label', 'content', 'filePath' ), 1 );
107+
foreach ( array_keys( $icon_properties ) as $key ) {
108+
if ( ! array_key_exists( $key, $allowed_keys ) ) {
109+
_doing_it_wrong(
110+
__METHOD__,
111+
sprintf(
112+
// translators: %s is the name of any user-provided key
113+
__( 'Invalid icon property: "%s".', 'gutenberg' ),
114+
$key
115+
),
116+
'7.0.0'
117+
);
118+
return false;
119+
}
120+
}
121+
122+
if ( ! isset( $icon_properties['label'] ) || ! is_string( $icon_properties['label'] ) ) {
123+
_doing_it_wrong(
124+
__METHOD__,
125+
__( 'Icon label must be a string.', 'gutenberg' ),
126+
'7.0.0'
127+
);
128+
return false;
129+
}
130+
131+
if (
132+
( ! isset( $icon_properties['content'] ) && ! isset( $icon_properties['filePath'] ) ) ||
133+
( isset( $icon_properties['content'] ) && isset( $icon_properties['filePath'] ) )
134+
) {
135+
_doing_it_wrong(
136+
__METHOD__,
137+
__( 'Icons must provide either `content` or `filePath`.', 'gutenberg' ),
138+
'7.0.0'
139+
);
140+
return false;
141+
}
142+
143+
if ( isset( $icon_properties['content'] ) ) {
144+
if ( ! is_string( $icon_properties['content'] ) ) {
145+
_doing_it_wrong(
146+
__METHOD__,
147+
__( 'Icon content must be a string.', 'gutenberg' ),
148+
'7.0.0'
149+
);
150+
return false;
151+
}
152+
153+
$sanitized_icon_content = $this->sanitize_icon_content( $icon_properties['content'] );
154+
if ( empty( $sanitized_icon_content ) ) {
155+
_doing_it_wrong(
156+
__METHOD__,
157+
__( 'Icon content does not contain valid SVG markup.', 'gutenberg' ),
158+
'7.0.0'
159+
);
160+
return false;
161+
}
162+
}
163+
164+
$icon = array_merge(
165+
$icon_properties,
166+
array( 'name' => $icon_name )
167+
);
168+
169+
$this->registered_icons[ $icon_name ] = $icon;
170+
171+
return true;
172+
}
173+
174+
/**
175+
* Modified to also search in icon labels
176+
*/
177+
public function get_registered_icons( $search = '' ) {
178+
$icons = array();
179+
180+
foreach ( $this->registered_icons as $icon ) {
181+
if ( ! empty( $search )
182+
&& false === stripos( $icon['name'], $search )
183+
&& false === stripos( $icon['label'], $search )
184+
) {
185+
continue;
186+
}
187+
188+
$icon['content'] = $icon['content'] ?? $this->get_content( $icon['name'] );
189+
$icons[] = $icon;
190+
}
191+
192+
return $icons;
193+
}
194+
195+
/**
196+
* Redefined to break away from base class.
197+
*/
198+
protected static $instance = null;
199+
200+
/**
201+
* Redefined to access new `$instance`
202+
*/
203+
public static function get_instance() {
204+
if ( null === self::$instance ) {
205+
self::$instance = new self();
206+
}
207+
208+
return self::$instance;
209+
}
210+
}

phpunit/experimental/wpIconsRegistry.php renamed to phpunit/experimental/class-gutenberg-icons-registry-7-1-test.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?php
22
/**
3-
* Tests for WP_Icons_Registry.
3+
* Tests for Gutenberg_Icons_Registry_7_1.
44
*
5-
* @package WordPress
5+
* @package Gutenberg
66
* @subpackage Icons
7-
* @since 7.0.0
7+
* @since 7.1.0
88
*
99
* @group icons
1010
*/
11-
class Tests_Icons_WpIconsRegistry extends WP_UnitTestCase {
11+
class Gutenberg_Icons_Registry_7_1_Test extends WP_UnitTestCase {
1212

1313
/**
1414
* Icons registry.
1515
*
16-
* @var WP_Icons_Registry
16+
* @var Gutenberg_Icons_Registry_7_1
1717
*/
1818
private $registry = null;
1919

@@ -22,7 +22,7 @@ class Tests_Icons_WpIconsRegistry extends WP_UnitTestCase {
2222
*/
2323
public function set_up() {
2424
parent::set_up();
25-
$this->registry = WP_Icons_Registry::get_instance();
25+
$this->registry = Gutenberg_Icons_Registry_7_1::get_instance();
2626
}
2727

2828
/**
@@ -41,7 +41,7 @@ public function tear_down() {
4141
* @return bool True on success, false on failure.
4242
* @throws ReflectionException If the method does not exist.
4343
*
44-
* @expectedIncorrectUsage WP_Icons_Registry::register
44+
* @expectedIncorrectUsage Gutenberg_Icons_Registry_7_1::register
4545
*/
4646
private function register_icon( $name, $args = array() ) {
4747
$method = new ReflectionMethod( $this->registry, 'register' );
@@ -52,7 +52,7 @@ private function register_icon( $name, $args = array() ) {
5252
/**
5353
* Should reject non-string names.
5454
*
55-
* @expectedIncorrectUsage WP_Icons_Registry::register
55+
* @expectedIncorrectUsage Gutenberg_Icons_Registry_7_1::register
5656
*/
5757
public function test_invalid_non_string_names() {
5858
$result = $this->register_icon( 1, array() );
@@ -62,7 +62,7 @@ public function test_invalid_non_string_names() {
6262
/**
6363
* Should reject icons without a namespace.
6464
*
65-
* @expectedIncorrectUsage WP_Icons_Registry::register
65+
* @expectedIncorrectUsage Gutenberg_Icons_Registry_7_1::register
6666
*/
6767
public function test_invalid_names_without_namespace() {
6868
$result = $this->register_icon( 'plus', array() );
@@ -72,7 +72,7 @@ public function test_invalid_names_without_namespace() {
7272
/**
7373
* Should reject icons with uppercase characters.
7474
*
75-
* @expectedIncorrectUsage WP_Icons_Registry::register
75+
* @expectedIncorrectUsage Gutenberg_Icons_Registry_7_1::register
7676
*/
7777
public function test_uppercase_characters() {
7878
$result = $this->register_icon( 'Core/Plus', array() );

0 commit comments

Comments
 (0)