2121 */
2222class Menu_Item_Command extends WP_CLI_Command {
2323
24- protected $ obj_fields = array (
24+ protected $ obj_fields = [
2525 'db_id ' ,
2626 'type ' ,
2727 'title ' ,
2828 'link ' ,
2929 'position ' ,
30- ) ;
30+ ] ;
3131
3232 /**
3333 * Gets a list of items associated with a menu.
@@ -92,21 +92,27 @@ public function list_( $args, $assoc_args ) {
9292
9393 $ items = wp_get_nav_menu_items ( $ args [0 ] );
9494 if ( false === $ items || is_wp_error ( $ items ) ) {
95- WP_CLI ::error ( " Invalid menu. " );
95+ WP_CLI ::error ( ' Invalid menu. ' );
9696 }
9797
9898 // Correct position inconsistency and
9999 // protected `url` param in WP-CLI
100- $ items = array_map ( function ( $ item ) use ( $ assoc_args ) {
101- $ item ->position = $ item ->menu_order ;
102- $ item ->link = $ item ->url ;
103- return $ item ;
104- }, $ items );
105-
106- if ( ! empty ( $ assoc_args ['format ' ] ) && 'ids ' == $ assoc_args ['format ' ] ) {
107- $ items = array_map ( function ( $ item ) {
108- return $ item ->db_id ;
109- }, $ items );
100+ $ items = array_map (
101+ function ( $ item ) use ( $ assoc_args ) {
102+ $ item ->position = $ item ->menu_order ;
103+ $ item ->link = $ item ->url ;
104+ return $ item ;
105+ },
106+ $ items
107+ );
108+
109+ if ( ! empty ( $ assoc_args ['format ' ] ) && 'ids ' === $ assoc_args ['format ' ] ) {
110+ $ items = array_map (
111+ function ( $ item ) {
112+ return $ item ->db_id ;
113+ },
114+ $ items
115+ );
110116 }
111117
112118 $ formatter = $ this ->get_formatter ( $ assoc_args );
@@ -165,7 +171,7 @@ public function add_post( $args, $assoc_args ) {
165171 unset( $ args [1 ] );
166172 $ post = get_post ( $ assoc_args ['object-id ' ] );
167173 if ( ! $ post ) {
168- WP_CLI ::error ( " Invalid post. " );
174+ WP_CLI ::error ( ' Invalid post. ' );
169175 }
170176 $ assoc_args ['object ' ] = $ post ->post_type ;
171177
@@ -228,7 +234,7 @@ public function add_term( $args, $assoc_args ) {
228234 unset( $ args [2 ] );
229235
230236 if ( ! get_term_by ( 'id ' , $ assoc_args ['object-id ' ], $ assoc_args ['object ' ] ) ) {
231- WP_CLI ::error ( " Invalid term. " );
237+ WP_CLI ::error ( ' Invalid term. ' );
232238 }
233239
234240 $ this ->add_or_update_item ( 'add ' , 'taxonomy ' , $ args , $ assoc_args );
@@ -328,9 +334,9 @@ public function update( $args, $assoc_args ) {
328334
329335 // Shuffle the position of these.
330336 $ args [1 ] = $ args [0 ];
331- $ terms = get_the_terms ( $ args [1 ], 'nav_menu ' );
337+ $ terms = get_the_terms ( $ args [1 ], 'nav_menu ' );
332338 if ( $ terms && ! is_wp_error ( $ terms ) ) {
333- $ args [0 ] = (int )$ terms [0 ]->term_id ;
339+ $ args [0 ] = (int ) $ terms [0 ]->term_id ;
334340 } else {
335341 $ args [0 ] = 0 ;
336342 }
@@ -357,30 +363,32 @@ public function update( $args, $assoc_args ) {
357363 public function delete ( $ args , $ _ ) {
358364 global $ wpdb ;
359365
360- $ count = $ errors = 0 ;
366+ $ count = 0 ;
367+ $ errors = 0 ;
361368
362- foreach ( $ args as $ arg ) {
369+ foreach ( $ args as $ arg ) {
363370
364371 $ parent_menu_id = (int ) get_post_meta ( $ arg , '_menu_item_menu_item_parent ' , true );
365- $ ret = wp_delete_post ( $ arg , true );
372+ $ ret = wp_delete_post ( $ arg , true );
366373 if ( ! $ ret ) {
367374 WP_CLI ::warning ( "Couldn't delete menu item {$ arg }. " );
368375 $ errors ++;
369- } else if ( $ parent_menu_id ) {
376+ } elseif ( $ parent_menu_id ) {
370377 $ 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 ) );
371378 if ( $ children ) {
372379 $ 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 );
380+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $children_query is already prepared above.
373381 $ wpdb ->query ( $ children_query );
374- foreach ( $ children as $ child ) {
382+ foreach ( $ children as $ child ) {
375383 clean_post_cache ( $ child );
376384 }
377385 }
378386 }
379387
388+ // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Will increase count for non existent menu.
380389 if ( false != $ ret ) {
381390 $ count ++;
382391 }
383-
384392 }
385393
386394 Utils \report_batch_operation_results ( 'menu item ' , 'delete ' , count ( $ args ), $ count , $ errors );
@@ -391,79 +399,79 @@ public function delete( $args, $_ ) {
391399 */
392400 private function add_or_update_item ( $ method , $ type , $ args , $ assoc_args ) {
393401
394- $ menu = $ args [0 ];
395- $ menu_item_db_id = \ WP_CLI \ Utils \get_flag_value ( $ args , 1 , 0 );
402+ $ menu = $ args [0 ];
403+ $ menu_item_db_id = Utils \get_flag_value ( $ args , 1 , 0 );
396404
397405 $ menu = wp_get_nav_menu_object ( $ menu );
398406 if ( ! $ menu || is_wp_error ( $ menu ) ) {
399- WP_CLI ::error ( " Invalid menu. " );
407+ WP_CLI ::error ( ' Invalid menu. ' );
400408 }
401409
402410 // `url` is protected in WP-CLI, so we use `link` instead
403- $ assoc_args ['url ' ] = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'link ' );
411+ $ assoc_args ['url ' ] = Utils \get_flag_value ( $ assoc_args , 'link ' );
404412
405413 // Need to persist the menu item data. See https://core.trac.wordpress.org/ticket/28138
406- if ( 'update ' == $ method ) {
414+ if ( 'update ' === $ method ) {
407415
408416 $ menu_item_obj = get_post ( $ menu_item_db_id );
409417 $ menu_item_obj = wp_setup_nav_menu_item ( $ menu_item_obj );
410418
411419 // Correct the menu position if this was the first item. See https://core.trac.wordpress.org/ticket/28140
412420 $ position = ( 0 === $ menu_item_obj ->menu_order ) ? 1 : $ menu_item_obj ->menu_order ;
413421
414- $ default_args = array (
415- 'position ' => $ position ,
416- 'title ' => $ menu_item_obj ->title ,
417- 'url ' => $ menu_item_obj ->url ,
418- 'description ' => $ menu_item_obj ->description ,
419- 'object ' => $ menu_item_obj ->object ,
420- 'object-id ' => $ menu_item_obj ->object_id ,
421- 'parent-id ' => $ menu_item_obj ->menu_item_parent ,
422- 'attr-title ' => $ menu_item_obj ->attr_title ,
423- 'target ' => $ menu_item_obj ->target ,
424- 'classes ' => implode ( ' ' , $ menu_item_obj ->classes ), // stored in the database as array
425- 'xfn ' => $ menu_item_obj ->xfn ,
426- 'status ' => $ menu_item_obj ->post_status ,
427- ) ;
422+ $ default_args = [
423+ 'position ' => $ position ,
424+ 'title ' => $ menu_item_obj ->title ,
425+ 'url ' => $ menu_item_obj ->url ,
426+ 'description ' => $ menu_item_obj ->description ,
427+ 'object ' => $ menu_item_obj ->object ,
428+ 'object-id ' => $ menu_item_obj ->object_id ,
429+ 'parent-id ' => $ menu_item_obj ->menu_item_parent ,
430+ 'attr-title ' => $ menu_item_obj ->attr_title ,
431+ 'target ' => $ menu_item_obj ->target ,
432+ 'classes ' => implode ( ' ' , $ menu_item_obj ->classes ), // stored in the database as array
433+ 'xfn ' => $ menu_item_obj ->xfn ,
434+ 'status ' => $ menu_item_obj ->post_status ,
435+ ] ;
428436
429437 } else {
430438
431- $ default_args = array (
432- 'position ' => 0 ,
433- 'title ' => '' ,
434- 'url ' => '' ,
435- 'description ' => '' ,
436- 'object ' => '' ,
437- 'object-id ' => 0 ,
438- 'parent-id ' => 0 ,
439- 'attr-title ' => '' ,
440- 'target ' => '' ,
441- 'classes ' => '' ,
442- 'xfn ' => '' ,
439+ $ default_args = [
440+ 'position ' => 0 ,
441+ 'title ' => '' ,
442+ 'url ' => '' ,
443+ 'description ' => '' ,
444+ 'object ' => '' ,
445+ 'object-id ' => 0 ,
446+ 'parent-id ' => 0 ,
447+ 'attr-title ' => '' ,
448+ 'target ' => '' ,
449+ 'classes ' => '' ,
450+ 'xfn ' => '' ,
443451 // Core oddly defaults to 'draft' for create,
444452 // and 'publish' for update
445453 // Easiest to always work with publish
446- 'status ' => 'publish ' ,
447- ) ;
454+ 'status ' => 'publish ' ,
455+ ] ;
448456
449457 }
450458
451- $ menu_item_args = array () ;
452- foreach ( $ default_args as $ key => $ default_value ) {
459+ $ menu_item_args = [] ;
460+ foreach ( $ default_args as $ key => $ default_value ) {
453461 // wp_update_nav_menu_item() has a weird argument prefix
454- $ new_key = 'menu-item- ' . $ key ;
455- $ menu_item_args [ $ new_key ] = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , $ key , $ default_value );
462+ $ new_key = 'menu-item- ' . $ key ;
463+ $ menu_item_args [ $ new_key ] = Utils \get_flag_value ( $ assoc_args , $ key , $ default_value );
456464 }
457465
458466 $ menu_item_args ['menu-item-type ' ] = $ type ;
459- $ ret = wp_update_nav_menu_item ( $ menu ->term_id , $ menu_item_db_id , $ menu_item_args );
467+ $ ret = wp_update_nav_menu_item ( $ menu ->term_id , $ menu_item_db_id , $ menu_item_args );
460468
461469 if ( is_wp_error ( $ ret ) ) {
462470 WP_CLI ::error ( $ ret ->get_error_message () );
463- } else if ( ! $ ret ) {
464- if ( 'add ' == $ method ) {
471+ } elseif ( ! $ ret ) {
472+ if ( 'add ' === $ method ) {
465473 WP_CLI ::error ( "Couldn't add menu item. " );
466- } else if ( 'update ' == $ method ) {
474+ } elseif ( 'update ' = == $ method ) {
467475 WP_CLI ::error ( "Couldn't update menu item. " );
468476 }
469477 } else {
@@ -478,16 +486,16 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
478486 * @see https://core.trac.wordpress.org/ticket/27113
479487 */
480488 if ( ! is_object_in_term ( $ ret , 'nav_menu ' , (int ) $ menu ->term_id ) ) {
481- wp_set_object_terms ( $ ret , array ( (int )$ menu ->term_id ) , 'nav_menu ' );
489+ wp_set_object_terms ( $ ret , [ (int ) $ menu ->term_id ] , 'nav_menu ' );
482490 }
483491
484- if ( 'add ' == $ method && ! empty ( $ assoc_args ['porcelain ' ] ) ) {
492+ if ( 'add ' === $ method && ! empty ( $ assoc_args ['porcelain ' ] ) ) {
485493 WP_CLI ::line ( $ ret );
486494 } else {
487- if ( 'add ' == $ method ) {
488- WP_CLI ::success ( " Menu item added. " );
489- } else if ( 'update ' == $ method ) {
490- WP_CLI ::success ( " Menu item updated. " );
495+ if ( 'add ' === $ method ) {
496+ WP_CLI ::success ( ' Menu item added. ' );
497+ } elseif ( 'update ' = == $ method ) {
498+ WP_CLI ::success ( ' Menu item updated. ' );
491499 }
492500 }
493501 }
0 commit comments