-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathMenu_Item_Command.php
More file actions
778 lines (707 loc) · 21.9 KB
/
Menu_Item_Command.php
File metadata and controls
778 lines (707 loc) · 21.9 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
<?php
use WP_CLI\Formatter;
use WP_CLI\Utils;
/**
* List, add, and delete items associated with a menu.
*
* ## EXAMPLES
*
* # Add an existing post to an existing menu
* $ wp menu item add-post sidebar-menu 33 --title="Custom Test Post"
* Success: Menu item added.
*
* # Create a new menu link item
* $ wp menu item add-custom sidebar-menu Apple http://apple.com
* Success: Menu item added.
*
* # Delete menu item
* $ wp menu item delete 45
* Success: Deleted 1 of 1 menu items.
*/
class Menu_Item_Command extends WP_CLI_Command {
protected $obj_fields = [
'db_id',
'type',
'title',
'link',
'position',
];
/**
* Gets a list of items associated with a menu.
*
* ## OPTIONS
*
* <menu>
* : The name, slug, or term ID for the menu.
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* - ids
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each menu item:
*
* * db_id
* * type
* * title
* * link
* * position
*
* These fields are optionally available:
*
* * menu_item_parent
* * object_id
* * object
* * type
* * type_label
* * target
* * attr_title
* * description
* * classes
* * xfn
*
* ## EXAMPLES
*
* $ wp menu item list main-menu
* +-------+-----------+-------------+---------------------------------+----------+
* | db_id | type | title | link | position |
* +-------+-----------+-------------+---------------------------------+----------+
* | 5 | custom | Home | http://example.com | 1 |
* | 6 | post_type | Sample Page | http://example.com/sample-page/ | 2 |
* +-------+-----------+-------------+---------------------------------+----------+
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
$items = wp_get_nav_menu_items( $args[0] );
if ( false === $items ) {
WP_CLI::error( 'Invalid menu.' );
}
// Correct position inconsistency and
// protected `url` param in WP-CLI
$items = array_map(
function ( $item ) {
$item->position = $item->menu_order;
$item->link = $item->url;
return $item;
},
$items
);
if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
$items = array_map(
function ( $item ) {
return $item->db_id;
},
$items
);
}
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( $items );
}
/**
* Gets details about a menu item.
*
* ## OPTIONS
*
* <db-id>
* : Database ID for the menu item.
*
* [--field=<field>]
* : Instead of returning the whole menu item, returns the value of a single field.
*
* [--fields=<fields>]
* : Limit the output to specific fields. Defaults to db_id, type, title, link, position.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields are available:
*
* * db_id
* * type
* * title
* * link
* * position
* * menu_item_parent
* * object_id
* * object
* * type_label
* * target
* * attr_title
* * description
* * classes
* * xfn
*
* ## EXAMPLES
*
* # Get details about a menu item with ID 45
* $ wp menu item get 45
* +-------------+----------------------------------+
* | Field | Value |
* +-------------+----------------------------------+
* | db_id | 45 |
* | type | custom |
* | title | WordPress |
* | link | https://wordpress.org |
* | position | 1 |
* +-------------+----------------------------------+
*
* # Get a specific field from a menu item
* $ wp menu item get 45 --field=title
* WordPress
*
* # Get menu item data in JSON format
* $ wp menu item get 45 --format=json
* {"db_id":45,"type":"custom","title":"WordPress","link":"https://wordpress.org","position":1}
*/
public function get( $args, $assoc_args ) {
$db_id = $args[0];
$menu_item = get_post( $db_id );
if ( ! $menu_item || 'nav_menu_item' !== $menu_item->post_type ) {
WP_CLI::error( 'Invalid menu item.' );
}
/**
* @var object{title: string, url: string, description: string, object: string, object_id: int, menu_item_parent: int, attr_title: string, target: string, classes: string[], xfn: string, type: string, type_label: string, menu_order: int, db_id: int, post_type: string}&\stdClass $menu_item
*/
$menu_item = wp_setup_nav_menu_item( $menu_item );
// Correct position inconsistency and protected `url` param in WP-CLI
$menu_item->position = ( 0 === $menu_item->menu_order ) ? 1 : $menu_item->menu_order;
$menu_item->link = $menu_item->url;
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_item( $menu_item );
}
/**
* Adds a post as a menu item.
*
* ## OPTIONS
*
* <menu>
* : The name, slug, or term ID for the menu.
*
* <post-id>
* : Post ID to add to the menu.
*
* [--title=<title>]
* : Set a custom title for the menu item.
*
* [--link=<link>]
* : Set a custom url for the menu item.
*
* [--description=<description>]
* : Set a custom description for the menu item.
*
* [--attr-title=<attr-title>]
* : Set a custom title attribute for the menu item.
*
* [--target=<target>]
* : Set a custom link target for the menu item.
*
* [--classes=<classes>]
* : Set a custom link classes for the menu item.
*
* [--position=<position>]
* : Specify the position of this menu item.
*
* [--parent-id=<parent-id>]
* : Make this menu item a child of another menu item.
*
* [--porcelain]
* : Output just the new menu item id.
*
* ## EXAMPLES
*
* $ wp menu item add-post sidebar-menu 33 --title="Custom Test Post"
* Success: Menu item added.
*
* @subcommand add-post
*/
public function add_post( $args, $assoc_args ) {
$assoc_args['object-id'] = $args[1];
unset( $args[1] );
$post = get_post( $assoc_args['object-id'] );
if ( ! $post ) {
WP_CLI::error( 'Invalid post.' );
}
$assoc_args['object'] = $post->post_type;
$this->add_or_update_item( 'add', 'post_type', $args, $assoc_args );
}
/**
* Adds a taxonomy term as a menu item.
*
* ## OPTIONS
*
* <menu>
* : The name, slug, or term ID for the menu.
*
* <taxonomy>
* : Taxonomy of the term to be added.
*
* <term-id>
* : Term ID of the term to be added.
*
* [--title=<title>]
* : Set a custom title for the menu item.
*
* [--link=<link>]
* : Set a custom url for the menu item.
*
* [--description=<description>]
* : Set a custom description for the menu item.
*
* [--attr-title=<attr-title>]
* : Set a custom title attribute for the menu item.
*
* [--target=<target>]
* : Set a custom link target for the menu item.
*
* [--classes=<classes>]
* : Set a custom link classes for the menu item.
*
* [--position=<position>]
* : Specify the position of this menu item.
*
* [--parent-id=<parent-id>]
* : Make this menu item a child of another menu item.
*
* [--porcelain]
* : Output just the new menu item id.
*
* ## EXAMPLES
*
* $ wp menu item add-term sidebar-menu post_tag 24
* Success: Menu item added.
*
* @subcommand add-term
*/
public function add_term( $args, $assoc_args ) {
$assoc_args['object'] = $args[1];
unset( $args[1] );
$assoc_args['object-id'] = $args[2];
unset( $args[2] );
if ( ! get_term_by( 'id', $assoc_args['object-id'], $assoc_args['object'] ) ) {
WP_CLI::error( 'Invalid term.' );
}
$this->add_or_update_item( 'add', 'taxonomy', $args, $assoc_args );
}
/**
* Adds a post type archive as a menu item.
*
* ## OPTIONS
*
* <menu>
* : The name, slug, or term ID for the menu.
*
* <post-type>
* : Post type slug.
*
* [--title=<title>]
* : Set a custom title for the menu item.
*
* [--link=<link>]
* : Set a custom url for the menu item.
*
* [--description=<description>]
* : Set a custom description for the menu item.
*
* [--attr-title=<attr-title>]
* : Set a custom title attribute for the menu item.
*
* [--target=<target>]
* : Set a custom link target for the menu item.
*
* [--classes=<classes>]
* : Set a custom link classes for the menu item.
*
* [--position=<position>]
* : Specify the position of this menu item.
*
* [--parent-id=<parent-id>]
* : Make this menu item a child of another menu item.
*
* [--porcelain]
* : Output just the new menu item id.
*
* ## EXAMPLES
*
* $ wp menu item add-post-type-archive sidebar-menu post
* Success: Menu item added.
*
* @subcommand add-post-type-archive
*/
public function add_post_type_archive( $args, $assoc_args ) {
$assoc_args['object'] = $args[1];
unset( $args[1] );
$post_type = $assoc_args['object'];
if ( ! get_post_type_object( $post_type ) ) {
WP_CLI::error( 'Invalid post type.' );
}
if ( false === get_post_type_archive_link( $post_type ) ) {
WP_CLI::error( 'Post type does not have an archive.' );
}
$this->add_or_update_item( 'add', 'post_type_archive', $args, $assoc_args );
}
/**
* Adds a custom menu item.
*
* ## OPTIONS
*
* <menu>
* : The name, slug, or term ID for the menu.
*
* <title>
* : Title for the link.
*
* <link>
* : Target URL for the link.
*
* [--description=<description>]
* : Set a custom description for the menu item.
*
* [--attr-title=<attr-title>]
* : Set a custom title attribute for the menu item.
*
* [--target=<target>]
* : Set a custom link target for the menu item.
*
* [--classes=<classes>]
* : Set a custom link classes for the menu item.
*
* [--position=<position>]
* : Specify the position of this menu item.
*
* [--parent-id=<parent-id>]
* : Make this menu item a child of another menu item.
*
* [--porcelain]
* : Output just the new menu item id.
*
* ## EXAMPLES
*
* $ wp menu item add-custom sidebar-menu Apple http://apple.com
* Success: Menu item added.
*
* @subcommand add-custom
*/
public function add_custom( $args, $assoc_args ) {
$assoc_args['title'] = $args[1];
unset( $args[1] );
$assoc_args['link'] = $args[2];
unset( $args[2] );
$this->add_or_update_item( 'add', 'custom', $args, $assoc_args );
}
/**
* Updates a menu item.
*
* ## OPTIONS
*
* <db-id>
* : Database ID for the menu item.
*
* [--title=<title>]
* : Set a custom title for the menu item.
*
* [--link=<link>]
* : Set a custom url for the menu item.
*
* [--description=<description>]
* : Set a custom description for the menu item.
*
* [--attr-title=<attr-title>]
* : Set a custom title attribute for the menu item.
*
* [--target=<target>]
* : Set a custom link target for the menu item.
*
* [--classes=<classes>]
* : Set a custom link classes for the menu item.
*
* [--position=<position>]
* : Specify the position of this menu item.
*
* [--parent-id=<parent-id>]
* : Make this menu item a child of another menu item.
*
* ## EXAMPLES
*
* $ wp menu item update 45 --title=WordPress --link='http://wordpress.org' --target=_blank --position=2
* Success: Menu item updated.
*
* @subcommand update
*/
public function update( $args, $assoc_args ) {
// Shuffle the position of these.
$args[1] = $args[0];
$terms = get_the_terms( $args[1], 'nav_menu' );
if ( $terms && ! is_wp_error( $terms ) ) {
$args[0] = (int) $terms[0]->term_id;
} else {
$args[0] = 0;
}
$type = get_post_meta( $args[1], '_menu_item_type', true );
$this->add_or_update_item( 'update', $type, $args, $assoc_args );
}
/**
* Deletes one or more items from a menu.
*
* ## OPTIONS
*
* <db-id>...
* : Database ID for the menu item(s).
*
* ## EXAMPLES
*
* $ wp menu item delete 45
* Success: Deleted 1 of 1 menu items.
*
* @subcommand delete
*/
public function delete( $args, $assoc_args ) {
global $wpdb;
$count = 0;
$errors = 0;
foreach ( $args as $arg ) {
$post = get_post( $arg );
$menu_term = get_the_terms( $arg, 'nav_menu' );
// @phpstan-ignore cast.int
$parent_menu_id = (int) get_post_meta( $arg, '_menu_item_menu_item_parent', true );
$result = wp_delete_post( $arg, true );
if ( ! $result ) {
WP_CLI::warning( "Couldn't delete menu item {$arg}." );
++$errors;
} else {
if ( is_array( $menu_term ) && ! empty( $menu_term ) && $post ) {
$this->reorder_menu_items( $menu_term[0]->term_id, $post->menu_order, -1, 0 );
}
if ( $parent_menu_id ) {
$children = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_menu_item_menu_item_parent' AND meta_value=%s", (int) $arg ) );
if ( $children ) {
$children_query = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = %d WHERE meta_key = '_menu_item_menu_item_parent' AND meta_value=%s", $parent_menu_id, (int) $arg );
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $children_query is already prepared above.
$wpdb->query( $children_query );
foreach ( $children as $child ) {
clean_post_cache( $child );
}
}
}
}
// phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual -- Will increase count for non existent menu.
if ( false != $result ) {
++$count;
}
}
Utils\report_batch_operation_results( 'menu item', 'delete', count( $args ), $count, $errors );
}
/**
* Worker method to create new items or update existing ones.
*/
private function add_or_update_item( $method, $type, $args, $assoc_args ) {
$menu = $args[0];
$menu_item_db_id = $args[1] ?? 0;
$menu = wp_get_nav_menu_object( $menu );
if ( false === $menu ) {
WP_CLI::error( 'Invalid menu.' );
}
// `url` is protected in WP-CLI, so we use `link` instead
$assoc_args['url'] = Utils\get_flag_value( $assoc_args, 'link' );
// Need to persist the menu item data. See https://core.trac.wordpress.org/ticket/28138
if ( 'update' === $method ) {
$menu_item_obj = get_post( $menu_item_db_id );
if ( ! $menu_item_obj ) {
WP_CLI::error( 'Invalid menu.' );
}
/**
* @var object{title: string, url: string, description: string, object: string, object_id: int, menu_item_parent: int, attr_title: string, target: string, classes: string[], xfn: string, post_status: string, menu_order: int} $menu_item_obj
*/
$menu_item_obj = wp_setup_nav_menu_item( $menu_item_obj );
// Correct the menu position if this was the first item. See https://core.trac.wordpress.org/ticket/28140
$position = ( 0 === $menu_item_obj->menu_order ) ? 1 : $menu_item_obj->menu_order;
$default_args = [
'position' => $position,
'title' => $menu_item_obj->title,
'url' => $menu_item_obj->url,
'description' => $menu_item_obj->description,
'object' => $menu_item_obj->object,
'object-id' => $menu_item_obj->object_id,
'parent-id' => $menu_item_obj->menu_item_parent,
'attr-title' => $menu_item_obj->attr_title,
'target' => $menu_item_obj->target,
'classes' => implode( ' ', $menu_item_obj->classes ), // stored in the database as array
'xfn' => $menu_item_obj->xfn,
'status' => $menu_item_obj->post_status,
];
} else {
$default_args = [
'position' => 0,
'title' => '',
'url' => '',
'description' => '',
'object' => '',
'object-id' => 0,
'parent-id' => 0,
'attr-title' => '',
'target' => '',
'classes' => '',
'xfn' => '',
// Core oddly defaults to 'draft' for create,
// and 'publish' for update
// Easiest to always work with publish
'status' => 'publish',
];
}
$menu_item_args = [];
foreach ( $default_args as $key => $default_value ) {
// wp_update_nav_menu_item() has a weird argument prefix
$new_key = 'menu-item-' . $key;
$menu_item_args[ $new_key ] = Utils\get_flag_value( $assoc_args, $key, $default_value );
}
$menu_item_args['menu-item-type'] = $type;
$pending_menu_order_updates = [];
// Reorder other menu items when the position changes on update.
if ( 'update' === $method ) {
$new_position = (int) $menu_item_args['menu-item-position'];
if ( $new_position > 0 ) {
// Fetch all menu items sorted by their raw menu_order to determine
// normalized (1-indexed) ranks, since wp_get_nav_menu_items(ARRAY_A)
// normalises menu_order to 1,2,3… which may differ from the raw DB values.
$sorted_item_ids = get_posts(
[
'post_type' => 'nav_menu_item',
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_status' => 'any',
'tax_query' => [
[
'taxonomy' => 'nav_menu',
'field' => 'term_taxonomy_id',
'terms' => $menu->term_taxonomy_id,
],
],
'fields' => 'ids',
]
);
// Normalise to integers so that strict comparisons below work regardless of
// whether $wpdb->get_col() returned strings or integers.
$sorted_item_ids = array_map( 'intval', $sorted_item_ids );
// Clamp the requested position to the valid range of menu items.
$max_position = count( $sorted_item_ids );
if ( $max_position > 0 && $new_position > $max_position ) {
// Treat out-of-range positions as "move to end", consistent with core behavior.
$new_position = $max_position;
}
// Find the 1-indexed normalized rank of the item being moved.
$item_idx = array_search( (int) $menu_item_db_id, $sorted_item_ids, true );
$old_position_normalized = ( false !== $item_idx ) ? $item_idx + 1 : 0;
if ( $old_position_normalized > 0 && $new_position !== $old_position_normalized ) {
if ( $new_position < $old_position_normalized ) {
// Moving up: items at 0-indexed [new_pos-1, old_pos-2] shift down by +1.
for ( $i = $new_position - 1; $i <= $old_position_normalized - 2; $i++ ) {
$pending_menu_order_updates[] = [
'ID' => $sorted_item_ids[ $i ],
'menu_order' => $i + 2,
];
}
} else {
// Moving down: items at 0-indexed [old_pos, new_pos-1] shift up by -1.
for ( $i = $old_position_normalized; $i <= $new_position - 1; $i++ ) {
$pending_menu_order_updates[] = [
'ID' => $sorted_item_ids[ $i ],
'menu_order' => $i,
];
}
}
}
}
}
$result = wp_update_nav_menu_item( $menu->term_id, $menu_item_db_id, $menu_item_args );
if ( is_wp_error( $result ) ) {
WP_CLI::error( $result->get_error_message() );
} elseif ( ! $result ) {
if ( 'add' === $method ) {
WP_CLI::error( "Couldn't add menu item." );
} elseif ( 'update' === $method ) {
WP_CLI::error( "Couldn't update menu item." );
}
} else {
// Apply deferred reordering of other menu items only after a successful update.
if ( ! empty( $pending_menu_order_updates ) ) {
global $wpdb;
$ids_to_update = [];
$case_clauses = '';
foreach ( $pending_menu_order_updates as $update_args ) {
$item_id = (int) $update_args['ID'];
$ids_to_update[] = $item_id;
$case_clauses .= $wpdb->prepare( ' WHEN %d THEN %d', $item_id, $update_args['menu_order'] );
}
$ids_sql = implode( ',', $ids_to_update );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $case_clauses and $ids_sql are constructed from prepared/safe integer values.
$wpdb->query( "UPDATE {$wpdb->posts} SET menu_order = CASE ID {$case_clauses} END WHERE ID IN ({$ids_sql})" );
foreach ( $ids_to_update as $id ) {
clean_post_cache( $id );
}
}
if ( ( 'add' === $method ) && $menu_item_args['menu-item-position'] ) {
$this->reorder_menu_items( $menu->term_id, $menu_item_args['menu-item-position'], +1, $result );
}
/**
* Set the menu
*
* wp_update_nav_menu_item() *should* take care of this, but
* depends on wp_insert_post()'s "tax_input" argument, which
* is ignored if the user can't edit the taxonomy
*
* @see https://core.trac.wordpress.org/ticket/27113
*/
if ( ! is_object_in_term( $result, 'nav_menu', (int) $menu->term_id ) ) {
wp_set_object_terms( $result, [ (int) $menu->term_id ], 'nav_menu' );
}
if ( 'add' === $method && ! empty( $assoc_args['porcelain'] ) ) {
WP_CLI::line( (string) $result );
} elseif ( 'add' === $method ) {
WP_CLI::success( 'Menu item added.' );
} elseif ( 'update' === $method ) {
WP_CLI::success( 'Menu item updated.' );
}
}
}
/**
* Move block of items in one nav_menu up or down by incrementing/decrementing their menu_order field.
* Expects the menu items to have proper menu_orders (i.e. doesn't fix errors from previous incorrect operations).
*
* @param int $menu_id ID of the nav_menu
* @param int $min_position minimal menu_order to touch
* @param int $increment how much to change menu_order: +1 to move down, -1 to move up
* @param int $ignore_item_id menu item that should be ignored by the change (e.g. newly created menu item)
* @return int number of rows affected
*/
private function reorder_menu_items( $menu_id, $min_position, $increment, $ignore_item_id = 0 ) {
global $wpdb;
return $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET `menu_order`=`menu_order`+(%d) WHERE `menu_order`>=%d AND ID IN (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id=%d) AND ID<>%d", (int) $increment, (int) $min_position, (int) $menu_id, (int) $ignore_item_id ) );
}
protected function get_formatter( &$assoc_args ) {
return new Formatter( $assoc_args, $this->obj_fields );
}
}