PluginProbe
Advanced Custom Fields (ACF®) / 5.8.9
Advanced Custom Fields (ACF®) v5.8.9
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / forms / form-nav-menu.php
form-nav-menu.php
393 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 if( ! class_exists('acf_form_nav_menu') ) :
6
7 class acf_form_nav_menu {
8
9 /*
10 * __construct
11 *
12 * This function will setup the class functionality
13 *
14 * @type function
15 * @date 5/03/2014
16 * @since 5.0.0
17 *
18 * @param n/a
19 * @return n/a
20 */
21
22 function __construct() {
23
24 // actions
25 add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
26 add_action('wp_update_nav_menu', array($this, 'update_nav_menu'));
27 add_action('acf/validate_save_post', array($this, 'acf_validate_save_post'), 5);
28 add_action('wp_nav_menu_item_custom_fields', array($this, 'wp_nav_menu_item_custom_fields'), 10, 5);
29
30 // filters
31 add_filter('wp_get_nav_menu_items', array($this, 'wp_get_nav_menu_items'), 10, 3);
32 add_filter('wp_edit_nav_menu_walker', array($this, 'wp_edit_nav_menu_walker'), 10, 2);
33
34 }
35
36
37 /*
38 * admin_enqueue_scripts
39 *
40 * This action is run after post query but before any admin script / head actions.
41 * It is a good place to register all actions.
42 *
43 * @type action (admin_enqueue_scripts)
44 * @date 26/01/13
45 * @since 3.6.0
46 *
47 * @param N/A
48 * @return N/A
49 */
50
51 function admin_enqueue_scripts() {
52
53 // validate screen
54 if( !acf_is_screen('nav-menus') ) return;
55
56
57 // load acf scripts
58 acf_enqueue_scripts();
59
60
61 // actions
62 add_action('admin_footer', array($this, 'admin_footer'), 1);
63
64 }
65
66
67 /**
68 * wp_nav_menu_item_custom_fields
69 *
70 * description
71 *
72 * @date 30/7/18
73 * @since 5.6.9
74 *
75 * @param type $var Description. Default.
76 * @return type Description.
77 */
78
79 function wp_nav_menu_item_custom_fields( $item_id, $item, $depth, $args, $id = '' ) {
80
81 // vars
82 $prefix = "menu-item-acf[$item_id]";
83
84 // get field groups
85 $field_groups = acf_get_field_groups(array(
86 'nav_menu_item' => $item->type,
87 'nav_menu_item_id' => $item_id,
88 'nav_menu_item_depth' => $depth
89 ));
90
91 // render
92 if( !empty($field_groups) ) {
93
94 // open
95 echo '<div class="acf-menu-item-fields acf-fields -clear">';
96
97 // loop
98 foreach( $field_groups as $field_group ) {
99
100 // load fields
101 $fields = acf_get_fields( $field_group );
102
103 // bail if not fields
104 if( empty($fields) ) continue;
105
106 // change prefix
107 acf_prefix_fields( $fields, $prefix );
108
109 // render
110 acf_render_fields( $fields, $item_id, 'div', $field_group['instruction_placement'] );
111 }
112
113 // close
114 echo '</div>';
115
116 // Trigger append for newly created menu item (via AJAX)
117 if( acf_is_ajax('add-menu-item') ): ?>
118 <script type="text/javascript">
119 (function($) {
120 acf.doAction('append', $('#menu-item-settings-<?php echo $item_id; ?>') );
121 })(jQuery);
122 </script>
123 <?php endif;
124 }
125 }
126
127
128 /*
129 * update_nav_menu
130 *
131 * description
132 *
133 * @type function
134 * @date 26/5/17
135 * @since 5.6.0
136 *
137 * @param $post_id (int)
138 * @return $post_id (int)
139 */
140
141 function update_nav_menu( $menu_id ) {
142
143 // vars
144 $post_id = acf_get_term_post_id( 'nav_menu', $menu_id );
145
146
147 // verify and remove nonce
148 if( !acf_verify_nonce('nav_menu') ) return $menu_id;
149
150
151 // validate and show errors
152 acf_validate_save_post( true );
153
154
155 // save
156 acf_save_post( $post_id );
157
158
159 // save nav menu items
160 $this->update_nav_menu_items( $menu_id );
161
162 }
163
164
165 /*
166 * update_nav_menu_items
167 *
168 * description
169 *
170 * @type function
171 * @date 26/5/17
172 * @since 5.6.0
173 *
174 * @param $post_id (int)
175 * @return $post_id (int)
176 */
177
178 function update_nav_menu_items( $menu_id ) {
179
180 // bail ealry if not set
181 if( empty($_POST['menu-item-acf']) ) return;
182
183
184 // loop
185 foreach( $_POST['menu-item-acf'] as $post_id => $values ) {
186
187 acf_save_post( $post_id, $values );
188
189 }
190
191 }
192
193
194 /**
195 * wp_get_nav_menu_items
196 *
197 * WordPress does not provide an easy way to find the current menu being edited.
198 * This function listens to when a menu's items are loaded and stores the menu.
199 * Needed on nav-menus.php page for new menu with no items
200 *
201 * @date 23/2/18
202 * @since 5.6.9
203 *
204 * @param type $var Description. Default.
205 * @return type Description.
206 */
207
208 function wp_get_nav_menu_items( $items, $menu, $args ) {
209 acf_set_data('nav_menu_id', $menu->term_id);
210 return $items;
211 }
212
213 /**
214 * Called when WP renders a menu edit form.
215 * Used to set global data and customize the Walker class.
216 *
217 * @date 26/5/17
218 * @since 5.6.0
219 *
220 * @param string $class The walker class to use. Default 'Walker_Nav_Menu_Edit'.
221 * @param int $menu_id ID of the menu being rendered.
222 * @return string
223 */
224 function wp_edit_nav_menu_walker( $class, $menu_id = 0 ) {
225
226 // update data (needed for ajax location rules to work)
227 acf_set_data('nav_menu_id', $menu_id);
228
229 // Use custom walker class to inject "wp_nav_menu_item_custom_fields" action prioir to WP 5.4.
230 if( acf_version_compare('wp', '<', '5.3.99') ) {
231 acf_include('includes/walkers/class-acf-walker-nav-menu-edit.php');
232 return 'ACF_Walker_Nav_Menu_Edit';
233 }
234
235 // Return class.
236 return $class;
237 }
238
239
240 /*
241 * acf_validate_save_post
242 *
243 * This function will loop over $_POST data and validate
244 *
245 * @type action 'acf/validate_save_post' 5
246 * @date 7/09/2016
247 * @since 5.4.0
248 *
249 * @param n/a
250 * @return n/a
251 */
252
253 function acf_validate_save_post() {
254
255 // bail ealry if not set
256 if( empty($_POST['menu-item-acf']) ) return;
257
258
259 // loop
260 foreach( $_POST['menu-item-acf'] as $post_id => $values ) {
261
262 // vars
263 $prefix = 'menu-item-acf['.$post_id.']';
264
265
266 // validate
267 acf_validate_values( $values, $prefix );
268
269 }
270
271 }
272
273
274 /*
275 * admin_footer
276 *
277 * This function will add some custom HTML to the footer of the edit page
278 *
279 * @type function
280 * @date 11/06/2014
281 * @since 5.0.0
282 *
283 * @param n/a
284 * @return n/a
285 */
286
287 function admin_footer() {
288
289 // vars
290 $nav_menu_id = acf_get_data('nav_menu_id');
291 $post_id = acf_get_term_post_id( 'nav_menu', $nav_menu_id );
292
293
294 // get field groups
295 $field_groups = acf_get_field_groups(array(
296 'nav_menu' => $nav_menu_id
297 ));
298
299 ?>
300 <div id="tmpl-acf-menu-settings" style="display: none;">
301 <?php
302
303 // data (always needed to save nav menu items)
304 acf_form_data(array(
305 'screen' => 'nav_menu',
306 'post_id' => $post_id,
307 'ajax' => 1
308 ));
309
310
311 // render
312 if( !empty($field_groups) ) {
313
314 // loop
315 foreach( $field_groups as $field_group ) {
316
317 $fields = acf_get_fields( $field_group );
318
319 echo '<div class="acf-menu-settings -'.$field_group['style'].'">';
320
321 echo '<h2>' . $field_group['title'] . '</h2>';
322
323 echo '<div class="acf-fields -left -clear">';
324
325 acf_render_fields( $fields, $post_id, 'div', $field_group['instruction_placement'] );
326
327 echo '</div>';
328
329 echo '</div>';
330
331 }
332
333 }
334
335 ?>
336 </div>
337 <script type="text/javascript">
338 (function($) {
339
340 // append html
341 $('#post-body-content').append( $('#tmpl-acf-menu-settings').html() );
342
343
344 // avoid WP over-writing $_POST data
345 // - https://core.trac.wordpress.org/ticket/41502#ticket
346 $(document).on('submit', '#update-nav-menu', function() {
347
348 // vars
349 var $form = $(this);
350 var $input = $('input[name="nav-menu-data"]');
351
352
353 // decode json
354 var json = $form.serializeArray();
355 var json2 = [];
356
357
358 // loop
359 $.each( json, function( i, pair ) {
360
361 // avoid nesting (unlike WP)
362 if( pair.name === 'nav-menu-data' ) return;
363
364
365 // bail early if is 'acf[' input
366 if( pair.name.indexOf('acf[') > -1 ) return;
367
368
369 // append
370 json2.push( pair );
371
372 });
373
374
375 // update
376 $input.val( JSON.stringify(json2) );
377
378 });
379
380
381 })(jQuery);
382 </script>
383 <?php
384
385 }
386
387 }
388
389 acf_new_instance('acf_form_nav_menu');
390
391 endif;
392
393 ?>