PluginProbe
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.8.0
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.8.0
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 All 70 releases
menu-icons / menu-icons.php
menu-icons.php
464 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Menu Icons
5 *
6 * @package Menu_Icons
7 * @version 0.8.0
8 * @author Dzikri Aziz <kvcrvt@gmail.com>
9 *
10 *
11 * Plugin name: Menu Icons
12 * Plugin URI: http://kucrut.org/
13 * Description: Spice up your navigation menus with pretty icons, easily.
14 * Version: 0.8.0
15 * Author: Dzikri Aziz
16 * Author URI: http://kucrut.org/
17 * License: GPLv2
18 * Text Domain: menu-icons
19 * Domain Path: /languages
20 */
21
22
23 /**
24 * Main plugin class
25 */
26 final class Menu_Icons {
27
28 const VERSION = '0.8.0';
29
30 /**
31 * Holds plugin data
32 *
33 * @access protected
34 * @since 0.1.0
35 * @var array
36 */
37 protected static $data;
38
39
40 /**
41 * Get plugin data
42 *
43 * @since 0.1.0
44 * @param string $name
45 *
46 * @return mixed
47 */
48 public static function get( $name = null ) {
49 if ( is_null( $name ) ) {
50 return self::$data;
51 }
52
53 if ( isset( self::$data[ $name ] ) ) {
54 return self::$data[ $name ];
55 }
56
57 return false;
58 }
59
60
61 /**
62 * Load plugin
63 *
64 * 1. Load translation
65 * 2. Set plugin data (directory and URL paths)
66 * 3. Register built-in icon types
67 * 4. Attach plugin initialization at wp_loaded hook
68 *
69 * @since 0.1.0
70 * @wp_hook action plugins_loaded/10
71 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/plugins_loaded Action: plugins_loaded/10
72 */
73 public static function _load() {
74 load_plugin_textdomain( 'menu-icons', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
75
76 /**
77 * Allow different system path for fontpacks
78 *
79 * @since 0.4.0
80 * @param string Directory path, defaults to /wp-content/fontpacks
81 */
82 $fontpacks_dir_path = apply_filters( 'menu_icons_fontpacks_dir_path', WP_CONTENT_DIR . '/fontpacks' );
83
84 /**
85 * Allow different URL path for fontpacks
86 *
87 * @since 0.4.0
88 * @param string URL path, defaults to /wp-content/fontpacks
89 */
90 $fontpacks_dir_url = apply_filters( 'menu_icons_fontpacks_dir_url', WP_CONTENT_URL . '/fontpacks' );
91
92 self::$data = array(
93 'dir' => plugin_dir_path( __FILE__ ),
94 'url' => plugin_dir_url( __FILE__ ),
95 'icon_types' => array(),
96 'default_style' => array(
97 'font-size' => '1.2em',
98 'vertical-align' => 'middle',
99 ),
100 'fontpacks_dir_path' => $fontpacks_dir_path,
101 'fontpacks_dir_url' => $fontpacks_dir_url,
102 );
103
104 require_once self::$data['dir'] . 'includes/library/functions.php';
105
106 add_filter( 'menu_icons_types', array( __CLASS__, '_register_icon_types' ), 7 );
107 add_filter( 'menu_icons_types', array( __CLASS__, '_register_font_packs' ), 8 );
108 add_filter( 'is_protected_meta', array( __CLASS__, '_protect_meta_key' ), 10, 3 );
109 add_action( 'wp_loaded', array( __CLASS__, '_init' ), 9 );
110 }
111
112
113 /**
114 * Protect meta key
115 *
116 * This prevents our meta key from showing up on Custom Fields meta box
117 *
118 * @since 0.3.0
119 * @wp_hook filter is_protected_meta
120 * @param bool $protected Protection status
121 * @param string $meta_key Meta key
122 * @param string $meta_type Meta type
123 * @return bool Protection status
124 */
125 public static function _protect_meta_key( $protected, $meta_key, $meta_type ) {
126 if ( 'menu-icons' === $meta_key ) {
127 $protected = true;
128 }
129
130 return $protected;
131 }
132
133
134 /**
135 * Initialize
136 *
137 * 1. Collect registered types
138 * 2. Load settings
139 *
140 * @since 0.1.0
141 * @wp_hook action wp_loaded/9
142 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_loaded Action: wp_loaded/9
143 */
144 public static function _init() {
145 self::_collect_icon_types();
146
147 // Nothing to do if there are no icon types registered
148 if ( empty( self::$data['icon_types'] ) ) {
149 trigger_error( esc_html__( 'Menu Icons: No registered icon types found.', 'menu-icons' ) );
150
151 return;
152 }
153
154 // Load settings
155 require_once self::$data['dir'] . 'includes/settings.php';
156 Menu_Icons_Settings::init();
157
158 if ( ! is_admin() ) {
159 self::_load_front_end();
160 }
161
162 do_action( 'menu_icons_loaded' );
163 }
164
165
166 /**
167 * Register built-in icon types
168 *
169 * @since 0.1.5
170 * @access protected
171 * @wp_hook filter menu_icons_types
172 *
173 * @param array $icon_types Current icon types
174 * @return array
175 */
176 public static function _register_icon_types( $icon_types ) {
177 $builtin_types = array(
178 'image',
179 'dashicons',
180 'elusive',
181 'fontawesome',
182 'foundation',
183 'genericons',
184 'svg',
185 );
186
187 foreach ( $builtin_types as $type ) {
188 require_once sprintf( '%s/includes/type-%s.php', self::$data['dir'], $type );
189
190 $class_name = sprintf( 'Menu_Icons_Type_%s', ucfirst( $type ) );
191 $type_instance = new $class_name;
192 $icon_types = $type_instance->register( $icon_types );
193 }
194
195 return $icon_types;
196 }
197
198
199 /**
200 * Register font packs
201 *
202 * Each directory under <code>fontpacks/</code> will be scanned. When a <code>config.json</code>
203 * file is found it'll be read and the font pack will be registered.
204 *
205 * Font packs can be obtained from Fontello ({@link http://fontello.com/})
206 *
207 * @since 0.4.0
208 * @access protected
209 * @wp_hook filter menu_icons_types
210 * @link http://fontello.com/ Fontello
211 *
212 * @param array $icon_types Current icon types
213 * @return array
214 */
215 public static function _register_font_packs( $icon_types ) {
216 $path = self::$data['fontpacks_dir_path'];
217 if ( ! is_dir( $path ) ) {
218 return $icon_types;
219 }
220
221 require_once sprintf( '%s/includes/type-fontpack.php', self::$data['dir'] );
222 $class_name = 'Menu_Icons_Type_Fontpack';
223 $iterator = new DirectoryIterator( $path );
224
225 foreach ( $iterator as $item ) {
226 if ( $item->isDot() || ! $item->isDir() ) {
227 continue;
228 }
229
230 $pack = $item->getFilename();
231 $instance = new $class_name( $pack );
232 $icon_types = $instance->register( $icon_types );
233 }
234
235 return $icon_types;
236 }
237
238
239 /**
240 * Collect icon types
241 *
242 * @since 0.1.0
243 * @access private
244 * @uses apply_filters() Calls 'menu_icons_types' to get registered types.
245 */
246 private static function _collect_icon_types() {
247 $types = (array) apply_filters( 'menu_icons_types', array() );
248 $defaults = array(
249 'label' => '',
250 'field_cb' => '',
251 'front_cb' => '',
252 'stylesheet' => '',
253 'version' => get_bloginfo( 'version' ),
254 );
255 $callbacks = array( 'field_cb', 'front_cb', 'frame_cb' );
256 $optionals = array( 'stylesheet', 'frame_cb' );
257 $messages = array(
258 'empty' => _x(
259 '%1$s cannot be empty, %2$s has been disabled.',
260 '1: Property key, 2: Icon type ID',
261 'menu-icons'
262 ),
263 'callback' => _x(
264 '%1$s must be callable, %2$s has been disabled.',
265 '1: Property key, 2: Icon type ID',
266 'menu-icons'
267 ),
268 );
269
270 foreach ( $types as $type => $props ) {
271 $type_props = wp_parse_args( $props, $defaults );
272 foreach ( $type_props as $key => $value ) {
273 if ( ! in_array( $key, $optionals ) && empty( $value ) ) {
274 trigger_error(
275 '<strong>Menu Icons</strong>: ' . vsprintf(
276 esc_html( $messages['empty'] ),
277 array( '<em>' . esc_html( $key ) . '</em>', '<em>' . esc_html( $type ) . '</em>' )
278 )
279 );
280 continue 2;
281 }
282
283 if ( in_array( $key, $callbacks ) && ! is_callable( $value ) ) {
284 trigger_error(
285 '<strong>Menu Icons</strong>: ' . vsprintf(
286 esc_html( $messages['callback'] ),
287 array( '<em>' . esc_html( $key ) . '</em>', '<em>' . esc_html( $type ) . '</em>' )
288 )
289 );
290 continue 2;
291 }
292 }
293
294 self::$data['icon_types'][ $type ] = $type_props;
295 }
296
297 ksort( self::$data['icon_types'] );
298 }
299
300
301 /**
302 * Load front-end tasks
303 *
304 * @since 0.1.0
305 * @access protected
306 * @return void
307 */
308 protected static function _load_front_end() {
309 foreach ( Menu_Icons_Settings::get( 'global', 'icon_types' ) as $id ) {
310 if ( isset( self::$data['icon_types'][ $id ] ) ) {
311 call_user_func( self::$data['icon_types'][ $id ]['front_cb'] );
312 }
313 }
314
315 add_action( 'wp_enqueue_scripts', array( __CLASS__, '_enqueue_styles' ), 7 );
316 }
317
318
319 /**
320 * Enqueue icon type's stylesheet
321 *
322 * @since 0.2.0
323 *
324 * @param string $id Icon type ID
325 * @param array $props Icon type properties
326 *
327 * @return void
328 */
329 public static function enqueue_type_stylesheet( $id, array $props ) {
330 if ( empty( $props['stylesheet'] ) ) {
331 return;
332 }
333
334 if ( wp_style_is( $props['stylesheet'], 'registered' ) ) {
335 wp_enqueue_style( $props['stylesheet_id'] );
336 } else {
337 wp_enqueue_style(
338 $props['stylesheet_id'],
339 $props['stylesheet'],
340 false,
341 $props['version']
342 );
343 }
344 }
345
346
347 /**
348 * Get script & style suffix
349 *
350 * When SCRIPT_DEBUG is defined true, this will return '.min'
351 *
352 * @since 0.2.0
353 * @return string
354 */
355 public static function get_script_suffix() {
356 return ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
357 }
358
359
360 /**
361 * Enqueue stylesheets
362 *
363 * @since 0.1.0
364 * @access protected
365 * @wp_hook action wp_enqueue_scripts/10
366 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts Action: wp_enqueue_scripts/10
367 */
368 public static function _enqueue_styles() {
369 // Enqueue icon types' stylesheets
370 foreach ( Menu_Icons_Settings::get( 'global', 'icon_types' ) as $id ) {
371 if ( isset( self::$data['icon_types'][ $id ] ) ) {
372 self::enqueue_type_stylesheet( $id, self::$data['icon_types'][ $id ] );
373 }
374 }
375
376 wp_enqueue_style(
377 'menu-icons-extra',
378 Menu_Icons::get( 'url' ) . 'css/extra' . self::get_script_suffix() .'.css',
379 false,
380 Menu_Icons::VERSION
381 );
382 }
383
384
385 /**
386 * Get nav menu ID based on arguments passed to wp_nav_menu()
387 *
388 * @since 0.3.0
389 * @param array $args wp_nav_menu() Arguments
390 * @return mixed Nav menu ID or FALSE on failure
391 */
392 public static function get_nav_menu_id( $args ) {
393 $args = (object) $args;
394 $menu = wp_get_nav_menu_object( $args->menu );
395
396 // Get the nav menu based on the theme_location
397 if ( ! $menu
398 && $args->theme_location
399 && ( $locations = get_nav_menu_locations() )
400 && isset( $locations[ $args->theme_location ] )
401 ) {
402 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
403 }
404
405 // get the first menu that has items if we still can't find a menu
406 if ( ! $menu && ! $args->theme_location ) {
407 $menus = wp_get_nav_menus();
408 foreach ( $menus as $menu_maybe ) {
409 if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) {
410 $menu = $menu_maybe;
411 break;
412 }
413 }
414 }
415
416 if ( is_object( $menu ) && ! is_wp_error( $menu ) ) {
417 return $menu->term_id;
418 } else {
419 return false;
420 }
421 }
422
423
424 /**
425 * Get menu item meta value
426 *
427 * @since 0.3.0
428 * @param int $item_id Menu item ID
429 * @return array
430 */
431 public static function get_meta( $item_id ) {
432 $values = get_post_meta( $item_id, 'menu-icons', true );
433
434 if ( empty( $values ) || ! is_array( $values ) ) {
435 $values = array();
436 } elseif ( isset( $values['size'] ) && ! isset( $values['font_size'] ) ) {
437 $values['font_size'] = $values['size'];
438 unset( $values['size'] );
439 }
440
441 return $values;
442 }
443
444
445 /**
446 * Get hidden label class
447 *
448 * @return string
449 */
450 public static function get_hidden_label_class() {
451 /**
452 * Allow themes/plugins to overrride the hidden label class
453 *
454 * @since 0.8.0
455 * @param string $hidden_label_class Hidden label class.
456 * @return string
457 */
458 $hidden_label_class = apply_filters( 'menu_icons_hidden_label_class', 'visuallyhidden' );
459
460 return $hidden_label_class;
461 }
462 }
463 add_action( 'plugins_loaded', array( 'Menu_Icons', '_load' ) );
464