PluginProbe ʕ •ᴥ•ʔ
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.4.0
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.4.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 11 years ago admin.php 11 years ago media-template.php 11 years ago settings.php 11 years ago type-dashicons.php 11 years ago type-elusive.php 11 years ago type-fontawesome.php 11 years ago type-fontpack.php 11 years ago type-fonts.php 11 years ago type-genericons.php 11 years ago type-image.php 11 years ago type.php 11 years ago walker-nav-menu-edit.php 11 years ago
admin.php
456 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 $data = array(
114 'text' => array(
115 'title' => __( 'Select Icon', 'menu-icons' ),
116 'select' => __( 'Select', 'menu-icons' ),
117 'all' => __( 'All', 'menu-icons' ),
118 'preview' => __( 'Preview', 'menu-icons' ),
119 'settingsInfo' => sprintf(
120 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' ),
121 '<a target="_blank" href="http://wordpress.org/plugins/simple-custom-css/">Simple Custom CSS</a>'
122 ),
123 ),
124 'base_url' => untrailingslashit( Menu_Icons::get( 'url' ) ),
125 'admin_url' => untrailingslashit( admin_url() ),
126 'settingsFields' => Menu_Icons_Settings::get_settings_fields(),
127 );
128
129 foreach ( self::$_icon_types as $id => $props ) {
130 if ( ! empty( $props['frame_cb'] ) ) {
131 $icon_types[ $id ] = array(
132 'type' => $id,
133 'id' => sprintf( 'mi-%s', $id ),
134 'title' => $props['label'],
135 'data' => call_user_func_array( $props['frame_cb'], array( $id ) ),
136 );
137 Menu_Icons::enqueue_type_stylesheet( $id, $props );
138 }
139 }
140
141 /**
142 * WP 3.8 bug, fixed in 3.9
143 *
144 * We need to dequeue and re-enqueue this one later,
145 * otherwise we won't get the dashboard's colors
146 *
147 * @todo Remove in 4.0.1
148 */
149 wp_dequeue_style( 'colors' );
150
151 $data['iconTypes'] = $icon_types;
152 $data['typeNames'] = array_keys( self::$_icon_types );
153
154 // re-enqueue color style
155 wp_enqueue_style( 'colors' );
156
157 wp_localize_script( 'menu-icons', 'menuIcons', $data );
158 }
159
160
161 /**
162 * Get preview
163 *
164 * @since 0.2.0
165 * @access private
166 * @param int $id Menu item ID
167 * @param array $meta_value Menu item meta value
168 * @return mixed
169 */
170 private static function _get_preview( $id, $meta_value ) {
171 $text = esc_html__( 'Select', 'menu-icons' );
172 if ( empty( $meta_value['type'] ) ) {
173 return $text;
174 }
175
176 $type = $meta_value['type'];
177 $types = self::_get_types();
178 if ( empty( $types[ $type ] ) ) {
179 return $text;
180 }
181
182 if ( empty( $meta_value[ "{$type}-icon" ] ) ) {
183 return $text;
184 }
185
186 if ( empty( $types[ $type ]['preview_cb'] )
187 || ! is_callable( $types[ $type ]['preview_cb'] )
188 ) {
189 return $text;
190 }
191
192 $preview = call_user_func_array(
193 $types[ $type ]['preview_cb'],
194 array( $id, $meta_value )
195 );
196 if ( ! empty( $preview ) ) {
197 return $preview;
198 }
199
200 return $text;
201 }
202
203
204 /**
205 * Get Fields
206 *
207 * @since 0.3.0
208 * @access private
209 * @return array
210 */
211 private static function _get_fields( Array $values = array() ) {
212 $fields = Menu_Icons_Settings::get_settings_fields( $values );
213
214 foreach ( $fields as &$field ) {
215 $field['attributes'] = array_merge(
216 array(
217 'class' => '_setting',
218 'data-key' => $field['id'],
219 ),
220 isset( $field['attributes'] ) ? $field['attributes'] : array()
221 );
222 }
223
224 unset( $field );
225
226 return $fields;
227 }
228
229
230 /**
231 * Print fields
232 *
233 * @since 0.1.0
234 * @access protected
235 * @uses add_action() Calls 'menu_icons_before_fields' hook
236 * @uses add_action() Calls 'menu_icons_after_fields' hook
237 * @wp_hook action menu_item_custom_fields/10/3
238 *
239 * @param object $item Menu item data object.
240 * @param int $depth Nav menu depth.
241 * @param array $args Menu item args.
242 * @param int $id Nav menu ID.
243 *
244 * @return string Form fields
245 */
246 public static function _fields( $item, $depth, $args = array(), $id = 0 ) {
247 require_once Menu_Icons::get( 'dir' ) . 'includes/library/form-fields.php';
248
249 $type_ids = array_values( array_filter( array_keys( self::_get_types() ) ) );
250 $input_id = sprintf( 'menu-icons-%d', $item->ID );
251 $input_name = sprintf( 'menu-icons[%d]', $item->ID );
252 $current = wp_parse_args(
253 Menu_Icons::get_meta( $item->ID ),
254 Menu_Icons_Settings::get_menu_settings( Menu_Icons_Settings::get_current_menu_id() )
255 );
256 ?>
257 <div class="field-icon description-wide menu-icons-wrap">
258 <?php
259 /**
260 * Allow plugins/themes to inject HTML before menu icons' fields
261 *
262 * @param object $item Menu item data object.
263 * @param int $depth Nav menu depth.
264 * @param array $args Menu item args.
265 * @param int $id Nav menu ID.
266 *
267 */
268 do_action( 'menu_icons_before_fields', $item, $depth, $args, $id );
269 ?>
270 <?php
271 ?>
272 <div class="easy">
273 <p class="description submitbox">
274 <label><?php esc_html_e( 'Icon:' ) ?></label>
275 <?php printf(
276 '<a id="menu-icons-%1$d-select" class="_select" title="%2$s" data-id="%1$d" data-text="%2$s">%3$s</a>',
277 esc_attr__( $item->ID ),
278 esc_attr__( 'Select icon', 'menu-icons' ),
279 self::_get_preview( $item->ID, $current )
280 ) ?>
281 <?php printf(
282 '<a id="menu-icons-%1$d-remove" class="_remove hidden submitdelete" data-id="%1$d">%2$s</a>',
283 $item->ID,
284 esc_attr__( 'Remove', 'menu-icons' )
285 ) ?>
286 </p>
287 </div>
288 <div class="original hidden">
289 <p class="description">
290 <label for="<?php echo esc_attr( $input_id ) ?>-type"><?php esc_html_e( 'Icon type', 'menu-icons' ); ?></label>
291 <?php printf(
292 '<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">',
293 esc_attr( $input_id ),
294 esc_attr( $input_name )
295 ) ?>
296 <?php foreach ( self::_get_types() as $id => $props ) : ?>
297 <?php printf(
298 '<option value="%s"%s>%s</option>',
299 esc_attr( $id ),
300 selected( ( isset( $current['type'] ) && $id === $current['type'] ), true, false ),
301 esc_html( $props['label'] )
302 ) ?>
303 <?php endforeach; ?>
304 </select>
305 </p>
306
307 <?php foreach ( self::_get_types() as $props ) : ?>
308 <?php if ( ! empty( $props['field_cb'] ) && is_callable( $props['field_cb'] ) ) : ?>
309 <?php call_user_func_array( $props['field_cb'], array( $item->ID, $current ) ); ?>
310 <?php endif; ?>
311 <?php endforeach; ?>
312
313 <?php foreach ( self::_get_fields( $current ) as $field ) :
314 $field = Kucrut_Form_Field::create(
315 $field,
316 array(
317 'keys' => array( 'menu-icons', $item->ID ),
318 'inline_description' => true,
319 )
320 );
321 ?>
322 <p class="description field-icon-child" data-dep-on='<?php echo json_encode( $type_ids ) ?>'>
323 <?php printf(
324 '<label for="%s">%s</label>',
325 esc_attr( $field->id ),
326 esc_html( $field->label )
327 ) ?>
328 <?php $field->render() ?>
329 </p>
330 <?php endforeach; ?>
331 </div>
332 <?php
333 /**
334 * Allow plugins/themes to inject HTML after menu icons' fields
335 *
336 * @param object $item Menu item data object.
337 * @param int $depth Nav menu depth.
338 * @param array $args Menu item args.
339 * @param int $id Nav menu ID.
340 *
341 */
342 do_action( 'menu_icons_after_fields', $item, $depth, $args, $id );
343 ?>
344 </div>
345 <?php
346 }
347
348
349 /**
350 * Add our field to the screen options toggle
351 *
352 * @since 0.1.0
353 * @access private
354 * @wp_hook action manage_nav-menus_columns
355 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/manage_posts_columns Action: manage_nav-menus_columns/99
356 *
357 * @param array $columns Menu item columns
358 * @return array
359 */
360 public static function _columns( $columns ) {
361 $columns['icon'] = __( 'Icon', 'menu-icons' );
362
363 return $columns;
364 }
365
366
367 /**
368 * Save menu item's icons values
369 *
370 * @since 0.1.0
371 * @access protected
372 * @uses apply_filters() Calls 'menu_icons_values' on returned array.
373 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_update_nav_menu_item Action: wp_update_nav_menu_item/10/2
374 *
375 * @param int $menu_id Nav menu ID
376 * @param int $menu_item_db_id Menu item ID
377 * @param array $menu_item_args Menu item data
378 */
379 public static function _save( $menu_id, $menu_item_db_id, $menu_item_args ) {
380 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
381 return;
382 }
383
384 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
385
386 // Sanitize
387 if ( ! empty( $_POST['menu-icons'][ $menu_item_db_id ] ) ) {
388 $value = (array) $_POST['menu-icons'][ $menu_item_db_id ];
389 }
390 else {
391 $value = array();
392 }
393
394 /**
395 * Allow plugins/themes to filter the values
396 *
397 * @since 0.1.0
398 * @param array $value Metadata value
399 */
400 $value = apply_filters( 'menu_icons_values', $value, $menu_item_db_id );
401
402 // Update
403 if ( ! empty( $value ) ) {
404 update_post_meta( $menu_item_db_id, 'menu-icons', $value );
405 }
406 else {
407 delete_post_meta( $menu_item_db_id, 'menu-icons' );
408 }
409 }
410
411
412 /**
413 * Get and print media templates from all types
414 *
415 * @since 0.2.0
416 * @wp_hook action print_media_templates
417 */
418 public static function _media_templates() {
419 $id_prefix = 'tmpl-menu-icons';
420 $templates = apply_filters( 'menu_icons_media_templates', array() );
421
422 foreach ( $templates as $key => $template ) {
423 $id = sprintf( '%s-%s', $id_prefix, $key );
424 self::_print_tempate( $id, $template );
425 }
426
427 // Icon type templates
428 foreach ( self::_get_types() as $type => $props ) {
429 if ( ! empty( $props['templates'] ) ) {
430 foreach ( $props['templates'] as $key => $template ) {
431 $id = sprintf( '%s-%s-%s', $id_prefix, $type, $key );
432 self::_print_tempate( $id, $template );
433 }
434 }
435 }
436
437 require_once dirname( __FILE__ ) . '/media-template.php';
438 }
439
440
441 /**
442 * Print media template
443 *
444 * @since 0.2.0
445 * @param string $id Template ID
446 * @param string $template Media template HTML
447 */
448 protected static function _print_tempate( $id, $template ) {
449 ?>
450 <script type="text/html" id="<?php echo esc_attr( $id ) ?>">
451 <?php echo $template // xss ok ?>
452 </script>
453 <?php
454 }
455 }
456