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