-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSvg.php
More file actions
136 lines (113 loc) · 3.37 KB
/
Svg.php
File metadata and controls
136 lines (113 loc) · 3.37 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
<?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 );
$hash_sprite = $this->get_sprite_hash( $sprite_name );
return sprintf( '<svg class="%s" aria-hidden="true" focusable="false"><use href="%s%s#%s"></use></svg>', implode( ' ', $classes ), \get_theme_file_uri( sprintf( '/dist/icons/%s.svg', $sprite_name ) ), $hash_sprite, $icon_slug ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* @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' => [],
];
$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
*/
public function get_sprite_hash( $sprite_name ) {
$sprite_hash_file = \get_theme_file_path( '/dist/sprite-hashes.json' );
if ( ! is_readable( $sprite_hash_file ) ) {
return '';
}
$sprite_hash = file_get_contents( $sprite_hash_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$sprite_hash = json_decode( $sprite_hash, true );
if ( empty( $sprite_hash ) ) {
return '';
}
if ( ! isset( $sprite_hash[ sprintf( 'icons/%s.svg', $sprite_name ) ] ) ) {
return '';
}
return '?' . $sprite_hash[ sprintf( 'icons/%s.svg', $sprite_name ) ];
}
}