Skip to content

Commit 4ec21e4

Browse files
committed
feat(Icon): register default bff icons collection for core/icon block
1 parent ecb4312 commit 4ec21e4

4 files changed

Lines changed: 225 additions & 1 deletion

File tree

‎config/svgo.config.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
name: 'removeViewBox',
88
active: false,
99
},
10+
// Plugins that are not in the "preset-default" and that you want to activate
1011
'removeTitle',
1112
'convertStyleToAttrs',
1213
],

‎inc/Framework.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use BEA\Theme\Framework\Services\Editor;
1010
use BEA\Theme\Framework\Services\Editor_Patterns;
1111
use BEA\Theme\Framework\Services\Gravity_Forms;
12+
use BEA\Theme\Framework\Services\Icon;
1213
use BEA\Theme\Framework\Services\Menu;
1314
use BEA\Theme\Framework\Services\Svg;
1415
use BEA\Theme\Framework\Services\Theme;
@@ -38,6 +39,7 @@ class Framework {
3839
Editor_Patterns::class,
3940
Gravity_Forms::class,
4041
Svg::class,
42+
Icon::class,
4143
Acf::class,
4244
Menu::class,
4345
Breadcrumb::class,

‎inc/Services/Editor.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ public function gutenberg_blocks_allowed( $allowed_blocks, \WP_Block_Editor_Cont
244244
'core/accordion-heading',
245245
'core/accordion-item',
246246
'core/accordion-panel',
247-
'core/icon',
248247
// Theme & site (unused).
249248
'core/archives',
250249
'core/calendar',

‎inc/Services/Icon.php‎

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<?php
2+
3+
namespace BEA\Theme\Framework\Services;
4+
5+
use BEA\Theme\Framework\Service;
6+
use BEA\Theme\Framework\Service_Container;
7+
8+
/**
9+
* Registers theme icon collections for the WordPress Icon block (7.1+).
10+
*/
11+
class Icon implements Service {
12+
13+
private const COLLECTION_SPRITE = 'beapi-sprite';
14+
15+
private const COLLECTION_SOCIAL = 'beapi-social';
16+
17+
/**
18+
* @param Service_Container $container Service container.
19+
*/
20+
public function register( Service_Container $container ): void {}
21+
22+
/**
23+
* @param Service_Container $container Service container.
24+
*/
25+
public function boot( Service_Container $container ): void {
26+
add_action( 'init', [ $this, 'register_icon_collections' ] );
27+
add_action( 'init', [ $this, 'unregister_core_icon_collection' ], 20 );
28+
}
29+
30+
/**
31+
* @return string
32+
*/
33+
public function get_service_name(): string {
34+
return 'icon';
35+
}
36+
37+
/**
38+
* Registers icon collections and icons from built SVG files.
39+
*
40+
* @return void
41+
*/
42+
public function register_icon_collections(): void {
43+
if ( ! function_exists( 'wp_register_icon_collection' ) || ! function_exists( 'wp_register_icon' ) ) {
44+
return;
45+
}
46+
47+
$collections = apply_filters(
48+
'bff_icon_collections',
49+
[
50+
self::COLLECTION_SPRITE => [
51+
'source_subdir' => 'sprite',
52+
'label' => __( 'General', 'beapi-frontend-framework' ),
53+
'description' => __( 'Theme UI icons.', 'beapi-frontend-framework' ),
54+
],
55+
self::COLLECTION_SOCIAL => [
56+
'source_subdir' => 'social',
57+
'label' => __( 'Social', 'beapi-frontend-framework' ),
58+
'description' => __( 'Social network icons.', 'beapi-frontend-framework' ),
59+
],
60+
]
61+
);
62+
63+
foreach ( $collections as $collection_slug => $collection_args ) {
64+
$this->register_collection( (string) $collection_slug, $collection_args );
65+
}
66+
}
67+
68+
/**
69+
* Unregisters the default WordPress core icon collection from the Icon block library.
70+
*
71+
* Runs on init at priority 20 so core icons are registered first (priority 10).
72+
*
73+
* @return void
74+
*/
75+
public function unregister_core_icon_collection(): void {
76+
if ( ! function_exists( 'wp_unregister_icon_collection' ) ) {
77+
return;
78+
}
79+
80+
if ( ! apply_filters( 'bff_unregister_core_icon_collection', true ) ) {
81+
return;
82+
}
83+
84+
wp_unregister_icon_collection( 'core' );
85+
}
86+
87+
/**
88+
* @param string $collection_slug Collection slug passed to wp_register_icon_collection().
89+
* @param array<string, mixed> $collection_args Collection configuration.
90+
*
91+
* @return void
92+
*/
93+
private function register_collection( string $collection_slug, array $collection_args ): void {
94+
if ( ! function_exists( 'wp_register_icon_collection' ) ) {
95+
return;
96+
}
97+
98+
$source_subdir = isset( $collection_args['source_subdir'] ) ? (string) $collection_args['source_subdir'] : '';
99+
100+
if ( '' === $source_subdir ) {
101+
return;
102+
}
103+
104+
wp_register_icon_collection(
105+
$collection_slug,
106+
[
107+
'label' => $collection_args['label'] ?? $collection_slug,
108+
'description' => $collection_args['description'] ?? '',
109+
]
110+
);
111+
112+
$icons_directory = $this->resolve_icons_directory( $source_subdir );
113+
114+
if ( null === $icons_directory ) {
115+
return;
116+
}
117+
118+
$svg_files = glob( $icons_directory . '/*.svg' );
119+
120+
if ( false === $svg_files ) {
121+
return;
122+
}
123+
124+
foreach ( $svg_files as $svg_file ) {
125+
$this->register_icon_file( $collection_slug, $svg_file );
126+
}
127+
}
128+
129+
/**
130+
* Resolves the directory containing standalone SVG files for a collection.
131+
*
132+
* @param string $source_subdir Subdirectory name (e.g. sprite, social).
133+
*
134+
* @return string|null Absolute path or null when not found.
135+
*/
136+
private function resolve_icons_directory( string $source_subdir ): ?string {
137+
$source_subdir = preg_replace( '/[^a-z0-9-]/', '', strtolower( $source_subdir ) );
138+
139+
if ( '' === $source_subdir ) {
140+
return null;
141+
}
142+
143+
$candidates = [
144+
get_theme_file_path( 'dist/images/' . $source_subdir ),
145+
get_theme_file_path( 'src/img/icons/' . $source_subdir ),
146+
];
147+
148+
foreach ( $candidates as $candidate ) {
149+
if ( ! is_dir( $candidate ) ) {
150+
continue;
151+
}
152+
153+
$real_path = realpath( $candidate );
154+
155+
if ( false !== $real_path ) {
156+
return $real_path;
157+
}
158+
}
159+
160+
return null;
161+
}
162+
163+
/**
164+
* @param string $collection_slug Collection slug.
165+
* @param string $svg_file Absolute path to an SVG file.
166+
*
167+
* @return void
168+
*/
169+
private function register_icon_file( string $collection_slug, string $svg_file ): void {
170+
if ( ! function_exists( 'wp_register_icon' ) ) {
171+
return;
172+
}
173+
174+
$real_file = realpath( $svg_file );
175+
176+
if ( false === $real_file || ! is_readable( $real_file ) ) {
177+
return;
178+
}
179+
180+
$icon_slug = pathinfo( $real_file, PATHINFO_FILENAME );
181+
$icon_slug = preg_replace( '/[^a-z0-9-]/', '', strtolower( (string) $icon_slug ) );
182+
183+
if ( '' === $icon_slug ) {
184+
return;
185+
}
186+
187+
$icon_name = $collection_slug . '/' . $icon_slug;
188+
$label = apply_filters(
189+
'bff_icon_label',
190+
$this->format_icon_label( $icon_slug ),
191+
$icon_slug,
192+
$collection_slug
193+
);
194+
195+
$properties = apply_filters(
196+
'bff_icon_properties',
197+
[
198+
'label' => $label,
199+
'file_path' => $real_file,
200+
],
201+
$icon_name,
202+
$collection_slug
203+
);
204+
205+
if ( empty( $properties['file_path'] ) && empty( $properties['content'] ) ) {
206+
return;
207+
}
208+
209+
wp_register_icon( $icon_name, $properties );
210+
}
211+
212+
/**
213+
* @param string $icon_slug Icon filename without extension.
214+
*
215+
* @return string
216+
*/
217+
private function format_icon_label( string $icon_slug ): string {
218+
$label = str_replace( [ '-', '_' ], ' ', $icon_slug );
219+
220+
return ucwords( $label );
221+
}
222+
}

0 commit comments

Comments
 (0)