-
Notifications
You must be signed in to change notification settings - Fork 385
/
class-amp-validation-error-taxonomy.php
1008 lines (927 loc) · 34.4 KB
/
class-amp-validation-error-taxonomy.php
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
/**
* Class AMP_Validation_Error_Taxonomy
*
* @package AMP
*/
/**
* Class AMP_Validation_Error_Taxonomy
*
* @since 1.0
*/
class AMP_Validation_Error_Taxonomy {
/**
* The slug of the taxonomy to store AMP errors.
*
* @var string
*/
const TAXONOMY_SLUG = 'amp_validation_error';
/**
* Term group for validation_error terms have not yet been acknowledged.
*
* @var int
*/
const VALIDATION_ERROR_NEW_STATUS = 0;
/**
* Term group for validation_error terms that the accepts and thus can be sanitized and does not disable AMP.
*
* @var int
*/
const VALIDATION_ERROR_ACCEPTED_STATUS = 1;
/**
* Term group for validation_error terms that the user flags as being blockers to enabling AMP.
*
* @var int
*/
const VALIDATION_ERROR_REJECTED_STATUS = 2;
/**
* Action name for ignoring a validation error.
*
* @var string
*/
const VALIDATION_ERROR_ACCEPT_ACTION = 'amp_validation_error_accept';
/**
* Action name for rejecting a validation error.
*
* @var string
*/
const VALIDATION_ERROR_REJECT_ACTION = 'amp_validation_error_reject';
/**
* Query var used when filtering by validation error status or passing updates.
*
* @var string
*/
const VALIDATION_ERROR_STATUS_QUERY_VAR = 'amp_validation_error_status';
/**
* Validation code for an invalid element.
*
* @var string
*/
const INVALID_ELEMENT_CODE = 'invalid_element';
/**
* Validation code for an invalid attribute.
*
* @var string
*/
const INVALID_ATTRIBUTE_CODE = 'invalid_attribute';
/**
* The key for removed elements.
*
* @var string
*/
const REMOVED_ELEMENTS = 'removed_elements';
/**
* The key for removed attributes.
*
* @var string
*/
const REMOVED_ATTRIBUTES = 'removed_attributes';
/**
* The key in the response for the sources that have invalid output.
*
* @var string
*/
const SOURCES_INVALID_OUTPUT = 'sources_with_invalid_output';
/**
* The key for removed sources.
*
* @var string
*/
const REMOVED_SOURCES = 'removed_sources';
/**
* Whether the terms_clauses filter should apply to a term query for validation errors to limit to a given status.
*
* This is set to false when calling wp_count_terms() for the admin menu and for the views.
*
* @see AMP_Validation_Manager::get_validation_error_count()
* @var bool
*/
protected static $should_filter_terms_clauses_for_error_validation_status;
/**
* Registers the taxonomy to store the validation errors.
*
* @return void
*/
public static function register() {
register_taxonomy( self::TAXONOMY_SLUG, AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG, array(
'labels' => array(
'name' => _x( 'AMP Validation Errors', 'taxonomy general name', 'amp' ),
'singular_name' => _x( 'AMP Validation Error', 'taxonomy singular name', 'amp' ),
'search_items' => __( 'Search AMP Validation Errors', 'amp' ),
'all_items' => __( 'All AMP Validation Errors', 'amp' ),
'edit_item' => __( 'Edit AMP Validation Error', 'amp' ),
'update_item' => __( 'Update AMP Validation Error', 'amp' ),
'menu_name' => __( 'Validation Errors', 'amp' ),
'back_to_items' => __( 'Back to AMP Validation Errors', 'amp' ),
'popular_items' => __( 'Frequent Validation Errors', 'amp' ),
'view_item' => __( 'View Validation Error', 'amp' ),
'add_new_item' => __( 'Add New Validation Error', 'amp' ), // Makes no sense.
'new_item_name' => __( 'New Validation Error Hash', 'amp' ), // Makes no sense.
'not_found' => __( 'No validation errors found.', 'amp' ),
'no_terms' => __( 'Validation Error', 'amp' ),
'items_list_navigation' => __( 'Validation errors navigation', 'amp' ),
'items_list' => __( 'Validation errors list', 'amp' ),
/* translators: Tab heading when selecting from the most used terms */
'most_used' => __( 'Most Used Validation Errors', 'amp' ),
),
'public' => false,
'show_ui' => true, // @todo False because we need a custom UI.
'show_tagcloud' => false,
'show_in_quick_edit' => false,
'hierarchical' => false, // Or true? Code could be the parent term?
'show_in_menu' => true,
'meta_box_cb' => false, // See print_validation_errors_meta_box().
'capabilities' => array(
'assign_terms' => 'do_not_allow',
'edit_terms' => 'do_not_allow',
// Note that delete_terms is needed so the checkbox (cb) table column will work.
),
) );
if ( is_admin() ) {
self::add_admin_hooks();
}
self::accept_validation_errors( AMP_Core_Theme_Sanitizer::get_acceptable_errors( get_template() ) );
}
/**
* Prepare a validation error for lookup or insertion as taxonomy term.
*
* @param array $error Validation error.
* @return array Term fields.
*/
public static function prepare_validation_error_taxonomy_term( $error ) {
unset( $error['sources'] );
ksort( $error );
$description = wp_json_encode( $error );
$term_slug = md5( $description );
return array(
'slug' => $term_slug,
'name' => $term_slug,
'description' => $description,
);
}
/**
* Determine whether a validation error should be sanitized.
*
* @param array $error Validation error.
*
* @return bool Whether error should be sanitized.
*/
public static function is_validation_error_sanitized( $error ) {
$sanitization = self::get_validation_error_sanitization( $error );
return self::VALIDATION_ERROR_ACCEPTED_STATUS === $sanitization['status'];
}
/**
* Get the validation error sanitization.
*
* @param array $error Validation error.
*
* @return array {
* Validation error sanitization.
*
* @type int $status Validation status (0=VALIDATION_ERROR_NEW_STATUS, 1=VALIDATION_ERROR_ACCEPTED_STATUS, 2=VALIDATION_ERROR_REJECTED_STATUS).
* @type int $term_status The initial validation status prior to being overridden by previewing, option, or filter.
* @type false|string $forced If and how the status is overridden from its initial term status.
* }
*/
public static function get_validation_error_sanitization( $error ) {
$term_data = self::prepare_validation_error_taxonomy_term( $error );
$term = get_term_by( 'slug', $term_data['slug'], self::TAXONOMY_SLUG );
$statuses = array(
self::VALIDATION_ERROR_NEW_STATUS,
self::VALIDATION_ERROR_ACCEPTED_STATUS,
self::VALIDATION_ERROR_REJECTED_STATUS,
);
if ( ! empty( $term ) && in_array( $term->term_group, $statuses, true ) ) {
$term_status = $term->term_group;
} else {
$term_status = self::VALIDATION_ERROR_NEW_STATUS;
}
$forced = false;
$status = $term_status;
// See note in AMP_Validation_Manager::add_validation_error_sourcing() for why amp_validation_error_sanitized filter isn't used.
if ( isset( AMP_Validation_Manager::$validation_error_status_overrides[ $term_data['slug'] ] ) ) {
$status = AMP_Validation_Manager::$validation_error_status_overrides[ $term_data['slug'] ];
$forced = 'with_preview';
}
$is_forced_with_option = (
amp_is_canonical()
||
AMP_Style_Sanitizer::TREE_SHAKING_ERROR_CODE === $error['code'] && AMP_Options_Manager::get_option( 'accept_tree_shaking' )
||
AMP_Options_Manager::get_option( 'force_sanitization' )
);
if ( $is_forced_with_option ) {
$forced = 'with_option';
$status = self::VALIDATION_ERROR_ACCEPTED_STATUS;
}
/**
* Filters whether the validation error should be sanitized.
*
* Returning true this indicates that the validation error is acceptable
* and should not be considered a blocker to render AMP. Returning null
* means that the default status should be used.
*
* Note that the $node is not passed here to ensure that the filter can be
* applied on validation errors that have been stored. Likewise, the $sources
* are also omitted because these are only available during an explicit
* validation request and so they are not suitable for plugins to vary
* sanitization by.
*
* @since 1.0
*
* @param null|bool $sanitized Whether sanitized; this is initially null, and changing it to bool causes the validation error to be forced.
* @param array $error Validation error being sanitized.
*/
$sanitized = apply_filters( 'amp_validation_error_sanitized', null, $error );
if ( null !== $sanitized ) {
$forced = 'with_filter';
$status = $sanitized ? self::VALIDATION_ERROR_ACCEPTED_STATUS : self::VALIDATION_ERROR_REJECTED_STATUS;
}
return compact( 'status', 'forced', 'term_status' );
}
/**
* Automatically (forcibly) accept validation errors that arise.
*
* @since 1.0
* @see AMP_Core_Theme_Sanitizer::get_acceptable_errors()
*
* @param array|true $acceptable_errors Acceptable validation errors, where keys are codes and values are either `true` or sparse array to check as subset. If just true, then all validation errors are accepted.
*/
public static function accept_validation_errors( $acceptable_errors ) {
if ( empty( $acceptable_errors ) ) {
return;
}
add_filter( 'amp_validation_error_sanitized', function( $sanitized, $error ) use ( $acceptable_errors ) {
if ( true === $acceptable_errors ) {
return true;
}
if ( isset( $acceptable_errors[ $error['code'] ] ) ) {
if ( true === $acceptable_errors[ $error['code'] ] ) {
return true;
}
foreach ( $acceptable_errors[ $error['code'] ] as $acceptable_error_props ) {
if ( AMP_Validation_Error_Taxonomy::is_array_subset( $error, $acceptable_error_props ) ) {
return true;
}
}
}
return $sanitized;
}, 10, 2 );
}
/**
* Check if one array is a sparse subset of another array.
*
* @param array $superset Superset array.
* @param array $subset Subset array.
*
* @return bool Whether subset is contained in superset.
*/
public static function is_array_subset( $superset, $subset ) {
foreach ( $subset as $key => $subset_value ) {
if ( ! isset( $superset[ $key ] ) || gettype( $subset_value ) !== gettype( $superset[ $key ] ) ) {
return false;
}
if ( is_array( $subset_value ) ) {
if ( ! self::is_array_subset( $superset[ $key ], $subset_value ) ) {
return false;
}
} elseif ( $superset[ $key ] !== $subset_value ) {
return false;
}
}
return true;
}
/**
* Get the count of validation error terms, optionally restricted by term group (e.g. accepted or rejected).
*
* @param array $args {
* Args passed into wp_count_terms().
*
* @type int|null $group Term group.
* }
* @return int Term count.
*/
public static function get_validation_error_count( $args = array() ) {
$args = array_merge(
array(
'group' => null,
),
$args
);
$filter = function( $clauses ) use ( $args ) {
global $wpdb;
$clauses['where'] .= $wpdb->prepare( ' AND t.term_group = %d', $args['group'] );
return $clauses;
};
if ( isset( $args['group'] ) ) {
add_filter( 'terms_clauses', $filter );
}
self::$should_filter_terms_clauses_for_error_validation_status = false;
$term_count = wp_count_terms( self::TAXONOMY_SLUG, $args );
self::$should_filter_terms_clauses_for_error_validation_status = true;
if ( isset( $args['group'] ) ) {
remove_filter( 'terms_clauses', $filter );
}
return $term_count;
}
/**
* Add support for querying posts by amp_validation_error_status.
*
* Add recognition of amp_validation_error_status query var for amp_invalid_url post queries.
*
* @see WP_Tax_Query::get_sql_for_clause()
*
* @param string $where SQL WHERE clause.
* @param WP_Query $query Query.
* @return string Modified WHERE clause.
*/
public static function filter_posts_where_for_validation_error_status( $where, WP_Query $query ) {
global $wpdb;
if (
in_array( AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG, (array) $query->get( 'post_type' ), true )
&&
is_numeric( $query->get( self::VALIDATION_ERROR_STATUS_QUERY_VAR ) )
) {
$where .= $wpdb->prepare(
" AND (
SELECT 1
FROM $wpdb->term_relationships
INNER JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
INNER JOIN $wpdb->terms ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id
WHERE
$wpdb->term_taxonomy.taxonomy = %s
AND
$wpdb->term_relationships.object_id = $wpdb->posts.ID
AND
$wpdb->terms.term_group = %d
LIMIT 1
)",
self::TAXONOMY_SLUG,
$query->get( self::VALIDATION_ERROR_STATUS_QUERY_VAR )
);
}
return $where;
}
/**
* Gets the AMP validation response.
*
* Returns the current validation errors the sanitizers found in rendering the page.
*
* @param array $validation_errors Validation errors.
* @return array The AMP validity of the markup.
*/
public static function summarize_validation_errors( $validation_errors ) {
$results = array();
$removed_elements = array();
$removed_attributes = array();
$invalid_sources = array();
foreach ( $validation_errors as $validation_error ) {
$code = isset( $validation_error['code'] ) ? $validation_error['code'] : null;
if ( self::INVALID_ELEMENT_CODE === $code ) {
if ( ! isset( $removed_elements[ $validation_error['node_name'] ] ) ) {
$removed_elements[ $validation_error['node_name'] ] = 0;
}
$removed_elements[ $validation_error['node_name'] ] += 1;
} elseif ( self::INVALID_ATTRIBUTE_CODE === $code ) {
if ( ! isset( $removed_attributes[ $validation_error['node_name'] ] ) ) {
$removed_attributes[ $validation_error['node_name'] ] = 0;
}
$removed_attributes[ $validation_error['node_name'] ] += 1;
}
if ( ! empty( $validation_error['sources'] ) ) {
$source = array_shift( $validation_error['sources'] );
if ( isset( $source['type'], $source['name'] ) ) {
$invalid_sources[ $source['type'] ][] = $source['name'];
}
}
}
$results = array_merge(
array(
self::SOURCES_INVALID_OUTPUT => $invalid_sources,
),
compact(
'removed_elements',
'removed_attributes'
),
$results
);
return $results;
}
/**
* Add admin hooks.
*/
public static function add_admin_hooks() {
add_action( 'load-edit-tags.php', array( __CLASS__, 'add_group_terms_clauses_filter' ) );
add_action( 'load-edit-tags.php', function() {
add_filter( 'user_has_cap', array( __CLASS__, 'filter_user_has_cap_for_hiding_term_list_table_checkbox' ), 10, 3 );
} );
add_filter( 'terms_clauses', array( __CLASS__, 'filter_terms_clauses_for_description_search' ), 10, 3 );
add_action( 'admin_notices', array( __CLASS__, 'add_admin_notices' ) );
add_filter( 'tag_row_actions', array( __CLASS__, 'filter_tag_row_actions' ), 10, 2 );
add_action( 'admin_menu', array( __CLASS__, 'add_admin_menu_validation_error_item' ) );
add_filter( 'manage_' . self::TAXONOMY_SLUG . '_custom_column', array( __CLASS__, 'filter_manage_custom_columns' ), 10, 3 );
add_filter( 'views_edit-' . self::TAXONOMY_SLUG, array( __CLASS__, 'filter_views_edit' ) );
add_filter( 'posts_where', array( __CLASS__, 'filter_posts_where_for_validation_error_status' ), 10, 2 );
add_filter( 'handle_bulk_actions-edit-' . self::TAXONOMY_SLUG, array( __CLASS__, 'handle_validation_error_update' ), 10, 3 );
add_action( 'load-edit-tags.php', array( __CLASS__, 'handle_inline_edit_request' ) );
// Prevent query vars from persisting after redirect.
add_filter( 'removable_query_args', function( $query_vars ) {
$query_vars[] = 'amp_actioned';
$query_vars[] = 'amp_actioned_count';
$query_vars[] = 'amp_validation_errors_not_deleted';
return $query_vars;
} );
// Add recognition of amp_validation_error_status query var (which will only apply in admin since post type is not publicly_queryable).
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_STATUS_QUERY_VAR;
return $query_vars;
} );
// Always exclude taxonomy terms when they have empty counts.
add_filter( 'get_terms_args', function( $args, $taxonomies ) {
if ( array( AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG ) === $taxonomies ) {
$args['hide_empty'] = true;
}
return $args;
}, 10, 2 );
// Default ordering terms by ID descending so that new terms appear at the top.
add_filter( 'get_terms_defaults', function( $args, $taxonomies ) {
if ( array( AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG ) === $taxonomies ) {
$args['orderby'] = 'term_id';
$args['order'] = 'DESC';
}
return $args;
}, 10, 2 );
// Add bulk actions.
add_filter( 'bulk_actions-edit-' . self::TAXONOMY_SLUG, function( $bulk_actions ) {
unset( $bulk_actions['delete'] );
$bulk_actions[ AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACCEPT_ACTION ] = __( 'Accept', 'amp' );
$bulk_actions[ AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_REJECT_ACTION ] = __( 'Reject', 'amp' );
return $bulk_actions;
} );
// Override the columns displayed for the validation error terms.
add_filter( 'manage_edit-' . self::TAXONOMY_SLUG . '_columns', function( $old_columns ) {
return array(
'cb' => $old_columns['cb'],
'error' => __( 'Error', 'amp' ),
'created_date_gmt' => __( 'Created Date', 'amp' ),
'status' => __( 'Status', 'amp' ),
'details' => __( 'Details', 'amp' ),
'posts' => __( 'URLs', 'amp' ),
);
} );
// Let the created date column sort by term ID.
add_filter( 'manage_edit-' . self::TAXONOMY_SLUG . '_sortable_columns', function( $sortable_columns ) {
$sortable_columns['created_date_gmt'] = 'term_id';
return $sortable_columns;
} );
// Hide empty term addition form.
add_action( 'admin_enqueue_scripts', function() {
if ( AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG === get_current_screen()->taxonomy ) {
wp_add_inline_style( 'common', '
#col-left { display: none; }
#col-right { float:none; width: auto; }
/* Improve column widths */
td.column-details pre, td.column-sources pre { overflow:auto; }
th.column-created_date_gmt { width:15%; }
th.column-status { width:10%; }
' );
}
} );
// Make sure parent menu item is expanded when visiting the taxonomy term page.
add_filter( 'parent_file', function( $parent_file ) {
if ( get_current_screen()->taxonomy === AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG ) {
$parent_file = AMP_Options_Manager::OPTION_NAME;
}
return $parent_file;
}, 10, 2 );
// Replace the primary column to be error instead of the removed name column..
add_filter( 'list_table_primary_column', function( $primary_column ) {
if ( get_current_screen() && AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG === get_current_screen()->taxonomy ) {
$primary_column = 'error';
}
return $primary_column;
} );
}
/**
* Filter amp_validation_error term query by term group when requested.
*/
public static function add_group_terms_clauses_filter() {
if ( self::TAXONOMY_SLUG !== get_current_screen()->taxonomy || ! isset( $_GET[ self::VALIDATION_ERROR_STATUS_QUERY_VAR ] ) ) { // WPCS: CSRF ok.
return;
}
self::$should_filter_terms_clauses_for_error_validation_status = true;
$group = intval( $_GET[ self::VALIDATION_ERROR_STATUS_QUERY_VAR ] ); // WPCS: CSRF ok.
if ( ! in_array( $group, array( self::VALIDATION_ERROR_NEW_STATUS, self::VALIDATION_ERROR_ACCEPTED_STATUS, self::VALIDATION_ERROR_REJECTED_STATUS ), true ) ) {
return;
}
add_filter( 'terms_clauses', function( $clauses, $taxonomies ) use ( $group ) {
global $wpdb;
if ( AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG === $taxonomies[0] && AMP_Validation_Error_Taxonomy::$should_filter_terms_clauses_for_error_validation_status ) {
$clauses['where'] .= $wpdb->prepare( ' AND t.term_group = %d', $group );
}
return $clauses;
}, 10, 2 );
}
/**
* Prevent user from being able to delete validation errors in order to disable the checkbox on the post list table.
*
* Yes, this is not ideal.
*
* @param array $allcaps All caps.
* @param array $caps Requested caps.
* @param array $args Cap args.
* @return array All caps.
*/
public static function filter_user_has_cap_for_hiding_term_list_table_checkbox( $allcaps, $caps, $args ) {
unset( $caps );
if ( isset( $args[0] ) && 'delete_term' === $args[0] ) {
$term = get_term( $args[2] );
$error = json_decode( $term->description, true );
if ( ! is_array( $error ) ) {
return $allcaps;
}
}
return $allcaps;
}
/**
* Include searching taxonomy term descriptions and sources term meta.
*
* @param array $clauses Clauses.
* @param array $taxonomies Taxonomies.
* @param array $args Args.
* @return array Clauses.
*/
public static function filter_terms_clauses_for_description_search( $clauses, $taxonomies, $args ) {
global $wpdb;
if ( ! empty( $args['search'] ) && in_array( self::TAXONOMY_SLUG, $taxonomies, true ) ) {
$clauses['where'] = preg_replace(
'#(?<=\()(?=\(t\.name LIKE \')#',
$wpdb->prepare( '(tt.description LIKE %s) OR ', '%' . $wpdb->esc_like( $args['search'] ) . '%' ),
$clauses['where']
);
}
return $clauses;
}
/**
* Show notices for changes to amp_validation_error terms.
*/
public static function add_admin_notices() {
if ( ! ( self::TAXONOMY_SLUG === get_current_screen()->taxonomy || AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG === get_current_screen()->post_type ) || empty( $_GET['amp_actioned'] ) || empty( $_GET['amp_actioned_count'] ) ) { // WPCS: CSRF ok.
return;
}
$actioned = sanitize_key( $_GET['amp_actioned'] ); // WPCS: CSRF ok.
$count = intval( $_GET['amp_actioned_count'] ); // WPCS: CSRF ok.
$message = null;
if ( self::VALIDATION_ERROR_ACCEPT_ACTION === $actioned ) {
$message = sprintf(
/* translators: %s is number of errors accepted */
_n(
'Accepted %s error. It will no longer block related URLs from being served as AMP.',
'Accepted %s errors. They will no longer block related URLs from being served as AMP.',
number_format_i18n( $count ),
'amp'
),
$count
);
} elseif ( self::VALIDATION_ERROR_REJECT_ACTION === $actioned ) {
$message = sprintf(
/* translators: %s is number of errors rejected */
_n(
'Rejected %s error. It will continue to block related URLs from being served as AMP.',
'Rejected %s errors. They will continue to block related URLs from being served as AMP.',
number_format_i18n( $count ),
'amp'
),
$count
);
}
if ( $message ) {
printf( '<div class="notice notice-success is-dismissible"><p>%s</p></div>', esc_html( $message ) );
}
}
/**
* Add row actions.
*
* @param array $actions Actions.
* @param WP_Term $tag Tag.
* @return array Actions.
*/
public static function filter_tag_row_actions( $actions, WP_Term $tag ) {
if ( self::TAXONOMY_SLUG === $tag->taxonomy ) {
$term_id = $tag->term_id;
$term = get_term( $tag->term_id ); // We don't want filter=display given by $tag.
/*
* Hide deletion link since a validation error should only be removed once
* it no longer has an occurrence on the site. When an invalid URL is re-checked
* and it no longer has this validation error, then the count will be decremented.
* When a validation error term no longer has a count, then it is hidden from the
* list table. A cron job could periodically delete terms that have no counts.
*/
unset( $actions['delete'] );
$sanitization = self::get_validation_error_sanitization( json_decode( $term->description, true ) );
if ( self::VALIDATION_ERROR_REJECTED_STATUS !== $sanitization['term_status'] ) {
$actions[ self::VALIDATION_ERROR_REJECT_ACTION ] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
wp_nonce_url(
add_query_arg( array_merge( array( 'action' => self::VALIDATION_ERROR_REJECT_ACTION ), compact( 'term_id' ) ) ),
self::VALIDATION_ERROR_REJECT_ACTION
),
esc_attr__( 'Rejecting an error acknowledges that it should block a URL from being served as AMP.', 'amp' ),
esc_html__( 'Reject', 'amp' )
);
}
if ( self::VALIDATION_ERROR_ACCEPTED_STATUS !== $sanitization['term_status'] ) {
$actions[ self::VALIDATION_ERROR_ACCEPT_ACTION ] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
wp_nonce_url(
add_query_arg( array_merge( array( 'action' => self::VALIDATION_ERROR_ACCEPT_ACTION ), compact( 'term_id' ) ) ),
self::VALIDATION_ERROR_ACCEPT_ACTION
),
esc_attr__( 'Accepting an error means it will get sanitized and not block a URL from being served as AMP.', 'amp' ),
esc_html__( 'Accept', 'amp' )
);
}
}
return $actions;
}
/**
* Show AMP validation errors under AMP admin menu.
*/
public static function add_admin_menu_validation_error_item() {
$menu_item_label = esc_html__( 'Validation Errors', 'amp' );
$new_error_count = self::get_validation_error_count( array(
'group' => self::VALIDATION_ERROR_NEW_STATUS,
) );
if ( $new_error_count ) {
$menu_item_label .= ' <span class="awaiting-mod"><span class="pending-count">' . esc_html( number_format_i18n( $new_error_count ) ) . '</span></span>';
}
$taxonomy_caps = (object) get_taxonomy( self::TAXONOMY_SLUG )->cap; // Yes, cap is an object not an array.
add_submenu_page(
AMP_Options_Manager::OPTION_NAME,
esc_html__( 'Validation Errors', 'amp' ),
$menu_item_label,
$taxonomy_caps->manage_terms,
// The following esc_attr() is sadly needed due to <https://github.com/WordPress/wordpress-develop/blob/4.9.5/src/wp-admin/menu-header.php#L201>.
esc_attr( 'edit-tags.php?taxonomy=' . self::TAXONOMY_SLUG . '&post_type=' . AMP_Invalid_URL_Post_Type::POST_TYPE_SLUG )
);
}
/**
* Add views for filtering validation errors by status.
*
* @param array $views Views.
* @return array Views.
*/
public static function filter_views_edit( $views ) {
$total_term_count = self::get_validation_error_count();
$rejected_term_count = self::get_validation_error_count( array( 'group' => self::VALIDATION_ERROR_REJECTED_STATUS ) );
$accepted_term_count = self::get_validation_error_count( array( 'group' => self::VALIDATION_ERROR_ACCEPTED_STATUS ) );
$new_term_count = $total_term_count - $rejected_term_count - $accepted_term_count;
$current_url = remove_query_arg(
array_merge(
wp_removable_query_args(),
array( 's' ) // For some reason behavior of posts list table is to not persist the search query.
),
wp_unslash( $_SERVER['REQUEST_URI'] )
);
$current_status = null;
if ( isset( $_GET[ self::VALIDATION_ERROR_STATUS_QUERY_VAR ] ) ) { // WPCS: CSRF ok.
$value = intval( $_GET[ self::VALIDATION_ERROR_STATUS_QUERY_VAR ] ); // WPCS: CSRF ok.
if ( in_array( $value, array( self::VALIDATION_ERROR_NEW_STATUS, self::VALIDATION_ERROR_ACCEPTED_STATUS, self::VALIDATION_ERROR_REJECTED_STATUS ), true ) ) {
$current_status = $value;
}
}
$views['all'] = sprintf(
'<a href="%s" class="%s">%s</a>',
esc_url( remove_query_arg( self::VALIDATION_ERROR_STATUS_QUERY_VAR, $current_url ) ),
null === $current_status ? 'current' : '',
sprintf(
/* translators: %s: the term count. */
_nx(
'All <span class="count">(%s)</span>',
'All <span class="count">(%s)</span>',
$total_term_count,
'terms',
'amp'
),
number_format_i18n( $total_term_count )
)
);
$views['new'] = sprintf(
'<a href="%s" class="%s">%s</a>',
esc_url(
add_query_arg(
self::VALIDATION_ERROR_STATUS_QUERY_VAR,
self::VALIDATION_ERROR_NEW_STATUS,
$current_url
)
),
self::VALIDATION_ERROR_NEW_STATUS === $current_status ? 'current' : '',
sprintf(
/* translators: %s: the term count. */
_nx(
'New <span class="count">(%s)</span>',
'New <span class="count">(%s)</span>',
$new_term_count,
'terms',
'amp'
),
number_format_i18n( $new_term_count )
)
);
$views['rejected'] = sprintf(
'<a href="%s" class="%s">%s</a>',
esc_url(
add_query_arg(
self::VALIDATION_ERROR_STATUS_QUERY_VAR,
self::VALIDATION_ERROR_REJECTED_STATUS,
$current_url
)
),
self::VALIDATION_ERROR_REJECTED_STATUS === $current_status ? 'current' : '',
sprintf(
/* translators: %s: the term count. */
_nx(
'Rejected <span class="count">(%s)</span>',
'Rejected <span class="count">(%s)</span>',
$rejected_term_count,
'terms',
'amp'
),
number_format_i18n( $rejected_term_count )
)
);
$views['accepted'] = sprintf(
'<a href="%s" class="%s">%s</a>',
esc_url(
add_query_arg(
self::VALIDATION_ERROR_STATUS_QUERY_VAR,
self::VALIDATION_ERROR_ACCEPTED_STATUS,
$current_url
)
),
self::VALIDATION_ERROR_ACCEPTED_STATUS === $current_status ? 'current' : '',
sprintf(
/* translators: %s: the term count. */
_nx(
'Accepted <span class="count">(%s)</span>',
'Accepted <span class="count">(%s)</span>',
$accepted_term_count,
'terms',
'amp'
),
number_format_i18n( $accepted_term_count )
)
);
return $views;
}
/**
* Supply the content for the custom columns.
*
* @param string $content Column content.
* @param string $column_name Column name.
* @param int $term_id Term ID.
* @return string Content.
*/
public static function filter_manage_custom_columns( $content, $column_name, $term_id ) {
$term = get_term( $term_id );
$validation_error = json_decode( $term->description, true );
if ( ! isset( $validation_error['code'] ) ) {
$validation_error['code'] = 'unknown';
}
switch ( $column_name ) {
case 'error':
$content .= '<p>';
$content .= sprintf( '<code>%s</code>', esc_html( $validation_error['code'] ) );
if ( 'invalid_element' === $validation_error['code'] || 'invalid_attribute' === $validation_error['code'] ) {
$content .= sprintf( ': <code>%s</code>', esc_html( $validation_error['node_name'] ) );
}
$content .= '</p>';
if ( isset( $validation_error['message'] ) ) {
$content .= sprintf( '<p>%s</p>', esc_html( $validation_error['message'] ) );
}
break;
case 'status':
$sanitization = self::get_validation_error_sanitization( $validation_error );
if ( self::VALIDATION_ERROR_ACCEPTED_STATUS === $sanitization['term_status'] ) {
if ( $sanitization['forced'] && $sanitization['term_status'] !== $sanitization['status'] ) {
$content .= '🚩';
} else {
$content .= '✅';
}
$content .= ' ' . esc_html__( 'Accepted', 'amp' );
} elseif ( self::VALIDATION_ERROR_REJECTED_STATUS === $sanitization['term_status'] ) {
if ( $sanitization['forced'] && $sanitization['term_status'] !== $sanitization['status'] ) {
$content .= '🚩';
} else {
$content .= '❌';
}
$content .= ' ' . esc_html__( 'Rejected', 'amp' );
} else {
$content = '❓ ' . esc_html__( 'New', 'amp' );
}
break;
case 'created_date_gmt':
$created_datetime = null;
$created_date_gmt = get_term_meta( $term_id, 'created_date_gmt', true );
if ( $created_date_gmt ) {
try {
$created_datetime = new DateTime( $created_date_gmt, new DateTimeZone( 'UTC' ) );
$timezone_string = get_option( 'timezone_string' );
if ( ! $timezone_string && get_option( 'gmt_offset' ) ) {
$timezone_string = timezone_name_from_abbr( '', get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, false );
}
if ( $timezone_string ) {
$created_datetime->setTimezone( new DateTimeZone( get_option( 'timezone_string' ) ) );
}
} catch ( Exception $e ) {
unset( $e );
}
}
if ( ! $created_datetime ) {
$time_ago = __( 'n/a', 'amp' );
} elseif ( time() - $created_datetime->getTimestamp() < DAY_IN_SECONDS ) {
$time_ago = sprintf(
'<abbr title="%s">%s</abbr>',
esc_attr( $created_datetime->format( __( 'Y/m/d g:i:s a', 'default' ) ) ),
/* translators: %s is relative time */
esc_html( sprintf( __( '%s ago', 'default' ), human_time_diff( $created_datetime->getTimestamp() ) ) )
);
} else {
$time_ago = mysql2date( __( 'Y/m/d g:i:s a', 'default' ), $created_date_gmt );
}
if ( $created_datetime ) {
$time_ago = sprintf(
'<time datetime="%s">%s</time>',
$created_datetime->format( 'c' ),
$time_ago
);
}
$content .= $time_ago;
break;
case 'details':
unset( $validation_error['code'] );
unset( $validation_error['message'] );
$content = sprintf( '<pre>%s</pre>', esc_html( wp_json_encode( $validation_error, 128 /* JSON_PRETTY_PRINT */ | 64 /* JSON_UNESCAPED_SLASHES */ ) ) );
break;
}
return $content;
}
/**
* Handle inline edit links.
*/
public static function handle_inline_edit_request() {
if ( self::TAXONOMY_SLUG !== get_current_screen()->taxonomy || ! isset( $_GET['action'] ) || ! isset( $_GET['_wpnonce'] ) || ! isset( $_GET['term_id'] ) ) { // WPCS: CSRF ok.
return;
}
$action = sanitize_key( $_GET['action'] ); // WPCS: CSRF ok.
check_admin_referer( $action );
$taxonomy_caps = (object) get_taxonomy( self::TAXONOMY_SLUG )->cap; // Yes, cap is an object not an array.
if ( ! current_user_can( $taxonomy_caps->manage_terms ) ) {
return;
}
$referer = wp_get_referer();
$term_id = intval( $_GET['term_id'] ); // WPCS: CSRF ok.
$redirect = self::handle_validation_error_update( $referer, $action, array( $term_id ) );
if ( $redirect !== $referer ) {
wp_safe_redirect( $redirect );
exit;
}
}
/**
* Handle bulk and inline edits to amp_validation_error terms.
*
* @param string $redirect_to Redirect to.
* @param string $action Action.
* @param int[] $term_ids Term IDs.
*
* @return string Redirect.
*/
public static function handle_validation_error_update( $redirect_to, $action, $term_ids ) {
$term_group = null;
if ( self::VALIDATION_ERROR_ACCEPT_ACTION === $action ) {
$term_group = self::VALIDATION_ERROR_ACCEPTED_STATUS;
} elseif ( self::VALIDATION_ERROR_REJECT_ACTION === $action ) {
$term_group = self::VALIDATION_ERROR_REJECTED_STATUS;
}
if ( $term_group ) {
$has_pre_term_description_filter = has_filter( 'pre_term_description', 'wp_filter_kses' );
if ( false !== $has_pre_term_description_filter ) {
remove_filter( 'pre_term_description', 'wp_filter_kses', $has_pre_term_description_filter );
}
foreach ( $term_ids as $term_id ) {
wp_update_term( $term_id, self::TAXONOMY_SLUG, compact( 'term_group' ) );
}
if ( false !== $has_pre_term_description_filter ) {
add_filter( 'pre_term_description', 'wp_filter_kses', $has_pre_term_description_filter );
}
$redirect_to = add_query_arg(
array(
'amp_actioned' => $action,
'amp_actioned_count' => count( $term_ids ),