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
settings.php
564 lines
| 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(), |
| 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 | /** |
| 45 | * Get setting value |
| 46 | * |
| 47 | * @since 0.3.0 |
| 48 | * @return mixed |
| 49 | */ |
| 50 | public static function get() { |
| 51 | $args = func_get_args(); |
| 52 | |
| 53 | return kucrut_get_array_value_deep( self::$settings, $args ); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Get setting values and apply sanitation |
| 59 | * |
| 60 | * @since 0.3.0 |
| 61 | * @acess private |
| 62 | */ |
| 63 | private static function _get() { |
| 64 | $settings = get_option( 'menu-icons', null ); |
| 65 | |
| 66 | if ( is_null( $settings ) ) { |
| 67 | $settings['global'] = self::$defaults['global']; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Check icon types |
| 72 | * |
| 73 | * A type could be enabled in the settings but disabled by a filter, |
| 74 | * so we need to 'fix' it here. |
| 75 | */ |
| 76 | if ( ! empty( $settings['global']['icon_types'] ) ) { |
| 77 | $active_types = array(); |
| 78 | $icon_types = Menu_Icons::get( 'icon_types' ); |
| 79 | |
| 80 | foreach ( (array) $settings['global']['icon_types'] as $index => $id ) { |
| 81 | if ( isset( $icon_types[ $id ] ) ) { |
| 82 | $active_types[] = $id; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if ( $settings['global']['icon_types'] !== $active_types ) { |
| 87 | $settings['global']['icon_types'] = $active_types; |
| 88 | update_option( 'menu-icons', $settings ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | self::$settings = $settings; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Get menu settings |
| 98 | * |
| 99 | * @since 0.3.0 |
| 100 | * @param int $menu_id |
| 101 | * @return array |
| 102 | */ |
| 103 | public static function get_menu_settings( $menu_id ) { |
| 104 | $menu_settings = self::get( sprintf( 'menu_%d', $menu_id ) ); |
| 105 | $menu_settings = apply_filters( 'menu_icons_menu_settings', $menu_settings, $menu_id ); |
| 106 | |
| 107 | if ( ! is_array( $menu_settings ) ) { |
| 108 | $menu_settings = array(); |
| 109 | } |
| 110 | |
| 111 | return $menu_settings; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * Settings init |
| 117 | * |
| 118 | * @since 0.3.0 |
| 119 | */ |
| 120 | public static function init() { |
| 121 | self::$defaults['global']['icon_types'] = array_keys( Menu_Icons::get( 'icon_types' ) ); |
| 122 | self::_get(); |
| 123 | |
| 124 | require_once Menu_Icons::get( 'dir' ) . 'includes/admin.php'; |
| 125 | Menu_Icons_Admin_Nav_Menus::init(); |
| 126 | |
| 127 | add_action( 'load-nav-menus.php', array( __CLASS__, '_load_nav_menus' ), 1 ); |
| 128 | add_action( 'wp_ajax_menu_icons_update_settings', array( __CLASS__, '_ajax_menu_icons_update_settings' ) ); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Prepare wp-admin/nav-menus.php page |
| 134 | * |
| 135 | * @since 0.3.0 |
| 136 | * @wp_hook load-nav-menus.php |
| 137 | */ |
| 138 | public static function _load_nav_menus() { |
| 139 | add_action( 'admin_enqueue_scripts', array( __CLASS__, '_enqueue_assets' ), 99 ); |
| 140 | |
| 141 | /** |
| 142 | * Allow settings meta box to be disabled. |
| 143 | * |
| 144 | * @since 0.4.0 |
| 145 | * @param bool $disabled Defaults to FALSE |
| 146 | */ |
| 147 | $settings_disabled = apply_filters( 'menu_icons_disable_settings', false ); |
| 148 | if ( true === $settings_disabled ) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | self::_maybe_update_settings(); |
| 153 | self::_add_settings_meta_box(); |
| 154 | |
| 155 | add_action( 'admin_notices', array( __CLASS__, '_admin_notices' ) ); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * Update settings |
| 161 | * |
| 162 | * @since 0.3.0 |
| 163 | * @access private |
| 164 | * @wp_hook load-nav-menus.php |
| 165 | */ |
| 166 | public static function _maybe_update_settings() { |
| 167 | if ( ! empty( $_POST['menu-icons']['settings'] ) ) { |
| 168 | check_admin_referer( self::UPDATE_KEY, self::UPDATE_KEY ); |
| 169 | |
| 170 | $redirect_url = self::_update_settings( $_POST['menu-icons']['settings'] ); |
| 171 | wp_redirect( $redirect ); |
| 172 | } |
| 173 | elseif ( ! empty( $_REQUEST[ self::RESET_KEY ] ) ) { |
| 174 | check_admin_referer( self::RESET_KEY, self::RESET_KEY ); |
| 175 | wp_redirect( self::_reset_settings() ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * Update settings |
| 182 | * |
| 183 | * @since 0.7.0 |
| 184 | * @access protected |
| 185 | * @param array $values Settings values |
| 186 | * @return string Redirect URL |
| 187 | */ |
| 188 | protected static function _update_settings( $values ) { |
| 189 | update_option( |
| 190 | 'menu-icons', |
| 191 | wp_parse_args( |
| 192 | kucrut_validate( $values ), |
| 193 | self::$settings |
| 194 | ) |
| 195 | ); |
| 196 | set_transient( self::TRANSIENT_KEY, 'updated', 30 ); |
| 197 | |
| 198 | $redirect_url = remove_query_arg( |
| 199 | array( 'menu-icons-reset' ), |
| 200 | wp_get_referer() |
| 201 | ); |
| 202 | |
| 203 | return $redirect_url; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /** |
| 208 | * Reset settings |
| 209 | * |
| 210 | * @since 0.7.0 |
| 211 | * @access protected |
| 212 | * @return string Redirect URL |
| 213 | */ |
| 214 | protected static function _reset_settings() { |
| 215 | delete_option( 'menu-icons' ); |
| 216 | set_transient( self::TRANSIENT_KEY, 'reset', 30 ); |
| 217 | |
| 218 | $redirect_url = remove_query_arg( |
| 219 | array( self::RESET_KEY, 'menu-icons-updated' ), |
| 220 | wp_get_referer() |
| 221 | ); |
| 222 | |
| 223 | return $redirect_url; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /** |
| 228 | * Update settings via ajax |
| 229 | * |
| 230 | * @since 0.7.0 |
| 231 | * @wp_hook action _ajax_menu_icons_update_settings |
| 232 | */ |
| 233 | public static function _ajax_menu_icons_update_settings() { |
| 234 | check_ajax_referer( self::UPDATE_KEY, self::UPDATE_KEY ); |
| 235 | |
| 236 | if ( empty( $_POST['menu-icons']['settings'] ) ) { |
| 237 | wp_send_json_error(); |
| 238 | } |
| 239 | |
| 240 | $redirect_url = self::_update_settings( $_POST['menu-icons']['settings'] ); |
| 241 | wp_send_json_success( array( 'redirectUrl' => $redirect_url ) ); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /** |
| 246 | * Print admin notices |
| 247 | * |
| 248 | * @since 0.3.0 |
| 249 | * @wp_hook admin_notices |
| 250 | */ |
| 251 | public static function _admin_notices() { |
| 252 | $messages = array( |
| 253 | 'updated' => __( '<strong>Menu Icons Settings</strong> have been successfully updated.', 'menu-icons' ), |
| 254 | 'reset' => __( '<strong>Menu Icons Settings</strong> have been successfully reset.', 'menu-icons' ), |
| 255 | ); |
| 256 | |
| 257 | $message_type = get_transient( self::TRANSIENT_KEY ); |
| 258 | if ( ! empty( $message_type ) && ! empty( $messages[ $message_type ] ) ) { |
| 259 | printf( |
| 260 | '<div class="updated"><p>%s</p></div>', |
| 261 | wp_kses( $messages[ $message_type ], array( 'strong' => true ) ) |
| 262 | ); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /** |
| 268 | * Settings meta box |
| 269 | * |
| 270 | * @since 0.3.0 |
| 271 | * @access private |
| 272 | */ |
| 273 | private static function _add_settings_meta_box() { |
| 274 | add_meta_box( |
| 275 | 'menu-icons-settings', |
| 276 | __( 'Menu Icons Settings', 'menu-icons' ), |
| 277 | array( __CLASS__, '_meta_box' ), |
| 278 | 'nav-menus', |
| 279 | 'side', |
| 280 | 'low', |
| 281 | array() |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | /** |
| 287 | * Get ID of nav menu being edited |
| 288 | * |
| 289 | * @since %ver |
| 290 | * @return int |
| 291 | */ |
| 292 | public static function get_current_menu_id() { |
| 293 | global $nav_menu_selected_id; |
| 294 | |
| 295 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX && ! empty( $_POST['menu'] ) ) { |
| 296 | $menu_id = absint( $_POST['menu'] ); |
| 297 | } |
| 298 | else { |
| 299 | $menu_id = $nav_menu_selected_id; |
| 300 | } |
| 301 | |
| 302 | return $menu_id; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | /** |
| 307 | * Get settings fields |
| 308 | * |
| 309 | * @since 0.4.0 |
| 310 | * @param array $values Values to be applied to each field |
| 311 | * @uses apply_filters() Calls 'menu_icons_settings_fields'. |
| 312 | * @return array |
| 313 | */ |
| 314 | public static function get_settings_fields( Array $values = array() ) { |
| 315 | $fields = array( |
| 316 | 'hide_label' => array( |
| 317 | 'id' => 'hide_label', |
| 318 | 'type' => 'select', |
| 319 | 'label' => __( 'Hide Label', 'menu-icons' ), |
| 320 | 'default' => '', |
| 321 | 'choices' => array( |
| 322 | array( |
| 323 | 'value' => '', |
| 324 | 'label' => __( 'No', 'menu-icons' ), |
| 325 | ), |
| 326 | array( |
| 327 | 'value' => '1', |
| 328 | 'label' => __( 'Yes', 'menu-icons' ), |
| 329 | ), |
| 330 | ), |
| 331 | ), |
| 332 | 'position' => array( |
| 333 | 'id' => 'position', |
| 334 | 'type' => 'select', |
| 335 | 'label' => __( 'Position', 'menu-icons' ), |
| 336 | 'default' => 'before', |
| 337 | 'choices' => array( |
| 338 | array( |
| 339 | 'value' => 'before', |
| 340 | 'label' => __( 'Before', 'menu-icons' ), |
| 341 | ), |
| 342 | array( |
| 343 | 'value' => 'after', |
| 344 | 'label' => __( 'After', 'menu-icons' ), |
| 345 | ), |
| 346 | ), |
| 347 | ), |
| 348 | ); |
| 349 | |
| 350 | $fields = apply_filters( 'menu_icons_settings_fields', $fields ); |
| 351 | |
| 352 | foreach ( $fields as &$field ) { |
| 353 | if ( isset( $values[ $field['id'] ] ) ) { |
| 354 | $field['value'] = $values[ $field['id'] ]; |
| 355 | } |
| 356 | |
| 357 | if ( ! isset( $field['value'] ) && isset( $field['default'] ) ) { |
| 358 | $field['value'] = $field['default']; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | unset( $field ); |
| 363 | |
| 364 | return $fields; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /** |
| 369 | * Get settings sections |
| 370 | * |
| 371 | * @since 0.3.0 |
| 372 | * @uses apply_filters() Calls 'menu_icons_settings_sections'. |
| 373 | * @return array |
| 374 | */ |
| 375 | public static function get_fields() { |
| 376 | $menu_id = self::get_current_menu_id(); |
| 377 | $icon_types = array(); |
| 378 | foreach ( Menu_Icons::get( 'icon_types' ) as $id => $props ) { |
| 379 | $icon_types[ $id ] = $props['label']; |
| 380 | } |
| 381 | |
| 382 | $sections = array( |
| 383 | 'global' => array( |
| 384 | 'id' => 'global', |
| 385 | 'title' => __( 'Global', 'menu-icons' ), |
| 386 | 'description' => __( 'Global settings', 'menu-icons' ), |
| 387 | 'fields' => array( |
| 388 | array( |
| 389 | 'id' => 'icon_types', |
| 390 | 'type' => 'checkbox', |
| 391 | 'label' => __( 'Icon Types', 'menu-icons' ), |
| 392 | 'choices' => $icon_types, |
| 393 | 'value' => self::get( 'global', 'icon_types' ), |
| 394 | ), |
| 395 | ), |
| 396 | 'args' => array(), |
| 397 | ), |
| 398 | ); |
| 399 | |
| 400 | if ( ! empty( $menu_id ) ) { |
| 401 | $menu_term = get_term( $menu_id, 'nav_menu' ); |
| 402 | $menu_key = sprintf( 'menu_%d', $menu_id ); |
| 403 | $menu_settings = self::get_menu_settings( $menu_id ); |
| 404 | |
| 405 | $sections['menu'] = array( |
| 406 | 'id' => $menu_key, |
| 407 | 'title' => __( 'Current Menu', 'menu-icons' ), |
| 408 | 'description' => sprintf( |
| 409 | __( '"%s" menu settings', 'menu-icons' ), |
| 410 | apply_filters( 'single_term_title', $menu_term->name ) |
| 411 | ), |
| 412 | 'fields' => self::get_settings_fields( $menu_settings ), |
| 413 | 'args' => array( 'inline_description' => true ), |
| 414 | ); |
| 415 | } |
| 416 | |
| 417 | return apply_filters( 'menu_icons_settings_sections', $sections, $menu_id ); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | /** |
| 422 | * Get processed settings fields |
| 423 | * |
| 424 | * @since 0.3.0 |
| 425 | * @access private |
| 426 | * @return array |
| 427 | */ |
| 428 | private static function _get_fields() { |
| 429 | if ( ! class_exists( 'Kucrut_Form_Field' ) ) { |
| 430 | require_once Menu_Icons::get( 'dir' ) . 'includes/library/form-fields.php'; |
| 431 | } |
| 432 | |
| 433 | $keys = array( 'menu-icons', 'settings' ); |
| 434 | $sections = self::get_fields(); |
| 435 | |
| 436 | foreach ( $sections as &$section ) { |
| 437 | $_keys = array_merge( $keys, array( $section['id'] ) ); |
| 438 | $_args = array_merge( array( 'keys' => $_keys ), $section['args'] ); |
| 439 | |
| 440 | foreach ( $section['fields'] as &$field ) { |
| 441 | $field = Kucrut_Form_Field::create( $field, $_args ); |
| 442 | } |
| 443 | |
| 444 | unset( $field ); |
| 445 | } |
| 446 | |
| 447 | unset( $section ); |
| 448 | |
| 449 | return $sections; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /** |
| 454 | * Settings meta box |
| 455 | * |
| 456 | * @since 0.3.0 |
| 457 | */ |
| 458 | public static function _meta_box() { |
| 459 | ?> |
| 460 | <div class="taxonomydiv"> |
| 461 | <ul id="menu-icons-settings-tabs" class="taxonomy-tabs add-menu-item-tabs hide-if-no-js"> |
| 462 | <?php foreach ( self::get_fields() as $section ) : ?> |
| 463 | <?php printf( |
| 464 | '<li><a href="#" title="%s" class="mi-settings-nav-tab" data-type="menu-icons-settings-%s">%s</a></li>', |
| 465 | esc_attr( $section['description'] ), |
| 466 | esc_attr( $section['id'] ), |
| 467 | esc_html( $section['title'] ) |
| 468 | ) ?> |
| 469 | <?php endforeach ?> |
| 470 | <?php printf( |
| 471 | '<li><a href="#" class="mi-settings-nav-tab" data-type="menu-icons-settings-extensions">%s</a></li>', |
| 472 | __( 'Extensions', 'menu-icons' ) |
| 473 | ) ?> |
| 474 | </ul> |
| 475 | <?php foreach ( self::_get_fields() as $section_index => $section ) : ?> |
| 476 | <div id="menu-icons-settings-<?php echo esc_attr( $section['id'] ) ?>" class="tabs-panel _<?php echo esc_attr( $section_index ) ?>"> |
| 477 | <h4 class="hide-if-js"><?php echo esc_html( $section['title'] ) ?></h4> |
| 478 | <?php foreach ( $section['fields'] as $field ) : ?> |
| 479 | <div class="_field"> |
| 480 | <?php printf( |
| 481 | '<label for="%s" class="_main">%s</label>', |
| 482 | esc_attr( $field->id ), |
| 483 | esc_html( $field->label ) |
| 484 | ) ?> |
| 485 | <?php $field->render() ?> |
| 486 | </div> |
| 487 | <?php endforeach; ?> |
| 488 | </div> |
| 489 | <?php endforeach; ?> |
| 490 | <div id="menu-icons-settings-extensions" class="tabs-panel _extensions"> |
| 491 | <h4 class="hide-if-js"><?php echo esc_html__( 'Extensions', 'menu-icons' ) ?></h4> |
| 492 | <ul> |
| 493 | <li><a target="_blank" href="http://wordpress.org/plugins/menu-icons-icomoon/">IcoMoon</a></li> |
| 494 | </ul> |
| 495 | </div> |
| 496 | </div> |
| 497 | <p class="submitbox button-controls"> |
| 498 | <?php wp_nonce_field( self::UPDATE_KEY, self::UPDATE_KEY ) ?> |
| 499 | <span class="list-controls"> |
| 500 | <?php printf( |
| 501 | '<a href="%s" title="%s" class="select-all submitdelete">%s</a>', |
| 502 | esc_url( |
| 503 | wp_nonce_url( |
| 504 | admin_url( '/nav-menus.php' ), |
| 505 | self::RESET_KEY, |
| 506 | self::RESET_KEY |
| 507 | ) |
| 508 | ), |
| 509 | esc_attr__( 'Discard all changes and reset to default state', 'menu-icons' ), |
| 510 | esc_html__( 'Reset', 'menu-icons' ) |
| 511 | ) ?> |
| 512 | </span> |
| 513 | |
| 514 | <span class="add-to-menu"> |
| 515 | <span class="spinner"></span> |
| 516 | <?php submit_button( |
| 517 | __( 'Save Settings', 'menu-icons' ), |
| 518 | 'secondary', |
| 519 | 'menu-item-settings-save', |
| 520 | false |
| 521 | ) ?> |
| 522 | </span> |
| 523 | </p> |
| 524 | <?php |
| 525 | } |
| 526 | |
| 527 | |
| 528 | /** |
| 529 | * Enqueue scripts & styles for admin page |
| 530 | * |
| 531 | * @since 0.3.0 |
| 532 | * @wp_hook action admin_enqueue_scripts |
| 533 | */ |
| 534 | public static function _enqueue_assets() { |
| 535 | $suffix = Menu_Icons::get_script_suffix(); |
| 536 | |
| 537 | wp_enqueue_style( |
| 538 | 'menu-icons', |
| 539 | Menu_Icons::get( 'url' ) . 'css/admin' . $suffix . '.css', |
| 540 | false, |
| 541 | Menu_Icons::VERSION |
| 542 | ); |
| 543 | wp_register_script( |
| 544 | 'kucrut-jquery-input-dependencies', |
| 545 | Menu_Icons::get( 'url' ) . 'js/input-dependencies' . $suffix . '.js', |
| 546 | array( 'jquery' ), |
| 547 | '0.1.0', |
| 548 | true |
| 549 | ); |
| 550 | |
| 551 | if ( ! empty( self::$settings['global']['icon_types'] ) ) { |
| 552 | wp_enqueue_media(); |
| 553 | } |
| 554 | |
| 555 | wp_enqueue_script( |
| 556 | 'menu-icons', |
| 557 | Menu_Icons::get( 'url' ) . 'js/admin' . $suffix . '.js', |
| 558 | array( 'kucrut-jquery-input-dependencies' ), |
| 559 | Menu_Icons::VERSION, |
| 560 | true |
| 561 | ); |
| 562 | } |
| 563 | } |
| 564 |