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