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