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
settings.php
505 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 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Prepare wp-admin/nav-menus.php page |
| 133 | * |
| 134 | * @since 0.3.0 |
| 135 | * @wp_hook load-nav-menus.php |
| 136 | */ |
| 137 | public static function _load_nav_menus() { |
| 138 | add_action( 'admin_enqueue_scripts', array( __CLASS__, '_enqueue_assets' ), 99 ); |
| 139 | |
| 140 | /** |
| 141 | * Allow settings meta box to be disabled. |
| 142 | * |
| 143 | * @since 0.4.0 |
| 144 | * @param bool $disabled Defaults to FALSE |
| 145 | */ |
| 146 | $settings_disabled = apply_filters( 'menu_icons_disable_settings', false ); |
| 147 | if ( true === $settings_disabled ) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | self::_maybe_update_settings(); |
| 152 | self::_add_settings_meta_box(); |
| 153 | |
| 154 | add_action( 'admin_notices', array( __CLASS__, '_admin_notices' ) ); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * Update settings |
| 160 | * |
| 161 | * @since 0.3.0 |
| 162 | * @access private |
| 163 | * @wp_hook load-nav-menus.php |
| 164 | */ |
| 165 | public static function _maybe_update_settings() { |
| 166 | if ( ! empty( $_POST['menu-icons']['settings'] ) ) { |
| 167 | check_admin_referer( self::UPDATE_KEY, self::UPDATE_KEY ); |
| 168 | |
| 169 | update_option( |
| 170 | 'menu-icons', |
| 171 | wp_parse_args( |
| 172 | kucrut_validate( $_POST['menu-icons']['settings'] ), |
| 173 | self::$settings |
| 174 | ) |
| 175 | ); |
| 176 | set_transient( self::TRANSIENT_KEY, 'updated', 30 ); |
| 177 | wp_redirect( |
| 178 | remove_query_arg( |
| 179 | array( 'menu-icons-reset' ), |
| 180 | wp_get_referer() |
| 181 | ) |
| 182 | ); |
| 183 | } |
| 184 | elseif ( ! empty( $_REQUEST[ self::RESET_KEY ] ) ) { |
| 185 | check_admin_referer( self::RESET_KEY, self::RESET_KEY ); |
| 186 | |
| 187 | delete_option( 'menu-icons' ); |
| 188 | set_transient( self::TRANSIENT_KEY, 'reset', 30 ); |
| 189 | wp_redirect( |
| 190 | remove_query_arg( |
| 191 | array( self::RESET_KEY, 'menu-icons-updated' ), |
| 192 | wp_get_referer() |
| 193 | ) |
| 194 | ); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * Print admin notices |
| 201 | * |
| 202 | * @since 0.3.0 |
| 203 | * @wp_hook admin_notices |
| 204 | */ |
| 205 | public static function _admin_notices() { |
| 206 | $messages = array( |
| 207 | 'updated' => __( '<strong>Menu Icons Settings</strong> have been successfully updated.', 'menu-icons' ), |
| 208 | 'reset' => __( '<strong>Menu Icons Settings</strong> have been successfully reset.', 'menu-icons' ), |
| 209 | ); |
| 210 | |
| 211 | $message_type = get_transient( self::TRANSIENT_KEY ); |
| 212 | if ( ! empty( $message_type ) && ! empty( $messages[ $message_type ] ) ) { |
| 213 | printf( |
| 214 | '<div class="updated"><p>%s</p></div>', |
| 215 | wp_kses( $messages[ $message_type ], array( 'strong' => true ) ) |
| 216 | ); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /** |
| 222 | * Settings meta box |
| 223 | * |
| 224 | * @since 0.3.0 |
| 225 | * @access private |
| 226 | */ |
| 227 | private static function _add_settings_meta_box() { |
| 228 | add_meta_box( |
| 229 | 'menu-icons-settings', |
| 230 | __( 'Menu Icons Settings', 'menu-icons' ), |
| 231 | array( __CLASS__, '_meta_box' ), |
| 232 | 'nav-menus', |
| 233 | 'side', |
| 234 | 'low', |
| 235 | array() |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /** |
| 241 | * Get ID of nav menu being edited |
| 242 | * |
| 243 | * @since %ver |
| 244 | * @return int |
| 245 | */ |
| 246 | public static function get_current_menu_id() { |
| 247 | global $nav_menu_selected_id; |
| 248 | |
| 249 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX && ! empty( $_POST['menu'] ) ) { |
| 250 | $menu_id = absint( $_POST['menu'] ); |
| 251 | } |
| 252 | else { |
| 253 | $menu_id = $nav_menu_selected_id; |
| 254 | } |
| 255 | |
| 256 | return $menu_id; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /** |
| 261 | * Get settings fields |
| 262 | * |
| 263 | * @since 0.4.0 |
| 264 | * @param array $values Values to be applied to each field |
| 265 | * @uses apply_filters() Calls 'menu_icons_settings_fields'. |
| 266 | * @return array |
| 267 | */ |
| 268 | public static function get_settings_fields( Array $values = array() ) { |
| 269 | $fields = array( |
| 270 | 'hide_label' => array( |
| 271 | 'id' => 'hide_label', |
| 272 | 'type' => 'select', |
| 273 | 'label' => __( 'Hide Label', 'menu-icons' ), |
| 274 | 'default' => '', |
| 275 | 'choices' => array( |
| 276 | array( |
| 277 | 'value' => '', |
| 278 | 'label' => __( 'No', 'menu-icons' ), |
| 279 | ), |
| 280 | array( |
| 281 | 'value' => '1', |
| 282 | 'label' => __( 'Yes', 'menu-icons' ), |
| 283 | ), |
| 284 | ), |
| 285 | ), |
| 286 | 'position' => array( |
| 287 | 'id' => 'position', |
| 288 | 'type' => 'select', |
| 289 | 'label' => __( 'Position', 'menu-icons' ), |
| 290 | 'default' => 'before', |
| 291 | 'choices' => array( |
| 292 | array( |
| 293 | 'value' => 'before', |
| 294 | 'label' => __( 'Before', 'menu-icons' ), |
| 295 | ), |
| 296 | array( |
| 297 | 'value' => 'after', |
| 298 | 'label' => __( 'After', 'menu-icons' ), |
| 299 | ), |
| 300 | ), |
| 301 | ), |
| 302 | ); |
| 303 | |
| 304 | $fields = apply_filters( 'menu_icons_settings_fields', $fields ); |
| 305 | |
| 306 | foreach ( $fields as &$field ) { |
| 307 | if ( isset( $values[ $field['id'] ] ) ) { |
| 308 | $field['value'] = $values[ $field['id'] ]; |
| 309 | } |
| 310 | |
| 311 | if ( ! isset( $field['value'] ) && isset( $field['default'] ) ) { |
| 312 | $field['value'] = $field['default']; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | unset( $field ); |
| 317 | |
| 318 | return $fields; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /** |
| 323 | * Get settings sections |
| 324 | * |
| 325 | * @since 0.3.0 |
| 326 | * @uses apply_filters() Calls 'menu_icons_settings_sections'. |
| 327 | * @return array |
| 328 | */ |
| 329 | public static function get_fields() { |
| 330 | $menu_id = self::get_current_menu_id(); |
| 331 | $icon_types = array(); |
| 332 | foreach ( Menu_Icons::get( 'icon_types' ) as $id => $props ) { |
| 333 | $icon_types[ $id ] = $props['label']; |
| 334 | } |
| 335 | |
| 336 | $sections = array( |
| 337 | 'global' => array( |
| 338 | 'id' => 'global', |
| 339 | 'title' => __( 'Global', 'menu-icons' ), |
| 340 | 'description' => __( 'Global settings', 'menu-icons' ), |
| 341 | 'fields' => array( |
| 342 | array( |
| 343 | 'id' => 'icon_types', |
| 344 | 'type' => 'checkbox', |
| 345 | 'label' => __( 'Icon Types', 'menu-icons' ), |
| 346 | 'choices' => $icon_types, |
| 347 | 'value' => self::get( 'global', 'icon_types' ), |
| 348 | ), |
| 349 | ), |
| 350 | 'args' => array(), |
| 351 | ), |
| 352 | ); |
| 353 | |
| 354 | if ( ! empty( $menu_id ) ) { |
| 355 | $menu_term = get_term( $menu_id, 'nav_menu' ); |
| 356 | $menu_key = sprintf( 'menu_%d', $menu_id ); |
| 357 | $menu_settings = self::get_menu_settings( $menu_id ); |
| 358 | |
| 359 | $sections['menu'] = array( |
| 360 | 'id' => $menu_key, |
| 361 | 'title' => __( 'Current Menu', 'menu-icons' ), |
| 362 | 'description' => sprintf( |
| 363 | __( '"%s" menu settings', 'menu-icons' ), |
| 364 | apply_filters( 'single_term_title', $menu_term->name ) |
| 365 | ), |
| 366 | 'fields' => self::get_settings_fields( $menu_settings ), |
| 367 | 'args' => array( 'inline_description' => true ), |
| 368 | ); |
| 369 | } |
| 370 | |
| 371 | return apply_filters( 'menu_icons_settings_sections', $sections, $menu_id ); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | /** |
| 376 | * Get processed settings fields |
| 377 | * |
| 378 | * @since 0.3.0 |
| 379 | * @access private |
| 380 | * @return array |
| 381 | */ |
| 382 | private static function _get_fields() { |
| 383 | require_once Menu_Icons::get( 'dir' ) . 'includes/library/form-fields.php'; |
| 384 | |
| 385 | $keys = array( 'menu-icons', 'settings' ); |
| 386 | $sections = self::get_fields(); |
| 387 | |
| 388 | foreach ( $sections as &$section ) { |
| 389 | $_keys = array_merge( $keys, array( $section['id'] ) ); |
| 390 | $_args = array_merge( array( 'keys' => $_keys ), $section['args'] ); |
| 391 | |
| 392 | foreach ( $section['fields'] as &$field ) { |
| 393 | $field = Kucrut_Form_Field::create( $field, $_args ); |
| 394 | } |
| 395 | |
| 396 | unset( $field ); |
| 397 | } |
| 398 | |
| 399 | unset( $section ); |
| 400 | |
| 401 | return $sections; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | /** |
| 406 | * Settings meta box |
| 407 | * |
| 408 | * @since 0.3.0 |
| 409 | */ |
| 410 | public static function _meta_box() { |
| 411 | ?> |
| 412 | <div class="taxonomydiv"> |
| 413 | <ul id="menu-icons-settings-tabs" class="taxonomy-tabs add-menu-item-tabs hide-if-no-js"> |
| 414 | <?php foreach ( self::get_fields() as $section ) : ?> |
| 415 | <?php printf( |
| 416 | '<li><a href="#" title="%s" class="mi-settings-nav-tab" data-type="menu-icons-settings-%s">%s</a></li>', |
| 417 | esc_attr( $section['description'] ), |
| 418 | esc_attr( $section['id'] ), |
| 419 | esc_html( $section['title'] ) |
| 420 | ) ?> |
| 421 | <?php endforeach ?> |
| 422 | </ul> |
| 423 | <?php foreach ( self::_get_fields() as $section_index => $section ) : ?> |
| 424 | <div id="menu-icons-settings-<?php echo esc_attr( $section['id'] ) ?>" class="tabs-panel _<?php echo esc_attr( $section_index ) ?>"> |
| 425 | <h4 class="hide-if-js"><?php echo esc_html( $section['title'] ) ?></h4> |
| 426 | <?php foreach ( $section['fields'] as $field ) : ?> |
| 427 | <div class="_field"> |
| 428 | <?php printf( |
| 429 | '<label for="%s" class="_main">%s</label>', |
| 430 | esc_attr( $field->id ), |
| 431 | esc_html( $field->label ) |
| 432 | ) ?> |
| 433 | <?php $field->render() ?> |
| 434 | </div> |
| 435 | <?php endforeach; ?> |
| 436 | </div> |
| 437 | <?php endforeach; ?> |
| 438 | </div> |
| 439 | <p class="submitbox button-controls"> |
| 440 | <?php wp_nonce_field( self::UPDATE_KEY, self::UPDATE_KEY ) ?> |
| 441 | <span class="list-controls"> |
| 442 | <?php printf( |
| 443 | '<a href="%s" title="%s" class="select-all submitdelete">%s</a>', |
| 444 | esc_url( |
| 445 | wp_nonce_url( |
| 446 | admin_url( '/nav-menus.php' ), |
| 447 | self::RESET_KEY, |
| 448 | self::RESET_KEY |
| 449 | ) |
| 450 | ), |
| 451 | esc_attr__( 'Discard all changes and reset to default state', 'menu-icons' ), |
| 452 | esc_html__( 'Reset', 'menu-icons' ) |
| 453 | ) ?> |
| 454 | </span> |
| 455 | |
| 456 | <span class="add-to-menu"> |
| 457 | <?php submit_button( |
| 458 | __( 'Save Settings', 'menu-icons' ), |
| 459 | 'secondary', |
| 460 | 'menu-item-settings-save', |
| 461 | false |
| 462 | ) ?> |
| 463 | </span> |
| 464 | </p> |
| 465 | <?php |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /** |
| 470 | * Enqueue scripts & styles for admin page |
| 471 | * |
| 472 | * @since 0.3.0 |
| 473 | * @wp_hook action admin_enqueue_scripts |
| 474 | */ |
| 475 | public static function _enqueue_assets() { |
| 476 | $suffix = Menu_Icons::get_script_suffix(); |
| 477 | |
| 478 | wp_enqueue_style( |
| 479 | 'menu-icons', |
| 480 | Menu_Icons::get( 'url' ) . 'css/admin' . $suffix . '.css', |
| 481 | false, |
| 482 | Menu_Icons::VERSION |
| 483 | ); |
| 484 | wp_register_script( |
| 485 | 'kucrut-jquery-input-dependencies', |
| 486 | Menu_Icons::get( 'url' ) . 'js/input-dependencies' . $suffix . '.js', |
| 487 | array( 'jquery' ), |
| 488 | '0.1.0', |
| 489 | true |
| 490 | ); |
| 491 | |
| 492 | if ( ! empty( self::$settings['global']['icon_types'] ) ) { |
| 493 | wp_enqueue_media(); |
| 494 | } |
| 495 | |
| 496 | wp_enqueue_script( |
| 497 | 'menu-icons', |
| 498 | Menu_Icons::get( 'url' ) . 'js/admin' . $suffix . '.js', |
| 499 | array( 'kucrut-jquery-input-dependencies' ), |
| 500 | Menu_Icons::VERSION, |
| 501 | true |
| 502 | ); |
| 503 | } |
| 504 | } |
| 505 |