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