PluginProbe
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.9.0
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.9.0
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 / includes / settings.php

settings.php in Menu Icons by Themeisle – Add Icons to Navigation Menus 0.9.0, at includes/settings.php

706 lines 17.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Settings
5 *
6 * @package Menu_Icons
7 * @author Dzikri Aziz <kvcrvt@gmail.com>
8 */
9
10 /**
11 * Menu Icons Settings module
12 */
13 final class Menu_Icons_Settings {
14
15 const UPDATE_KEY = 'menu-icons-settings-update';
16
17 const RESET_KEY = 'menu-icons-settings-reset';
18
19 const TRANSIENT_KEY = 'menu_icons_message';
20
21 /**
22 * Default setting values
23 *
24 * @since 0.3.0
25 * @var array
26 * @acess protected
27 */
28 protected static $defaults = array(
29 'global' => array(
30 'icon_types' => array( 'dashicons' ),
31 ),
32 );
33
34 /**
35 * Setting values
36 *
37 * @since 0.3.0
38 * @var array
39 * @acess protected
40 */
41 protected static $settings = array();
42
43 /**
44 * Script dependencies
45 *
46 * @since 0.9.0
47 * @access protected
48 * @var array
49 */
50 protected static $script_deps = array( 'jquery' );
51
52
53 /**
54 * Get setting value
55 *
56 * @since 0.3.0
57 * @return mixed
58 */
59 public static function get() {
60 $args = func_get_args();
61
62 return kucrut_get_array_value_deep( self::$settings, $args );
63 }
64
65
66 /**
67 * Get menu settings
68 *
69 * @since 0.3.0
70 * @param int $menu_id
71 * @return array
72 */
73 public static function get_menu_settings( $menu_id ) {
74 $menu_settings = self::get( sprintf( 'menu_%d', $menu_id ) );
75 $menu_settings = apply_filters( 'menu_icons_menu_settings', $menu_settings, $menu_id );
76
77 if ( ! is_array( $menu_settings ) ) {
78 $menu_settings = array();
79 }
80
81 return $menu_settings;
82 }
83
84
85 /**
86 * Check if menu icons is disabled for a menu
87 *
88 * @since 0.8.0
89 *
90 * @param int $menu_id Menu ID. Defaults to current menu being edited.
91 *
92 * @return bool
93 */
94 public static function is_menu_icons_disabled_for_menu( $menu_id = 0 ) {
95 if ( empty( $menu_id ) ) {
96 $menu_id = self::get_current_menu_id();
97 }
98
99 // When we're creating a new menu or the recently edited menu
100 // could not be found.
101 if ( empty( $menu_id ) ) {
102 return true;
103 }
104
105 $menu_settings = self::get_menu_settings( $menu_id );
106 $is_disabled = ! empty( $menu_settings['disabled'] );
107
108 return $is_disabled;
109 }
110
111
112 /**
113 * Settings init
114 *
115 * @since 0.3.0
116 */
117 public static function init() {
118 /**
119 * Allow themes/plugins to override the default settings
120 *
121 * @since 0.9.0
122 * @param array $default_settings Default settings.
123 */
124 self::$defaults = apply_filters( 'menu_icons_settings_defaults', self::$defaults );
125
126 self::$settings = get_option( 'menu-icons', self::$defaults );
127
128 foreach ( self::$settings as $key => &$value ) {
129 if ( 'global' === $key ) {
130 // Remove unregistered icon types.
131 $value['icon_types'] = array_values( array_intersect(
132 array_keys( Menu_Icons::get( 'types' ) ),
133 array_filter( (array) $value['icon_types'] )
134 ) );
135 } else {
136 // Backward-compatibility.
137 if ( isset( $value['width'] ) && ! isset( $value['svg_width'] ) ) {
138 $value['svg_width'] = $value['width'];
139 }
140
141 unset( $value['width'] );
142 }
143 }
144
145 unset( $value );
146
147 /**
148 * Allow themes/plugins to override the settings
149 *
150 * @since 0.9.0
151 * @param array $settings Menu Icons settings.
152 */
153 self::$settings = apply_filters( 'menu_icons_settings', self::$settings );
154
155 if ( self::is_menu_icons_disabled_for_menu() ) {
156 return;
157 }
158
159 if ( ! empty( self::$settings['global']['icon_types'] ) ) {
160 require_once Menu_Icons::get( 'dir' ) . 'includes/picker.php';
161 Menu_Icons_Picker::init();
162 self::$script_deps[] = 'icon-picker';
163 }
164
165 add_action( 'load-nav-menus.php', array( __CLASS__, '_load_nav_menus' ), 1 );
166 add_action( 'wp_ajax_menu_icons_update_settings', array( __CLASS__, '_ajax_menu_icons_update_settings' ) );
167 }
168
169
170 /**
171 * Prepare wp-admin/nav-menus.php page
172 *
173 * @since 0.3.0
174 * @wp_hook action load-nav-menus.php
175 */
176 public static function _load_nav_menus() {
177 add_action( 'admin_enqueue_scripts', array( __CLASS__, '_enqueue_assets' ), 99 );
178
179 /**
180 * Allow settings meta box to be disabled.
181 *
182 * @since 0.4.0
183 * @param bool $disabled Defaults to FALSE.
184 */
185 $settings_disabled = apply_filters( 'menu_icons_disable_settings', false );
186 if ( true === $settings_disabled ) {
187 return;
188 }
189
190 self::_maybe_update_settings();
191 self::_add_settings_meta_box();
192
193 add_action( 'admin_notices', array( __CLASS__, '_admin_notices' ) );
194 }
195
196
197 /**
198 * Update settings
199 *
200 * @since 0.3.0
201 */
202 public static function _maybe_update_settings() {
203 if ( ! empty( $_POST['menu-icons']['settings'] ) ) {
204 check_admin_referer( self::UPDATE_KEY, self::UPDATE_KEY );
205
206 $redirect_url = self::_update_settings( $_POST['menu-icons']['settings'] ); // Input var okay.
207 wp_redirect( $redirect );
208 } elseif ( ! empty( $_REQUEST[ self::RESET_KEY ] ) ) {
209 check_admin_referer( self::RESET_KEY, self::RESET_KEY );
210 wp_redirect( self::_reset_settings() );
211 }
212 }
213
214
215 /**
216 * Update settings
217 *
218 * @since 0.7.0
219 * @access protected
220 * @param array $values Settings values.
221 * @return string Redirect URL.
222 */
223 protected static function _update_settings( $values ) {
224 update_option(
225 'menu-icons',
226 wp_parse_args(
227 kucrut_validate( $values ),
228 self::$settings
229 )
230 );
231 set_transient( self::TRANSIENT_KEY, 'updated', 30 );
232
233 $redirect_url = remove_query_arg(
234 array( 'menu-icons-reset' ),
235 wp_get_referer()
236 );
237
238 return $redirect_url;
239 }
240
241
242 /**
243 * Reset settings
244 *
245 * @since 0.7.0
246 * @access protected
247 * @return string Redirect URL.
248 */
249 protected static function _reset_settings() {
250 delete_option( 'menu-icons' );
251 set_transient( self::TRANSIENT_KEY, 'reset', 30 );
252
253 $redirect_url = remove_query_arg(
254 array( self::RESET_KEY, 'menu-icons-updated' ),
255 wp_get_referer()
256 );
257
258 return $redirect_url;
259 }
260
261
262 /**
263 * Update settings via ajax
264 *
265 * @since 0.7.0
266 * @wp_hook action wp_ajax_menu_icons_update_settings
267 */
268 public static function _ajax_menu_icons_update_settings() {
269 check_ajax_referer( self::UPDATE_KEY, self::UPDATE_KEY );
270
271 if ( empty( $_POST['menu-icons']['settings'] ) ) {
272 wp_send_json_error();
273 }
274
275 $redirect_url = self::_update_settings( $_POST['menu-icons']['settings'] ); // Input var okay.
276 wp_send_json_success( array( 'redirectUrl' => $redirect_url ) );
277 }
278
279
280 /**
281 * Print admin notices
282 *
283 * @since 0.3.0
284 * @wp_hook action admin_notices
285 */
286 public static function _admin_notices() {
287 $messages = array(
288 'updated' => __( '<strong>Menu Icons Settings</strong> have been successfully updated.', 'menu-icons' ),
289 'reset' => __( '<strong>Menu Icons Settings</strong> have been successfully reset.', 'menu-icons' ),
290 );
291
292 $message_type = get_transient( self::TRANSIENT_KEY );
293
294 if ( ! empty( $message_type ) && ! empty( $messages[ $message_type ] ) ) {
295 printf(
296 '<div class="updated notice is-dismissible"><p>%s</p></div>',
297 wp_kses( $messages[ $message_type ], array( 'strong' => true ) )
298 );
299 }
300
301 delete_transient( self::TRANSIENT_KEY );
302 }
303
304
305 /**
306 * Settings meta box
307 *
308 * @since 0.3.0
309 * @access private
310 */
311 private static function _add_settings_meta_box() {
312 add_meta_box(
313 'menu-icons-settings',
314 __( 'Menu Icons Settings', 'menu-icons' ),
315 array( __CLASS__, '_meta_box' ),
316 'nav-menus',
317 'side',
318 'low',
319 array()
320 );
321 }
322
323
324 /**
325 * Get ID of menu being edited
326 *
327 * @since 0.7.0
328 * @since 0.8.0 Get the recently edited menu from user option.
329 *
330 * @return int
331 */
332 public static function get_current_menu_id() {
333 global $nav_menu_selected_id;
334
335 if ( ! empty( $nav_menu_selected_id ) ) {
336 return $nav_menu_selected_id;
337 }
338
339 if ( is_admin() && isset( $_REQUEST['menu'] ) ) {
340 $menu_id = absint( $_REQUEST['menu'] );
341 } else {
342 $menu_id = absint( get_user_option( 'nav_menu_recently_edited' ) );
343 }
344
345 return $menu_id;
346 }
347
348
349 /**
350 * Get settings fields
351 *
352 * @since 0.4.0
353 * @param array $values Values to be applied to each field.
354 * @uses apply_filters() Calls 'menu_icons_settings_fields'.
355 * @return array
356 */
357 public static function get_settings_fields( Array $values = array() ) {
358 $fields = array(
359 'hide_label' => array(
360 'id' => 'hide_label',
361 'type' => 'select',
362 'label' => __( 'Hide Label', 'menu-icons' ),
363 'default' => '',
364 'choices' => array(
365 array(
366 'value' => '',
367 'label' => __( 'No', 'menu-icons' ),
368 ),
369 array(
370 'value' => '1',
371 'label' => __( 'Yes', 'menu-icons' ),
372 ),
373 ),
374 ),
375 'position' => array(
376 'id' => 'position',
377 'type' => 'select',
378 'label' => __( 'Position', 'menu-icons' ),
379 'default' => 'before',
380 'choices' => array(
381 array(
382 'value' => 'before',
383 'label' => __( 'Before', 'menu-icons' ),
384 ),
385 array(
386 'value' => 'after',
387 'label' => __( 'After', 'menu-icons' ),
388 ),
389 ),
390 ),
391 'vertical_align' => array(
392 'id' => 'vertical_align',
393 'type' => 'select',
394 'label' => __( 'Vertical Align', 'menu-icons' ),
395 'default' => 'middle',
396 'choices' => array(
397 array(
398 'value' => 'super',
399 'label' => __( 'Super', 'menu-icons' ),
400 ),
401 array(
402 'value' => 'top',
403 'label' => __( 'Top', 'menu-icons' ),
404 ),
405 array(
406 'value' => 'text-top',
407 'label' => __( 'Text Top', 'menu-icons' ),
408 ),
409 array(
410 'value' => 'middle',
411 'label' => __( 'Middle', 'menu-icons' ),
412 ),
413 array(
414 'value' => 'baseline',
415 'label' => __( 'Baseline', 'menu-icons' ),
416 ),
417 array(
418 'value' => 'text-bottom',
419 'label' => __( 'Text Bottom', 'menu-icons' ),
420 ),
421 array(
422 'value' => 'bottom',
423 'label' => __( 'Bottom', 'menu-icons' ),
424 ),
425 array(
426 'value' => 'sub',
427 'label' => __( 'Sub', 'menu-icons' ),
428 ),
429 ),
430 ),
431 'font_size' => array(
432 'id' => 'font_size',
433 'type' => 'number',
434 'label' => __( 'Font Size', 'menu-icons' ),
435 'default' => '1.2',
436 'description' => 'em',
437 'attributes' => array(
438 'min' => '0.1',
439 'step' => '0.1',
440 ),
441 ),
442 'svg_width' => array(
443 'id' => 'svg_width',
444 'type' => 'number',
445 'label' => __( 'SVG Width', 'menu-icons' ),
446 'default' => '1',
447 'description' => 'em',
448 'attributes' => array(
449 'min' => '.5',
450 'step' => '.1',
451 ),
452 ),
453 'image_size' => array(
454 'id' => 'image_size',
455 'type' => 'select',
456 'label' => __( 'Image Size', 'menu-icons' ),
457 'default' => 'thumbnail',
458 'choices' => kucrut_get_image_sizes(),
459 ),
460 );
461
462 $fields = apply_filters( 'menu_icons_settings_fields', $fields );
463
464 foreach ( $fields as &$field ) {
465 if ( isset( $values[ $field['id'] ] ) ) {
466 $field['value'] = $values[ $field['id'] ];
467 }
468
469 if ( ! isset( $field['value'] ) && isset( $field['default'] ) ) {
470 $field['value'] = $field['default'];
471 }
472 }
473
474 unset( $field );
475
476 return $fields;
477 }
478
479
480 /**
481 * Get settings sections
482 *
483 * @since 0.3.0
484 * @uses apply_filters() Calls 'menu_icons_settings_sections'.
485 * @return array
486 */
487 public static function get_fields() {
488 $menu_id = self::get_current_menu_id();
489 $icon_types = wp_list_pluck( Menu_Icons::get( 'types' ), 'name' );
490
491 asort( $icon_types );
492
493 $sections = array(
494 'global' => array(
495 'id' => 'global',
496 'title' => __( 'Global', 'menu-icons' ),
497 'description' => __( 'Global settings', 'menu-icons' ),
498 'fields' => array(
499 array(
500 'id' => 'icon_types',
501 'type' => 'checkbox',
502 'label' => __( 'Icon Types', 'menu-icons' ),
503 'choices' => $icon_types,
504 'value' => self::get( 'global', 'icon_types' ),
505 ),
506 ),
507 'args' => array(),
508 ),
509 );
510
511 if ( ! empty( $menu_id ) ) {
512 $menu_term = get_term( $menu_id, 'nav_menu' );
513 $menu_key = sprintf( 'menu_%d', $menu_id );
514 $menu_settings = self::get_menu_settings( $menu_id );
515
516 $sections['menu'] = array(
517 'id' => $menu_key,
518 'title' => __( 'Current Menu', 'menu-icons' ),
519 'description' => sprintf(
520 __( '"%s" menu settings', 'menu-icons' ),
521 apply_filters( 'single_term_title', $menu_term->name )
522 ),
523 'fields' => self::get_settings_fields( $menu_settings ),
524 'args' => array( 'inline_description' => true ),
525 );
526 }
527
528 return apply_filters( 'menu_icons_settings_sections', $sections, $menu_id );
529 }
530
531
532 /**
533 * Get processed settings fields
534 *
535 * @since 0.3.0
536 * @access private
537 * @return array
538 */
539 private static function _get_fields() {
540 if ( ! class_exists( 'Kucrut_Form_Field' ) ) {
541 require_once Menu_Icons::get( 'dir' ) . 'includes/library/form-fields.php';
542 }
543
544 $keys = array( 'menu-icons', 'settings' );
545 $sections = self::get_fields();
546
547 foreach ( $sections as &$section ) {
548 $_keys = array_merge( $keys, array( $section['id'] ) );
549 $_args = array_merge( array( 'keys' => $_keys ), $section['args'] );
550
551 foreach ( $section['fields'] as &$field ) {
552 $field = Kucrut_Form_Field::create( $field, $_args );
553 }
554
555 unset( $field );
556 }
557
558 unset( $section );
559
560 return $sections;
561 }
562
563
564 /**
565 * Settings meta box
566 *
567 * @since 0.3.0
568 */
569 public static function _meta_box() {
570 ?>
571 <div class="taxonomydiv">
572 <ul id="menu-icons-settings-tabs" class="taxonomy-tabs add-menu-item-tabs hide-if-no-js">
573 <?php foreach ( self::get_fields() as $section ) : ?>
574 <?php
575 printf(
576 '<li><a href="#" title="%s" class="mi-settings-nav-tab" data-type="menu-icons-settings-%s">%s</a></li>',
577 esc_attr( $section['description'] ),
578 esc_attr( $section['id'] ),
579 esc_html( $section['title'] )
580 );
581 ?>
582 <?php endforeach; ?>
583 <?php
584 printf(
585 '<li><a href="#" class="mi-settings-nav-tab" data-type="menu-icons-settings-extensions">%s</a></li>',
586 esc_html__( 'Extensions', 'menu-icons' )
587 );
588 ?>
589 </ul>
590 <?php foreach ( self::_get_fields() as $section_index => $section ) : ?>
591 <div id="menu-icons-settings-<?php echo esc_attr( $section['id'] ) ?>" class="tabs-panel _<?php echo esc_attr( $section_index ) ?>">
592 <h4 class="hide-if-js"><?php echo esc_html( $section['title'] ) ?></h4>
593 <?php foreach ( $section['fields'] as $field ) : ?>
594 <div class="_field">
595 <?php
596 printf(
597 '<label for="%s" class="_main">%s</label>',
598 esc_attr( $field->id ),
599 esc_html( $field->label )
600 );
601 ?>
602 <?php $field->render() ?>
603 </div>
604 <?php endforeach; ?>
605 </div>
606 <?php endforeach; ?>
607 <div id="menu-icons-settings-extensions" class="tabs-panel _extensions">
608 <h4 class="hide-if-js"><?php echo esc_html__( 'Extensions', 'menu-icons' ) ?></h4>
609 <ul>
610 <li><a target="_blank" href="http://wordpress.org/plugins/menu-icons-icomoon/">IcoMoon</a></li>
611 </ul>
612 </div>
613 </div>
614 <p class="submitbox button-controls">
615 <?php wp_nonce_field( self::UPDATE_KEY, self::UPDATE_KEY ) ?>
616 <span class="list-controls">
617 <?php
618 printf(
619 '<a href="%s" title="%s" class="select-all submitdelete">%s</a>',
620 esc_url(
621 wp_nonce_url(
622 admin_url( '/nav-menus.php' ),
623 self::RESET_KEY,
624 self::RESET_KEY
625 )
626 ),
627 esc_attr__( 'Discard all changes and reset to default state', 'menu-icons' ),
628 esc_html__( 'Reset', 'menu-icons' )
629 );
630 ?>
631 </span>
632
633 <span class="add-to-menu">
634 <span class="spinner"></span>
635 <?php
636 submit_button(
637 __( 'Save Settings', 'menu-icons' ),
638 'secondary',
639 'menu-icons-settings-save',
640 false
641 );
642 ?>
643 </span>
644 </p>
645 <?php
646 }
647
648
649 /**
650 * Enqueue scripts & styles for Appearance > Menus page
651 *
652 * @since 0.3.0
653 * @wp_hook action admin_enqueue_scripts
654 */
655 public static function _enqueue_assets() {
656 $url = Menu_Icons::get( 'url' );
657 $suffix = kucrut_get_script_suffix();
658
659 wp_enqueue_style(
660 'menu-icons',
661 "{$url}css/admin{$suffix}.css",
662 false,
663 Menu_Icons::VERSION
664 );
665 wp_enqueue_script(
666 'menu-icons',
667 "{$url}js/admin{$suffix}.js",
668 self::$script_deps,
669 Menu_Icons::VERSION,
670 true
671 );
672
673 /**
674 * Allow plugins/themes to filter the settings' JS data
675 *
676 * @since 0.9.0
677 * @param array $js_data JS Data.
678 */
679 $js_data = apply_filters(
680 'menu_icons_settings_js_data',
681 array(
682 'text' => array(
683 'title' => __( 'Select Icon', 'menu-icons' ),
684 'select' => __( 'Select', 'menu-icons' ),
685 'remove' => __( 'Remove', 'menu-icons' ),
686 'change' => __( 'Change', 'menu-icons' ),
687 'all' => __( 'All', 'menu-icons' ),
688 'preview' => __( 'Preview', 'menu-icons' ),
689 'settingsInfo' => sprintf(
690 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' ),
691 '<a target="_blank" href="http://wordpress.org/plugins/simple-custom-css/">Simple Custom CSS</a>'
692 ),
693 ),
694 'settingsFields' => self::get_settings_fields(),
695 'activeTypes' => self::get( 'global', 'icon_types' ),
696 'ajaxUrls' => array(
697 'update' => add_query_arg( 'action', 'menu_icons_update_settings', admin_url( '/admin-ajax.php' ) ),
698 ),
699 'menuSettings' => self::get_menu_settings( self::get_current_menu_id() ),
700 )
701 );
702
703 wp_localize_script( 'menu-icons', 'menuIcons', $js_data );
704 }
705 }
706