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