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