-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathWidget_Command.php
More file actions
822 lines (706 loc) · 22.5 KB
/
Widget_Command.php
File metadata and controls
822 lines (706 loc) · 22.5 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
<?php
use WP_CLI\Formatter;
use WP_CLI\Traverser\RecursiveDataStructureTraverser;
use WP_CLI\Utils;
/**
* Manages widgets, including adding and moving them within sidebars.
*
* A [widget](https://developer.wordpress.org/themes/functionality/widgets/) adds content and features to a widget area (also called a [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/)).
*
* ## EXAMPLES
*
* # List widgets on a given sidebar
* $ wp widget list sidebar-1
* +----------+------------+----------+----------------------+
* | name | id | position | options |
* +----------+------------+----------+----------------------+
* | meta | meta-6 | 1 | {"title":"Meta"} |
* | calendar | calendar-2 | 2 | {"title":"Calendar"} |
* +----------+------------+----------+----------------------+
*
* # Add a calendar widget to the second position on the sidebar
* $ wp widget add calendar sidebar-1 2
* Success: Added widget to sidebar.
*
* # Update option(s) associated with a given widget
* $ wp widget update calendar-1 --title="Calendar"
* Success: Widget updated.
*
* # Delete one or more widgets entirely
* $ wp widget delete calendar-2 archive-1
* Success: 2 widgets removed from sidebar.
*/
class Widget_Command extends WP_CLI_Command {
/**
* @var string[]
*/
private $fields = [
'name',
'id',
'position',
'options',
];
/**
* Lists widgets associated with a sidebar.
*
* ## OPTIONS
*
* <sidebar-id>
* : ID for the corresponding sidebar.
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - ids
* - json
* - count
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each widget:
*
* * name
* * id
* * position
* * options
*
* There are no optionally available fields.
*
* ## EXAMPLES
*
* $ wp widget list sidebar-1 --fields=name,id --format=csv
* name,id
* meta,meta-5
* search,search-3
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
list( $sidebar_id ) = $args;
$this->validate_sidebar( $sidebar_id );
$output_widgets = $this->get_sidebar_widgets( $sidebar_id );
if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
$output_widgets = wp_list_pluck( $output_widgets, 'id' );
}
$formatter = new Formatter( $assoc_args, $this->fields );
$formatter->display_items( $output_widgets );
}
/**
* Adds a widget to a sidebar.
*
* Creates a new widget entry in the database, and associates it with the
* sidebar.
*
* ## OPTIONS
*
* <name>
* : Widget name.
*
* <sidebar-id>
* : ID for the corresponding sidebar.
*
* [<position>]
* : Widget's current position within the sidebar. Defaults to last
*
* [--<field>=<value>]
* : Widget option to add, with its new value
*
* ## EXAMPLES
*
* # Add a new calendar widget to sidebar-1 with title "Calendar"
* $ wp widget add calendar sidebar-1 2 --title="Calendar"
* Success: Added widget to sidebar.
*
* @subcommand add
*/
public function add( $args, $assoc_args ) {
list( $name, $sidebar_id ) = $args;
$this->validate_sidebar( $sidebar_id );
$position = count( $args ) > 2
? $args[2] - 1
: count( $this->get_sidebar_widgets( $sidebar_id ) );
$widget = $this->get_widget_obj( $name );
if ( false === $widget ) {
WP_CLI::error( 'Invalid widget type.' );
}
/*
* Adding a widget is as easy as:
* 1. Creating a new widget option
* 2. Adding the widget to the sidebar
* 3. Positioning appropriately
*/
$widget_options = $this->get_widget_options( $name );
$option_keys = $widget_options;
if ( ! isset( $widget_options['_multiwidget'] ) ) {
$widget_options['_multiwidget'] = 1;
}
unset( $option_keys['_multiwidget'] );
$option_keys = array_keys( $option_keys );
$last_key = (int) array_pop( $option_keys );
$option_index = $last_key + 1;
$widget_options[ $option_index ] = $this->sanitize_widget_options( $name, $assoc_args, array() );
$this->update_widget_options( $name, $widget_options );
$widget_id = $name . '-' . $option_index;
$this->move_sidebar_widget( $widget_id, null, $sidebar_id, null, $position );
WP_CLI::success( 'Added widget to sidebar.' );
}
/**
* Updates options for an existing widget.
*
* ## OPTIONS
*
* <widget-id>
* : Unique ID for the widget
*
* [--<field>=<value>]
* : Field to update, with its new value
*
* ## EXAMPLES
*
* # Change calendar-1 widget title to "Our Calendar"
* $ wp widget update calendar-1 --title="Our Calendar"
* Success: Widget updated.
*
* @subcommand update
*/
public function update( $args, $assoc_args ) {
list( $widget_id ) = $args;
if ( ! $this->validate_sidebar_widget( $widget_id ) ) {
WP_CLI::error( "Widget doesn't exist." );
}
if ( empty( $assoc_args ) ) {
WP_CLI::error( 'No options specified to update.' );
}
list( $name, $option_index ) = $this->get_widget_data( $widget_id );
$widget_options = $this->get_widget_options( $name );
$clean_options = $this->sanitize_widget_options( $name, $assoc_args, $widget_options[ $option_index ] );
$widget_options[ $option_index ] = array_merge( (array) $widget_options[ $option_index ], $clean_options );
$this->update_widget_options( $name, $widget_options );
WP_CLI::success( 'Widget updated.' );
}
/**
* Updates a nested value in a widget's options.
*
* ## OPTIONS
*
* <action>
* : Patch action to perform.
* ---
* options:
* - insert
* - update
* - delete
* ---
*
* <widget-id>
* : Unique ID for the widget.
*
* <key-path>...
* : The name(s) of the keys within the value to locate the value to patch.
*
* [<value>]
* : The new value. If omitted, the value is read from STDIN.
*
* [--format=<format>]
* : The serialization format for the value.
* ---
* default: plaintext
* options:
* - plaintext
* - json
* ---
*
* ## EXAMPLES
*
* # Update a nested value in the options of the archives-1 widget
* $ wp widget patch update archives-1 title "My Archives"
* Success: Widget updated.
*
* # Insert a new nested value into the options of the archives-1 widget
* $ wp widget patch insert archives-1 new_key "New Value"
* Success: Widget updated.
*
* # Delete a nested value from the options of the archives-1 widget
* $ wp widget patch delete archives-1 title
* Success: Widget updated.
*
* @subcommand patch
*/
public function patch( $args, $assoc_args ) {
list( $action, $widget_id ) = $args;
if ( ! $this->validate_sidebar_widget( $widget_id ) ) {
WP_CLI::error( "Widget doesn't exist." );
}
$key_path = array_map(
function ( $key ) {
if ( is_numeric( $key ) && ( (string) intval( $key ) === $key ) ) {
return (int) $key;
}
return $key;
},
array_slice( $args, 2 )
);
if ( 'delete' === $action ) {
$patch_value = null;
} else {
$stdin_value = Utils\has_stdin()
? trim( WP_CLI::get_value_from_arg_or_stdin( $args, -1 ) )
: null;
if ( null !== $stdin_value && '' !== $stdin_value ) {
$patch_value = WP_CLI::read_value( $stdin_value, $assoc_args );
} elseif ( count( $key_path ) > 1 ) {
$patch_value = WP_CLI::read_value( array_pop( $key_path ), $assoc_args );
} else {
$patch_value = null;
}
if ( null === $patch_value ) {
WP_CLI::error( "Please provide a value to {$action}." );
}
}
list( $name, $option_index ) = $this->get_widget_data( $widget_id );
$widget_options = $this->get_widget_options( $name );
$instance_options = isset( $widget_options[ $option_index ] ) ? $widget_options[ $option_index ] : array();
$traverser = new RecursiveDataStructureTraverser( $instance_options );
try {
$traverser->$action( $key_path, $patch_value );
} catch ( Exception $exception ) {
WP_CLI::error( $exception->getMessage() );
}
$widget_options[ $option_index ] = $traverser->value();
$this->update_widget_options( $name, $widget_options );
WP_CLI::success( 'Widget updated.' );
}
/**
* Moves the position of a widget.
*
* Changes the order of a widget in its existing sidebar, or moves it to a
* new sidebar.
*
* ## OPTIONS
*
* <widget-id>
* : Unique ID for the widget
*
* [--position=<position>]
* : Assign the widget to a new position.
*
* [--sidebar-id=<sidebar-id>]
* : Assign the widget to a new sidebar
*
* ## EXAMPLES
*
* # Change position of widget
* $ wp widget move recent-comments-2 --position=2
* Success: Widget moved.
*
* # Move widget to Inactive Widgets
* $ wp widget move recent-comments-2 --sidebar-id=wp_inactive_widgets
* Success: Widget moved.
*
* @subcommand move
*/
public function move( $args, $assoc_args ) {
list( $widget_id ) = $args;
if ( ! $this->validate_sidebar_widget( $widget_id ) ) {
WP_CLI::error( "Widget doesn't exist." );
}
if ( empty( $assoc_args['position'] ) && empty( $assoc_args['sidebar-id'] ) ) {
WP_CLI::error( 'A new position or new sidebar must be specified.' );
}
list( $name, $option_index, $current_sidebar_id, $current_sidebar_index ) = $this->get_widget_data( $widget_id );
$new_sidebar_id = ! empty( $assoc_args['sidebar-id'] ) ? $assoc_args['sidebar-id'] : $current_sidebar_id;
$this->validate_sidebar( $new_sidebar_id );
$new_sidebar_index = ! empty( $assoc_args['position'] ) ? $assoc_args['position'] - 1 : $current_sidebar_index;
// Moving between sidebars adds to the top.
if ( $new_sidebar_id !== $current_sidebar_id && $new_sidebar_index === $current_sidebar_index ) {
// Human-readable positions are different than numerically indexed array.
$new_sidebar_index = 0;
}
$this->move_sidebar_widget( $widget_id, $current_sidebar_id, $new_sidebar_id, $current_sidebar_index, $new_sidebar_index );
WP_CLI::success( 'Widget moved.' );
}
/**
* Deactivates one or more widgets from an active sidebar.
*
* Moves widgets to Inactive Widgets.
*
* ## OPTIONS
*
* <widget-id>...
* : Unique ID for the widget(s)
*
* ## EXAMPLES
*
* # Deactivate the recent-comments-2 widget.
* $ wp widget deactivate recent-comments-2
* Success: 1 widget deactivated.
*
* @subcommand deactivate
*/
public function deactivate( $args, $assoc_args ) {
$count = 0;
$errors = 0;
foreach ( $args as $widget_id ) {
if ( ! $this->validate_sidebar_widget( $widget_id ) ) {
WP_CLI::warning( "Widget '{$widget_id}' doesn't exist." );
++$errors;
continue;
}
list( $name, $option_index, $sidebar_id, $sidebar_index ) = $this->get_widget_data( $widget_id );
if ( 'wp_inactive_widgets' === $sidebar_id ) {
WP_CLI::warning( sprintf( "'%s' is already deactivated.", $widget_id ) );
continue;
}
$this->move_sidebar_widget(
$widget_id,
$sidebar_id,
'wp_inactive_widgets',
$sidebar_index,
count( $this->get_sidebar_widgets( 'wp_inactive_widgets' ) )
);
++$count;
}
Utils\report_batch_operation_results( 'widget', 'deactivate', count( $args ), $count, $errors );
}
/**
* Deletes one or more widgets from a sidebar.
*
* ## OPTIONS
*
* <widget-id>...
* : Unique ID for the widget(s)
*
* ## EXAMPLES
*
* # Delete the recent-comments-2 widget from its sidebar.
* $ wp widget delete recent-comments-2
* Success: Deleted 1 of 1 widgets.
*
* @subcommand delete
*/
public function delete( $args, $assoc_args ) {
$count = 0;
$errors = 0;
foreach ( $args as $widget_id ) {
if ( ! $this->validate_sidebar_widget( $widget_id ) ) {
WP_CLI::warning( "Widget '{$widget_id}' doesn't exist." );
++$errors;
continue;
}
// Remove the widget's settings.
list( $name, $option_index, $sidebar_id, $sidebar_index ) = $this->get_widget_data( $widget_id );
$widget_options = $this->get_widget_options( $name );
unset( $widget_options[ $option_index ] );
$this->update_widget_options( $name, $widget_options );
// Remove the widget from the sidebar.
$all_widgets = $this->wp_get_sidebars_widgets();
unset( $all_widgets[ $sidebar_id ][ $sidebar_index ] );
$all_widgets[ $sidebar_id ] = array_values( $all_widgets[ $sidebar_id ] );
update_option( 'sidebars_widgets', $all_widgets );
++$count;
}
Utils\report_batch_operation_results( 'widget', 'delete', count( $args ), $count, $errors );
}
/**
* Resets sidebar.
*
* Removes all widgets from the sidebar and places them in Inactive Widgets.
*
* ## OPTIONS
*
* [<sidebar-id>...]
* : One or more sidebars to reset.
*
* [--all]
* : If set, all sidebars will be reset.
*
* [--inactive]
* : If set, all inactive sidebars will also be reset, in addition to any sidebars specified via <sidebar-id>... or selected with --all.
*
* ## EXAMPLES
*
* # Reset a sidebar
* $ wp widget reset sidebar-1
* Sidebar 'sidebar-1' reset.
*
* # Reset multiple sidebars
* $ wp widget reset sidebar-1 sidebar-2
* Sidebar 'sidebar-1' reset.
* Sidebar 'sidebar-2' reset.
*
* # Reset all sidebars
* $ wp widget reset --all
* Sidebar 'sidebar-1' reset.
* Sidebar 'sidebar-2' reset.
* Sidebar 'sidebar-3' reset.
*
* # Reset all inactive sidebars
* $ wp widget reset --inactive
* Sidebar 'old-sidebar-1' reset.
*/
public function reset( $args, $assoc_args ) {
global $wp_registered_sidebars;
$all = Utils\get_flag_value( $assoc_args, 'all', false );
$inactive = Utils\get_flag_value( $assoc_args, 'inactive', false );
// Bail if no arguments and no --all or --inactive flag.
if ( ! $all && ! $inactive && empty( $args ) ) {
WP_CLI::error( 'Please specify one or more sidebars, or use --all or --inactive.' );
}
// Explicitly handle reserved sidebar ID for inactive widgets.
if ( in_array( 'wp_inactive_widgets', $args, true ) ) {
WP_CLI::error( "Sidebar 'wp_inactive_widgets' is reserved for inactive widgets and cannot be reset with this command. The --inactive flag only targets widgets from orphaned or unregistered sidebars, not 'wp_inactive_widgets' itself." );
}
// Fetch all registered sidebars if --all flag is set.
if ( $all ) {
$args = array_keys( $wp_registered_sidebars );
}
// Sidebar ID wp_inactive_widgets is reserved by WP core for inactive widgets.
$args = array_values(
array_filter(
$args,
static function ( $id ) {
return 'wp_inactive_widgets' !== $id;
}
)
);
// Collect inactive (unregistered) sidebar IDs if --inactive flag is set.
$inactive_args = [];
if ( $inactive ) {
$inactive_args = Sidebar_Command::get_inactive_sidebar_ids();
}
$all_args = array_merge( $args, $inactive_args );
$all_args = array_values( array_unique( $all_args ) );
// Check if there are no sidebars to reset.
if ( empty( $all_args ) ) {
if ( $inactive && empty( $inactive_args ) ) {
WP_CLI::error( 'No inactive sidebars found.' );
}
WP_CLI::error( 'No sidebar registered.' );
}
$count = 0;
$errors = 0;
foreach ( $all_args as $sidebar_id ) {
// Skip registration validation for sidebars resolved via --inactive.
if ( ! in_array( $sidebar_id, $inactive_args, true ) &&
! array_key_exists( $sidebar_id, $wp_registered_sidebars ) ) {
WP_CLI::warning( sprintf( 'Invalid sidebar: %s', $sidebar_id ) );
++$errors;
continue;
}
$widgets = $this->get_sidebar_widgets( $sidebar_id );
if ( empty( $widgets ) ) {
WP_CLI::warning( sprintf( "Sidebar '%s' is already empty.", $sidebar_id ) );
} else {
foreach ( $widgets as $widget ) {
$widget_id = $widget->id;
list( $name, $option_index, $new_sidebar_id, $sidebar_index ) = $this->get_widget_data( $widget_id );
$this->move_sidebar_widget(
$widget_id,
$new_sidebar_id,
'wp_inactive_widgets',
$sidebar_index,
count( $this->get_sidebar_widgets( 'wp_inactive_widgets' ) )
);
}
WP_CLI::log( sprintf( "Sidebar '%s' reset.", $sidebar_id ) );
++$count;
}
}
Utils\report_batch_operation_results( 'sidebar', 'reset', count( $all_args ), $count, $errors );
}
/**
* Checks whether a sidebar is a valid sidebar
*
* @param string $sidebar_id
*/
private function validate_sidebar( $sidebar_id ) {
global $wp_registered_sidebars;
Utils\wp_register_unused_sidebar();
if ( ! array_key_exists( $sidebar_id, $wp_registered_sidebars ) ) {
WP_CLI::error( 'Invalid sidebar.' );
}
}
/**
* Checks whether the specified widget is on the sidebar
*
* @param string $widget_id
*/
private function validate_sidebar_widget( $widget_id ) {
$sidebars_widgets = $this->wp_get_sidebars_widgets();
$widget_exists = false;
foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
if ( in_array( $widget_id, $widgets, true ) ) {
$widget_exists = true;
break;
}
}
return $widget_exists;
}
/**
* Gets the widgets (and their associated data) for a given sidebar
*
* @param string $sidebar_id
* @return list<object{name: string, id: string, position: int, options: array<string, mixed>}&\stdClass>
*/
private function get_sidebar_widgets( $sidebar_id ) {
$all_widgets = $this->wp_get_sidebars_widgets();
if ( empty( $all_widgets[ $sidebar_id ] ) ) {
return array();
}
$prepared_widgets = array();
foreach ( $all_widgets[ $sidebar_id ] as $key => $widget_id ) {
$prepared_widget = new stdClass();
$parts = explode( '-', $widget_id );
$option_index = (string) array_pop( $parts );
$widget_name = implode( '-', $parts );
$prepared_widget->name = $widget_name;
$prepared_widget->id = $widget_id;
$prepared_widget->position = $key + 1;
$widget_options = get_option( 'widget_' . $widget_name, [] );
/**
* @var array<string, mixed> $widget_options
*/
$prepared_widget->options = $widget_options[ $option_index ];
$prepared_widgets[] = $prepared_widget;
}
/**
* @phpstan-var list<object{name: string, id: string, position: int, options: array<string, mixed>}&\stdClass> $prepared_widgets
*/
return $prepared_widgets;
}
/**
* Re-implementation of wp_get_sidebars_widgets()
* because the original has a nasty global component.
*
* @return array<string, array<int, string>>
*/
private function wp_get_sidebars_widgets() {
$sidebars_widgets = get_option( 'sidebars_widgets', array() );
if ( is_array( $sidebars_widgets ) && isset( $sidebars_widgets['array_version'] ) ) {
unset( $sidebars_widgets['array_version'] );
}
/**
* @var array<string, array<int, string>> $sidebars_widgets
*/
return $sidebars_widgets;
}
/**
* Gets the widget's name, option index, sidebar, and sidebar index from its ID
*
* @param string $widget_id
* @return array{0: string, 1: string, 2: string, 3: int}
*/
private function get_widget_data( $widget_id ) {
$parts = explode( '-', $widget_id );
$option_index = array_pop( $parts );
$name = implode( '-', $parts );
$sidebar_id = false;
$sidebar_index = false;
$all_widgets = $this->wp_get_sidebars_widgets();
foreach ( $all_widgets as $s_id => &$widgets ) {
$key = array_search( $widget_id, $widgets, true );
if ( false !== $key ) {
$sidebar_id = $s_id;
$sidebar_index = $key;
break;
}
}
return array( $name, $option_index, (string) $sidebar_id, (int) $sidebar_index );
}
/**
* Gets the options for a given widget
*
* @param string $name
* @return array<string, mixed>
*/
private function get_widget_options( $name ) {
$options = get_option( 'widget_' . $name, array() );
/**
* @var array<string,mixed> $options
*/
return $options;
}
/**
* Updates the options for a given widget
*
* @param string $name
* @param mixed $value
*/
private function update_widget_options( $name, $value ) {
update_option( 'widget_' . $name, $value );
}
/**
* Repositions a widget within a sidebar or move to another sidebar.
*
* @param string $widget_id
* @param string|null $current_sidebar_id
* @param string $new_sidebar_id
* @param int|null $current_index
* @param int $new_index
*/
private function move_sidebar_widget( $widget_id, $current_sidebar_id, $new_sidebar_id, $current_index, $new_index ) {
$all_widgets = $this->wp_get_sidebars_widgets();
$needs_placement = true;
// Existing widget
if ( $current_sidebar_id && ! is_null( $current_index ) ) {
$widgets = $all_widgets[ $current_sidebar_id ];
if ( $current_sidebar_id !== $new_sidebar_id ) {
unset( $widgets[ $current_index ] );
} else {
$part = array_splice( $widgets, $current_index, 1 );
array_splice( $widgets, $new_index, 0, $part );
$needs_placement = false;
}
$all_widgets[ $current_sidebar_id ] = array_values( $widgets );
}
if ( $needs_placement ) {
$widgets = ! empty( $all_widgets[ $new_sidebar_id ] ) ? $all_widgets[ $new_sidebar_id ] : array();
$before = array_slice( $widgets, 0, $new_index, true );
$after = array_slice( $widgets, $new_index, count( $widgets ), true );
$widgets = array_merge( $before, array( $widget_id ), $after );
$all_widgets[ $new_sidebar_id ] = array_values( $widgets );
}
update_option( 'sidebars_widgets', $all_widgets );
}
/**
* Gets a widget's instantiated object based on its name
*
* @param string $id_base Name of the widget
* @return WP_Widget|false
*/
private function get_widget_obj( $id_base ) {
global $wp_widget_factory;
$widget = wp_filter_object_list( $wp_widget_factory->widgets, array( 'id_base' => $id_base ) );
if ( empty( $widget ) ) {
return false;
}
return array_pop( $widget );
}
/**
* Cleans up a widget's options based on its update callback
*
* @param string $id_base Name of the widget
* @param mixed $dirty_options
* @param mixed $old_options
* @return array<string, mixed>
*/
private function sanitize_widget_options( $id_base, $dirty_options, $old_options ) {
$widget = $this->get_widget_obj( $id_base );
if ( empty( $widget ) ) {
return array();
}
// No easy way to determine expected array keys for $dirty_options
// because Widget API dependent on the form fields
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Whitelisting due to above reason.
return @$widget->update( $dirty_options, $old_options ); // @phpstan-ignore argument.type, argument.type
}
}