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