PluginProbe ʕ •ᴥ•ʔ
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.2.0
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.2.0
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 / menu-icons.php
menu-icons Last commit date
css 12 years ago includes 12 years ago js 12 years ago languages 12 years ago LICENSE 12 years ago menu-icons.php 12 years ago readme.txt 12 years ago
menu-icons.php
328 lines
1 <?php
2
3 /**
4 * Menu Icons
5 *
6 * @package Menu_Icons
7 * @version 0.2.0
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.2.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.2.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 self::$data = array(
76 'dir' => plugin_dir_path( __FILE__ ),
77 'url' => plugin_dir_url( __FILE__ ),
78 'icon_types' => array(),
79 );
80
81 add_filter( 'menu_icons_types', array( __CLASS__, '_register_icon_types' ), 7 );
82 add_action( 'wp_loaded', array( __CLASS__, 'init' ), 9 );
83 }
84
85
86 /**
87 * Initialize plugin
88 *
89 * 1. Collect registered types
90 * 2. Add hook callbacks
91 *
92 * @since 0.1.0
93 * @wp_hook action wp_loaded/9
94 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_loaded Action: wp_loaded/9
95 */
96 public static function init() {
97 self::_collect_icon_types();
98
99 // Nothing to do if there are no icon types registered
100 if ( empty( self::$data['icon_types'] ) ) {
101 return;
102 }
103
104 // Back
105 add_filter( 'load-nav-menus.php', array( __CLASS__, '_load_nav_menus' ) );
106 add_filter( 'wp_edit_nav_menu_walker', array( __CLASS__, '_filter_wp_edit_nav_menu_walker' ), 99 );
107
108 // Front
109 add_action( 'get_header', array( __CLASS__, '_load_front_end' ) );
110 add_action( 'wp_enqueue_scripts', array( __CLASS__, '_enqueue_styles' ), 7 );
111 }
112
113
114 /**
115 * Register built-in icon types
116 *
117 * @since 0.1.5
118 * @access protected
119 * @wp_hook filter menu_icons_types
120 *
121 * @param array $icon_types Current icon types
122 * @return array
123 */
124 public static function _register_icon_types( $icon_types ) {
125 $builtin_types = array(
126 'dashicons',
127 'genericons',
128 'fontawesome',
129 );
130
131 foreach ( $builtin_types as $type ) {
132 require_once sprintf( '%s/includes/type-%s.php', self::$data['dir'], $type );
133
134 $class_name = sprintf( 'Menu_Icons_Type_%s', ucfirst( $type ) );
135 $type_instance = new $class_name;
136 $icon_types = $type_instance->register( $icon_types );
137 }
138
139 return $icon_types;
140 }
141
142
143 /**
144 * Collect icon types
145 *
146 * @since 0.1.0
147 * @access private
148 * @uses apply_filters() Calls 'menu_icons_types' to get registered types.
149 */
150 private static function _collect_icon_types() {
151 $types = (array) apply_filters( 'menu_icons_types', array() );
152 $defaults = array(
153 'label' => '',
154 'field_cb' => '',
155 'front_cb' => '',
156 'stylesheet' => '',
157 'version' => get_bloginfo( 'version' ),
158 );
159 $callbacks = array( 'field_cb', 'front_cb', 'frame_cb' );
160 $optionals = array( 'stylesheet', 'frame_cb' );
161 $messages = array(
162 'empty' => _x(
163 '%1$s cannot be empty, %2$s has been disabled.',
164 '1: Property key, 2: Icon type ID',
165 'menu-icons'
166 ),
167 'callback' => _x(
168 '%1$s must be callable, %2$s has been disabled.',
169 '1: Property key, 2: Icon type ID',
170 'menu-icons'
171 ),
172 );
173
174 foreach ( $types as $type => $props ) {
175 $type_props = wp_parse_args( $props, $defaults );
176 foreach ( $type_props as $key => $value ) {
177 if ( ! in_array( $key, $optionals ) && empty( $value ) ) {
178 trigger_error(
179 '<strong>Menu Icons</strong>: ' . vsprintf(
180 $messages['empty'],
181 array( '<em>'.$key.'</em>', '<em>'.$type.'</em>' )
182 )
183 );
184 continue 2;
185 }
186
187 if ( in_array( $key, $callbacks ) && ! is_callable( $value ) ) {
188 trigger_error(
189 '<strong>Menu Icons</strong>: ' . vsprintf(
190 $messages['callback'],
191 array( '<em>'.$key.'</em>', '<em>'.$type.'</em>' )
192 )
193 );
194 continue 2;
195 }
196 }
197
198 self::$data['icon_types'][ $type ] = $type_props;
199 }
200
201 ksort( self::$data['icon_types'] );
202 }
203
204
205 /**
206 * Prepare custom walker and custom field
207 *
208 * @since 0.1.3
209 * @access protected
210 * @wp_hook filter wp_edit_nav_menu_walker/10/1
211 */
212 public static function _filter_wp_edit_nav_menu_walker( $walker ) {
213 // Load custom fields
214 require_once self::$data['dir'] . 'includes/admin.php';
215 add_filter( 'menu_item_custom_fields', array( 'Menu_Icons_Admin_Nav_Menus', '_fields' ), 10, 3 );
216
217 // Load menu item custom fields plugin
218 if ( ! class_exists( 'Menu_Item_Custom_Fields_Walker' ) ) {
219 require_once self::$data['dir'] . '/includes/walker-nav-menu-edit.php';
220 }
221 $walker = 'Menu_Item_Custom_Fields_Walker';
222
223 return $walker;
224 }
225
226
227 /**
228 * Prepare wp-admin/nav-menus.php
229 *
230 * @since 0.1.5
231 * @access protected
232 * @wp_hook action load-nav-menus.php/10
233 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/load-%28page%29 Action: load-nav-menus.php/10
234 */
235 public static function _load_nav_menus() {
236 // Load custom fields
237 require_once self::$data['dir'] . 'includes/admin.php';
238 Menu_Icons_Admin_Nav_Menus::init();
239 }
240
241
242 /**
243 * Load front-end tasks
244 *
245 * @since 0.1.0
246 * @access protected
247 * @wp_hook action load-nav-menus.php/10
248 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/get_header Action: get_header/10
249 */
250 public static function _load_front_end() {
251 foreach ( self::$data['icon_types'] as $props ) {
252 call_user_func( $props['front_cb'] );
253 }
254 }
255
256
257 /**
258 * Enqueue icon type's stylesheet
259 *
260 * @since 0.2.0
261 * @param string $id Stylesheet ID
262 * @param array $props Icon type properties
263 */
264 public static function enqueue_type_stylesheet( $id, $props ) {
265 if ( empty( $props['stylesheet'] ) ) {
266 return;
267 }
268
269 if ( wp_style_is( $props['stylesheet'], 'registered' ) ) {
270 wp_enqueue_style( $id );
271 }
272 else {
273 wp_enqueue_style( $id, $props['stylesheet'], false, $props['version'] );
274 }
275 }
276
277
278 /**
279 * Get script & style suffix
280 *
281 * When SCRIPT_DEBUG is defined true, this will return '.min'
282 *
283 * @since 0.2.0
284 * @return string
285 */
286 public static function get_script_suffix() {
287 return ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
288 }
289
290
291 /**
292 * Enqueue extra stylesheet
293 *
294 * This stylesheet will override some styles of the icons
295 *
296 * @since 0.1.0
297 * @access protected
298 * @wp_hook action wp_enqueue_scripts/10
299 * @uses apply_filters() Calls 'menu_icons_load_extra_style' allow plugins/themes to
300 * enable/disable the loading of the extra stylesheet
301 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts Action: wp_enqueue_scripts/10
302 */
303 public static function _enqueue_styles() {
304 // Enqueue icon types' stylesheets
305 foreach ( self::$data['icon_types'] as $id => $props ) {
306 self::enqueue_type_stylesheet( $id, $props );
307 }
308
309 /**
310 * Enable/disable loading of extra stylesheet
311 *
312 * @since 0.1.0
313 * @param bool $load_extra_style
314 */
315 $load_extra_style = (bool) apply_filters( 'menu_icons_load_extra_style', true );
316
317 if ( true === $load_extra_style ) {
318 wp_enqueue_style(
319 'menu-icons-extra',
320 Menu_Icons::get( 'url' ) . 'css/extra' . self::get_script_suffix() .'.css',
321 false,
322 Menu_Icons::VERSION
323 );
324 }
325 }
326 }
327 add_action( 'plugins_loaded', array( 'Menu_Icons', 'load' ) );
328