-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-plugin-directory.php
More file actions
1595 lines (1374 loc) · 56 KB
/
Copy pathclass-plugin-directory.php
File metadata and controls
1595 lines (1374 loc) · 56 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
namespace WordPressdotorg\Plugin_Directory;
use WordPressdotorg\Plugin_Directory\Admin\Customizations;
use WordPressdotorg\Plugin_Directory\Tools;
use WordPressdotorg\Plugin_Directory\Admin\Tools\Author_Cards;
use WordPressdotorg\Plugin_Directory\Admin\Tools\Stats_Report;
/**
* The main Plugin Directory class, it handles most of the bootstrap and basic operations of the plugin.
*
* @package WordPressdotorg\Plugin_Directory
*/
class Plugin_Directory {
/**
* Fetch the instance of the Plugin_Directory class.
*
* @static
*/
public static function instance() {
static $instance = null;
return ! is_null( $instance ) ? $instance : $instance = new Plugin_Directory();
}
/**
* Plugin_Directory constructor.
*
* @access private
*/
private function __construct() {
add_action( 'init', array( $this, 'init' ) );
add_action( 'init', array( $this, 'register_shortcodes' ) );
add_action( 'init', array( $this, 'remove_other_shortcodes' ), 999 );
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
add_filter( 'post_type_link', array( $this, 'filter_post_type_link' ), 10, 2 );
add_filter( 'term_link', array( $this, 'filter_term_link' ), 10, 2 );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
add_filter( 'found_posts', array( $this, 'filter_found_posts' ), 10, 2 );
add_filter( 'rest_api_allowed_post_types', array( $this, 'filter_allowed_post_types' ) );
add_filter( 'pre_update_option_jetpack_options', array( $this, 'filter_jetpack_options' ) );
add_filter( 'jetpack_sitemap_post_types', array( $this, 'jetpack_sitemap_post_types' ) );
add_filter( 'jetpack_sitemap_skip_post', array( $this, 'jetpack_sitemap_skip_post' ), 10, 2 );
add_action( 'template_redirect', array( $this, 'prevent_canonical_for_plugins' ), 9 );
add_action( 'template_redirect', array( $this, 'custom_redirects' ), 1 );
add_action( 'template_redirect', array( $this, 'geopattern_icon_route' ), 0 );
add_filter( 'query_vars', array( $this, 'filter_query_vars' ) );
add_filter( 'single_term_title', array( $this, 'filter_single_term_title' ) );
add_filter( 'the_content', array( $this, 'filter_rel_nofollow_ugc' ) );
add_action( 'wp_head', array( Template::class, 'json_ld_schema' ), 1 );
add_action( 'wp_head', array( Template::class, 'hreflang_link_attributes' ), 2 );
// Add no-index headers where appropriate.
add_filter( 'wporg_noindex_request', [ Template::class, 'should_noindex_request' ] );
// Fix the Canonical link when needed.
add_action( 'wporg_canonical_url', [ Template::class, 'wporg_canonical_url' ] );
// Cron tasks.
new Jobs\Manager();
// Add upload size limit to limit plugin ZIP file uploads to 10M
add_filter( 'upload_size_limit', function( $size ) {
return 10 * MB_IN_BYTES;
} );
// oEmbed whitlisting.
add_filter( 'embed_oembed_discover', '__return_false' );
add_filter( 'oembed_providers', array( $this, 'oembed_whitelist' ) );
// Capability mapping
add_filter( 'map_meta_cap', array( __NAMESPACE__ . '\Capabilities', 'map_meta_cap' ), 10, 4 );
// Load the API routes.
add_action( 'rest_api_init', array( __NAMESPACE__ . '\API\Base', 'init' ) );
// Allow post_modified not to be modified when we don't specifically bump it.
add_filter( 'wp_insert_post_data', array( $this, 'filter_wp_insert_post_data' ), 10, 2 );
add_filter( 'jetpack_active_modules', function( $modules ) {
// Disable Jetpack Search
if ( false !== ( $i = array_search( 'search', $modules ) ) ) {
unset( $modules[$i] );
}
// Disable Jetpack Sitemaps on Rosetta sites.
if ( !empty( $GLOBALS['rosetta'] ) ) {
if ( false !== ( $i = array_search( 'sitemaps', $modules ) ) ) {
unset( $modules[$i] );
}
}
return $modules;
} );
// Work around caching issues
add_filter( 'pre_option_jetpack_sync_full__started', array( $this, 'bypass_options_cache' ), 10, 2 );
add_filter( 'default_option_jetpack_sync_full__started', '__return_null' );
add_filter( 'pre_option_jetpack_sync_full__params', array( $this, 'bypass_options_cache' ), 10, 2 );
add_filter( 'default_option_jetpack_sync_full__params', '__return_null' );
add_filter( 'pre_option_jetpack_sync_full__queue_finished', array( $this, 'bypass_options_cache' ), 10, 2 );
add_filter( 'default_option_jetpack_sync_full__queue_finished', '__return_null' );
add_filter( 'pre_option_jetpack_sync_full__send_started', array( $this, 'bypass_options_cache' ), 10, 2 );
add_filter( 'default_option_jetpack_sync_full__send_started', '__return_null' );
add_filter( 'pre_option_jetpack_sync_full__finished', array( $this, 'bypass_options_cache' ), 10, 2 );
add_filter( 'default_option_jetpack_sync_full__finished', '__return_null' );
// Fix login URLs in admin bar
add_filter( 'login_url', array( $this, 'fix_login_url' ), 10, 3 );
/*
* Load all Admin-specific items.
* Cannot be included on `admin_init` to allow access to menu hooks.
*/
if ( defined( 'WP_ADMIN' ) && WP_ADMIN ) {
Customizations::instance();
Author_Cards::instance();
Stats_Report::instance();
add_action( 'wp_insert_post_data', array( __NAMESPACE__ . '\Admin\Status_Transitions', 'can_change_post_status' ), 10, 2 );
add_action( 'transition_post_status', array( __NAMESPACE__ . '\Admin\Status_Transitions', 'instance' ) );
}
register_activation_hook( PLUGIN_FILE, array( $this, 'activate' ) );
register_deactivation_hook( PLUGIN_FILE, array( $this, 'deactivate' ) );
}
/**
* Filters `wp_insert_post()` to respect the presented data.
* This function overrides `wp_insert_post()`s constant updating of
* the post_modified fields.
*
* @param array $data The data to be inserted into the database.
* @param array $postarr The raw data passed to `wp_insert_post()`.
*
* @return array The data to insert into the database.
*/
public function filter_wp_insert_post_data( $data, $postarr ) {
if ( 'plugin' === $postarr['post_type'] ) {
$data['post_modified'] = $postarr['post_modified'];
$data['post_modified_gmt'] = $postarr['post_modified_gmt'];
}
return $data;
}
/**
* Set up the Plugin Directory.
*/
public function init() {
load_plugin_textdomain( 'wporg-plugins' );
wp_cache_add_global_groups( 'wporg-plugins' );
register_post_type( 'plugin', array(
'labels' => array(
'name' => __( 'Repo Plugins', 'wporg-plugins' ),
'singular_name' => __( 'Repo Plugin', 'wporg-plugins' ),
'menu_name' => __( 'Repo Plugins', 'wporg-plugins' ),
'add_new' => __( 'Add New', 'wporg-plugins' ),
'add_new_item' => __( 'Add New Plugin', 'wporg-plugins' ),
'new_item' => __( 'New Plugin', 'wporg-plugins' ),
'view_item' => __( 'View Plugin', 'wporg-plugins' ),
'search_items' => __( 'Search Plugins', 'wporg-plugins' ),
'not_found' => __( 'No plugins found', 'wporg-plugins' ),
'not_found_in_trash' => __( 'No plugins found in Trash', 'wporg-plugins' ),
// Context only available in admin, not in toolbar.
'edit_item' => is_admin() ? __( 'Editing Plugin: %s', 'wporg-plugins' ) : __( 'Edit Plugin', 'wporg-plugins' ),
),
'description' => __( 'A Repo Plugin', 'wporg-plugins' ),
'supports' => array( 'comments', 'author', 'custom-fields' ),
'public' => true,
'show_ui' => true,
'show_in_rest' => true,
'has_archive' => true,
'rewrite' => false,
'menu_icon' => 'dashicons-admin-plugins',
'capabilities' => array(
'edit_post' => 'plugin_edit',
'read_post' => 'read',
'edit_posts' => 'plugin_dashboard_access',
'edit_others_posts' => 'plugin_edit_others',
'publish_posts' => 'plugin_approve',
'read_private_posts' => 'do_not_allow',
'delete_posts' => is_super_admin() ? 'manage_options' : 'do_not_allow',
'create_posts' => 'do_not_allow',
),
) );
register_taxonomy( 'plugin_section', 'plugin', array(
'hierarchical' => true,
'query_var' => 'browse',
'rewrite' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'capabilities' => array(
'assign_terms' => 'plugin_set_section',
),
'labels' => array(
'name' => __( 'Browse', 'wporg-plugins' ),
),
) );
register_taxonomy( 'plugin_category', 'plugin', array(
'hierarchical' => true, /* for tax_input[] handling on post saves. */
'query_var' => 'plugin_category',
'rewrite' => array(
'hierarchical' => false,
'slug' => 'category',
'with_front' => false,
'ep_mask' => EP_TAGS,
),
'labels' => array(
'name' => __( 'Plugin Categories', 'wporg-plugins' ),
'singular_name' => __( 'Plugin Category', 'wporg-plugins' ),
'edit_item' => __( 'Edit Category', 'wporg-plugins' ),
'update_item' => __( 'Update Category', 'wporg-plugins' ),
'add_new_item' => __( 'Add New Category', 'wporg-plugins' ),
'new_item_name' => __( 'New Category Name', 'wporg-plugins' ),
'search_items' => __( 'Search Categories', 'wporg-plugins' ),
),
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'capabilities' => array(
'assign_terms' => 'plugin_set_category',
),
) );
register_taxonomy( 'plugin_built_for', 'plugin', array(
'hierarchical' => true, /* for tax_input[] handling on post saves. */
'query_var' => 'plugin_built_for',
'rewrite' => false,
'labels' => array(
'name' => __( 'Built For', 'wporg-plugins' ),
),
'public' => true,
'show_ui' => false,
'show_admin_column' => false,
'meta_box_cb' => false,
'capabilities' => array(
'assign_terms' => 'plugin_set_category',
),
) );
register_taxonomy( 'plugin_business_model', 'plugin', array(
'hierarchical' => true, /* for tax_input[] handling on post saves. */
'query_var' => 'plugin_business_model',
'rewrite' => false,
'labels' => array(
'name' => __( 'Business Model', 'wporg-plugins' ),
),
'public' => true,
'show_ui' => false,
'show_admin_column' => false,
'meta_box_cb' => false,
'capabilities' => array(
'assign_terms' => 'plugin_set_category',
),
) );
register_taxonomy( 'plugin_contributors', array( 'plugin', 'force-count-to-include-all-post_status' ), array(
'hierarchical' => false,
'query_var' => 'plugin_contributor',
'sort' => true,
'rewrite' => false,
'labels' => array(
'name' => __( 'Contributors', 'wporg-plugins' ),
'singular_name' => __( 'Contributor', 'wporg-plugins' ),
),
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'capabilities' => array(
'assign_terms' => 'do_not_allow',
),
) );
register_taxonomy( 'plugin_committers', array( 'plugin', 'force-count-to-include-all-post_status' ), array(
'hierarchical' => false,
'query_var' => 'plugin_committer',
'rewrite' => false,
'labels' => array(
'name' => __( 'Committers', 'wporg-plugins' ),
'singular_name' => __( 'Committer', 'wporg-plugins' ),
),
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'capabilities' => array(
'assign_terms' => 'do_not_allow',
),
) );
register_taxonomy( 'plugin_support_reps', array( 'plugin', 'force-count-to-include-all-post_status' ), array(
'hierarchical' => false,
'query_var' => 'plugin_support_rep',
'rewrite' => false,
'labels' => array(
'name' => __( 'Support Reps', 'wporg-plugins' ),
'singular_name' => __( 'Support Rep', 'wporg-plugins' ),
),
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'capabilities' => array(
'assign_terms' => 'do_not_allow',
),
) );
register_taxonomy( 'plugin_tags', array( 'plugin', 'force-count-to-include-all-post_status' ), array(
'hierarchical' => false,
'query_var' => 'plugin_tags',
'rewrite' => array(
'hierarchical' => false,
'slug' => 'tags',
'with_front' => false,
'ep_mask' => EP_TAGS,
),
'labels' => array(
'name' => __( 'Plugin Tags', 'wporg-plugins' ),
'singular_name' => __( 'Plugin Tag', 'wporg-plugins' ),
'edit_item' => __( 'Edit Tag', 'wporg-plugins' ),
'update_item' => __( 'Update Tag', 'wporg-plugins' ),
'add_new_item' => __( 'Add New Tag', 'wporg-plugins' ),
'new_item_name' => __( 'New Tag Name', 'wporg-plugins' ),
'search_items' => __( 'Search Tags', 'wporg-plugins' ),
),
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'meta_box_cb' => false,
'capabilities' => array(
'assign_terms' => 'do_not_allow',
),
) );
register_post_status( 'new', array(
'label' => _x( 'Pending Initial Review', 'plugin status', 'wporg-plugins' ),
'public' => false,
'show_in_admin_status_list' => current_user_can( 'plugin_review' ),
'label_count' => _n_noop( 'Pending Initial Review <span class="count">(%s)</span>', 'Pending Initial Review <span class="count">(%s)</span>', 'wporg-plugins' ),
) );
register_post_status( 'pending', array(
'label' => _x( 'Pending', 'plugin status', 'wporg-plugins' ),
'public' => false,
'show_in_admin_status_list' => current_user_can( 'plugin_review' ),
'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>', 'wporg-plugins' ),
) );
register_post_status( 'disabled', array(
'label' => _x( 'Disabled', 'plugin status', 'wporg-plugins' ),
'public' => true,
'show_in_admin_status_list' => current_user_can( 'plugin_disable' ),
'label_count' => _n_noop( 'Disabled <span class="count">(%s)</span>', 'Disabled <span class="count">(%s)</span>', 'wporg-plugins' ),
) );
register_post_status( 'approved', array(
'label' => _x( 'Approved', 'plugin status', 'wporg-plugins' ),
'public' => false,
'show_in_admin_status_list' => current_user_can( 'plugin_approve' ),
'label_count' => _n_noop( 'Approved <span class="count">(%s)</span>', 'Approved <span class="count">(%s)</span>', 'wporg-plugins' ),
) );
register_post_status( 'closed', array(
'label' => _x( 'Closed', 'plugin status', 'wporg-plugins' ),
'public' => true,
'show_in_admin_status_list' => current_user_can( 'plugin_close' ),
'label_count' => _n_noop( 'Closed <span class="count">(%s)</span>', 'Closed <span class="count">(%s)</span>', 'wporg-plugins' ),
) );
register_post_status( 'rejected', array(
'label' => _x( 'Rejected', 'plugin status', 'wporg-plugins' ),
'public' => false,
'show_in_admin_status_list' => current_user_can( 'plugin_reject' ),
'label_count' => _n_noop( 'Rejected <span class="count">(%s)</span>', 'Rejected <span class="count">(%s)</span>', 'wporg-plugins' ),
) );
/**
* TODO
* Use register_rest_field() to add array and object meta data to the API:
* ratings, upgrade_notice, contributors, screenshots, sections, assets_screenshots,
* assets_icons, assets_banners,
*/
register_meta( 'post', 'rating', array(
'type' => 'number',
'description' => __( 'Overall rating of the plugin.', 'wporg-plugins' ),
'single' => true,
// todo 'sanitize_callback' => 'absint',
'show_in_rest' => true,
) );
register_meta( 'post', 'active_installs', array(
'type' => 'integer',
'description' => __( 'Number of installations.', 'wporg-plugins' ),
'single' => true,
'sanitize_callback' => 'absint',
'show_in_rest' => true,
) );
register_meta( 'post', 'downloads', array(
'type' => 'integer',
'description' => __( 'Number of downloads.', 'wporg-plugins' ),
'single' => true,
'sanitize_callback' => 'absint',
'show_in_rest' => true,
) );
register_meta( 'post', 'tested', array(
'description' => __( 'The version of WordPress the plugin was tested with.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'absint',
'show_in_rest' => true,
) );
register_meta( 'post', 'requires', array(
'description' => __( 'The minimum version of WordPress the plugin needs to run.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'absint',
'show_in_rest' => true,
) );
register_meta( 'post', 'requires_php', array(
'description' => __( 'The minimum version of PHP the plugin needs to run.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'absint',
'show_in_rest' => true,
) );
register_meta( 'post', 'stable_tag', array(
'description' => __( 'Stable version of the plugin.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'absint',
'show_in_rest' => true,
) );
register_meta( 'post', 'donate_link', array(
'description' => __( 'Link to donate to the plugin.', 'wporg-plugins' ),
'single' => true,
'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
) );
register_meta( 'post', 'version', array(
'description' => __( 'Current stable version.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
) );
register_meta( 'post', 'header_name', array(
'description' => __( 'Name of the plugin.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
) );
register_meta( 'post', 'header_plugin_uri', array(
'description' => __( 'URL to the homepage of the plugin.', 'wporg-plugins' ),
'single' => true,
'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
) );
register_meta( 'post', 'header_name', array(
'description' => __( 'Name of the plugin.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
) );
register_meta( 'post', 'header_author', array(
'description' => __( 'Name of the plugin author.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
) );
register_meta( 'post', 'header_author_uri', array(
'description' => __( 'URL to the homepage of the author.', 'wporg-plugins' ),
'single' => true,
'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
) );
register_meta( 'post', 'header_description', array(
'description' => __( 'Description of the plugin.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
) );
register_meta( 'post', 'assets_icons', array(
'type' => 'UserDefinedarray',
'description' => __( 'Icon images of the plugin.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
) );
register_meta( 'post', 'assets_banners_color', array(
'description' => __( 'Fallback color for the plugin.', 'wporg-plugins' ),
'single' => true,
// TODO 'sanitize_callback' => 'esc_url_raw',
'show_in_rest' => true,
) );
register_meta( 'post', 'support_threads', array(
'type' => 'integer',
'description' => __( 'Amount of support threads for the plugin.', 'wporg-plugins' ),
'single' => true,
'sanitize_callback' => 'absint',
'show_in_rest' => true,
) );
register_meta( 'post', 'support_threads_resolved', array(
'type' => 'integer',
'description' => __( 'Amount of resolved support threads for the plugin.', 'wporg-plugins' ),
'single' => true,
'sanitize_callback' => 'absint',
'show_in_rest' => true,
) );
// Add the browse/* views.
add_rewrite_tag( '%browse%', '(featured|popular|beta|blocks|block|new|favorites|adopt-me)' );
add_permastruct( 'browse', 'browse/%browse%' );
// Create an archive for a users favorites too.
add_rewrite_rule( '^browse/favorites/([^/]+)$', 'index.php?browse=favorites&favorites_user=$matches[1]', 'top' );
// Add duplicate search rule which will be hit before the following old-plugin tab rules
add_rewrite_rule( '^search/([^/]+)/?$', 'index.php?s=$matches[1]', 'top' );
// Add a rule for generated plugin icons. geopattern-icon/demo.svg | geopattern-icon/demo_abc123.svg
add_rewrite_rule( '^geopattern-icon/([^/_]+)(_([a-f0-9]{6}))?\.svg$', 'index.php?name=$matches[1]&geopattern_icon=$matches[3]', 'top' );
// Handle plugin admin requests
add_rewrite_rule( '^([^/]+)/advanced/?$', 'index.php?name=$matches[1]&plugin_advanced=1', 'top' );
// Handle the old plugin tabs URLs.
add_rewrite_rule( '^([^/]+)/(installation|faq|screenshots|changelog|stats|developers|other_notes)/?$', 'index.php?redirect_plugin=$matches[1]&redirect_plugin_tab=$matches[2]', 'top' );
// Handle content for broken clients that send #'s to the server
add_rewrite_rule( '^([^/]+)/\#(.*)/?$', 'index.php?name=$matches[1]', 'top' );
// If changing capabilities around, uncomment this.
// Capabilities::add_roles();
// Remove the /admin$ redirect to wp-admin
remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
// disable feeds
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
add_filter( 'get_term', array( __NAMESPACE__ . '\I18n', 'translate_term' ) );
add_filter( 'the_content', array( $this, 'translate_post_content' ), 1, 2 );
add_filter( 'the_title', array( $this, 'translate_post_title' ), 1, 2 );
add_filter( 'single_post_title', array( $this, 'translate_post_title' ), 1, 2 );
add_filter( 'get_the_excerpt', array( $this, 'translate_post_excerpt' ), 1, 2 );
// Instantiate our copy of the Jetpack_Search class.
if ( class_exists( 'Jetpack' ) && \Jetpack::get_option( 'id' ) && ! class_exists( 'Jetpack_Search' )
&& ! isset( $_GET['s'] ) ) { // Don't run the ES query if we're going to redirect to the pretty search URL
require_once __DIR__ . '/libs/site-search/jetpack-search.php';
\Jetpack_Search::instance();
}
}
/**
* Register the Shortcodes used within the content.
*/
public function register_shortcodes() {
add_shortcode( 'wporg-plugins-developers', array( __NAMESPACE__ . '\Shortcodes\Developers', 'display' ) );
add_shortcode( 'wporg-plugin-upload', array( __NAMESPACE__ . '\Shortcodes\Upload', 'display' ) );
add_shortcode( 'wporg-plugins-screenshots', array( __NAMESPACE__ . '\Shortcodes\Screenshots', 'display' ) );
add_shortcode( 'wporg-plugins-reviews', array( __NAMESPACE__ . '\Shortcodes\Reviews', 'display' ) );
add_shortcode( 'readme-validator', array( __NAMESPACE__ . '\Shortcodes\Readme_Validator', 'display' ) );
}
/**
* deregister any shortcodes which we haven't explicitly allowed.
*/
public function remove_other_shortcodes() {
global $shortcode_tags;
$allowed_shortcodes = array(
'youtube',
'vimeo',
'wporg-plugins-developers',
'wporg-plugin-upload',
'wporg-plugins-screenshots',
'wporg-plugins-reviews',
'readme-validator',
);
$not_allowed_shortcodes = array_diff( array_keys( $shortcode_tags ), $allowed_shortcodes );
foreach ( $not_allowed_shortcodes as $tag ) {
remove_shortcode( $tag );
}
// remove special embed shortcode handling
remove_filter( 'the_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 );
}
/**
* Register the Widgets used plugin detail pages.
*/
public function register_widgets() {
register_widget( __NAMESPACE__ . '\Widgets\Donate' );
register_widget( __NAMESPACE__ . '\Widgets\Meta' );
register_widget( __NAMESPACE__ . '\Widgets\Ratings' );
register_widget( __NAMESPACE__ . '\Widgets\Support' );
register_widget( __NAMESPACE__ . '\Widgets\Committers' );
register_widget( __NAMESPACE__ . '\Widgets\Contributors' );
register_widget( __NAMESPACE__ . '\Widgets\Support_Reps' );
register_widget( __NAMESPACE__ . '\Widgets\Adopt_Me' );
}
/**
* Upon plugin activation, set up the current site for acting
* as the plugin directory.
*
* Setting up the site requires setting up the theme and proper
* rewrite permastructs.
*
* @global \WP_Rewrite $wp_rewrite WordPress rewrite component.
*/
public function activate() {
global $wp_rewrite;
// Setup the environment.
$this->init();
// %postname% is required.
$wp_rewrite->set_permalink_structure( '/%postname%/' );
// /tags/ & /category/ shouldn't conflict
$wp_rewrite->set_tag_base( '/post-tags' );
$wp_rewrite->set_category_base( '/post-categories' );
// Add our custom capabilitie and roles.
Capabilities::add_roles();
// We require the WordPress.org Ratings plugin also be active.
if ( ! is_plugin_active( 'wporg-ratings/wporg-ratings.php' ) ) {
activate_plugin( 'wporg-ratings/wporg-ratings.php' );
}
/**
* Enable the WordPress.org Plugin Repo Theme.
*
* @var \WP_Theme $theme
*/
foreach ( wp_get_themes() as $theme ) {
if ( $theme->get( 'Name' ) === 'WordPress.org Plugins' ) {
switch_theme( $theme->get_stylesheet() );
break;
}
}
flush_rewrite_rules();
do_action( 'wporg_plugins_activation' );
}
/**
* Clean up options & rewrite rules after plugin deactivation.
*/
public function deactivate() {
flush_rewrite_rules();
do_action( 'wporg_plugins_deactivation' );
}
/**
* Filter the permalink for the Plugins to be /plugin-name/.
*
* @param string $link The generated permalink.
* @param \WP_Post $post The Plugin post object.
* @return string
*/
public function filter_post_type_link( $link, $post ) {
if ( 'plugin' !== $post->post_type ) {
return $link;
}
return trailingslashit( home_url( $post->post_name ) );
}
/**
* Filter the permalink for terms to be more useful.
*
* @param string $term_link The generated term link.
* @param \WP_Term $term The term the link is for.
* @return string|false
*/
public function filter_term_link( $term_link, $term ) {
if ( 'plugin_business_model' == $term->taxonomy ) {
return false;
}
if ( 'plugin_built_for' == $term->taxonomy ) {
// Term slug = Post Slug = /%postname%/
return trailingslashit( home_url( $term->slug ) );
}
// browse/%
if ( 'plugin_section' == $term->taxonomy && 'favorites' == $term->slug ) {
return trailingslashit( home_url( 'browse/favorites/' . get_query_var( 'favorites_user' ) ) );
} elseif ( 'plugin_section' == $term->taxonomy ) {
return trailingslashit( home_url( 'browse/' . $term->slug ) );
}
// author/%
if ( 'plugin_contributors' == $term->taxonomy ) {
return trailingslashit( home_url( 'author/' . $term->slug ) );
}
return $term_link;
}
/**
* Filter content to make links rel="nofollow ugc" on plugin pages only
*
* @param string $content The content.
* @return string
*/
public function filter_rel_nofollow_ugc( $content ) {
if ( get_post_type() == 'plugin' ) {
// regex copied from wp_rel_ugc(). Not calling that function because it messes with slashes.
$content = preg_replace_callback(
'|<a (.+?)>|i',
function( $matches ) {
return wp_rel_callback( $matches, 'nofollow ugc' );
},
$content
);
}
return $content;
}
/**
* @param \WP_Query $wp_query The WordPress Query object.
*/
public function pre_get_posts( $wp_query ) {
if ( is_admin() ) {
return;
}
// Unless otherwise specified, we start off by querying for publish'd plugins.
if ( empty( $wp_query->query_vars['pagename'] ) && ( empty( $wp_query->query_vars['post_type'] ) || 'post' == $wp_query->query_vars['post_type'] ) ) {
$wp_query->query_vars['post_type'] = array( 'plugin' );
$wp_query->query_vars['post_status'] = array( 'publish' );
}
// By default, if no query is made, we're querying /browse/featured/
if ( empty( $wp_query->query ) ) {
$wp_query->query_vars['browse'] = 'featured';
}
// For any invalid values passed to browse, set it to featured instead
if ( !empty ( $wp_query->query ['browse'] ) &&
!in_array( $wp_query->query['browse'], array( 'featured', 'popular', 'beta', 'blocks', 'block', 'new', 'favorites', 'adopt-me' ) ) ) {
$wp_query->query['browse'] = 'featured';
$wp_query->query_vars['browse'] = 'featured';
}
// Set up custom queries for the /browse/ URLs
switch ( $wp_query->get( 'browse' ) ) {
case 'beta':
$wp_query->query_vars['meta_key'] = 'last_updated';
$wp_query->query_vars['orderby'] = 'meta_value';
$wp_query->query_vars['order'] = 'DESC';
break;
case 'favorites':
$favorites_user = wp_get_current_user();
if ( ! empty( $wp_query->query_vars['favorites_user'] ) ) {
$favorites_user = $wp_query->query_vars['favorites_user'];
} elseif ( ! empty( $_GET['favorites_user'] ) ) {
$favorites_user = $_GET['favorites_user'];
}
if ( ! $favorites_user instanceof \WP_User ) {
$favorites_user = get_user_by( 'slug', $favorites_user );
}
if ( $favorites_user ) {
$wp_query->query_vars['favorites_user'] = $favorites_user->user_nicename;
$wp_query->query_vars['post_name__in'] = get_user_meta( $favorites_user->ID, 'plugin_favorites', true );
$wp_query->query_vars['orderby'] = 'post_title';
$wp_query->query_vars['order'] = 'ASC';
}
if ( ! $favorites_user || ! $wp_query->query_vars['post_name__in'] ) {
$wp_query->query_vars['p'] = -1;
}
break;
case 'block':
case 'new':
$wp_query->query_vars['orderby'] = 'post_date';
break;
}
// For /browse/ requests, we conditionally need to avoid querying the taxonomy for most views (as it's handled in code above)
if ( isset( $wp_query->query['browse'] ) && ! in_array( $wp_query->query['browse'], array( 'beta', 'blocks', 'block', 'featured', 'adopt-me' ) ) ) {
unset( $wp_query->query_vars['browse'] );
add_filter( 'the_posts', function( $posts, $wp_query ) {
// Fix the queried object for the archive view.
if ( ! $wp_query->queried_object && isset( $wp_query->query['browse'] ) ) {
$wp_query->query_vars['browse'] = $wp_query->query['browse'];
$wp_query->queried_object = get_term_by( 'slug', $wp_query->query['browse'], 'plugin_section' );
}
return $posts;
}, 10, 2 );
}
// Holds a truthful value when viewing an author archive for the current user, or a plugin reviewer viewing an author archive
$viewing_own_author_archive = false;
// Author Archives need to be created
if ( $wp_query->is_main_query() && $wp_query->is_author() ) {
$user = isset( $wp_query->query['author_name'] ) ? $wp_query->query['author_name'] : get_user_by( 'id', $wp_query->query['author'] )->user_nicename;
$viewing_own_author_archive = is_user_logged_in() && ( current_user_can( 'plugin_review' ) || 0 === strcasecmp( $user, wp_get_current_user()->user_nicename ) );
// Author archives by default list plugins you're a contributor on.
$wp_query->query_vars['tax_query'] = array(
'relation' => 'OR',
array(
'taxonomy' => 'plugin_contributors',
'field' => 'slug',
'terms' => $user,
),
);
// Author archives for self include plugins you're a committer on, not just publically a contributor
// Plugin Reviewers also see plugins you're a committer on here.
if ( $viewing_own_author_archive ) {
$wp_query->query_vars['tax_query'][] = array(
'taxonomy' => 'plugin_committers',
'field' => 'slug',
'terms' => $user,
);
}
$wp_query->query_vars['orderby'] = 'post_title';
$wp_query->query_vars['order'] = 'ASC';
// Treat it as a taxonomy query now, not the author archive.
$wp_query->is_author = false;
$wp_query->is_tax = true;
unset( $wp_query->query_vars['author_name'], $wp_query->query_vars['author'] );
}
// For singular requests, or self-author profile requests allow restricted post_status items to show on the front-end.
if ( $wp_query->is_main_query() && ( $viewing_own_author_archive || is_user_logged_in() && ! empty( $wp_query->query_vars['name'] ) ) ) {
$wp_query->query_vars['post_status'] = array( 'approved', 'publish', 'closed', 'disabled' );
add_filter( 'posts_results', function( $posts, $this_wp_query ) use ( $wp_query ) {
if ( $this_wp_query != $wp_query ) {
return $posts;
}
// Published, closed, or disabled plugins shouldn't be affected by cap checks.
$restricted_access_statii = array_diff( $wp_query->query_vars['post_status'], array( 'publish', 'closed', 'disabled' ) );
foreach ( $posts as $i => $post ) {
// If the plugin is not in the restricted statuses list, show it
if ( 'plugin' != $post->post_type || ! in_array( $post->post_status, $restricted_access_statii, true ) ) {
continue;
}
// If the current user can view the plugin admin, show it
if ( current_user_can( 'plugin_admin_view', $post ) ) {
continue;
}
// Else hide it.
unset( $posts[ $i ] );
}
return $posts;
}, 10, 2 );
}
// Allow anyone to view a closed plugin directly from its page. It won't show in search results or lists.
if ( $wp_query->is_main_query() && ! empty( $wp_query->query_vars['name'] ) ) {
$wp_query->query_vars['post_status'] = (array) $wp_query->query_vars['post_status'];
$wp_query->query_vars['post_status'][] = 'closed';
$wp_query->query_vars['post_status'][] = 'disabled';
$wp_query->query_vars['post_status'] = array_unique( $wp_query->query_vars['post_status'] );
}
// Sanitize / cleanup the search query a little bit.
if ( $wp_query->is_search() ) {
$s = $wp_query->get( 's' );
$s = urldecode( $s );
// If a URL-like request comes in, reduce to a slug
if ( preg_match( '!^http.+/plugins/([^/]+)(/|$)!i', $s, $m ) ) {
$s = $m[1];
}
// Jetpack Search has a limit, limit to 200char. This is intentionally using ASCII length + Multibyte substr.
if ( strlen( $s ) > 200 ) {
$s = mb_substr( $s, 0, 200 );
}
// Trim off special characters, only allowing wordy characters at the end of searches.
$s = preg_replace( '!(\W+)$!iu', '', $s );
// ..and whitespace
$s = trim( $s );
$wp_query->set( 's', $s );
}
// By default, all archives are sorted by active installs
if ( $wp_query->is_archive() && empty( $wp_query->query_vars['orderby'] ) ) {
$wp_query->query_vars['orderby'] = 'meta_value_num';
$wp_query->query_vars['meta_key'] = '_active_installs';
}
}
/**
* Filter to limit the total number of found posts in browse queries.
* Stops search crawlers from paginating through the entire DB.
*/
public function filter_found_posts( $found_posts, $wp_query ) {
if ( isset( $wp_query->query['browse'] ) && is_array( $wp_query->query_vars['post_type'] ) && in_array( 'plugin', $wp_query->query_vars['post_type'] ) ) {
return min( $found_posts, 99 * $wp_query->query_vars['posts_per_page'] ); // 99 pages
}
return $found_posts;
}
/**
* Filter to bypass caching for options critical to Jetpack sync to work around race conditions and other unidentified bugs.
* If this works and becomes a permanent solution, it probably belongs elsewhere.
*/
public function bypass_options_cache( $value, $option ) {
global $wpdb;
$value = $wpdb->get_var( $wpdb->prepare(
"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
$option
) );
$value = maybe_unserialize( $value );
return $value;
}
/**
* Adjust the login URL to point back to whatever part of the support forums we're
* currently looking at. This allows the redirect to come back to the same place
* instead of the main /support URL by default.
*/
public function fix_login_url( $login_url, $redirect, $force_reauth ) {
// modify the redirect_to for the support forums to point to the current page
if ( 0 === strpos( $_SERVER['REQUEST_URI'], '/plugins' ) ) {
// Note that this is not normal because of the code in /mu-plugins/wporg-sso/class-wporg-sso.php.
// The login_url function there expects the redirect_to as the first parameter passed into it instead of the second
// Since we're changing this with a filter on login_url, then we have to change the login_url to the
// place we want to redirect instead, and then let the SSO plugin do the rest.
//
// If the SSO code gets fixed, this will need to be modified.
//
// parse_url is used here to remove any additional query args from the REQUEST_URI before redirection
// The SSO code handles the urlencoding of the redirect_to parameter
$url_parts = parse_url( set_url_scheme( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) );
$constructed_url = $url_parts['scheme'] . '://' . $url_parts['host'] . ( isset( $url_parts['path'] ) ? $url_parts['path'] : '' );
if ( class_exists( 'WPOrg_SSO' ) ) {
$login_url = $constructed_url;
} else {
$login_url = add_query_arg( 'redirect_to', urlencode( $constructed_url ), $login_url );
}
}
return $login_url;
}
/**
* Returns the requested page's content, translated.
*
* @param string $content Post content.
* @param string $section Optional. Which readme section to translate.
* @param int $post_id Optional. Post ID. Default: 0.
* @return string
*/
public function translate_post_content( $content, $section = null, $post_id = 0 ) {
if ( is_null( $section ) ) {
return $content;
}