PluginProbe ʕ •ᴥ•ʔ
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.13.24
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.13.24
0.13.24 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.1.4 0.1.5 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.11.2 0.11.3 0.11.4 0.11.5 0.12.0 0.12.1 0.12.10 0.12.11 0.12.12 0.12.2 0.12.3 0.12.4 0.12.5 0.12.6 0.12.7 0.12.8 0.12.9 0.13.0 0.13.1 0.13.10 0.13.11 0.13.12 0.13.13 0.13.14 0.13.15 0.13.16 0.13.17 0.13.18 0.13.19 0.13.2 0.13.20 0.13.21 0.13.22 0.13.23 0.13.3 0.13.4 0.13.5 0.13.6 0.13.7 0.13.8 0.13.9 0.2.0 0.2.1 0.2.2 0.2.3 0.3.0 0.3.1 0.3.2 0.4.0 0.5.0 0.5.1 0.6.0 0.7.0 0.8.0 0.8.1 0.9.0 0.9.2
menu-icons / vendor / codeinwp / icon-picker / includes / fontpack.php
menu-icons / vendor / codeinwp / icon-picker / includes Last commit date
fields 7 years ago types 2 years ago fontpack.php 7 years ago loader.php 7 years ago registry.php 7 years ago
fontpack.php
286 lines
1 <?php
2
3 /**
4 * Fontpack
5 *
6 * @package Icon_Picker
7 * @version 0.1.0
8 * @author Dzikri Aziz <kvcrvt@gmail.com>
9 */
10
11 final class Icon_Picker_Fontpack {
12
13 /**
14 * Icon_Picker_Fontpack singleton
15 *
16 * @static
17 * @since 0.1.0
18 * @access protected
19 * @var Icon_Picker_Fontpack
20 */
21 protected static $instance;
22
23 /**
24 * Fontpack directory path
25 *
26 * @since 0.1.0
27 * @access protected
28 * @var string
29 */
30 protected $dir;
31
32 /**
33 * Fontpack directory url path
34 *
35 * @since 0.1.0
36 * @access protected
37 * @var string
38 */
39 protected $url;
40
41 /**
42 * Error messages
43 *
44 * @since 0.1.0
45 * @access protected
46 * @var array
47 */
48 protected $messages = array();
49
50 /**
51 * Icon packs
52 *
53 * @since 0.1.0
54 * @access protected
55 * @var array
56 */
57 protected $packs = array();
58
59
60 /**
61 * Get instance
62 *
63 * @static
64 * @since 0.1.0
65 * @return Icon_Picker_Fontpack
66 */
67 public static function instance() {
68 if ( is_null( self::$instance ) ) {
69 self::$instance = new self();
70 }
71
72 return self::$instance;
73 }
74
75
76 /**
77 * Getter magic
78 *
79 * @since 0.1.0
80 * @param string $name Property name.
81 * @return mixed NULL if attribute doesn't exist.
82 */
83 public function __get( $name ) {
84 if ( isset( $this->$name ) ) {
85 return $this->$name;
86 }
87
88 return null;
89 }
90
91
92 /**
93 * Setter magic
94 *
95 * @since 0.1.0
96 * @return bool
97 */
98 public function __isset( $name ) {
99 return isset( $this->$name );
100 }
101
102
103 /**
104 * Constructor
105 *
106 * @since 0.1.0
107 * @access protected
108 * @return Icon_Picker_Fontpack
109 */
110 protected function __construct() {
111 /**
112 * Allow different system path for fontpacks
113 *
114 * @since 0.1.0
115 * @param string $dir Directory path, defaults to /wp-content/fontpacks.
116 */
117 $this->dir = apply_filters( 'icon_picker_fontpacks_dir_path', WP_CONTENT_DIR . '/fontpacks' );
118
119 if ( ! is_readable( $this->dir ) ) {
120 return;
121 }
122
123 /**
124 * Allow different URL path for fontpacks
125 *
126 * @since 0.4.0
127 * @param string $url URL path, defaults to /wp-content/fontpacks
128 */
129 $this->url = apply_filters( 'icon_picker_fontpacks_dir_url', WP_CONTENT_URL . '/fontpacks' );
130
131 $this->messages = array(
132 'no_config' => __( 'Icon Picker: %1$s was not found in %2$s.', 'icon-picker' ),
133 'config_error' => __( 'Icon Picker: %s contains an error or more.', 'icon-picker' ),
134 'invalid' => __( 'Icon Picker: %1$s is not set or invalid in %2$s.', 'icon-picker' ),
135 'duplicate' => __( 'Icon Picker: %1$s is already registered. Please check your font pack config file: %2$s.', 'icon-picker' ),
136 );
137
138 $this->collect_packs();
139 $this->register_packs();
140 }
141
142
143 /**
144 * Collect icon packs
145 *
146 * @since 0.1.0
147 * @access protected
148 * @return void
149 */
150 protected function collect_packs() {
151 $iterator = new DirectoryIterator( $this->dir );
152
153 foreach ( $iterator as $pack_dir ) {
154 if ( $pack_dir->isDot() || ! $pack_dir->isDir() || ! $pack_dir->isReadable() ) {
155 continue;
156 }
157
158 $pack_dirname = $pack_dir->getFilename();
159 $pack_data = $this->get_pack_data( $pack_dir );
160
161 if ( ! empty( $pack_data ) ) {
162 $this->packs[ $pack_dirname ] = $pack_data;
163 }
164 }
165 }
166
167
168 /**
169 * Register icon packs
170 *
171 * @since 0.1.0
172 * @access protected
173 * @return void
174 */
175 protected function register_packs() {
176 if ( empty( $this->packs ) ) {
177 return;
178 }
179
180 $icon_picker = Icon_Picker::instance();
181 require_once "{$icon_picker->dir}/includes/types/fontello.php";
182
183 foreach ( $this->packs as $pack_data ) {
184 $icon_picker->registry->add( new Icon_Picker_Type_Fontello( $pack_data ) );
185 }
186 }
187
188
189 /**
190 * Get icon pack data
191 *
192 * @since 0.1.0
193 * @access protected
194 * @param DirectoryIterator $pack_dir Icon pack directory object.
195 * @return array Icon pack data array or FALSE.
196 */
197 protected function get_pack_data( DirectoryIterator $pack_dir ) {
198 $pack_dirname = $pack_dir->getFilename();
199 $pack_path = $pack_dir->getPathname();
200 $cache_id = "icon_picker_fontpack_{$pack_dirname}";
201 $cache_data = get_transient( $cache_id );
202 $config_file = "{$pack_path}/config.json";
203
204 if ( false !== $cache_data && $cache_data['version'] === $pack_dir->getMTime() ) {
205 return $cache_data;
206 }
207
208 // Make sure the config file exists and is readable.
209 if ( ! is_readable( $config_file ) ) {
210 trigger_error(
211 sprintf(
212 esc_html( $this->messages['no_config'] ),
213 '<code>config.json</code>',
214 sprintf( '<code>%s</code>', esc_html( $pack_path ) )
215 )
216 );
217
218 return false;
219 }
220
221 $config = json_decode( file_get_contents( $config_file ), true );
222 $errors = json_last_error();
223
224 if ( ! empty( $errors ) ) {
225 trigger_error(
226 sprintf(
227 esc_html( $this->messages['config_error'] ),
228 sprintf( '<code>%s/config.json</code>', esc_html( $pack_path ) )
229 )
230 );
231
232 return false;
233 }
234
235 $keys = array( 'name', 'glyphs', 'css_prefix_text' );
236 $items = array();
237
238 // Check each required config.
239 foreach ( $keys as $key ) {
240 if ( empty( $config[ $key ] ) ) {
241 trigger_error(
242 sprintf(
243 esc_html( $this->messages['invalid'] ),
244 sprintf( '<code><em>%s</em></code>', esc_html( $key ) ),
245 esc_html( $config_file )
246 )
247 );
248
249 return false;
250 }
251 }
252
253 // Bail if no glyphs found.
254 if ( ! is_array( $config['glyphs'] ) || empty( $config['glyphs'] ) ) {
255 return false;
256 }
257
258 foreach ( $config['glyphs'] as $glyph ) {
259 if ( ! empty( $glyph['css'] ) ) {
260 $items[] = array(
261 'id' => $config['css_prefix_text'] . $glyph['css'],
262 'name' => $glyph['css'],
263 );
264 }
265 }
266
267 if ( empty( $items ) ) {
268 return false;
269 }
270
271 $pack_data = array(
272 'id' => "pack-{$config['name']}",
273 'name' => sprintf( __( 'Pack: %s', 'icon-picker' ), $config['name'] ),
274 'version' => $pack_dir->getMTime(),
275 'items' => $items,
276 'stylesheet_uri' => "{$this->url}/{$pack_dirname}/css/{$config['name']}.css",
277 'dir' => "{$this->dir}/{$pack_dirname}",
278 'url' => "{$this->url}/{$pack_dirname}",
279 );
280
281 set_transient( $cache_id, $pack_data, DAY_IN_SECONDS );
282
283 return $pack_data;
284 }
285 }
286