-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSvg.php
More file actions
148 lines (125 loc) · 3.81 KB
/
Svg.php
File metadata and controls
148 lines (125 loc) · 3.81 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
<?php
namespace BEA\Theme\Framework\Services;
use BEA\Theme\Framework\Service;
use BEA\Theme\Framework\Service_Container;
/**
* Class Svg
*
* @package BEA\Theme\Framework
*/
class Svg implements Service {
/**
* @param Service_Container $container
*/
public function register( Service_Container $container ): void {
add_filter( 'wp_kses_allowed_html', [ $this, 'allow_svg_tag' ] );
}
/**
* @param Service_Container $container
*/
public function boot( Service_Container $container ): void {
}
/**
* @return string
*/
public function get_service_name(): string {
return 'svg';
}
/**
* @param string $icon_class
* @param array $additionnal_classes
*
* @return string
*/
public function get_the_icon( string $icon_class, array $additionnal_classes = [] ): string {
if ( empty( $icon_class ) ) {
return '';
}
// acf-svg-icon already return sprite-name.svg#icon-name, ex: social.svg#icon-facebook
// format the string to obtain sprite-name/icon-name
$icon_class = str_replace( '.svg#icon-', '/', $icon_class );
$sprite_name = 'sprite';
$slash_pos = strpos( $icon_class, '/' );
if ( false !== $slash_pos ) {
$sprite_name = strtok( $icon_class, '/' );
$icon_class = substr( $icon_class, $slash_pos + 1 );
}
$icon_slug = strpos( $icon_class, 'icon-' ) === 0 ? $icon_class : sprintf( 'icon-%s', $icon_class );
$classes = [ 'icon', $icon_slug ];
$classes = array_merge( $classes, $additionnal_classes );
$classes = array_map( 'sanitize_html_class', $classes );
$icon_url = \get_theme_file_uri( sprintf( '/dist/icons/%s.svg', $sprite_name ) );
$hash_sprite = $this->get_sprite_hash( $sprite_name );
return sprintf( '<svg class="%s" aria-hidden="true" focusable="false"><use href="%s#%s"></use></svg>', implode( ' ', $classes ), add_query_arg( [ 'v' => $hash_sprite ], $icon_url ), $icon_slug );
}
/**
* @param string $icon_class
* @param array $additionnal_classes
*/
public function the_icon( string $icon_class, array $additionnal_classes = [] ): void {
echo $this->get_the_icon( $icon_class, $additionnal_classes ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Allow svg tag
*
* @param $tags
*
* @return mixed
* @author Egidio CORICA
*/
public function allow_svg_tag( $tags ) {
$tags['svg'] = [
'xmlns' => [],
'fill' => [],
'viewbox' => [],
'role' => [],
'aria-hidden' => [],
'focusable' => [],
'class' => [],
'style' => [],
'width' => [],
'height' => [],
];
$tags['path'] = [
'd' => [],
'fill' => [],
];
$tags['use'] = [
'href' => [],
'xmlns:xlink' => [],
'xlink:href' => [],
];
return $tags;
}
/**
* Get the hash of the sprite
*
* @param string $sprite_name
*
* @return string | null
*/
public function get_sprite_hash( string $sprite_name ): ?string {
static $sprite_hashes = null;
if ( null === $sprite_hashes ) {
$php_file = get_theme_file_path( '/dist/sprite-hashes.php' );
$json_file = get_theme_file_path( '/dist/sprite-hashes.json' );
if ( is_readable( $php_file ) ) {
$sprite_hashes = require $php_file;
$sprite_hashes = \is_array( $sprite_hashes ) ? $sprite_hashes : [];
} elseif ( is_readable( $json_file ) ) {
$json_content = file_get_contents( $json_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
try {
$sprite_hashes = json_decode( $json_content, true, 512, JSON_THROW_ON_ERROR );
$sprite_hashes = \is_array( $sprite_hashes ) ? $sprite_hashes : [];
} catch ( \JsonException $e ) {
$sprite_hashes = [];
}
} else {
$sprite_hashes = [];
return null;
}
$sprite_hashes = $sprite_hash;
}
return $sprite_hash[ sprintf( 'icons/%s.svg', $sprite_name ) ] ?? null;
}
}