library
8 years ago
front.php
8 years ago
media-template.php
8 years ago
meta.php
8 years ago
picker.php
8 years ago
settings.php
8 years ago
type-fonts.php
8 years ago
type.php
8 years ago
picker.php
303 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 | * Nav menu admin |
| 12 | */ |
| 13 | final class Menu_Icons_Picker { |
| 14 | |
| 15 | /** |
| 16 | * Initialize class |
| 17 | * |
| 18 | * @since 0.1.0 |
| 19 | */ |
| 20 | public static function init() { |
| 21 | add_action( 'load-nav-menus.php', array( __CLASS__, '_load_nav_menus' ) ); |
| 22 | add_filter( 'wp_edit_nav_menu_walker', array( __CLASS__, '_filter_wp_edit_nav_menu_walker' ), 99 ); |
| 23 | add_filter( 'wp_nav_menu_item_custom_fields', array( __CLASS__, '_fields' ), 10, 4 ); |
| 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 | add_filter( 'icon_picker_type_props', array( __CLASS__, '_add_extra_type_props_data' ), 10, 3 ); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Load Icon Picker |
| 32 | * |
| 33 | * @since 0.9.0 |
| 34 | * @wp_hook action load-nav-menus.php |
| 35 | */ |
| 36 | public static function _load_nav_menus() { |
| 37 | Icon_Picker::instance()->load(); |
| 38 | |
| 39 | add_action( 'print_media_templates', array( __CLASS__, '_media_templates' ) ); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Custom walker |
| 46 | * |
| 47 | * @since 0.3.0 |
| 48 | * @access protected |
| 49 | * @wp_hook filter wp_edit_nav_menu_walker |
| 50 | */ |
| 51 | public static function _filter_wp_edit_nav_menu_walker( $walker ) { |
| 52 | // Load menu item custom fields plugin |
| 53 | if ( ! class_exists( 'Menu_Item_Custom_Fields_Walker' ) ) { |
| 54 | require_once Menu_Icons::get( 'dir' ) . 'includes/library/menu-item-custom-fields/walker-nav-menu-edit.php'; |
| 55 | } |
| 56 | $walker = 'Menu_Item_Custom_Fields_Walker'; |
| 57 | |
| 58 | return $walker; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Get menu item setting fields |
| 64 | * |
| 65 | * @since 0.9.0 |
| 66 | * @access protected |
| 67 | * @param array $meta Menu item meta value. |
| 68 | * @return array |
| 69 | */ |
| 70 | protected static function _get_menu_item_fields( $meta ) { |
| 71 | $fields = array_merge( |
| 72 | array( |
| 73 | array( |
| 74 | 'id' => 'type', |
| 75 | 'label' => __( 'Type', 'menu-icons' ), |
| 76 | 'value' => $meta['type'], |
| 77 | ), |
| 78 | array( |
| 79 | 'id' => 'icon', |
| 80 | 'label' => __( 'Icon', 'menu-icons' ), |
| 81 | 'value' => $meta['icon'], |
| 82 | ), |
| 83 | ), |
| 84 | Menu_Icons_Settings::get_settings_fields( $meta ) |
| 85 | ); |
| 86 | |
| 87 | return $fields; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * Print fields |
| 93 | * |
| 94 | * @since 0.1.0 |
| 95 | * @access protected |
| 96 | * @uses add_action() Calls 'menu_icons_before_fields' hook |
| 97 | * @uses add_action() Calls 'menu_icons_after_fields' hook |
| 98 | * @wp_hook action menu_item_custom_fields |
| 99 | * |
| 100 | * @param object $item Menu item data object. |
| 101 | * @param int $depth Nav menu depth. |
| 102 | * @param array $args Menu item args. |
| 103 | * @param int $id Nav menu ID. |
| 104 | * |
| 105 | * @return string Form fields |
| 106 | */ |
| 107 | public static function _fields( $id, $item, $depth, $args ) { |
| 108 | $input_id = sprintf( 'menu-icons-%d', $item->ID ); |
| 109 | $input_name = sprintf( 'menu-icons[%d]', $item->ID ); |
| 110 | $menu_settings = Menu_Icons_Settings::get_menu_settings( Menu_Icons_Settings::get_current_menu_id() ); |
| 111 | $meta = Menu_Icons_Meta::get( $item->ID, $menu_settings ); |
| 112 | $fields = self::_get_menu_item_fields( $meta ); |
| 113 | ?> |
| 114 | <div class="field-icon description-wide menu-icons-wrap" data-id="<?php echo json_encode( $item->ID ); ?>"> |
| 115 | <?php |
| 116 | /** |
| 117 | * Allow plugins/themes to inject HTML before menu icons' fields |
| 118 | * |
| 119 | * @param object $item Menu item data object. |
| 120 | * @param int $depth Nav menu depth. |
| 121 | * @param array $args Menu item args. |
| 122 | * @param int $id Nav menu ID. |
| 123 | * |
| 124 | */ |
| 125 | do_action( 'menu_icons_before_fields', $item, $depth, $args, $id ); |
| 126 | ?> |
| 127 | <p class="description submitbox"> |
| 128 | <label><?php esc_html_e( 'Icon:', 'menu-icons' ) ?></label> |
| 129 | <?php printf( '<a class="_select">%s</a>', esc_html__( 'Select', 'menu-icons' ) ); ?> |
| 130 | <?php printf( '<a class="_remove submitdelete hidden">%s</a>', esc_html__( 'Remove', 'menu-icons' ) ); ?> |
| 131 | </p> |
| 132 | <div class="_settings hidden"> |
| 133 | <?php |
| 134 | foreach ( $fields as $field ) { |
| 135 | printf( |
| 136 | '<label>%1$s: <input type="text" name="%2$s" class="_mi-%3$s" value="%4$s" /></label><br />', |
| 137 | esc_html( $field['label'] ), |
| 138 | esc_attr( "{$input_name}[{$field['id']}]" ), |
| 139 | esc_attr( $field['id'] ), |
| 140 | esc_attr( $field['value'] ) |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | // The fields below will not be saved. They're only used for the preview. |
| 145 | printf( '<input type="hidden" class="_mi-url" value="%s" />', esc_attr( $meta['url'] ) ); |
| 146 | ?> |
| 147 | </div> |
| 148 | <?php |
| 149 | /** |
| 150 | * Allow plugins/themes to inject HTML after menu icons' fields |
| 151 | * |
| 152 | * @param object $item Menu item data object. |
| 153 | * @param int $depth Nav menu depth. |
| 154 | * @param array $args Menu item args. |
| 155 | * @param int $id Nav menu ID. |
| 156 | * |
| 157 | */ |
| 158 | do_action( 'menu_icons_after_fields', $item, $depth, $args, $id ); |
| 159 | ?> |
| 160 | </div> |
| 161 | <?php |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /** |
| 166 | * Add our field to the screen options toggle |
| 167 | * |
| 168 | * @since 0.1.0 |
| 169 | * @access private |
| 170 | * @wp_hook action manage_nav-menus_columns |
| 171 | * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/manage_posts_columns |
| 172 | * |
| 173 | * @param array $columns Menu item columns |
| 174 | * |
| 175 | * @return array |
| 176 | */ |
| 177 | public static function _columns( $columns ) { |
| 178 | $columns['icon'] = __( 'Icon', 'menu-icons' ); |
| 179 | |
| 180 | return $columns; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /** |
| 185 | * Save menu item's icons metadata |
| 186 | * |
| 187 | * @since 0.1.0 |
| 188 | * @access protected |
| 189 | * @wp_hook action wp_update_nav_menu_item |
| 190 | * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_update_nav_menu_item |
| 191 | * |
| 192 | * @param int $menu_id Nav menu ID. |
| 193 | * @param int $menu_item_db_id Menu item ID. |
| 194 | * @param array $menu_item_args Menu item data. |
| 195 | */ |
| 196 | public static function _save( $menu_id, $menu_item_db_id, $menu_item_args ) { |
| 197 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | $screen = get_current_screen(); |
| 202 | if ( ! $screen instanceof WP_Screen || 'nav-menus' !== $screen->id ) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' ); |
| 207 | |
| 208 | // Sanitize |
| 209 | if ( ! empty( $_POST['menu-icons'][ $menu_item_db_id ] ) ) { |
| 210 | $value = array_map( |
| 211 | 'sanitize_text_field', |
| 212 | wp_unslash( (array) $_POST['menu-icons'][ $menu_item_db_id ] ) |
| 213 | ); |
| 214 | } else { |
| 215 | $value = array(); |
| 216 | } |
| 217 | |
| 218 | Menu_Icons_Meta::update( $menu_item_db_id, $value ); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * Get and print media templates from all types |
| 224 | * |
| 225 | * @since 0.2.0 |
| 226 | * @since 0.9.0 Deprecate menu_icons_media_templates filter. |
| 227 | * @wp_hook action print_media_templates |
| 228 | */ |
| 229 | public static function _media_templates() { |
| 230 | $id_prefix = 'tmpl-menu-icons'; |
| 231 | |
| 232 | // Deprecated. |
| 233 | $templates = apply_filters( 'menu_icons_media_templates', array() ); |
| 234 | |
| 235 | if ( ! empty( $templates ) ) { |
| 236 | if ( WP_DEBUG ) { |
| 237 | _deprecated_function( 'menu_icons_media_templates', '0.9.0', 'menu_icons_js_templates' ); |
| 238 | } |
| 239 | |
| 240 | foreach ( $templates as $key => $template ) { |
| 241 | $id = sprintf( '%s-%s', $id_prefix, $key ); |
| 242 | self::_print_tempate( $id, $template ); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | require_once dirname( __FILE__ ) . '/media-template.php'; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | /** |
| 251 | * Print media template |
| 252 | * |
| 253 | * @since 0.2.0 |
| 254 | * @param string $id Template ID. |
| 255 | * @param string $template Media template HTML. |
| 256 | */ |
| 257 | protected static function _print_tempate( $id, $template ) { |
| 258 | ?> |
| 259 | <script type="text/html" id="<?php echo esc_attr( $id ) ?>"> |
| 260 | <?php echo $template; // xss ok ?> |
| 261 | </script> |
| 262 | <?php |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * Add extra icon type properties data |
| 268 | * |
| 269 | * @since 0.9.0 |
| 270 | * @wp_hook action icon_picker_type_props |
| 271 | * |
| 272 | * @param array $props Icon type properties. |
| 273 | * @param string $id Icon type ID. |
| 274 | * @param Icon_Picker_Type $type Icon_Picker_Type object. |
| 275 | * |
| 276 | * @return array |
| 277 | */ |
| 278 | public static function _add_extra_type_props_data( $props, $id, $type ) { |
| 279 | $settings_fields = array( |
| 280 | 'hide_label', |
| 281 | 'position', |
| 282 | 'vertical_align', |
| 283 | ); |
| 284 | |
| 285 | if ( 'Font' === $props['controller'] ) { |
| 286 | $settings_fields[] = 'font_size'; |
| 287 | } |
| 288 | |
| 289 | switch ( $id ) { |
| 290 | case 'image': |
| 291 | $settings_fields[] = 'image_size'; |
| 292 | break; |
| 293 | case 'svg': |
| 294 | $settings_fields[] = 'svg_width'; |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | $props['data']['settingsFields'] = $settings_fields; |
| 299 | |
| 300 | return $props; |
| 301 | } |
| 302 | } |
| 303 |