PluginProbe
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.3.1
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.3.1
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
373 lines 8.9 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.3.1
8 * @author Dzikri Aziz <kvcrvt@gmail.com>
9 *
10 *
11 * Plugin name: Menu Icons
12 * Plugin URI: http://kucrut.org/
13 * Description: Easily add icons to your navigation menu items
14 * Version: 0.3.1
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.3.1';
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 self::$data = array(
76 'dir' => plugin_dir_path( __FILE__ ),
77 'url' => plugin_dir_url( __FILE__ ),
78 'icon_types' => array(),
79 'default_style' => array(
80 'font-size' => '1.2em',
81 'vertical-align' => 'middle',
82 ),
83 );
84
85 require_once self::$data['dir'] . 'includes/library/functions.php';
86
87 add_filter( 'menu_icons_types', array( __CLASS__, '_register_icon_types' ), 7 );
88 add_filter( 'is_protected_meta', array( __CLASS__, '_protect_meta_key' ), 10, 3 );
89 add_action( 'wp_loaded', array( __CLASS__, '_init' ), 9 );
90 add_action( 'get_header', array( __CLASS__, '_load_front_end' ) );
91 }
92
93
94 /**
95 * Protect meta key
96 *
97 * This prevents our meta key from showing up on Custom Fields meta box
98 *
99 * @since 0.3.0
100 * @wp_hook filter is_protected_meta
101 * @param bool $protected Protection status
102 * @param string $meta_key Meta key
103 * @param string $meta_type Meta type
104 * @return bool Protection status
105 */
106 public static function _protect_meta_key( $protected, $meta_key, $meta_type ) {
107 if ( 'menu-icons' === $meta_key ) {
108 $protected = true;
109 }
110
111 return $protected;
112 }
113
114
115 /**
116 * Initialize
117 *
118 * 1. Collect registered types
119 * 2. Load settings
120 *
121 * @since 0.1.0
122 * @wp_hook action wp_loaded/9
123 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_loaded Action: wp_loaded/9
124 */
125 public static function _init() {
126 self::_collect_icon_types();
127
128 // Nothing to do if there are no icon types registered
129 if ( empty( self::$data['icon_types'] ) ) {
130 trigger_error( __( 'Menu Icons: No registered icon types found.', 'menu-icons' ) );
131
132 return;
133 }
134
135 // Load settings
136 require_once self::$data['dir'] . 'includes/settings.php';
137 Menu_Icons_Settings::init();
138 }
139
140
141 /**
142 * Register built-in icon types
143 *
144 * @since 0.1.5
145 * @access protected
146 * @wp_hook filter menu_icons_types
147 *
148 * @param array $icon_types Current icon types
149 * @return array
150 */
151 public static function _register_icon_types( $icon_types ) {
152 $builtin_types = array(
153 'dashicons',
154 'elusive',
155 'fontawesome',
156 'genericons',
157 );
158
159 foreach ( $builtin_types as $type ) {
160 require_once sprintf( '%s/includes/type-%s.php', self::$data['dir'], $type );
161
162 $class_name = sprintf( 'Menu_Icons_Type_%s', ucfirst( $type ) );
163 $type_instance = new $class_name;
164 $icon_types = $type_instance->register( $icon_types );
165 }
166
167 return $icon_types;
168 }
169
170
171 /**
172 * Collect icon types
173 *
174 * @since 0.1.0
175 * @access private
176 * @uses apply_filters() Calls 'menu_icons_types' to get registered types.
177 */
178 private static function _collect_icon_types() {
179 $types = (array) apply_filters( 'menu_icons_types', array() );
180 $defaults = array(
181 'label' => '',
182 'field_cb' => '',
183 'front_cb' => '',
184 'stylesheet' => '',
185 'version' => get_bloginfo( 'version' ),
186 );
187 $callbacks = array( 'field_cb', 'front_cb', 'frame_cb' );
188 $optionals = array( 'stylesheet', 'frame_cb' );
189 $messages = array(
190 'empty' => _x(
191 '%1$s cannot be empty, %2$s has been disabled.',
192 '1: Property key, 2: Icon type ID',
193 'menu-icons'
194 ),
195 'callback' => _x(
196 '%1$s must be callable, %2$s has been disabled.',
197 '1: Property key, 2: Icon type ID',
198 'menu-icons'
199 ),
200 );
201
202 foreach ( $types as $type => $props ) {
203 $type_props = wp_parse_args( $props, $defaults );
204 foreach ( $type_props as $key => $value ) {
205 if ( ! in_array( $key, $optionals ) && empty( $value ) ) {
206 trigger_error(
207 '<strong>Menu Icons</strong>: ' . vsprintf(
208 $messages['empty'],
209 array( '<em>'.$key.'</em>', '<em>'.$type.'</em>' )
210 )
211 );
212 continue 2;
213 }
214
215 if ( in_array( $key, $callbacks ) && ! is_callable( $value ) ) {
216 trigger_error(
217 '<strong>Menu Icons</strong>: ' . vsprintf(
218 $messages['callback'],
219 array( '<em>'.$key.'</em>', '<em>'.$type.'</em>' )
220 )
221 );
222 continue 2;
223 }
224 }
225
226 self::$data['icon_types'][ $type ] = $type_props;
227 }
228
229 ksort( self::$data['icon_types'] );
230 }
231
232
233 /**
234 * Load front-end tasks
235 *
236 * @since 0.1.0
237 * @access protected
238 * @wp_hook action load-nav-menus.php/10
239 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/get_header Action: get_header/10
240 */
241 public static function _load_front_end() {
242 foreach ( Menu_Icons_Settings::get( 'global', 'icon_types' ) as $id ) {
243 if ( isset( self::$data['icon_types'][ $id ] ) ) {
244 call_user_func( self::$data['icon_types'][ $id ]['front_cb'] );
245 }
246 }
247
248 add_action( 'wp_enqueue_scripts', array( __CLASS__, '_enqueue_styles' ), 7 );
249 }
250
251
252 /**
253 * Enqueue icon type's stylesheet
254 *
255 * @since 0.2.0
256 * @param string $id Stylesheet ID
257 * @param array $props Icon type properties
258 */
259 public static function enqueue_type_stylesheet( $id, $props ) {
260 if ( empty( $props['stylesheet'] ) ) {
261 return;
262 }
263
264 if ( wp_style_is( $props['stylesheet'], 'registered' ) ) {
265 wp_enqueue_style( $id );
266 }
267 else {
268 wp_enqueue_style( $id, $props['stylesheet'], false, $props['version'] );
269 }
270 }
271
272
273 /**
274 * Get script & style suffix
275 *
276 * When SCRIPT_DEBUG is defined true, this will return '.min'
277 *
278 * @since 0.2.0
279 * @return string
280 */
281 public static function get_script_suffix() {
282 return ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
283 }
284
285
286 /**
287 * Enqueue stylesheets
288 *
289 * @since 0.1.0
290 * @access protected
291 * @wp_hook action wp_enqueue_scripts/10
292 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts Action: wp_enqueue_scripts/10
293 */
294 public static function _enqueue_styles() {
295 // Enqueue icon types' stylesheets
296 foreach ( Menu_Icons_Settings::get( 'global', 'icon_types' ) as $id ) {
297 if ( isset( self::$data['icon_types'][ $id ] ) ) {
298 self::enqueue_type_stylesheet( $id, self::$data['icon_types'][ $id ] );
299 }
300 }
301
302 wp_enqueue_style(
303 'menu-icons-extra',
304 Menu_Icons::get( 'url' ) . 'css/extra' . self::get_script_suffix() .'.css',
305 false,
306 Menu_Icons::VERSION
307 );
308 }
309
310
311 /**
312 * Get nav menu ID based on arguments passed to wp_nav_menu()
313 *
314 * @since 0.3.0
315 * @param array $args wp_nav_menu() Arguments
316 * @return mixed Nav menu ID or FALSE on failure
317 */
318 public static function get_nav_menu_id( $args ) {
319 $args = (object) $args;
320 $menu = wp_get_nav_menu_object( $args->menu );
321
322 // Get the nav menu based on the theme_location
323 if ( ! $menu
324 && $args->theme_location
325 && ( $locations = get_nav_menu_locations() )
326 && isset( $locations[ $args->theme_location ] )
327 ) {
328 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
329 }
330
331 // get the first menu that has items if we still can't find a menu
332 if ( ! $menu && ! $args->theme_location ) {
333 $menus = wp_get_nav_menus();
334 foreach ( $menus as $menu_maybe ) {
335 if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) {
336 $menu = $menu_maybe;
337 break;
338 }
339 }
340 }
341
342 if ( is_object( $menu ) && ! is_wp_error( $menu ) ) {
343 return $menu->term_id;
344 }
345 else {
346 return false;
347 }
348 }
349
350
351 /**
352 * Get menu item meta value
353 *
354 * @since 0.3.0
355 * @param int $item_id Menu item ID
356 * @return array
357 */
358 public static function get_meta( $item_id ) {
359 $values = get_post_meta( $item_id, 'menu-icons', true );
360
361 if ( empty( $values ) || ! is_array( $values ) ) {
362 $values = array();
363 }
364 elseif ( isset( $values['size'] ) && ! isset( $values['font_size'] ) ) {
365 $values['font_size'] = $values['size'];
366 unset( $values['size'] );
367 }
368
369 return $values;
370 }
371 }
372 add_action( 'plugins_loaded', array( 'Menu_Icons', '_load' ) );
373