font-icons-manager.php
5.38 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
<?php
namespace Getwid;
class FontIconsManager {
private $defaultFontHandle = 'fontawesome-free';
private $defaultFontName = 'fontawesome';
/**
* @var array
*/
private $fonts;
public function __construct() {
add_action( 'init', [ $this, 'extendFontIcons' ] );
add_filter( 'getwid/editor_blocks_js/localize_data', [ $this, 'setIconsListLocalizeData' ] );
add_filter( 'getwid/editor_blocks_css/dependencies', [ $this, 'enqueueFonts' ] );
}
public function getFonts() {
return $this->fonts;
}
public function getDefaultFontHandle() {
return apply_filters(
'getwid/icons_manager/default_font_handle',
$this->defaultFontHandle
);
}
public function getDefaultFontName() {
return $this->defaultFontName;
}
public function extendFontIcons() {
$this->registerFontAwesome();
do_action( 'getwid/icons-manager/init', $this );
}
private function registerFontAwesome() {
/**
* Register FontAwesome by default
*
* Blocks with FontAwesome icons:
* - video-popup
* - image-hotspot
* - toggle
* - accordion
* - icon
* - social-links
* - icon-box
* - section
* - instagram
* - template-post-tags
* - template-post-date
* - template-post-categories
* - template-post-comments
* - template-post-author
*/
$this->registerFont(
$this->getDefaultFontName(),
[
'icons' => require( GETWID_PLUGIN_DIR . 'includes/data-list/font-awesome-icon-list.php' ),
'handle' => $this->getDefaultFontHandle(),
'src' => getwid_get_plugin_url( 'vendors/fontawesome-free/css/all.min.css' ),
'deps' => null,
'ver' => '5.5.0',
]
);
}
/**
* @param string $fontName
* @param array $args Required. Array of icon font arguments.
* @type array $icons Array of categories that contains icons
* @type string $handle Handle of style
* @type string $src Full URL of the stylesheet
* @return boolean
*/
public function registerFont( $fontName, $args ) {
// compatibility with 1.5.2
if ( ! empty( $args['style'] ) ) {
$this->deprecated_registerFont( $fontName, $args );
return true;
}
// since 1.5.3
if ( strlen( $fontName ) ) {
$this->fonts[ $fontName ] = [
'icons' => ! empty( $args['icons'] ) ? $args['icons'] : [],
'handle' => ! empty( $args['handle'] ) ? $args['handle'] : '',
'src' => ! empty( $args['src'] ) ? $args['src'] : false,
'deps' => ! empty( $args['deps'] ) ? $args['deps'] : [],
'ver' => ! empty( $args['ver'] ) ? $args['ver'] : false,
];
// https://developer.wordpress.org/reference/functions/wp_register_style/
return wp_register_style(
$this->fonts[ $fontName ]['handle'],
$this->fonts[ $fontName ]['src'],
$this->fonts[ $fontName ]['deps'],
$this->fonts[ $fontName ]['ver']
);
}
return false;
}
private function deprecated_registerFont( $fontName, $args ) {
$this->fonts[ $fontName ] = [
'icons' => ! empty( $args['icons'] ) ? $args['icons'] : [],
'style' => ! empty( $args['style'] ) ? $args['style'] : '',
'enqueue_callback' => ! empty( $args['enqueue_callback'] ) ? $args['enqueue_callback'] : null,
'callback_priority' => ! empty( $args['callback_priority'] ) ? $args['callback_priority'] : 10,
];
// Register the enqueue hook
if ( !is_null( $this->fonts[ $fontName ][ 'enqueue_callback' ] ) ) {
add_action(
'enqueue_block_assets',
$this->fonts[ $fontName ][ 'enqueue_callback' ],
$this->fonts[ $fontName ][ 'callback_priority' ]
);
}
}
/**
* @param string $fontName
*/
public function deregisterFont( $fontName ) {
if ( isset( $this->fonts[ $fontName ] ) ) {
if ( array_key_exists( 'handle', $this->fonts[ $fontName ] ) ) {
wp_deregister_style( $this->fonts[$fontName]['handle'] );
} else {
// compatibility with 1.5.2
$this->deprecated_deregisterFont( $fontName );
}
unset ( $this->fonts[ $fontName ] );
}
}
private function deprecated_deregisterFont( $fontName ) {
if ( !is_null( $this->fonts[ $fontName ][ 'enqueue_callback' ] ) ) {
remove_action(
'enqueue_block_assets',
$this->fonts[ $fontName ][ 'enqueue_callback' ],
$this->fonts[ $fontName ][ 'callback_priority' ]
);
}
}
/**
* @return array
*/
private function getCategorizedIconList() {
$iconsByFonts = array_values( array_column( $this->fonts, 'icons' ) );
$buff_arr = [];
foreach($iconsByFonts as $iconsArrs){
if(!empty($iconsArrs)){
$buff_arr = array_merge($buff_arr, $iconsArrs);
}
}
return count( $iconsByFonts ) > 1 ? $buff_arr : current( $iconsByFonts );
}
/**
* @param array $localizeData
*
* @return array
*/
public function setIconsListLocalizeData( $localizeData ) {
$localizeData['settings']['iconList'] = $this->getCategorizedIconList();
return $localizeData;
}
/**
* @param array $deps
*
* @return array
*/
public function enqueueFonts( $deps ) {
foreach ( $this->fonts as $name => $font ) {
if ( ! array_key_exists( 'handle', $font ) ) {
continue;
}
if ( ! in_array( $font['handle'], $deps ) && wp_style_is( $font['handle'], 'registered' ) ) {
$deps[] = $font['handle'];
}
}
return $deps;
}
}