PluginProbe ʕ •ᴥ•ʔ
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.3.0
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.3.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 / includes / admin.php
menu-icons / includes Last commit date
library 12 years ago admin.php 12 years ago settings.php 12 years ago type-dashicons.php 12 years ago type-elusive.php 12 years ago type-fontawesome.php 12 years ago type-fonts.php 12 years ago type-genericons.php 12 years ago type.php 12 years ago walker-nav-menu-edit.php 12 years ago
admin.php
517 lines
1 <?php
2 /**
3 * Menu editor handler
4 *
5 * @package Menu_Icons
6 * @author Dzikri Aziz <kvcrvt@gmail.com>
7 */
8
9
10 /**
11 * Nav menu admin
12 */
13 final class Menu_Icons_Admin_Nav_Menus {
14
15 /**
16 * Holds active icon types
17 *
18 * @since 0.3.0
19 * @access private
20 * @var array
21 */
22 private static $_icon_types;
23
24
25 /**
26 * Initialize class
27 *
28 * @since 0.1.0
29 * @wp_hook action load-nav-menus.php
30 */
31 public static function init() {
32 $active_types = Menu_Icons_Settings::get( 'global', 'icon_types' );
33 if ( empty( $active_types ) ) {
34 return;
35 }
36
37 self::_collect_icon_types();
38
39 add_filter( 'wp_edit_nav_menu_walker', array( __CLASS__, '_filter_wp_edit_nav_menu_walker' ), 99 );
40 add_filter( 'menu_item_custom_fields', array( __CLASS__, '_fields' ), 10, 3 );
41 add_action( 'admin_enqueue_scripts', array( __CLASS__, '_enqueue_assets' ), 101 );
42 add_action( 'print_media_templates', array( __CLASS__, '_media_templates' ) );
43
44 add_filter( 'manage_nav-menus_columns', array( __CLASS__, '_columns' ), 99 );
45 add_action( 'wp_update_nav_menu_item', array( __CLASS__, '_save' ), 10, 3 );
46 }
47
48
49 /**
50 * Custom walker
51 *
52 * @since 0.3.0
53 * @access protected
54 * @wp_hook filter wp_edit_nav_menu_walker/10/1
55 */
56 public static function _filter_wp_edit_nav_menu_walker( $walker ) {
57 // Load menu item custom fields plugin
58 if ( ! class_exists( 'Menu_Item_Custom_Fields_Walker' ) ) {
59 require_once Menu_Icons::get( 'dir' ) . 'includes/walker-nav-menu-edit.php';
60 }
61 $walker = 'Menu_Item_Custom_Fields_Walker';
62
63 return $walker;
64 }
65
66
67 /**
68 * Collect icon types
69 *
70 * @since 0.3.0
71 * @access private
72 */
73 private static function _collect_icon_types() {
74 $registered_types = Menu_Icons::get( 'icon_types' );
75 foreach ( Menu_Icons_Settings::get( 'global', 'icon_types' ) as $id ) {
76 self::$_icon_types[ $id ] = $registered_types[ $id ];
77 }
78 }
79
80
81 /**
82 * Get icon types
83 *
84 * @since 0.1.0
85 * @access protected
86 * @uses apply_filters() Calls 'menu_icons_types' on returned array.
87 *
88 * @return array
89 */
90 protected static function _get_types() {
91 $types = array_merge(
92 array(
93 '' => array(
94 'id' => '',
95 'label' => __( '&mdash; Select &mdash;', 'menu-icons' )
96 ),
97 ),
98 self::$_icon_types
99 );
100
101 return $types;
102 }
103
104
105 /**
106 * Enqueue scripts & styles on wp-admin/nav-menus.php
107 *
108 * @since 0.1.1
109 * @access protected
110 * @wp_hook admin_enqueue_scripts
111 */
112 public static function _enqueue_assets() {
113 global $nav_menu_selected_id;
114
115 $data = array(
116 'text' => array(
117 'title' => __( 'Select Icon', 'menu-icons' ),
118 'select' => __( 'Select', 'menu-icons' ),
119 'all' => __( 'All', 'menu-icons' ),
120 ),
121 'base_url' => untrailingslashit( Menu_Icons::get( 'url' ) ),
122 'admin_url' => untrailingslashit( admin_url() ),
123 'menuSettings' => Menu_Icons_Settings::get_menu_settings( $nav_menu_selected_id ),
124 );
125
126 foreach ( self::$_icon_types as $id => $props ) {
127 if ( ! empty( $props['frame_cb'] ) ) {
128 $icon_types[ $id ] = array(
129 'type' => $id,
130 'id' => sprintf( 'mi-%s', $id ),
131 'title' => $props['label'],
132 'data' => call_user_func_array( $props['frame_cb'], array( $id ) ),
133 );
134 Menu_Icons::enqueue_type_stylesheet( $id, $props );
135 }
136 }
137
138 /**
139 * WP 3.8 bug, fixed in 3.9
140 *
141 * We need to dequeue and re-enqueue this one later,
142 * otherwise we won't get the dashboard's colors
143 *
144 * @todo Remove in 4.0.1
145 */
146 wp_dequeue_style( 'colors' );
147
148 $data['iconTypes'] = $icon_types;
149 $data['typeNames'] = array_keys( self::$_icon_types );
150
151 // re-enqueue color style
152 wp_enqueue_style( 'colors' );
153
154 wp_localize_script( 'menu-icons', 'menuIcons', $data );
155 }
156
157
158 /**
159 * Get preview
160 *
161 * @since 0.2.0
162 * @access private
163 * @param int $id Menu item ID
164 * @param array $meta_value Menu item meta value
165 * @return mixed
166 */
167 private static function _get_preview( $id, $meta_value ) {
168 $text = esc_html__( 'Select', 'menu-icons' );
169 if ( empty( $meta_value['type'] ) ) {
170 return $text;
171 }
172
173 $type = $meta_value['type'];
174 $types = self::_get_types();
175 if ( empty( $types[ $type ] ) ) {
176 return $text;
177 }
178
179 if ( empty( $meta_value[ "{$type}-icon" ] ) ) {
180 return $text;
181 }
182
183 if ( empty( $types[ $type ]['preview_cb'] )
184 || ! is_callable( $types[ $type ]['preview_cb'] )
185 ) {
186 return $text;
187 }
188
189 $preview = call_user_func_array(
190 $types[ $type ]['preview_cb'],
191 array( $id, $meta_value )
192 );
193 if ( ! empty( $preview ) ) {
194 return $preview;
195 }
196
197 return $text;
198 }
199
200
201 /**
202 * Get Fields
203 *
204 * @since 0.3.0
205 * @access private
206 * @return array
207 */
208 private static function _get_fields() {
209 $sections = Menu_Icons_Settings::get_fields();
210 $fields = $sections['menu']['fields'];
211
212 foreach ( $fields as &$field ) {
213 $field['default'] = $field['value'];
214 $field['attributes'] = array_merge(
215 array(
216 'class' => '_setting',
217 'data-key' => $field['id'],
218 ),
219 isset( $field['attributes'] ) ? $field['attributes'] : array()
220 );
221 }
222
223 return $fields;
224 }
225
226
227 /**
228 * Print fields
229 *
230 * @since 0.1.0
231 * @access protected
232 * @uses add_action() Calls 'menu_icons_before_fields' hook
233 * @uses add_action() Calls 'menu_icons_after_fields' hook
234 * @wp_hook action menu_item_custom_fields/10/3
235 *
236 * @param object $item Menu item data object.
237 * @param int $depth Nav menu depth.
238 * @param array $args Menu item args.
239 * @param int $id Nav menu ID.
240 *
241 * @return string Form fields
242 */
243 public static function _fields( $item, $depth, $args = array(), $id = 0 ) {
244 require_once Menu_Icons::get( 'dir' ) . 'includes/library/form-fields.php';
245
246 $type_ids = array_values( array_filter( array_keys( self::_get_types() ) ) );
247 $input_id = sprintf( 'menu-icons-%d', $item->ID );
248 $input_name = sprintf( 'menu-icons[%d]', $item->ID );
249 $current = wp_parse_args(
250 Menu_Icons::get_meta( $item->ID ),
251 Menu_Icons_Settings::get_menu_settings( Menu_Icons_Settings::get_current_menu_id() )
252 );
253 ?>
254 <div class="field-icon description-wide menu-icons-wrap">
255 <?php
256 /**
257 * Allow plugins/themes to inject HTML before menu icons' fields
258 *
259 * @param object $item Menu item data object.
260 * @param int $depth Nav menu depth.
261 * @param array $args Menu item args.
262 * @param int $id Nav menu ID.
263 *
264 */
265 do_action( 'menu_icons_before_fields', $item, $depth, $args, $id );
266 ?>
267 <?php
268 ?>
269 <div class="easy">
270 <p class="description submitbox">
271 <label><?php esc_html_e( 'Icon:' ) ?></label>
272 <?php printf(
273 '<a id="menu-icons-%1$d-select" class="_select" title="%2$s" data-id="%1$d" data-text="%2$s">%3$s</a>',
274 esc_attr__( $item->ID ),
275 esc_attr__( 'Select icon', 'menu-icons' ),
276 self::_get_preview( $item->ID, $current )
277 ) ?>
278 <?php printf(
279 '<a id="menu-icons-%1$d-remove" class="_remove hidden submitdelete" data-id="%1$d">%2$s</a>',
280 $item->ID,
281 esc_attr__( 'Remove', 'menu-icons' )
282 ) ?>
283 </p>
284 </div>
285 <div class="original hidden">
286 <p class="description">
287 <label for="<?php echo esc_attr( $input_id ) ?>-type"><?php esc_html_e( 'Icon type', 'menu-icons' ); ?></label>
288 <?php printf(
289 '<select id="%s-type" name="%s[type]" class="_type hasdep" data-dep-scope="div.menu-icons-wrap" data-dep-children=".field-icon-child" data-key="type">',
290 esc_attr( $input_id ),
291 esc_attr( $input_name )
292 ) ?>
293 <?php foreach ( self::_get_types() as $id => $props ) : ?>
294 <?php printf(
295 '<option value="%s"%s>%s</option>',
296 esc_attr( $id ),
297 selected( ( isset( $current['type'] ) && $id === $current['type'] ), true, false ),
298 esc_html( $props['label'] )
299 ) ?>
300 <?php endforeach; ?>
301 </select>
302 </p>
303
304 <?php foreach ( self::_get_types() as $props ) : ?>
305 <?php if ( ! empty( $props['field_cb'] ) && is_callable( $props['field_cb'] ) ) : ?>
306 <?php call_user_func_array( $props['field_cb'], array( $item->ID, $current ) ); ?>
307 <?php endif; ?>
308 <?php endforeach; ?>
309
310 <?php foreach ( self::_get_fields() as $field ) :
311 $field['value'] = $current[ $field['id'] ];
312 $field = Kucrut_Form_Field::create(
313 $field,
314 array(
315 'keys' => array( 'menu-icons', $item->ID ),
316 'inline_description' => true,
317 )
318 );
319 ?>
320 <p class="description field-icon-child" data-dep-on='<?php echo json_encode( $type_ids ) ?>'>
321 <?php printf(
322 '<label for="%s">%s</label>',
323 esc_attr( $field->id ),
324 esc_html( $field->label )
325 ) ?>
326 <?php $field->render() ?>
327 </p>
328 <?php endforeach; ?>
329 </div>
330 <?php
331 /**
332 * Allow plugins/themes to inject HTML after menu icons' fields
333 *
334 * @param object $item Menu item data object.
335 * @param int $depth Nav menu depth.
336 * @param array $args Menu item args.
337 * @param int $id Nav menu ID.
338 *
339 */
340 do_action( 'menu_icons_after_fields', $item, $depth, $args, $id );
341 ?>
342 </div>
343 <?php
344 }
345
346
347 /**
348 * Add our field to the screen options toggle
349 *
350 * @since 0.1.0
351 * @access private
352 * @wp_hook action manage_nav-menus_columns
353 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/manage_posts_columns Action: manage_nav-menus_columns/99
354 *
355 * @param array $columns Menu item columns
356 * @return array
357 */
358 public static function _columns( $columns ) {
359 $columns['icon'] = __( 'Icon', 'menu-icons' );
360
361 return $columns;
362 }
363
364
365 /**
366 * Save menu item's icons values
367 *
368 * @since 0.1.0
369 * @access protected
370 * @uses apply_filters() Calls 'menu_icons_values' on returned array.
371 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_update_nav_menu_item Action: wp_update_nav_menu_item/10/2
372 *
373 * @param int $menu_id Nav menu ID
374 * @param int $menu_item_db_id Menu item ID
375 * @param array $menu_item_args Menu item data
376 */
377 public static function _save( $menu_id, $menu_item_db_id, $menu_item_args ) {
378 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
379 return;
380 }
381
382 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
383
384 // Sanitize
385 if ( ! empty( $_POST['menu-icons'][ $menu_item_db_id ] ) ) {
386 $value = (array) $_POST['menu-icons'][ $menu_item_db_id ];
387 }
388 else {
389 $value = array();
390 }
391
392 /**
393 * Allow plugins/themes to filter the values
394 *
395 * @since 0.1.0
396 * @param array $value Metadata value
397 */
398 $value = apply_filters( 'menu_icons_values', $value, $menu_item_db_id );
399
400 // Update
401 if ( ! empty( $value ) ) {
402 update_post_meta( $menu_item_db_id, 'menu-icons', $value );
403 }
404 else {
405 delete_post_meta( $menu_item_db_id, 'menu-icons' );
406 }
407 }
408
409
410 /**
411 * Get and print media templates from all types
412 *
413 * @since 0.2.0
414 * @wp_hook action print_media_templates
415 */
416 public static function _media_templates() {
417 $id_prefix = 'tmpl-menu-icons';
418
419 // Common templates
420 $templates = array(
421 'sidebar-title' => sprintf(
422 '<h3>%s</h3>',
423 esc_html__( 'Preview', 'menu-icons' )
424 ),
425 'settings' => sprintf(
426 '<label class="setting">
427 <span>%1$s</span>
428 <select data-setting="position">
429 <option value="before">%2$s</option>
430 <option value="after">%3$s</option>
431 </select>
432 </label>
433 <label class="setting">
434 <span>%4$s</span>
435 <input type="number" min="0.1" step="0.1" data-setting="font_size" value="{{ data.font_size }}" />
436 em
437 </label>
438 <label class="setting">
439 <span>%5$s</span>
440 <select data-setting="vertical_align">
441 <option value="">%6$s</option>
442 <option value="super">%7$s</option>
443 <option value="top">%8$s</option>
444 <option value="text-top">%9$s</option>
445 <option value="middle">%10$s</option>
446 <option value="baseline">%11$s</option>
447 <option value="text-bottom">%12$s</option>
448 <option value="bottom">%13$s</option>
449 <option value="sub">%14$s</option>
450 </select>
451 </label>
452 <label class="setting">
453 <span>%15$s</span>
454 <select data-setting="hide_label">
455 <option value="">%16$s</option>
456 <option value="1">%17$s</option>
457 </select>
458 </label>
459 <p class="_info"><em>%18$s</em></p>',
460 esc_html__( 'Position', 'menu-icons' ),
461 esc_html__( 'Before', 'menu-icons' ),
462 esc_html__( 'After', 'menu-icons' ),
463 esc_html__( 'Size', 'menu-icons' ),
464 esc_html__( 'Vertical Align', 'menu-icons' ),
465 esc_html__( '&ndash; Select &ndash;', 'menu-icons' ),
466 esc_html__( 'Super', 'menu-icons' ),
467 esc_html__( 'Top', 'menu-icons' ),
468 esc_html__( 'Text Top', 'menu-icons' ),
469 esc_html__( 'Middle', 'menu-icons' ),
470 esc_html__( 'Baseline', 'menu-icons' ),
471 esc_html__( 'Bottom', 'menu-icons' ),
472 esc_html__( 'Text Bottom', 'menu-icons' ),
473 esc_html__( 'Sub', 'menu-icons' ),
474 esc_html__( 'Hide Label', 'menu-icons' ),
475 esc_html__( 'No', 'menu-icons' ),
476 esc_html__( 'Yes', 'menu-icons' ),
477 sprintf(
478 esc_html__( "Please note that the actual look of the icons on the front-end will also be affected by your active theme's style. You can use %s if you need to override it.", 'menu-icons' ),
479 '<a target="_blank" href="http://wordpress.org/plugins/simple-custom-css/">Simple Custom CSS</a>'
480 )
481 ),
482 );
483 $templates = apply_filters( 'menu_icons_media_templates', $templates );
484
485 foreach ( $templates as $key => $template ) {
486 $id = sprintf( '%s-%s', $id_prefix, $key );
487 self::_print_tempate( $id, $template );
488 }
489
490 // Icon type templates
491 foreach ( self::_get_types() as $type => $props ) {
492 if ( ! empty( $props['templates'] ) ) {
493 foreach ( $props['templates'] as $key => $template ) {
494 $id = sprintf( '%s-%s-%s', $id_prefix, $type, $key );
495 self::_print_tempate( $id, $template );
496 }
497 }
498 }
499 }
500
501
502 /**
503 * Print media template
504 *
505 * @since 0.2.0
506 * @param string $id Template ID
507 * @param string $template Media template HTML
508 */
509 protected static function _print_tempate( $id, $template ) {
510 ?>
511 <script type="text/html" id="<?php echo esc_attr( $id ) ?>">
512 <?php echo $template // xss ok ?>
513 </script>
514 <?php
515 }
516 }
517