class.wpcom-json-api-menus-v1-1-endpoint.php
30.4 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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
/**
* Menus abstract endpoint class.
*/
abstract class WPCOM_JSON_API_Menus_Abstract_Endpoint extends WPCOM_JSON_API_Endpoint {
/**
* Switch to blog and validate user.
*
* @param string $site - the site we want to validate.
*
* @return int
*/
protected function switch_to_blog_and_validate_user( $site ) {
$site_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site ) );
if ( is_wp_error( $site_id ) ) {
return $site_id;
}
if ( ! current_user_can( 'edit_theme_options' ) ) {
return new WP_Error( 'unauthorised', 'User cannot edit theme options on this site.', 403 );
}
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
$this->load_theme_functions();
}
return $site_id;
}
/**
* Get the locations of the menus.
*
* @return array[]
*/
protected function get_locations() {
$locations = array();
$menus = get_registered_nav_menus();
if ( ! empty( $menus ) ) {
foreach ( $menus as $name => $description ) {
$locations[] = array(
'name' => $name,
'description' => $description,
);
}
}
$locations = array_merge( $locations, WPCOM_JSON_API_Menus_Widgets::get() );
// Primary (first) location should have defaultState -> default,
// all other locations (including widgets) should have defaultState -> empty.
for ( $i = 0, $l = count( $locations ); $i < $l; $i++ ) {
$locations[ $i ]['defaultState'] = $i ? 'empty' : 'default';
}
return $locations;
}
/**
* Simplify the menus.
*
* @param WP_Term|WP_Term[] $data - the menus we're simplifying.
* @return array|array[] Simplified menu data.
*/
protected function simplify( $data ) {
$simplifier = new WPCOM_JSON_API_Menus_Simplifier( $data );
return $simplifier->translate();
}
/**
* Complexify the menus.
*
* @param array[] $data - the menu data we're complexifying.
* @return array[]|WP_Error Complexified menu data, or WP_Error on error.
*/
protected function complexify( $data ) {
$complexifier = new WPCOM_JSON_API_Menus_Complexify( $data );
return $complexifier->translate();
}
}
/**
* The menu translator class.
*/
abstract class WPCOM_JSON_API_Menus_Translator {
/**
* A string identifying this class.
*
* @var string
*/
protected $filter = '';
/**
* List of filter method names.
*
* Filter methods are passed an array, and return a transformed array or WP_Error.
*
* @var array
*/
protected $filters = array();
/**
* Class constructor.
*
* @param mixed $menus - a menu or list of menus.
*/
public function __construct( $menus ) {
$this->is_single_menu = ! is_array( $menus );
$this->menus = is_array( $menus ) ? $menus : array( $menus );
}
/**
* Translate the menus.
*
* @return array|array[]|WP_Error
*/
public function translate() {
$result = $this->menus;
foreach ( $this->filters as $f ) {
$result = call_user_func( array( $this, $f ), $result );
if ( is_wp_error( $result ) ) {
return $result;
}
}
return $this->maybe_extract( $result );
}
/**
* Return a single menu or an array of menus.
*
* @param array $menus - the menu list.
*
* @return array|array[]
*/
protected function maybe_extract( $menus ) {
return $this->is_single_menu ? $menus[0] : $menus;
}
/**
* See if we need to whitelist and rename.
*
* @param object|array $object - the object (or associative array) we're checking.
* @param array $dict Associative array holding the key whitelist and renaming/casting data.
* Keys are the keys from $object` to preserve. Values are the key to use in the output or an
* assoc where 'name' specifies the output key and 'type' specifies the PHP type to cast the value to.
*
* @return array
*/
public function whitelist_and_rename_with( $object, $dict ) {
$return = array();
foreach ( (array) $object as $k => $v ) {
if ( isset( $dict[ $k ] ) ) {
if ( is_array( $dict[ $k ] ) ) {
settype( $v, $dict[ $k ]['type'] );
$return[ $dict[ $k ]['name'] ] = $v;
} else {
$new_k = $dict[ $k ];
$return[ $new_k ] = $v;
}
}
}
return $return;
}
}
/**
* The simplifier class.
*/
class WPCOM_JSON_API_Menus_Simplifier extends WPCOM_JSON_API_Menus_Translator {
/**
* The simplify translator class.
*
* @var string
*/
protected $filter = 'wpcom_menu_api_translator_simplify';
/**
* The simplify filters.
*
* @var array
*/
protected $filters = array(
'whitelist_and_rename_keys',
'add_locations',
'treeify',
'add_widget_locations',
);
/**
* The menu whitelist.
*
* @var array
*/
protected $menu_whitelist = array(
'term_id' => array(
'name' => 'id',
'type' => 'int',
),
'name' => array(
'name' => 'name',
'type' => 'string',
),
'description' => array(
'name' => 'description',
'type' => 'string',
),
'items' => array(
'name' => 'items',
'type' => 'array',
),
);
/**
* The menu item whitelist.
*
* @var array
*/
protected $menu_item_whitelist = array(
'db_id' => array(
'name' => 'id',
'type' => 'int',
),
'object_id' => array(
'name' => 'content_id',
'type' => 'int',
),
'object' => array(
'name' => 'type',
'type' => 'string',
),
'type' => array(
'name' => 'type_family',
'type' => 'string',
),
'type_label' => array(
'name' => 'type_label',
'type' => 'string',
),
'title' => array(
'name' => 'name',
'type' => 'string',
),
'menu_order' => array(
'name' => 'order',
'type' => 'int',
),
'menu_item_parent' => array(
'name' => 'parent',
'type' => 'int',
),
'url' => array(
'name' => 'url',
'type' => 'string',
),
'target' => array(
'name' => 'link_target',
'type' => 'string',
),
'attr_title' => array(
'name' => 'link_title',
'type' => 'string',
),
'description' => array(
'name' => 'description',
'type' => 'string',
),
'classes' => array(
'name' => 'classes',
'type' => 'array',
),
'xfn' => array(
'name' => 'xfn',
'type' => 'string',
),
);
/**************************
* Filters methods
**************************/
/**
* Treeify the menus.
*
* @param array $menus - the menu list.
*
* @return array
*/
public function treeify( $menus ) {
return array_map( array( $this, 'treeify_menu' ), $menus );
}
/**
* Turn the flat item list into a tree of items.
*
* @param array $menu - the menu.
*
* @return array
*/
protected function treeify_menu( $menu ) {
$indexed_nodes = array();
$tree = array();
foreach ( $menu['items'] as &$item ) {
$indexed_nodes[ $item['id'] ] = &$item;
}
foreach ( $menu['items'] as &$item ) {
if ( $item['parent'] && isset( $indexed_nodes[ $item['parent'] ] ) ) {
$parent_node = &$indexed_nodes[ $item['parent'] ];
if ( ! isset( $parent_node['items'] ) ) {
$parent_node['items'] = array();
}
$parent_node['items'][ $item['order'] ] = &$item;
} else {
$tree[ $item['order'] ] = &$item;
}
unset( $item['order'] );
unset( $item['parent'] );
}
$menu['items'] = $tree;
$this->remove_item_keys( $menu );
return $menu;
}
/**
* Recursively ensure item lists are contiguous.
*
* @param array $item - the item list.
*/
protected function remove_item_keys( &$item ) {
if ( ! isset( $item['items'] ) || ! is_array( $item['items'] ) ) {
return;
}
foreach ( $item['items'] as &$it ) {
$this->remove_item_keys( $it );
}
$item['items'] = array_values( $item['items'] );
}
/**
* Whitelist and rename keys.
*
* @param (object|array)[] $menus - the menu list.
*
* @return array[]
*/
protected function whitelist_and_rename_keys( $menus ) {
$transformed_menus = array();
foreach ( $menus as $menu ) {
$menu = $this->whitelist_and_rename_with( $menu, $this->menu_whitelist );
if ( isset( $menu['items'] ) ) {
foreach ( $menu['items'] as &$item ) {
$item = $this->whitelist_and_rename_with( $item, $this->menu_item_whitelist );
}
}
$transformed_menus[] = $menu;
}
return $transformed_menus;
}
/**
* Add menu locations.
*
* @param array $menus - the menu list.
*
* @return array[]
*/
protected function add_locations( $menus ) {
$menus_with_locations = array();
foreach ( $menus as $menu ) {
$menu['locations'] = array_keys( get_nav_menu_locations(), $menu['id'] ); // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$menus_with_locations[] = $menu;
}
return $menus_with_locations;
}
/**
* Add widget locations.
*
* @param array $menus - the menu list.
*
* @return array[]
*/
protected function add_widget_locations( $menus ) {
$nav_menu_widgets = WPCOM_JSON_API_Menus_Widgets::get();
if ( ! is_array( $nav_menu_widgets ) ) {
return $menus;
}
foreach ( $menus as &$menu ) {
$widget_locations = array();
foreach ( $nav_menu_widgets as $key => $widget ) {
if ( is_array( $widget ) && isset( $widget['nav_menu'] ) &&
$widget['nav_menu'] === $menu['id'] ) {
$widget_locations[] = 'nav_menu_widget-' . $key;
}
}
$menu['locations'] = array_merge( $menu['locations'], $widget_locations );
}
return $menus;
}
}
/**
* Complexify menu class.
*/
class WPCOM_JSON_API_Menus_Complexify extends WPCOM_JSON_API_Menus_Translator {
/**
* The complexify filter.
*
* @var string
*/
protected $filter = 'wpcom_menu_api_translator_complexify';
/**
* The filters.
*
* @var array
*/
protected $filters = array(
'untreeify',
'set_locations',
'whitelist_and_rename_keys',
);
/**
* The menu whitelist.
*
* @var array
*/
protected $menu_whitelist = array(
'id' => 'term_id',
'name' => 'menu-name',
'description' => 'description',
'items' => 'items',
);
/**
* The item whitelist.
*
* @var array
*/
protected $menu_item_whitelist = array(
'id' => 'menu-item-db-id',
'content_id' => 'menu-item-object-id',
'type' => 'menu-item-object',
'type_family' => 'menu-item-type',
'type_label' => 'menu-item-type-label',
'name' => 'menu-item-title',
'order' => 'menu-item-position',
'parent' => 'menu-item-parent-id',
'url' => 'menu-item-url',
'link_target' => 'menu-item-target',
'link_title' => 'menu-item-attr-title',
'status' => 'menu-item-status',
'tmp_id' => 'tmp_id',
'tmp_parent' => 'tmp_parent',
'description' => 'menu-item-description',
'classes' => 'menu-item-classes',
'xfn' => 'menu-item-xfn',
);
/**************************
* Filters methods
**************************/
/**
* Untreeify the menu.
*
* @param array $menus - the list of menus.
*
* @return array[]
*/
public function untreeify( $menus ) {
return array_map( array( $this, 'untreeify_menu' ), $menus );
}
/**
* Convert the tree of menu items to a flat list suitable for the nav_menu APIs.
*
* @param array $menu - the menu we're untreeifying.
*
* @return array
*/
protected function untreeify_menu( $menu ) {
if ( empty( $menu['items'] ) ) {
return $menu;
}
$items_list = array();
$counter = 1;
foreach ( $menu['items'] as &$item ) {
$item['parent'] = 0;
}
$this->untreeify_items( $menu['items'], $items_list, $counter );
$menu['items'] = $items_list;
return $menu;
}
/**
* Recurse the items tree adding each item to a flat list and restoring
* `order` and `parent` fields.
*
* @param array $items item tree.
* @param array $items_list output flat list of items.
* @param int $counter for creating temporary IDs.
*/
protected function untreeify_items( $items, &$items_list, &$counter ) {
foreach ( $items as $index => $item ) {
$item['order'] = $index + 1;
if ( ! isset( $item['id'] ) ) {
$this->set_tmp_id( $item, $counter++ );
}
if ( isset( $item['items'] ) && is_array( $item['items'] ) ) {
foreach ( $item['items'] as &$i ) {
$i['parent'] = $item['id'];
}
$this->untreeify_items( $item['items'], $items_list, $counter );
unset( $item['items'] );
}
$items_list[] = $item;
}
}
/**
* Populate `tmp_id` field for a new item, and `tmp_parent` field
* for all its children, to maintain the hierarchy.
* These fields will be used when creating
* new items with wp_update_nav_menu_item().
*
* @param array $item - the item tree.
* @param string $tmp_id - the tmp ID.
*/
private function set_tmp_id( &$item, $tmp_id ) {
$item['tmp_id'] = $tmp_id;
if ( ! isset( $item['items'] ) || ! is_array( $item['items'] ) ) {
return;
}
foreach ( $item['items'] as &$child ) {
$child['tmp_parent'] = $tmp_id;
}
}
/**
* Whitelist and rename keys.
*
* @param array $menus - the menus.
*
* @return array[]
*/
protected function whitelist_and_rename_keys( $menus ) {
$transformed_menus = array();
foreach ( $menus as $menu ) {
$menu = $this->whitelist_and_rename_with( $menu, $this->menu_whitelist );
if ( isset( $menu['items'] ) ) {
$menu['items'] = array_map( array( $this, 'whitelist_and_rename_item_keys' ), $menu['items'] );
}
$transformed_menus[] = $menu;
}
return $transformed_menus;
}
/**
* Whitelist and rename item keys.
*
* @param array $item - the item.
*
* @return array
*/
protected function whitelist_and_rename_item_keys( $item ) {
$item = $this->implode_array_fields( $item );
$item = $this->whitelist_and_rename_with( $item, $this->menu_item_whitelist );
return $item;
}
/**
* All item fields are set as strings.
*
* @param array $menu_item - the menu item.
* @return array Item with fields imploded.
*/
protected function implode_array_fields( $menu_item ) {
return array_map( array( $this, 'implode_array_field' ), $menu_item );
}
/**
* Implode an array field.
*
* @param mixed $field - the field we're imploding.
*
* @return mixed The imploded string if `$field` was an array, otherwise `$field` unchanged.
*/
protected function implode_array_field( $field ) {
if ( is_array( $field ) ) {
return implode( ' ', $field );
}
return $field;
}
/**
* Set the menu locations.
*
* @param array $menus - the menu list.
*
* @return array[]|WP_Error
*/
protected function set_locations( $menus ) {
foreach ( $menus as $menu ) {
if ( isset( $menu['locations'] ) ) {
if ( true !== $this->locations_are_valid( $menu['locations'] ) ) {
return $this->locations_are_valid( $menu['locations'] );
}
}
}
return array_map( array( $this, 'set_location' ), $menus );
}
/**
* Set the menu locations.
*
* @param array $menu - the menu.
*
* @return array
*/
protected function set_location( $menu ) {
$this->set_menu_at_locations( $menu['locations'], $menu['id'] );
return $menu;
}
/**
* Set the menu at locations.
*
* @param array $locations - the locations.
* @param int $menu_id - the menu ID.
*/
protected function set_menu_at_locations( $locations, $menu_id ) {
$location_map = get_nav_menu_locations();
$this->remove_menu_from_all_locations( $menu_id, $location_map );
if ( is_array( $locations ) ) {
foreach ( $locations as $location ) {
$location_map[ $location ] = $menu_id;
}
}
set_theme_mod( 'nav_menu_locations', $location_map );
$this->set_widget_menu_at_locations( $locations, $menu_id );
}
/**
* Remove from all locations.
*
* @param int $menu_id - the menu ID.
* @param array $location_map - the location map.
*/
protected function remove_menu_from_all_locations( $menu_id, &$location_map ) {
foreach ( get_nav_menu_locations() as $existing_location => $existing_menu_id ) {
if ( $existing_menu_id === $menu_id ) {
unset( $location_map[ $existing_location ] );
}
}
}
/**
* Set widget menu at locations.
*
* @param array $locations - the locations.
* @param int $menu_id - the menu ID.
*/
protected function set_widget_menu_at_locations( $locations, $menu_id ) {
$nav_menu_widgets = get_option( 'widget_nav_menu' );
if ( ! is_array( $nav_menu_widgets ) ) {
return;
}
// Remove menus from all custom menu widget locations
foreach ( $nav_menu_widgets as &$widget ) {
if ( is_array( $widget ) && isset( $widget['nav_menu'] ) && $widget['nav_menu'] === $menu_id ) {
$widget['nav_menu'] = 0;
}
}
if ( is_array( $locations ) ) {
foreach ( $locations as $location ) {
if ( preg_match( '/^nav_menu_widget-(\d+)/', $location, $matches ) ) {
if ( isset( $matches[1] ) ) {
$nav_menu_widgets[ $matches[1] ]['nav_menu'] = $menu_id;
}
}
}
}
update_option( 'widget_nav_menu', $nav_menu_widgets );
}
/**
* Check if the locations are valid.
*
* @param int|array $locations - the location we're checking.
*
* @return bool|WP_Error
*/
protected function locations_are_valid( $locations ) {
if ( is_int( $locations ) ) {
if ( $locations !== 0 ) {
return new WP_Error( 'locations-int', 'Locations int must be 0.', 400 );
} else {
return true;
}
} elseif ( is_array( $locations ) ) {
foreach ( $locations as $location_name ) {
if ( ! $this->location_name_exists( $location_name ) ) {
return new WP_Error(
'locations-array',
sprintf( "Location '%s' does not exist.", $location_name ),
404
);
}
}
return true;
}
return new WP_Error( 'locations', 'Locations must be array or integer.', 400 );
}
/**
* Check if the location name exists.
*
* @param string $location_name - the location name.
*
* @return bool
*/
protected function location_name_exists( $location_name ) {
$widget_location_names = wp_list_pluck( WPCOM_JSON_API_Menus_Widgets::get(), 'name' );
$existing_locations = get_nav_menu_locations();
if ( ! is_array( get_registered_nav_menus() ) ) {
return false;
}
return array_key_exists( $location_name, get_registered_nav_menus() ) ||
array_key_exists( $location_name, $existing_locations ) ||
in_array( $location_name, $widget_location_names, true );
}
}
new WPCOM_JSON_API_Menus_New_Menu_Endpoint(
array(
'method' => 'POST',
'description' => 'Create a new navigation menu.',
'group' => 'menus',
'stat' => 'menus:new-menu',
'path' => '/sites/%s/menus/new',
'path_labels' => array(
'$site' => '(int|string) Site ID or domain',
),
'request_format' => array(
'name' => '(string) Name of menu',
),
'response_format' => array(
'id' => '(int) Newly created menu ID',
),
'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/menus/new',
'example_request_data' => array(
'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
'body' => array(
'name' => 'Menu 1',
),
),
)
);
/**
* New menu endpoint class.
*/
class WPCOM_JSON_API_Menus_New_Menu_Endpoint extends WPCOM_JSON_API_Menus_Abstract_Endpoint {
/**
* The API Callback.
*
* @param string $path - the path.
* @param int $site - the site ID.
*
* @return array|WP_Error
*/
public function callback( $path = '', $site = 0 ) {
$site_id = $this->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site ) );
if ( is_wp_error( $site_id ) ) {
return $site_id;
}
$data = $this->input();
$id = wp_create_nav_menu( $data['name'] );
if ( is_wp_error( $id ) ) {
return $id;
}
return array( 'id' => $id );
}
}
new WPCOM_JSON_API_Menus_Update_Menu_Endpoint(
array(
'method' => 'POST',
'description' => 'Update a navigation menu.',
'group' => 'menus',
'stat' => 'menus:update-menu',
'path' => '/sites/%s/menus/%d',
'path_labels' => array(
'$site' => '(int|string) Site ID or domain',
'$menu_id' => '(int) Menu ID',
),
'request_format' => array(
'name' => '(string) Name of menu',
'items' => '(array) A list of menu item objects.
<br/><br/>
Item objects contain fields relating to that item, e.g. id, type, content_id,
but they can also contain other items objects - this nesting represents parents
and child items in the item tree.',
),
'response_format' => array(
'menu' => '(object) Updated menu object',
),
'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/menus/510604099',
'example_request_data' => array(
'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
'body' => array(
'name' => 'Test Menu',
),
),
)
);
/**
* Update menu endpoint class.
*/
class WPCOM_JSON_API_Menus_Update_Menu_Endpoint extends WPCOM_JSON_API_Menus_Abstract_Endpoint {
/**
* The API Callback.
*
* @param string $path - the path.
* @param int $site - the site ID.
* @param int $menu_id - the menu ID.
*
* @return array|WP_Error
*/
public function callback( $path = '', $site = 0, $menu_id = 0 ) {
$site_id = $this->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site ) );
if ( is_wp_error( $site_id ) ) {
return $site_id;
}
if ( $menu_id <= 0 ) {
return new WP_Error( 'menu-id', 'Menu ID must be greater than 0.', 400 );
}
$data = $this->input( true, false );
$data['id'] = $menu_id;
$data = $this->complexify( array( $data ) );
if ( is_wp_error( $data ) ) {
return $data;
}
$data = $data[0];
// Avoid special-case handling of an unset 'items' field in empty menus
$data['items'] = isset( $data['items'] ) ? $data['items'] : array();
$data = $this->create_new_items( $data, $menu_id );
$result = wp_update_nav_menu_object( $menu_id, array( 'menu-name' => $data['menu-name'] ) );
if ( is_wp_error( $result ) ) {
return $result;
}
$delete_status = $this->delete_items_not_present( $menu_id, $data['items'] );
if ( is_wp_error( $delete_status ) ) {
return $delete_status;
}
foreach ( $data['items'] as $item ) {
$item_id = isset( $item['menu-item-db-id'] ) ? $item['menu-item-db-id'] : 0;
$result = wp_update_nav_menu_item( $menu_id, $item_id, $item );
if ( is_wp_error( $result ) ) {
return $result;
}
}
$items = wp_get_nav_menu_items( $menu_id, array( 'update_post_term_cache' => false ) );
if ( is_wp_error( $items ) ) {
return $items;
}
$menu = wp_get_nav_menu_object( $menu_id );
$menu->items = $items;
return array( 'menu' => $this->simplify( $menu ) );
}
/**
* New items can have a 'tmp_id', allowing them to
* be used as parent items before they have been created.
*
* This function will create items that have a 'tmp_id' set, and
* update any items with a 'tmp_parent' to use the
* newly created item as a parent.
*
* @param array $data - the data we're checking.
* @param int $menu_id - the menu ID.
* @return array `$data` with new item IDs filled in.
*/
public function create_new_items( $data, $menu_id ) {
$tmp_to_actual_ids = array();
foreach ( $data['items'] as &$item ) {
if ( isset( $item['tmp_id'] ) ) {
$actual_id = wp_update_nav_menu_item( $menu_id, 0, $item );
$tmp_to_actual_ids[ $item['tmp_id'] ] = $actual_id;
unset( $item['tmp_id'] );
$item['menu-item-db-id'] = $actual_id;
}
}
foreach ( $data['items'] as &$item ) {
if ( isset( $item['tmp_parent'] ) ) {
$item['menu-item-parent-id'] = $tmp_to_actual_ids[ $item['tmp_parent'] ];
unset( $item['tmp_parent'] );
}
}
return $data;
}
/**
* Remove any existing menu items not present in the supplied array.
* returns wp_error if an item cannot be deleted.
*
* @param int $menu_id - the menu ID.
* @param array $menu_items - the menu items.
*
* @return bool|WP_Error
*/
public function delete_items_not_present( $menu_id, $menu_items ) {
$existing_items = wp_get_nav_menu_items( $menu_id, array( 'update_post_term_cache' => false ) );
if ( ! is_array( $existing_items ) ) {
return true;
}
$existing_ids = wp_list_pluck( $existing_items, 'db_id' );
$ids_to_keep = wp_list_pluck( $menu_items, 'menu-item-db-id' );
$ids_to_remove = array_diff( $existing_ids, $ids_to_keep );
foreach ( $ids_to_remove as $id ) {
if ( false === wp_delete_post( $id, true ) ) {
return new WP_Error(
'menu-item',
sprintf( 'Failed to delete menu item with id: %d.', $id ),
400
);
}
}
return true;
}
}
new WPCOM_JSON_API_Menus_List_Menus_Endpoint(
array(
'method' => 'GET',
'description' => 'Get a list of all navigation menus.',
'group' => 'menus',
'stat' => 'menus:list-menu',
'path' => '/sites/%s/menus',
'path_labels' => array(
'$site' => '(int|string) Site ID or domain',
),
'response_format' => array(
'menus' => '(array) A list of menu objects.<br/><br/>
A menu object contains a name, items, locations, etc.
Check the example response for the full structure.
<br/><br/>
Item objects contain fields relating to that item, e.g. id, type, content_id,
but they can also contain other items objects - this nesting represents parents
and child items in the item tree.',
'locations' => '(array) Locations where menus can be placed. List of objects, one per location.',
),
'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/menus',
'example_request_data' => array(
'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
),
)
);
/**
* List menus endpoint class.
*/
class WPCOM_JSON_API_Menus_List_Menus_Endpoint extends WPCOM_JSON_API_Menus_Abstract_Endpoint {
/**
* The API Callback.
*
* @param string $path - the path.
* @param int $site - the site ID.
*
* @return array|WP_Error
*/
public function callback( $path = '', $site = 0 ) {
$site_id = $this->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site ) );
if ( is_wp_error( $site_id ) ) {
return $site_id;
}
$menus = wp_get_nav_menus( array( 'orderby' => 'term_id' ) );
if ( is_wp_error( $menus ) ) {
return $menus;
}
foreach ( $menus as $m ) {
$items = wp_get_nav_menu_items( $m->term_id, array( 'update_post_term_cache' => false ) );
if ( is_wp_error( $items ) ) {
return $items;
}
$m->items = $items;
}
$menus = $this->simplify( $menus );
if ( is_wp_error( $this->get_locations() ) ) {
return $this->get_locations();
}
return array(
'menus' => $menus,
'locations' => $this->get_locations(),
);
}
}
new WPCOM_JSON_API_Menus_Get_Menu_Endpoint(
array(
'method' => 'GET',
'description' => 'Get a single navigation menu.',
'group' => 'menus',
'stat' => 'menus:get-menu',
'path' => '/sites/%s/menus/%d',
'path_labels' => array(
'$site' => '(int|string) Site ID or domain',
'$menu_id' => '(int) Menu ID',
),
'response_format' => array(
'menu' => '(object) A menu object.<br/><br/>
A menu object contains a name, items, locations, etc.
Check the example response for the full structure.
<br/><br/>
Item objects contain fields relating to that item, e.g. id, type, content_id,
but they can also contain other items objects - this nesting represents parents
and child items in the item tree.',
),
'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/menus/510604099',
'example_request_data' => array(
'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
),
)
);
/**
* Get menu endpoint class.
*/
class WPCOM_JSON_API_Menus_Get_Menu_Endpoint extends WPCOM_JSON_API_Menus_Abstract_Endpoint {
/**
* The API Callback.
*
* @param string $path - the path.
* @param int $site - the site ID.
* @param int $menu_id - the menu ID.
*
* @return array|WP_Error
*/
public function callback( $path = '', $site = 0, $menu_id = 0 ) {
$site_id = $this->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site ) );
if ( is_wp_error( $site_id ) ) {
return $site_id;
}
if ( $menu_id <= 0 ) {
return new WP_Error( 'menu-id', 'Menu ID must be greater than 0.', 400 );
}
$menu = get_term( $menu_id, 'nav_menu' );
if ( is_wp_error( $menu ) ) {
return $menu;
}
$items = wp_get_nav_menu_items( $menu_id, array( 'update_post_term_cache' => false ) );
if ( is_wp_error( $items ) ) {
return $items;
}
$menu->items = $items;
return array( 'menu' => $this->simplify( $menu ) );
}
}
new WPCOM_JSON_API_Menus_Delete_Menu_Endpoint(
array(
'method' => 'POST',
'description' => 'Delete a navigation menu',
'group' => 'menus',
'stat' => 'menus:delete-menu',
'path' => '/sites/%s/menus/%d/delete',
'path_labels' => array(
'$site' => '(int|string) Site ID or domain',
'$menu_id' => '(int) Menu ID',
),
'response_format' => array(
'deleted' => '(bool) Has the menu been deleted?',
),
'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/menus/$menu_id/delete',
'example_request_data' => array(
'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
),
)
);
/**
* Delete menu endpoint class.
*/
class WPCOM_JSON_API_Menus_Delete_Menu_Endpoint extends WPCOM_JSON_API_Menus_Abstract_Endpoint {
/**
* The API Callback.
*
* @param string $path - the path.
* @param int $site - the site ID.
* @param int $menu_id - the menu ID.
*
* @return array|WP_Error
*/
public function callback( $path = '', $site = 0, $menu_id = 0 ) {
$site_id = $this->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site ) );
if ( is_wp_error( $site_id ) ) {
return $site_id;
}
if ( $menu_id <= 0 ) {
return new WP_Error( 'menu-id', 'Menu ID must be greater than 0.', 400 );
}
$result = wp_delete_nav_menu( $menu_id );
if ( ! is_wp_error( $result ) ) {
$result = array( 'deleted' => $result );
}
return $result;
}
}
/**
* API Menus widgets class.
*/
class WPCOM_JSON_API_Menus_Widgets {
/**
* Get the menu locations.
*
* @return array
*/
public static function get() {
$locations = array();
$nav_menu_widgets = get_option( 'widget_nav_menu' );
if ( ! is_array( $nav_menu_widgets ) ) {
return $locations;
}
foreach ( $nav_menu_widgets as $k => $v ) {
if ( is_array( $v ) && isset( $v['title'] ) ) {
$locations[ $k ] = array(
'name' => 'nav_menu_widget-' . $k,
'description' => $v['title'],
);
}
}
return $locations;
}
}