admin.php
12 years ago
type-dashicons.php
12 years ago
type-fontawesome.php
12 years ago
type-fonts.php
12 years ago
type-genericons.php
12 years ago
type.php
12 years ago
walker-nav-menu-edit.php
12 years ago
admin.php
479 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Menu editor handler |
| 4 | * |
| 5 | * @package Menu_Icons |
| 6 | * @author Dzikri Aziz <kvcrvt@gmail.com> |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | /** |
| 11 | * Menu item metadata |
| 12 | * |
| 13 | */ |
| 14 | final class Menu_Icons_Admin_Nav_Menus { |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * Initialize class |
| 19 | */ |
| 20 | public static function init() { |
| 21 | add_action( 'admin_enqueue_scripts', array( __CLASS__, '_scripts_styles' ) ); |
| 22 | add_action( 'print_media_templates', array( __CLASS__, '_media_templates' ) ); |
| 23 | |
| 24 | add_filter( 'manage_nav-menus_columns', array( __CLASS__, '_columns' ), 99 ); |
| 25 | add_action( 'wp_update_nav_menu_item', array( __CLASS__, '_save' ), 10, 3 ); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Get icon types |
| 31 | * |
| 32 | * @since 0.1.0 |
| 33 | * @access protected |
| 34 | * @uses apply_filters() Calls 'menu_icons_types' on returned array. |
| 35 | * |
| 36 | * @return array |
| 37 | */ |
| 38 | protected static function _get_types() { |
| 39 | $types = array_merge( |
| 40 | array( |
| 41 | '' => array( |
| 42 | 'id' => '', |
| 43 | 'label' => __( '— Select —', 'menu-icons' ) |
| 44 | ), |
| 45 | ), |
| 46 | Menu_Icons::get( 'icon_types' ) |
| 47 | ); |
| 48 | |
| 49 | return $types; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Enqueue scripts & styles on wp-admin/nav-menus.php |
| 55 | * |
| 56 | * @since 0.1.1 |
| 57 | * @access protected |
| 58 | * @wp_hook admin_enqueue_scripts |
| 59 | */ |
| 60 | public static function _scripts_styles() { |
| 61 | // WP 3.8 bug, fixed in 3.9 |
| 62 | // We need to dequeue and re-enqueue this one later, |
| 63 | // otherwise we won't get the dashboard's colors |
| 64 | wp_dequeue_style( 'colors' ); |
| 65 | |
| 66 | $data = array( |
| 67 | 'text' => array( |
| 68 | 'title' => __( 'Select Icon', 'menu-icons' ), |
| 69 | 'select' => __( 'Select', 'menu-icons' ), |
| 70 | 'all' => __( 'All', 'menu-icons' ), |
| 71 | ), |
| 72 | 'base_url' => untrailingslashit( Menu_Icons::get( 'url' ) ), |
| 73 | 'admin_url' => untrailingslashit( admin_url() ), |
| 74 | ); |
| 75 | |
| 76 | $_icon_types = Menu_Icons::get( 'icon_types' ); |
| 77 | $icon_types = array(); |
| 78 | |
| 79 | foreach ( $_icon_types as $id => $props ) { |
| 80 | if ( ! empty( $props['frame_cb'] ) ) { |
| 81 | $icon_types[ $id ] = array( |
| 82 | 'type' => $id, |
| 83 | 'id' => sprintf( 'mi-%s', $id ), |
| 84 | 'title' => $props['label'], |
| 85 | 'data' => call_user_func_array( $props['frame_cb'], array( $id ) ), |
| 86 | ); |
| 87 | Menu_Icons::enqueue_type_stylesheet( $id, $props ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if ( count( $_icon_types ) === count( $icon_types ) ) { |
| 92 | wp_enqueue_media(); |
| 93 | $data['iconTypes'] = $icon_types; |
| 94 | $data['typeNames'] = array_keys( $icon_types ); |
| 95 | } |
| 96 | |
| 97 | wp_enqueue_style( |
| 98 | 'menu-icons', |
| 99 | Menu_Icons::get( 'url' ) . 'css/admin' . Menu_Icons::get_script_suffix() . '.css', |
| 100 | false, |
| 101 | Menu_Icons::VERSION |
| 102 | ); |
| 103 | |
| 104 | // re-enqueue |
| 105 | wp_enqueue_style( 'colors' ); |
| 106 | |
| 107 | wp_register_script( |
| 108 | 'kucrut-jquery-input-dependencies', |
| 109 | Menu_Icons::get( 'url' ) . 'js/input-dependencies' . Menu_Icons::get_script_suffix() . '.js', |
| 110 | array( 'jquery' ), |
| 111 | '0.1.0', |
| 112 | true |
| 113 | ); |
| 114 | // TODO: WHY U NO WANT MINIFY? |
| 115 | wp_enqueue_script( |
| 116 | 'menu-icons', |
| 117 | Menu_Icons::get( 'url' ) . 'js/admin.js', |
| 118 | array( 'kucrut-jquery-input-dependencies' ), |
| 119 | Menu_Icons::VERSION, |
| 120 | true |
| 121 | ); |
| 122 | wp_localize_script( 'menu-icons', 'menuIcons', $data ); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * Get preview |
| 128 | * |
| 129 | * @since 0.2.0 |
| 130 | * @access private |
| 131 | * @param int $id Menu item ID |
| 132 | * @param array $meta_value Menu item meta value |
| 133 | * @return mixed |
| 134 | */ |
| 135 | private static function _get_preview( $id, $meta_value ) { |
| 136 | $text = esc_html__( 'Select', 'menu-icons' ); |
| 137 | if ( empty( $meta_value['type'] ) ) { |
| 138 | return $text; |
| 139 | } |
| 140 | |
| 141 | $type = $meta_value['type']; |
| 142 | $types = self::_get_types(); |
| 143 | if ( empty( $types[ $type ] ) ) { |
| 144 | return $text; |
| 145 | } |
| 146 | |
| 147 | if ( empty( $meta_value[ "{$type}-icon" ] ) ) { |
| 148 | return $text; |
| 149 | } |
| 150 | |
| 151 | if ( empty( $types[ $type ]['preview_cb'] ) |
| 152 | || ! is_callable( $types[ $type ]['preview_cb'] ) |
| 153 | ) { |
| 154 | return $text; |
| 155 | } |
| 156 | |
| 157 | $preview = call_user_func_array( |
| 158 | $types[ $type ]['preview_cb'], |
| 159 | array( $id, $meta_value ) |
| 160 | ); |
| 161 | if ( ! empty( $preview ) ) { |
| 162 | return $preview; |
| 163 | } |
| 164 | |
| 165 | return $text; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * Print fields |
| 171 | * |
| 172 | * @since 0.1.0 |
| 173 | * @access protected |
| 174 | * @uses add_action() Calls 'menu_icons_before_fields' hook |
| 175 | * @uses add_action() Calls 'menu_icons_after_fields' hook |
| 176 | * @wp_hook action menu_item_custom_fields/10/3 |
| 177 | * |
| 178 | * @param object $item Menu item data object. |
| 179 | * @param int $depth Nav menu depth. |
| 180 | * @param array $args Menu item args. |
| 181 | * @param int $id Nav menu ID. |
| 182 | * |
| 183 | * @return string Form fields |
| 184 | */ |
| 185 | public static function _fields( $item, $depth, $args = array(), $id = 0 ) { |
| 186 | $current = array_filter( (array) get_post_meta( $item->ID, 'menu-icons', true ) ); |
| 187 | ?> |
| 188 | <div class="field-icon description-wide menu-icons-wrap"> |
| 189 | <?php |
| 190 | /** |
| 191 | * Allow plugins/themes to inject HTML before menu icons' fields |
| 192 | * |
| 193 | * @param object $item Menu item data object. |
| 194 | * @param int $depth Nav menu depth. |
| 195 | * @param array $args Menu item args. |
| 196 | * @param int $id Nav menu ID. |
| 197 | * |
| 198 | */ |
| 199 | do_action( 'menu_icons_before_fields', $item, $depth, $args, $id ); |
| 200 | ?> |
| 201 | <?php |
| 202 | $input_id = sprintf( 'menu-icons-%d', $item->ID ); |
| 203 | $input_name = sprintf( 'menu-icons[%d]', $item->ID ); |
| 204 | $type_ids = array_values( array_filter( array_keys( self::_get_types() ) ) ); |
| 205 | $choices = array( |
| 206 | 'positions' => array( |
| 207 | 'before' => __( 'Before', 'menu-icons' ), |
| 208 | 'after' => __( 'After', 'menu-icons' ), |
| 209 | ), |
| 210 | 'vertical-align' => array( |
| 211 | '' => __( '– Select –', 'menu-icons' ), |
| 212 | 'super' => __( 'Super', 'menu-icons' ), |
| 213 | 'top' => __( 'Top', 'menu-icons' ), |
| 214 | 'text-top' => __( 'Text Top', 'menu-icons' ), |
| 215 | 'middle' => __( 'Middle', 'menu-icons' ), |
| 216 | 'baseline' => __( 'Baseline', 'menu-icons' ), |
| 217 | 'text-bottom' => __( 'Text Bottom', 'menu-icons' ), |
| 218 | 'bottom' => __( 'Bottom', 'menu-icons' ), |
| 219 | 'sub' => __( 'Sub', 'menu-icons' ), |
| 220 | ), |
| 221 | ); |
| 222 | ?> |
| 223 | <div class="easy"> |
| 224 | <p class="description"> |
| 225 | <label><?php esc_html_e( 'Icon:' ) ?></label> |
| 226 | <?php printf( |
| 227 | '<a id="menu-icons-%1$d-select" class="_select" title="%2$s" data-id="%1$d" data-text="%2$s">%3$s</a>', |
| 228 | esc_attr__( $item->ID ), |
| 229 | esc_attr__( 'Select icon', 'menu-icons' ), |
| 230 | self::_get_preview( $item->ID, $current ) |
| 231 | ) ?> |
| 232 | <?php printf( |
| 233 | '<a id="menu-icons-%1$d-remove" class="_remove hidden" data-id="%1$d">%2$s</a>', |
| 234 | $item->ID, |
| 235 | esc_attr__( 'Remove', 'menu-icons' ) |
| 236 | ) ?> |
| 237 | </p> |
| 238 | </div> |
| 239 | <div class="original hidden"> |
| 240 | <p class="description"> |
| 241 | <label for="<?php echo esc_attr( $input_id ) ?>-type"><?php esc_html_e( 'Icon type', 'menu-icons' ); ?></label> |
| 242 | <?php printf( |
| 243 | '<select id="%s-type" name="%s[type]" class="_type hasdep" data-dep-scope="div.menu-icons-wrap" data-dep-children=".field-icon-child" data-key="type">', |
| 244 | esc_attr( $input_id ), |
| 245 | esc_attr( $input_name ) |
| 246 | ) ?> |
| 247 | <?php foreach ( self::_get_types() as $id => $props ) : ?> |
| 248 | <?php printf( |
| 249 | '<option value="%s"%s>%s</option>', |
| 250 | esc_attr( $id ), |
| 251 | selected( ( isset( $current['type'] ) && $id === $current['type'] ), true, false ), |
| 252 | esc_html( $props['label'] ) |
| 253 | ) ?> |
| 254 | <?php endforeach; ?> |
| 255 | </select> |
| 256 | </p> |
| 257 | <?php foreach ( self::_get_types() as $props ) : ?> |
| 258 | <?php if ( ! empty( $props['field_cb'] ) && is_callable( $props['field_cb'] ) ) : ?> |
| 259 | <?php call_user_func_array( $props['field_cb'], array( $item->ID, $current ) ); ?> |
| 260 | <?php endif; ?> |
| 261 | <?php endforeach; ?> |
| 262 | <p class="description field-icon-child" data-dep-on='<?php echo json_encode( $type_ids ) ?>'> |
| 263 | <label for="<?php echo esc_attr( $input_id ) ?>-position"><?php esc_html_e( 'Position', 'menu-icons' ); ?></label> |
| 264 | <?php printf( |
| 265 | '<select class="_setting" id="%s-position" name="%s[position]" data-key="position">', |
| 266 | esc_attr( $input_id ), |
| 267 | esc_attr( $input_name ) |
| 268 | ) ?> |
| 269 | <?php foreach ( $choices['positions'] as $value => $label ) : ?> |
| 270 | <?php printf( |
| 271 | '<option value="%s"%s>%s</option>', |
| 272 | esc_attr( $value ), |
| 273 | selected( ( isset( $current['position'] ) && $value === $current['position'] ), true, false ), |
| 274 | esc_html( $label ) |
| 275 | ) ?> |
| 276 | <?php endforeach; ?> |
| 277 | </select> |
| 278 | </p> |
| 279 | <p class="description field-icon-child" data-dep-on='<?php echo json_encode( $type_ids ) ?>'> |
| 280 | <label for="<?php echo esc_attr( $input_id ) ?>-size"><?php esc_html_e( 'Size', 'menu-icons' ); ?></label> |
| 281 | <?php printf( |
| 282 | '<input class="_setting" type="text" id="%s-size" name="%s[size]" data-key="size" value="%s">', |
| 283 | esc_attr( $input_id ), |
| 284 | esc_attr( $input_name ), |
| 285 | isset( $current['size'] ) ? $current['size'] : '' |
| 286 | ) ?> |
| 287 | </p> |
| 288 | <p class="description field-icon-child" data-dep-on='<?php echo json_encode( $type_ids ) ?>'> |
| 289 | <label for="<?php echo esc_attr( $input_id ) ?>-vertical-align"><?php esc_html_e( 'Vertical Align', 'menu-icons' ); ?></label> |
| 290 | <?php printf( |
| 291 | '<select class="_setting" id="%s-vertical-align" name="%s[vertical-align]" data-key="vertical-align">', |
| 292 | esc_attr( $input_id ), |
| 293 | esc_attr( $input_name ) |
| 294 | ) ?> |
| 295 | <?php foreach ( $choices['vertical-align'] as $value => $label ) : ?> |
| 296 | <?php printf( |
| 297 | '<option value="%s"%s>%s</option>', |
| 298 | esc_attr( $value ), |
| 299 | selected( ( isset( $current['vertical-align'] ) && $value === $current['vertical-align'] ), true, false ), |
| 300 | esc_html( $label ) |
| 301 | ) ?> |
| 302 | <?php endforeach; ?> |
| 303 | </select> |
| 304 | </p> |
| 305 | </div> |
| 306 | <?php |
| 307 | /** |
| 308 | * Allow plugins/themes to inject HTML after menu icons' fields |
| 309 | * |
| 310 | * @param object $item Menu item data object. |
| 311 | * @param int $depth Nav menu depth. |
| 312 | * @param array $args Menu item args. |
| 313 | * @param int $id Nav menu ID. |
| 314 | * |
| 315 | */ |
| 316 | do_action( 'menu_icons_after_fields', $item, $depth, $args, $id ); |
| 317 | ?> |
| 318 | </div> |
| 319 | <?php |
| 320 | } |
| 321 | |
| 322 | |
| 323 | /** |
| 324 | * Add our field to the screen options toggle |
| 325 | * |
| 326 | * @since 0.1.0 |
| 327 | * @access private |
| 328 | * @wp_hook action manage_nav-menus_columns |
| 329 | * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/manage_posts_columns Action: manage_nav-menus_columns/99 |
| 330 | * |
| 331 | * @param array $columns Menu item columns |
| 332 | * @return array |
| 333 | */ |
| 334 | public static function _columns( $columns ) { |
| 335 | $columns['icon'] = __( 'Icon', 'menu-icons' ); |
| 336 | |
| 337 | return $columns; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | /** |
| 342 | * Save menu item's icons values |
| 343 | * |
| 344 | * @since 0.1.0 |
| 345 | * @access protected |
| 346 | * @uses apply_filters() Calls 'menu_icons_values' on returned array. |
| 347 | * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_update_nav_menu_item Action: wp_update_nav_menu_item/10/2 |
| 348 | * |
| 349 | * @param int $menu_id Nav menu ID |
| 350 | * @param int $menu_item_db_id Menu item ID |
| 351 | * @param array $menu_item_args Menu item data |
| 352 | */ |
| 353 | public static function _save( $menu_id, $menu_item_db_id, $menu_item_args ) { |
| 354 | check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); |
| 355 | |
| 356 | // Sanitize |
| 357 | if ( ! empty( $_POST['menu-icons'][ $menu_item_db_id ] ) ) { |
| 358 | $value = array_filter( (array) $_POST['menu-icons'][ $menu_item_db_id ] ); |
| 359 | } |
| 360 | else { |
| 361 | $value = array(); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Allow plugins/themes to filter the values |
| 366 | * |
| 367 | * @since 0.1.0 |
| 368 | * @param array $value Metadata value |
| 369 | */ |
| 370 | $value = apply_filters( 'menu_icons_values', $value, $menu_item_db_id ); |
| 371 | |
| 372 | // Update |
| 373 | if ( ! empty( $value ) ) { |
| 374 | update_post_meta( $menu_item_db_id, 'menu-icons', $value ); |
| 375 | } |
| 376 | else { |
| 377 | delete_post_meta( $menu_item_db_id, 'menu-icons' ); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | |
| 382 | /** |
| 383 | * Get and print media templates from all types |
| 384 | * |
| 385 | * @since 0.2.0 |
| 386 | * @wp_hook action print_media_templates |
| 387 | */ |
| 388 | public static function _media_templates() { |
| 389 | $id_prefix = 'tmpl-menu-icons'; |
| 390 | |
| 391 | // Common templates |
| 392 | $templates = array( |
| 393 | 'sidebar-title' => sprintf( |
| 394 | '<h3>%s</h3>', |
| 395 | esc_html__( 'Preview', 'menu-icons' ) |
| 396 | ), |
| 397 | 'settings' => sprintf( |
| 398 | '<label class="setting"> |
| 399 | <span>%1$s</span> |
| 400 | <select data-setting="position"> |
| 401 | <option value="before">%2$s</option> |
| 402 | <option value="after">%3$s</option> |
| 403 | </select> |
| 404 | </label> |
| 405 | <label class="setting"> |
| 406 | <span>%4$s</span> |
| 407 | <input type="number" min="0.1" step="0.1" data-setting="size" value="{{ data.size }}" /> |
| 408 | em |
| 409 | </label> |
| 410 | <label class="setting"> |
| 411 | <span>%5$s</span> |
| 412 | <select data-setting="vertical-align"> |
| 413 | <option value="">%6$s</option> |
| 414 | <option value="super">%7$s</option> |
| 415 | <option value="top">%8$s</option> |
| 416 | <option value="text-top">%9$s</option> |
| 417 | <option value="middle">%10$s</option> |
| 418 | <option value="baseline">%11$s</option> |
| 419 | <option value="text-bottom">%12$s</option> |
| 420 | <option value="bottom">%13$s</option> |
| 421 | <option value="sub">%14$s</option> |
| 422 | </select> |
| 423 | </label> |
| 424 | <p class="_info"><em>%15$s</em></p>', |
| 425 | esc_html__( 'Position', 'menu-icons' ), |
| 426 | esc_html__( 'Before', 'menu-icons' ), |
| 427 | esc_html__( 'After', 'menu-icons' ), |
| 428 | esc_html__( 'Size', 'menu-icons' ), |
| 429 | esc_html__( 'Vertical Align', 'menu-icons' ), |
| 430 | esc_html__( '– Select –', 'menu-icons' ), |
| 431 | esc_html__( 'Super', 'menu-icons' ), |
| 432 | esc_html__( 'Top', 'menu-icons' ), |
| 433 | esc_html__( 'Text Top', 'menu-icons' ), |
| 434 | esc_html__( 'Middle', 'menu-icons' ), |
| 435 | esc_html__( 'Baseline', 'menu-icons' ), |
| 436 | esc_html__( 'Bottom', 'menu-icons' ), |
| 437 | esc_html__( 'Text Bottom', 'menu-icons' ), |
| 438 | esc_html__( 'Sub', 'menu-icons' ), |
| 439 | sprintf( |
| 440 | 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' ), |
| 441 | '<a target="_blank" href="http://wordpress.org/plugins/simple-custom-css/">Simple Custom CSS</a>' |
| 442 | ) |
| 443 | ), |
| 444 | ); |
| 445 | $templates = apply_filters( 'menu_icons_media_templates', $templates ); |
| 446 | |
| 447 | foreach ( $templates as $key => $template ) { |
| 448 | $id = sprintf( '%s-%s', $id_prefix, $key ); |
| 449 | self::_print_tempate( $id, $template ); |
| 450 | } |
| 451 | |
| 452 | // Icon type templates |
| 453 | foreach ( self::_get_types() as $type => $props ) { |
| 454 | if ( ! empty( $props['templates'] ) ) { |
| 455 | foreach ( $props['templates'] as $key => $template ) { |
| 456 | $id = sprintf( '%s-%s-%s', $id_prefix, $type, $key ); |
| 457 | self::_print_tempate( $id, $template ); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | |
| 464 | /** |
| 465 | * Print media template |
| 466 | * |
| 467 | * @since 0.2.0 |
| 468 | * @param string $id Template ID |
| 469 | * @param string $template Media template HTML |
| 470 | */ |
| 471 | protected static function _print_tempate( $id, $template ) { |
| 472 | ?> |
| 473 | <script type="text/html" id="<?php echo esc_attr( $id ) ?>"> |
| 474 | <?php echo $template // xss ok ?> |
| 475 | </script> |
| 476 | <?php |
| 477 | } |
| 478 | } |
| 479 |