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