forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-widget.php
More file actions
1058 lines (964 loc) · 34 KB
/
text-widget.php
File metadata and controls
1058 lines (964 loc) · 34 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
<?php
/**
* Unit tests covering WP_Widget_Text functionality.
*
* @package WordPress
* @subpackage widgets
*/
/**
* Test wp-includes/widgets/class-wp-widget-text.php
*
* @group widgets
*/
class Test_WP_Widget_Text extends WP_UnitTestCase {
/**
* Args passed to the widget_text filter.
*
* @var array
*/
protected $widget_text_args;
/**
* Args passed to the widget_text_content filter.
*
* @var array
*/
protected $widget_text_content_args;
/**
* Clean up global scope.
*
* @global WP_Scripts $wp_scripts
* @global WP_Styles $wp_style
* @global WP_Customize_Manager $wp_customize
*/
function clean_up_global_scope() {
global $wp_scripts, $wp_styles, $wp_customize;
parent::clean_up_global_scope();
$wp_scripts = null;
$wp_styles = null;
$wp_customize = null;
}
/**
* Test constructor method.
*
* @covers WP_Widget_Text::__construct
*/
function test_construct() {
$widget = new WP_Widget_Text();
$this->assertEquals( 'text', $widget->id_base );
$this->assertEquals( 'widget_text', $widget->widget_options['classname'] );
$this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 400, $widget->control_options['width'] );
$this->assertEquals( 350, $widget->control_options['height'] );
}
/**
* Test enqueue_admin_scripts method.
*
* @covers WP_Widget_Text::_register
*/
function test__register() {
set_current_screen( 'widgets.php' );
$widget = new WP_Widget_Text();
$widget->_register();
$this->assertEquals( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
$this->assertEquals( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) ) );
$this->assertContains( 'wp.textWidgets.idBases.push( "text" );', wp_scripts()->registered['text-widgets']->extra['after'] );
$this->assertFalse( has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
}
/**
* Test register in customize preview.
*
* @global WP_Customize_Manager $wp_customize
* @covers WP_Widget_Text::__construct()
* @covers WP_Widget_Text::_register()
*/
function test__register_in_customize_preview() {
global $wp_customize;
wp_set_current_user(
$this->factory()->user->create(
array(
'role' => 'administrator',
)
)
);
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$wp_customize = new WP_Customize_Manager(
array(
'changeset_uuid' => wp_generate_uuid4(),
)
);
$wp_customize->start_previewing_theme();
$widget = new WP_Widget_Text();
$widget->_register();
$this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
}
/**
* Test enqueue_preview_scripts method.
*
* @global WP_Scripts $wp_scripts
* @global WP_Styles $wp_styles
* @covers WP_Widget_Text::enqueue_preview_scripts
*/
function test_enqueue_preview_scripts() {
global $wp_scripts, $wp_styles;
$wp_scripts = null;
$wp_styles = null;
$widget = new WP_Widget_Text();
$this->assertFalse( wp_style_is( 'wp-mediaelement' ) );
$this->assertFalse( wp_script_is( 'wp-playlist' ) );
ob_start();
$widget->enqueue_preview_scripts();
ob_end_clean();
$this->assertTrue( wp_style_is( 'wp-mediaelement' ) );
$this->assertTrue( wp_script_is( 'wp-playlist' ) );
}
/**
* Test widget method.
*
* @covers WP_Widget_Text::widget
*/
function test_widget() {
$widget = new WP_Widget_Text();
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Praesent ut turpis consequat lorem volutpat bibendum vitae vitae ante.";
$args = array(
'before_title' => '<h2>',
'after_title' => "</h2>\n",
'before_widget' => '<section>',
'after_widget' => "</section>\n",
);
add_filter( 'widget_text_content', array( $this, 'filter_widget_text_content' ), 5, 3 );
add_filter( 'widget_text', array( $this, 'filter_widget_text' ), 5, 3 );
// Test with filter=false, implicit legacy mode.
$this->widget_text_content_args = null;
ob_start();
$instance = array(
'title' => 'Foo',
'text' => $text,
'filter' => false,
);
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertNotContains( '<p>', $output );
$this->assertNotContains( '<br />', $output );
$this->assertEmpty( $this->widget_text_content_args );
$this->assertNotEmpty( $this->widget_text_args );
$this->assertContains( '[filter:widget_text]', $output );
$this->assertNotContains( '[filter:widget_text_content]', $output );
// Test with filter=true, implicit legacy mode.
$this->widget_text_content_args = null;
$instance = array(
'title' => 'Foo',
'text' => $text,
'filter' => true,
);
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertContains( '<p>', $output );
$this->assertContains( '<br />', $output );
$this->assertNotEmpty( $this->widget_text_args );
$this->assertEquals( $instance['text'], $this->widget_text_args[0] );
$this->assertEquals( $instance, $this->widget_text_args[1] );
$this->assertEquals( $widget, $this->widget_text_args[2] );
$this->assertEmpty( $this->widget_text_content_args );
$this->assertContains( '[filter:widget_text]', $output );
$this->assertNotContains( '[filter:widget_text_content]', $output );
// Test with filter=content, the upgraded widget, in 4.8.0 only.
$this->widget_text_content_args = null;
$instance = array(
'title' => 'Foo',
'text' => $text,
'filter' => 'content',
);
$expected_instance = array_merge(
$instance,
array(
'filter' => true,
'visual' => true,
)
);
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertContains( '<p>', $output );
$this->assertContains( '<br />', $output );
$this->assertCount( 3, $this->widget_text_args );
$this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] );
$this->assertEquals( $expected_instance, $this->widget_text_args[1] );
$this->assertEquals( $widget, $this->widget_text_args[2] );
$this->assertCount( 3, $this->widget_text_content_args );
$this->assertEquals( $expected_instance['text'] . '[filter:widget_text]', $this->widget_text_content_args[0] );
$this->assertEquals( $expected_instance, $this->widget_text_content_args[1] );
$this->assertEquals( $widget, $this->widget_text_content_args[2] );
$this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output );
// Test with filter=true&visual=true, the upgraded widget, in 4.8.1 and above.
$this->widget_text_content_args = null;
$instance = array(
'title' => 'Foo',
'text' => $text,
'filter' => true,
'visual' => true,
);
$expected_instance = $instance;
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertContains( '<p>', $output );
$this->assertContains( '<br />', $output );
$this->assertCount( 3, $this->widget_text_args );
$this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] );
$this->assertEquals( $expected_instance, $this->widget_text_args[1] );
$this->assertEquals( $widget, $this->widget_text_args[2] );
$this->assertCount( 3, $this->widget_text_content_args );
$this->assertEquals( $expected_instance['text'] . '[filter:widget_text]', $this->widget_text_content_args[0] );
$this->assertEquals( $expected_instance, $this->widget_text_content_args[1] );
$this->assertEquals( $widget, $this->widget_text_content_args[2] );
$this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output );
// Test with filter=true&visual=true, the upgraded widget, in 4.8.1 and above.
$this->widget_text_content_args = null;
$instance = array(
'title' => 'Foo',
'text' => $text,
'filter' => true,
'visual' => false,
);
$expected_instance = $instance;
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertContains( '<p>', $output );
$this->assertContains( '<br />', $output );
$this->assertCount( 3, $this->widget_text_args );
$this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] );
$this->assertEquals( $expected_instance, $this->widget_text_args[1] );
$this->assertEquals( $widget, $this->widget_text_args[2] );
$this->assertNull( $this->widget_text_content_args );
$this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text]' ), $output );
// Test with filter=false&visual=false, the upgraded widget, in 4.8.1 and above.
$this->widget_text_content_args = null;
$instance = array(
'title' => 'Foo',
'text' => $text,
'filter' => false,
'visual' => false,
);
$expected_instance = $instance;
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertNotContains( '<p>', $output );
$this->assertNotContains( '<br />', $output );
$this->assertCount( 3, $this->widget_text_args );
$this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] );
$this->assertEquals( $expected_instance, $this->widget_text_args[1] );
$this->assertEquals( $widget, $this->widget_text_args[2] );
$this->assertNull( $this->widget_text_content_args );
$this->assertContains( $expected_instance['text'] . '[filter:widget_text]', $output );
}
/**
* Example shortcode content to test for wpautop corruption.
*
* @var string
*/
protected $example_shortcode_content = "<p class='sortcodep'>One\nTwo\n\nThree\n\nThis is testing the <code>[example note='This will not get processed since it is part of shortcode output itself.']</code> shortcode.</p>\n<script>\ndocument.write('Test1');\n\ndocument.write('Test2');\n</script>";
/**
* The captured global post during shortcode rendering.
*
* @var WP_Post|null
*/
protected $post_during_shortcode = null;
/**
* Number of times the shortcode was rendered.
*
* @var int
*/
protected $shortcode_render_count = 0;
/**
* Do example shortcode.
*
* @return string Shortcode content.
*/
function do_example_shortcode() {
$this->post_during_shortcode = get_post();
$this->shortcode_render_count++;
return $this->example_shortcode_content;
}
/**
* Test widget method with shortcodes.
*
* @covers WP_Widget_Text::widget
*/
function test_widget_shortcodes() {
global $post;
$post_id = $this->factory()->post->create();
$post = get_post( $post_id );
$args = array(
'before_title' => '<h2>',
'after_title' => "</h2>\n",
'before_widget' => '<section>',
'after_widget' => "</section>\n",
);
$widget = new WP_Widget_Text();
add_shortcode( 'example', array( $this, 'do_example_shortcode' ) );
$base_instance = array(
'title' => 'Example',
'text' => "This is an example:\n\n[example]\n\nHello.",
'filter' => false,
);
// Legacy Text Widget without wpautop().
$instance = array_merge(
$base_instance,
array(
'filter' => false,
)
);
$this->shortcode_render_count = 0;
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertEquals( 1, $this->shortcode_render_count );
$this->assertNotContains( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
$this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
$this->assertNotContains( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
$this->assertNull( $this->post_during_shortcode );
// Legacy Text Widget with wpautop().
$instance = array_merge(
$base_instance,
array(
'filter' => true,
'visual' => false,
)
);
$this->shortcode_render_count = 0;
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertEquals( 1, $this->shortcode_render_count );
$this->assertNotContains( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
$this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
$this->assertNotContains( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
$this->assertNull( $this->post_during_shortcode );
// Legacy text widget with plugin adding shortcode support as well.
add_filter( 'widget_text', 'do_shortcode' );
$this->shortcode_render_count = 0;
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertEquals( 1, $this->shortcode_render_count );
$this->assertNotContains( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
$this->assertContains( wpautop( $this->example_shortcode_content ), $output, 'Shortcode was applied *with* wpautop() applying to shortcode output since plugin used legacy filter.' );
$this->assertNull( $this->post_during_shortcode );
remove_filter( 'widget_text', 'do_shortcode' );
$instance = array_merge(
$base_instance,
array(
'filter' => true,
'visual' => true,
)
);
// Visual Text Widget with only core-added widget_text_content filter for do_shortcode().
$this->assertFalse( has_filter( 'widget_text', 'do_shortcode' ) );
$this->assertEquals( 11, has_filter( 'widget_text_content', 'do_shortcode' ), 'Expected core to have set do_shortcode as widget_text_content filter.' );
$this->shortcode_render_count = 0;
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertEquals( 1, $this->shortcode_render_count );
$this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
$this->assertNotContains( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
$this->assertFalse( has_filter( 'widget_text', 'do_shortcode' ), 'The widget_text filter still lacks do_shortcode handler.' );
$this->assertEquals( 11, has_filter( 'widget_text_content', 'do_shortcode' ), 'The widget_text_content filter still has do_shortcode handler.' );
$this->assertNull( $this->post_during_shortcode );
// Visual Text Widget with both filters applied added, one from core and another via plugin.
add_filter( 'widget_text', 'do_shortcode' );
$this->shortcode_render_count = 0;
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertEquals( 1, $this->shortcode_render_count );
$this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
$this->assertNotContains( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
$this->assertEquals( 10, has_filter( 'widget_text', 'do_shortcode' ), 'Expected do_shortcode to be restored to widget_text.' );
$this->assertNull( $this->post_during_shortcode );
$this->assertNull( $this->post_during_shortcode );
remove_filter( 'widget_text', 'do_shortcode' );
// Visual Text Widget with shortcode handling disabled via plugin removing filter.
remove_filter( 'widget_text_content', 'do_shortcode', 11 );
remove_filter( 'widget_text', 'do_shortcode' );
$this->shortcode_render_count = 0;
ob_start();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertEquals( 0, $this->shortcode_render_count );
$this->assertContains( '[example]', $output );
$this->assertNotContains( $this->example_shortcode_content, $output );
$this->assertFalse( has_filter( 'widget_text', 'do_shortcode' ) );
$this->assertFalse( has_filter( 'widget_text_content', 'do_shortcode' ) );
}
/**
* Filters the content of the Text widget.
*
* @param string $widget_text The widget content.
* @param array $instance Array of settings for the current widget.
* @param WP_Widget_Text $widget Current Text widget instance.
* @return string Widget text.
*/
function filter_widget_text( $widget_text, $instance, $widget ) {
$this->widget_text_args = func_get_args();
$widget_text .= '[filter:widget_text]';
return $widget_text;
}
/**
* Filters the content of the Text widget to apply changes expected from the visual (TinyMCE) editor.
*
* @param string $widget_text The widget content.
* @param array $instance Array of settings for the current widget.
* @param WP_Widget_Text $widget Current Text widget instance.
* @return string Widget content.
*/
function filter_widget_text_content( $widget_text, $instance, $widget ) {
$this->widget_text_content_args = func_get_args();
$widget_text .= '[filter:widget_text_content]';
return $widget_text;
}
/**
* Test is_legacy_instance method.
*
* @covers WP_Widget_Text::is_legacy_instance
*/
function test_is_legacy_instance() {
$widget = new WP_Widget_Text();
$base_instance = array(
'title' => 'Title',
'text' => "Hello\n\nWorld",
);
$instance = array_merge(
$base_instance,
array(
'visual' => false,
)
);
$this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when visual=false prop is present.' );
$instance = array_merge(
$base_instance,
array(
'visual' => true,
)
);
$this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when visual=true prop is present.' );
$instance = array_merge(
$base_instance,
array(
'filter' => 'content',
)
);
$this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when filter is explicitly content (in WP 4.8.0 only).' );
$instance = array_merge(
$base_instance,
array(
'text' => '',
'filter' => true,
)
);
$this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when text is empty.' );
$instance = array_merge(
$base_instance,
array(
'text' => "\nOne line",
'filter' => false,
)
);
$this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when there is leading whitespace.' );
$instance = array_merge(
$base_instance,
array(
'text' => "\nOne line\n\n",
'filter' => false,
)
);
$this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when there is trailing whitespace.' );
$instance = array_merge(
$base_instance,
array(
'text' => "One\nTwo",
'filter' => false,
)
);
$this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when not-wpautop and there are line breaks.' );
$instance = array_merge(
$base_instance,
array(
'text' => "One\n\nTwo",
'filter' => false,
)
);
$this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when not-wpautop and there are paragraph breaks.' );
$instance = array_merge(
$base_instance,
array(
'text' => "One\nTwo",
'filter' => true,
)
);
$this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not automatically legacy when wpautop and there are line breaks.' );
$instance = array_merge(
$base_instance,
array(
'text' => "One\n\nTwo",
'filter' => true,
)
);
$this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not automatically legacy when wpautop and there are paragraph breaks.' );
$instance = array_merge(
$base_instance,
array(
'text' => 'Test<!-- comment -->',
'filter' => true,
)
);
$this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when HTML comment is present.' );
// Check text examples that will not migrate to TinyMCE.
$legacy_text_examples = array(
'<span class="hello"></span>',
'<blockquote>Quote <footer>Citation</footer></blockquote>',
'<img src=\"http://example.com/img.jpg\" border=\"0\" title=\"Example\" /></a>',
'<span></span>',
"<ul>\n<li><a href=\"#\" class=\"location\"></a>List Item 1</li>\n<li><a href=\"#\" class=\"location\"></a>List Item 2</li>\n</ul>",
'<a href="#" class="map"></a>',
"<script>\n\\Line one\n\n\\Line two</script>",
"<style>body {\ncolor:red;\n}</style>",
'<span class="fa fa-cc-discover fa-2x" aria-hidden="true"></span>',
"<p>\nStay updated with our latest news and specials. We never sell your information and you can unsubscribe at any time.\n</p>\n\n<div class=\"custom-form-class\">\n\t<form action=\"#\" method=\"post\" name=\"mc-embedded-subscribe-form\">\n\n\t\t<label class=\"screen-reader-text\" for=\"mce-EMAIL-b\">Email </label>\n\t\t<input id=\"mce-EMAIL-b\" class=\"required email\" name=\"EMAIL\" required=\"\" type=\"email\" value=\"\" placeholder=\"Email Address*\" />\n\n\t\t<input class=\"button\" name=\"subscribe\" type=\"submit\" value=\"Go!\" />\n\n\t</form>\n</div>",
'<span class="sectiondown"><a href="#front-page-3"><i class="fa fa-chevron-circle-down"></i></a></span>',
);
foreach ( $legacy_text_examples as $legacy_text_example ) {
$instance = array_merge(
$base_instance,
array(
'text' => $legacy_text_example,
'filter' => true,
)
);
$this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when wpautop and there is HTML that is not liable to be mutated.' );
$instance = array_merge(
$base_instance,
array(
'text' => $legacy_text_example,
'filter' => false,
)
);
$this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when not-wpautop and there is HTML that is not liable to be mutated.' );
}
// Check text examples that will migrate to TinyMCE, where elements and attributes are not in the allowed list.
$migratable_text_examples = array(
'Check out <a href="http://example.com">Example</a>',
'<img src="http://example.com/img.jpg" alt="Img">',
'<strong><em>Hello</em></strong>',
'<b><i><u><s>Hello</s></u></i></b>',
"<ul>\n<li>One</li>\n<li>One</li>\n<li>One</li>\n</ul>",
"<ol>\n<li>One</li>\n<li>One</li>\n<li>One</li>\n</ol>",
"Text\n<hr>\nAddendum",
"Look at this code:\n\n<code>echo 'Hello World!';</code>",
);
foreach ( $migratable_text_examples as $migratable_text_example ) {
$instance = array_merge(
$base_instance,
array(
'text' => $migratable_text_example,
'filter' => true,
)
);
$this->assertFalse( $widget->is_legacy_instance( $instance ), 'Legacy when wpautop and there is HTML that is not liable to be mutated.' );
}
}
/**
* Test update method.
*
* @covers WP_Widget_Text::form
*/
function test_form() {
add_filter( 'user_can_richedit', '__return_true' );
$widget = new WP_Widget_Text();
$widget->_set( 2 );
$instance = array(
'title' => 'Title',
'text' => 'Text',
'filter' => false,
'visual' => false,
);
$this->assertTrue( $widget->is_legacy_instance( $instance ) );
ob_start();
$widget->form( $instance );
$form = ob_get_clean();
$this->assertContains( 'class="visual" type="hidden" value=""', $form );
$this->assertNotContains( 'class="visual sync-input" type="hidden" value="on"', $form );
$instance = array(
'title' => 'Title',
'text' => 'Text',
'filter' => 'content',
);
$this->assertFalse( $widget->is_legacy_instance( $instance ) );
ob_start();
$widget->form( $instance );
$form = ob_get_clean();
$this->assertContains( 'class="visual sync-input" type="hidden" value="on"', $form );
$this->assertNotContains( 'class="visual sync-input" type="hidden" value=""', $form );
$instance = array(
'title' => 'Title',
'text' => 'Text',
'filter' => true,
);
$this->assertFalse( $widget->is_legacy_instance( $instance ) );
ob_start();
$widget->form( $instance );
$form = ob_get_clean();
$this->assertContains( 'class="visual sync-input" type="hidden" value="on"', $form );
$this->assertNotContains( 'class="visual sync-input" type="hidden" value=""', $form );
$instance = array(
'title' => 'Title',
'text' => 'This is some HTML Code: <code><strong>BOLD!</strong></code>',
'filter' => true,
'visual' => true,
);
$this->assertFalse( $widget->is_legacy_instance( $instance ) );
ob_start();
$widget->form( $instance );
$form = ob_get_clean();
$this->assertContains( 'class="visual sync-input" type="hidden" value="on"', $form );
$this->assertContains( '<code>&lt;strong&gt;BOLD!', $form );
$this->assertNotContains( 'class="visual sync-input" type="hidden" value=""', $form );
remove_filter( 'user_can_richedit', '__return_true' );
add_filter( 'user_can_richedit', '__return_false' );
$instance = array(
'title' => 'Title',
'text' => 'Evil:</textarea><script>alert("XSS")</script>',
'filter' => true,
'visual' => true,
);
$this->assertFalse( $widget->is_legacy_instance( $instance ) );
ob_start();
$widget->form( $instance );
$form = ob_get_clean();
$this->assertNotContains( 'Evil:</textarea>', $form );
$this->assertContains( 'Evil:</textarea>', $form );
}
/**
* Test update method.
*
* @covers WP_Widget_Text::update
*/
function test_update() {
$widget = new WP_Widget_Text();
$instance = array(
'title' => "The\nTitle",
'text' => "The\n\nText",
'filter' => true,
'visual' => true,
);
wp_set_current_user(
$this->factory()->user->create(
array(
'role' => 'administrator',
)
)
);
$expected = array(
'title' => sanitize_text_field( $instance['title'] ),
'text' => $instance['text'],
'filter' => true,
'visual' => true,
);
$result = $widget->update( $instance, array() );
$this->assertEquals( $expected, $result );
$this->assertTrue( ! empty( $expected['filter'] ), 'Expected filter prop to be truthy, to handle case where 4.8 is downgraded to 4.7.' );
add_filter( 'map_meta_cap', array( $this, 'grant_unfiltered_html_cap' ), 10, 2 );
$this->assertTrue( current_user_can( 'unfiltered_html' ) );
$instance['text'] = '<script>alert( "Howdy!" );</script>';
$expected['text'] = $instance['text'];
$result = $widget->update( $instance, array() );
$this->assertEquals( $expected, $result, 'KSES should apply as expected.' );
remove_filter( 'map_meta_cap', array( $this, 'grant_unfiltered_html_cap' ) );
add_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10, 2 );
$this->assertFalse( current_user_can( 'unfiltered_html' ) );
$instance['text'] = '<script>alert( "Howdy!" );</script>';
$expected['text'] = wp_kses_post( $instance['text'] );
$result = $widget->update( $instance, array() );
$this->assertEquals( $expected, $result, 'KSES should not apply since user can unfiltered_html.' );
remove_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10 );
}
/**
* Test update for legacy widgets.
*
* @covers WP_Widget_Text::update
*/
function test_update_legacy() {
$widget = new WP_Widget_Text();
// --
$instance = array(
'title' => 'Legacy',
'text' => 'Text',
'filter' => false,
);
$result = $widget->update( $instance, array() );
$this->assertEquals( $instance, $result, 'Updating a widget without visual prop and explicit filter=false leaves visual prop absent' );
// --
$instance = array(
'title' => 'Legacy',
'text' => 'Text',
'filter' => true,
);
$result = $widget->update( $instance, array() );
$this->assertEquals( $instance, $result, 'Updating a widget without visual prop and explicit filter=true leaves legacy prop absent.' );
// --
$instance = array(
'title' => 'Legacy',
'text' => 'Text',
'visual' => true,
);
$old_instance = array_merge(
$instance,
array(
'filter' => false,
)
);
$expected = array_merge(
$instance,
array(
'filter' => true,
)
);
$result = $widget->update( $instance, $old_instance );
$this->assertEquals( $expected, $result, 'Updating a pre-existing widget with visual mode forces filter to be true.' );
// --
$instance = array(
'title' => 'Legacy',
'text' => 'Text',
'filter' => true,
);
$old_instance = array_merge(
$instance,
array(
'visual' => true,
)
);
$result = $widget->update( $instance, $old_instance );
$expected = array_merge(
$instance,
array(
'visual' => true,
)
);
$this->assertEquals( $expected, $result, 'Updating a pre-existing visual widget retains visual mode when updated.' );
// --
$instance = array(
'title' => 'Legacy',
'text' => 'Text',
);
$old_instance = array_merge(
$instance,
array(
'visual' => true,
)
);
$result = $widget->update( $instance, $old_instance );
$expected = array_merge(
$instance,
array(
'visual' => true,
'filter' => true,
)
);
$this->assertEquals( $expected, $result, 'Updating a pre-existing visual widget retains visual=true and supplies missing filter=true.' );
// --
$instance = array(
'title' => 'Legacy',
'text' => 'Text',
'visual' => true,
);
$expected = array_merge(
$instance,
array(
'filter' => true,
)
);
$result = $widget->update( $instance, array() );
$this->assertEquals( $expected, $result, 'Updating a widget with explicit visual=true and absent filter prop causes filter to be set to true.' );
// --
$instance = array(
'title' => 'Legacy',
'text' => 'Text',
'visual' => false,
);
$result = $widget->update( $instance, array() );
$expected = array_merge(
$instance,
array(
'filter' => false,
)
);
$this->assertEquals( $expected, $result, 'Updating a widget in legacy mode results in filter=false as if checkbox not checked.' );
// --
$instance = array(
'title' => 'Title',
'text' => 'Text',
'filter' => false,
);
$old_instance = array_merge(
$instance,
array(
'visual' => false,
'filter' => true,
)
);
$result = $widget->update( $instance, $old_instance );
$expected = array_merge(
$instance,
array(
'visual' => false,
'filter' => false,
)
);
$this->assertEquals( $expected, $result, 'Updating a widget that previously had legacy form results in filter allowed to be false.' );
// --
$instance = array(
'title' => 'Title',
'text' => 'Text',
'filter' => 'content',
);
$result = $widget->update( $instance, array() );
$expected = array_merge(
$instance,
array(
'filter' => true,
'visual' => true,
)
);
$this->assertEquals( $expected, $result, 'Updating a widget that had \'content\' as its filter value persists non-legacy mode. This only existed in WP 4.8.0.' );
// --
$instance = array(
'title' => 'Title',
'text' => 'Text',
);
$old_instance = array_merge(
$instance,
array(
'filter' => 'content',
)
);
$result = $widget->update( $instance, $old_instance );
$expected = array_merge(
$instance,
array(
'visual' => true,
'filter' => true,
)
);
$this->assertEquals( $expected, $result, 'Updating a pre-existing widget with the filter=content prop in WP 4.8.0 upgrades to filter=true&visual=true.' );
// --
$instance = array(
'title' => 'Title',
'text' => 'Text',
'filter' => 'content',
);
$result = $widget->update( $instance, array() );
$expected = array_merge(
$instance,
array(
'filter' => true,
'visual' => true,
)
);
$this->assertEquals( $expected, $result, 'Updating a widget with filter=content (from WP 4.8.0) upgrades to filter=true&visual=true.' );
}
/**
* Grant unfiltered_html cap via map_meta_cap.
*
* @param array $caps Returns the user's actual capabilities.
* @param string $cap Capability name.
* @return array Caps.
*/
function grant_unfiltered_html_cap( $caps, $cap ) {
if ( 'unfiltered_html' === $cap ) {
$caps = array_diff( $caps, array( 'do_not_allow' ) );
$caps[] = 'unfiltered_html';
}
return $caps;
}
/**
* Revoke unfiltered_html cap via map_meta_cap.
*
* @param array $caps Returns the user's actual capabilities.
* @param string $cap Capability name.
* @return array Caps.
*/
function revoke_unfiltered_html_cap( $caps, $cap ) {
if ( 'unfiltered_html' === $cap ) {
$caps = array_diff( $caps, array( 'unfiltered_html' ) );
$caps[] = 'do_not_allow';
}
return $caps;
}
/**
* Test enqueue_admin_scripts method.
*
* @covers WP_Widget_Text::enqueue_admin_scripts
*/
function test_enqueue_admin_scripts() {
set_current_screen( 'widgets.php' );
$widget = new WP_Widget_Text();
$widget->enqueue_admin_scripts();
$this->assertTrue( wp_script_is( 'text-widgets' ) );
}
/**
* Test render_control_template_scripts method.
*
* @covers WP_Widget_Text::render_control_template_scripts
*/
function test_render_control_template_scripts() {
ob_start();
WP_Widget_Text::render_control_template_scripts();
$output = ob_get_clean();