-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathclass-import.php
More file actions
1754 lines (1517 loc) · 64.5 KB
/
Copy pathclass-import.php
File metadata and controls
1754 lines (1517 loc) · 64.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
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\CLI;
use Exception;
use WordPressdotorg\Plugin_Directory\Jobs\API_Update_Updater;
use WordPressdotorg\Plugin_Directory\Block_JSON;
use WordPressdotorg\Plugin_Directory\Plugin_Directory;
use WordPressdotorg\Plugin_Directory\Email\Release_Confirmation as Release_Confirmation_Email;
use WordPressdotorg\Plugin_Directory\Readme\{ Parser as Readme_Parser, Validator as Readme_Validator };
use WordPressdotorg\Plugin_Directory\Standalone\Plugins_Info_API;
use WordPressdotorg\Plugin_Directory\Template;
use WordPressdotorg\Plugin_Directory\Tools;
use WordPressdotorg\Plugin_Directory\Tools\Filesystem;
use WordPressdotorg\Plugin_Directory\Tools\SVN;
use WordPressdotorg\Plugin_Directory\Tools\Tokenisation_Helpers;
use WordPressdotorg\Plugin_Directory\Zip\Builder;
/**
* The functionality required to process a plugin import into the Directory.
*
* This will normally be called on the CLI in response to a plugin commit.
*
* @package WordPressdotorg\Plugin_Directory\CLI
*/
class Import {
const PLUGIN_SVN_BASE = 'https://plugins.svn.wordpress.org';
// Readme fields which get stored in plugin meta
public $readme_fields = array(
'donate_link',
'license',
'license_uri',
'upgrade_notice',
'screenshots',
// These headers are stored as post meta, but are handled separately.
// 'tested',
// 'requires',
// 'requires_php',
);
// Plugin headers that are stored in plugin meta
public $plugin_headers = array(
// Header => meta_key
'Name' => 'header_name',
'PluginURI' => 'header_plugin_uri',
'Author' => 'header_author',
'AuthorURI' => 'header_author_uri',
'TextDomain' => 'header_textdomain',
// These headers are stored in these fields, but are handled separately.
// 'Version' => 'version',
// 'RequiresWP' => 'requires',
// 'RequiresPHP' => 'requires_php',
// 'RequiresPlugins' => 'requires_plugins'
);
/**
* List of warnings generated during the import process.
*
* @var array
*/
public $warnings = array();
/**
* The last plugin imported.
*
* @var \WP_Post
*/
public $plugin;
/**
* Whether a tag's code changed since its release's confirmation state was established.
*
* Each release remembers the revision it was approved at; a newer commit to the tag means it
* changed. Older releases from before we tracked that lean on the best revision we have, and when
* unsure are treated as changed rather than trusted — just once, until they record their own.
*
* @param array|false $release Stored release record, per Plugin_Directory::get_release().
* @param int $tag_revision The tag path's current "Last Changed Rev".
* @return bool Whether the tag changed after the recorded source revision.
*/
public static function tag_modified_after_release( $release, $tag_revision ) {
if ( ! $release ) {
return false;
}
if ( isset( $release['source_revision'] ) ) {
return (int) $tag_revision > (int) $release['source_revision'];
}
// An unbuilt legacy release isn't served, so nothing to protect: leave it for the backfill, don't wipe its confirmations.
if ( empty( $release['zips_built'] ) ) {
return false;
}
return (int) $tag_revision > (int) ( $release['zips_built_from_revision'] ?? 0 );
}
/**
* Process an import for a Plugin into the Plugin Directory.
*
* @throws \Exception
*
* @param string $plugin_slug The slug of the plugin to import.
* @param array $svn_changed_tags A list of tags/trunk which the SVN change touched. Optional.
* @param array $svn_tags_deleted A list of tags/trunk which were deleted in the SVN change. Optional.
* @param array $svn_revision_triggered The SVN revision which this import has been triggered by. Optional.
*/
public function import_from_svn( $plugin_slug, $svn_changed_tags = array( 'trunk' ), $svn_tags_deleted = array(), $svn_revision_triggered = 0 ) {
// Reset properties.
$this->warnings = [];
$plugin = $this->plugin = Plugin_Directory::get_plugin_post( $plugin_slug );
if ( ! $plugin ) {
throw new Exception( 'Unknown Plugin' );
}
$data = $this->export_and_parse_plugin( $plugin_slug );
$readme = $data['readme'];
$assets = $data['assets'];
$headers = $data['plugin_headers'];
$version = $headers->Version ?? '';
$stable_tag = $data['stable_tag'];
$last_committer = $data['last_committer'];
$last_revision = $data['last_revision'];
$tagged_versions = $data['tagged_versions'];
$last_modified = $data['last_modified'];
$blocks = $data['blocks'];
$block_files = $data['block_files'];
$dashboard_widgets = $data['dashboard_widgets'] ?? array();
$current_stable_tag = get_post_meta( $plugin->ID, 'stable_tag', true ) ?: 'trunk';
$touches_stable_tag = (bool) array_intersect( [ $stable_tag, $current_stable_tag ], $svn_changed_tags );
// If the readme generated any warnings, raise it to self::$import_warnings;
if ( $readme->warnings ) {
$this->warnings = array_merge( $this->warnings, $readme->warnings );
}
/**
* Fire an import action, now that we've exported most of the plugin data.
*
* NOTE: This is prior to any validation checks.
*
* @param Import $this The Plugin Importer object.
* @param WP_Post $plugin The plugin being imported.
* @param array $data The data from the import process.
* @param array $svn_changed_tags The list of SVN tags/trunk affected to trigger the import.
* @param array $svn_tags_deleted The list of SVN tags/trunk deleted in the import.
* @param int $svn_revision_triggered The SVN revision that triggered the import.
*/
do_action( 'wporg_plugins_import', $this, $plugin, $data, $svn_changed_tags, $svn_tags_deleted, $svn_revision_triggered );
// Validate various headers:
/*
* Warn when the plugin's Version header has anything other than digits, dots, and an
* optional `-rc` / `-beta` / `-alpha` pre-release suffix (with optional digits).
*
* Catches headers that include an accidental duplicate `Version:` prefix, stray
* letters or punctuation, or other free-form text mixed in with the version number.
*
* The strict format matters because WordPress core uses `version_compare()` to decide
* whether to offer an update — a malformed header can silently give the wrong answer
* for users running an older release.
*/
if ( $version && ! preg_match( '/^\d+(?:\.\d+)*(?:-(?:rc|beta|alpha)(?:\.?\d+)?)?$/i', $version ) ) {
$this->warnings['version_header_unexpected_chars'] = $version;
}
// Stored as post meta, served by the API, and used as a path component downstream.
if ( $version && ! self::version_is_path_safe( $version ) ) {
$this->warnings['invalid_version_header'] = $version;
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- CLI context, callers write the message to STDERR.
throw new Exception( Readme_Validator::instance()->translate_code_to_message( 'invalid_version_header', $version ) );
}
/*
* Warn when the plugin's Version header doesn't appear to match the tag it was released from.
*
* Trunk releases skip this check — there's no tag folder to compare against.
*/
if ( 'trunk' !== $stable_tag && $version && ! self::version_matches_tag( $version, $stable_tag ) ) {
$this->warnings['version_tag_mismatch'] = [
'version' => $version,
'tag' => $stable_tag,
];
}
/*
* Check to see if the plugin is using the `Update URI` header.
*
* Plugins on WordPress.org should NOT use this header, but we do accept some URI formats for it in the API,
* so those are allowed to pass here.
* Any documentation suggesting that a WordPress.org hosted plugin should use this header is incorrect.
*/
if ( $headers->UpdateURI ) {
$update_uri_valid = preg_match( '!^(https?://)?(wordpress.org|w.org)/plugins?/(?P<slug>[^/]+)/?$!i', $headers->UpdateURI, $update_uri_matches );
if ( ! $update_uri_valid || $update_uri_matches['slug'] !== $plugin_slug ) {
$this->warnings['invalid_update_uri'] = $headers->UpdateURI;
throw new Exception( Readme_Validator::instance()->translate_code_to_message( 'invalid_update_uri' ) );
}
}
$_requires_plugins = array_filter( array_map( 'trim', explode( ',', $headers->RequiresPlugins ) ) );
$requires_plugins = [];
$unmet_dependencies = [];
foreach ( $_requires_plugins as $requires_plugin_slug ) {
$requires_plugin_post = Plugin_Directory::get_plugin_post( $requires_plugin_slug );
// get_plugin_post() will resolve some edge-cases, but we only want exact slug-matches, anything else is wrong.
if (
$requires_plugin_post &&
$requires_plugin_slug === $requires_plugin_post->post_name &&
'publish' === $requires_plugin_post->post_status
) {
$requires_plugins[] = $requires_plugin_post->post_name;
} else {
$unmet_dependencies[] = $requires_plugin_slug;
}
}
if ( $unmet_dependencies ) {
$this->warnings['unmet_dependencies'] = $unmet_dependencies;
throw new Exception( Readme_Validator::instance()->translate_code_to_message( 'unmet_dependencies', $unmet_dependencies ) );
}
unset( $_requires_plugins, $unmet_dependencies );
/*
* If a tag has been deleted, we should also remove any unconfirmed releases.
* NOTE: remove_release() will not remove a confirmed release, but will remove a discarded release.
*
* Additionally; this must occur before the below release confirmation checks,
* if the trunk readme has it's stable_tag set to one of these deleted (now non-existent) tags,
* then $stable_tag will be set to the fallback 'trunk', causing the RC checks to fail.
*/
foreach ( $svn_tags_deleted as $svn_deleted_tag ) {
if ( Plugin_Directory::remove_release( $plugin, $svn_deleted_tag ) ) {
echo "Plugin tag {$svn_deleted_tag} deleted; release removed.\n";
}
}
// Release confirmation
if ( $plugin->release_confirmation ) {
// If the stable tag is trunk, we shouldn't continue, as we don't support that for RC.
if ( 'trunk' === $stable_tag ) {
throw new Exception( 'Plugin cannot be released from trunk due to release confirmation being enabled.' );
}
// Per-tag last-changed rev/author, from the listing export_and_parse_plugin() already fetched.
$tag_last_changed = [];
foreach ( $tagged_versions as $tag_meta ) {
if ( isset( $tag_meta['tag'] ) ) {
$tag_last_changed[ $tag_meta['tag'] ] = [
'revision' => (int) ( $tag_meta['revision'] ?? 0 ),
'author' => (string) ( $tag_meta['author'] ?? '' ),
];
}
}
// Check to see if the commit has touched tags that don't have known confirmed releases.
foreach ( $svn_changed_tags as $svn_changed_tag ) {
if ( 'trunk' === $svn_changed_tag ) {
continue;
}
$release = Plugin_Directory::get_release( $plugin, $svn_changed_tag );
// get_release()'s trunk@ fallback can match a different release; only act on an exact-tag record.
if ( $release && (string) ( $release['tag'] ?? '' ) !== (string) $svn_changed_tag ) {
$release = false;
}
// $last_revision/$last_committer describe the stable path; other tags need their own.
if ( isset( $tag_last_changed[ $svn_changed_tag ] ) ) {
$tag_revision = $tag_last_changed[ $svn_changed_tag ]['revision'];
$tag_committer = $tag_last_changed[ $svn_changed_tag ]['author'] ?: $last_committer;
} elseif ( $svn_changed_tag === $stable_tag ) {
$tag_revision = (int) $last_revision;
$tag_committer = $last_committer;
} else {
// Unknown revision (tag deleted mid-import, or listing failure): don't guess and risk a false reset.
$this->warnings['tag_revision_unresolved'][] = $svn_changed_tag;
continue;
}
// Re-committed code must re-confirm, not inherit the tag's old approval; an unchanged re-import is a no-op.
$modified_after_release = self::tag_modified_after_release( $release, $tag_revision );
if ( ! $release || $modified_after_release ) {
if ( $svn_changed_tag === $stable_tag ) {
// Stable release, described by the parsed plugin headers; don't clobber a stored version with an empty header.
$release_version = $version ?: ( ( $release['version'] ?? '' ) ?: $svn_changed_tag );
} elseif ( $release ) {
// Keep the stored version; there's no header data for non-stable tags.
$release_version = $release['version'] ?: $svn_changed_tag;
} else {
// New non-stable release; fallback to the tag name.
$release_version = $svn_changed_tag;
}
$release_data = [
'tag' => $svn_changed_tag,
'version' => $release_version,
'committer' => [ $tag_committer ],
'revision' => [ $tag_revision ],
// Baseline for later modification checks.
'source_revision' => $tag_revision,
];
// Discard the prior approval when re-opening a modified release.
if ( $modified_after_release ) {
$release_data['reset_confirmation'] = true;
}
Plugin_Directory::add_release( $plugin, $release_data );
/*
* Trigger the release confirmation email.
*
* This goes to ALL committers, including who commited the change.
* "bot" accounts are NOT emailed, nor are accounts that have web login disabled.
*/
$who_to_email = array_diff(
Tools::get_plugin_committers( $plugin_slug ),
$GLOBALS['bot_accounts'] ?? [],
$GLOBALS['nologin_accounts'] ?? []
);
$email = new Release_Confirmation_Email(
$plugin,
$who_to_email,
[
'who' => $tag_committer,
'readme' => $readme,
'headers' => $headers,
'version' => $release_version,
]
);
$email->send();
if ( $modified_after_release ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CLI progress output.
echo "Plugin release {$svn_changed_tag} modified after release; confirmation reset.\n";
} else {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CLI progress output.
echo "Plugin release {$svn_changed_tag} not confirmed; email triggered.\n";
}
} elseif ( ! isset( $release['source_revision'] ) ) {
// Legacy record, unchanged: record its baseline so later checks are tag-specific.
Plugin_Directory::add_release(
$plugin,
[
'tag' => $svn_changed_tag,
'source_revision' => $tag_revision,
]
);
}
}
// Now check to see if the stable has been confirmed.
$release = Plugin_Directory::get_release( $plugin, $stable_tag );
if ( ! $release ) {
throw new Exception( "Plugin release {$stable_tag} not found." );
}
/*
* If the stable release isn't confirmed, the next section will abort processing,
* but if this commit didn't touch a stable tag, but rather a confirmed release tag,
* then we need to build a new zip for that tag.
*
* This is required as ZIP building occurs at the end of the import process, yet with
* release confirmations that will not be reached when the release isn't yet confirmed.
*/
if ( ! $release['confirmed'] && ! $touches_stable_tag ) {
$zips_to_build = [];
foreach ( $svn_changed_tags as $svn_changed_tag ) {
// Never build the stable tag zips here.
if ( $svn_changed_tag === $stable_tag ) {
continue;
}
// Always allow trunk to be rebuilt.
if ( 'trunk' === $svn_changed_tag ) {
$zips_to_build[] = 'trunk';
continue;
}
/*
* If the tag is confirmed, but the zips haven't been built, then build them.
* This can be a confirmed release, but one which isn't set as stable.
*/
$this_release = Plugin_Directory::get_release( $plugin, $svn_changed_tag );
if ( $this_release && $this_release['confirmed'] && ! $this_release['zips_built'] ) {
$zips_to_build[] = $this_release['tag'];
}
}
if ( $zips_to_build ) {
// NOTE: $stable_tag not passed, as it's not yet stable and won't be.
$this->rebuild_affected_zips( $plugin_slug, $current_stable_tag, $current_stable_tag, $zips_to_build, $svn_revision_triggered );
}
}
// Check that the tag is approved (If the release needed to be confirmed).
if ( ! $release['confirmed'] && $release['confirmations_required'] ) {
if ( ! in_array( $last_committer, $release['committer'], true ) ) {
$release['committer'][] = $last_committer;
}
if ( ! in_array( $last_revision, $release['revision'], true ) ) {
$release['revision'][] = $last_revision;
}
// Update with ^
Plugin_Directory::add_release( $plugin, $release );
/**
* Fire an action to let other code know this plugin has a pending release.
*
* @param WP_Post $plugin The plugin being imported.
* @param array $release The release data.
* @param array $data The data from the import process.
*/
do_action( 'wporg_plugins_import_release_pending', $plugin, $release, $data );
throw new Exception( "Plugin release {$stable_tag} not confirmed." );
}
// At this point we can assume that the release was confirmed, and should be imported.
}
/**
* Fire an import action, now that we've exported the plugin data, and validates that it's ready for release.
*
* NOTE: This fires after Release Confirmation, such that the plugin is 100% ready to be released.
*
* @param Import $this The Plugin Importer object.
* @param WP_Post $plugin The plugin being imported.
* @param array $release The release data. Only present if the plugin uses Release Confirmation.
* @param array $data The data from the import process.
* @param array $svn_changed_tags The list of SVN tags/trunk affected to trigger the import.
* @param array $svn_tags_deleted The list of SVN tags/trunk deleted in the import.
* @param int $svn_revision_triggered The SVN revision that triggered the import.
*/
do_action( 'wporg_plugins_import_process', $this, $plugin, $release ?? false, $data, $svn_changed_tags, $svn_tags_deleted, $svn_revision_triggered );
$content = '';
if ( $readme->sections ) {
foreach ( $readme->sections as $section => $section_content ) {
$content .= "\n\n<!--section={$section}-->\n{$section_content}";
}
} elseif ( ! empty( $headers->Description ) ) {
$content = "<!--section=description-->\n{$headers->Description}";
}
// Use the Readme name, as long as it's not the plugin slug.
if (
$readme->name &&
$readme->name !== $plugin->post_name
) {
$plugin->post_title = $readme->name;
} elseif ( $headers->Name ) {
$plugin->post_title = strip_tags( $headers->Name );
}
$plugin->post_content = trim( $content ) ?: $plugin->post_content;
$plugin->post_excerpt = trim( $readme->short_description ) ?: $headers->Description ?: $plugin->post_excerpt;
/*
* Bump last updated if:
* - The version has changed.
* - The post_modified is empty, which is the case for many initial checkins.
* - A tag (or trunk) commit is made to the current stable. The build has changed, even if not new version.
*/
if (
( ! $version || $version != get_post_meta( $plugin->ID, 'version', true ) ) ||
$plugin->post_modified == '0000-00-00 00:00:00' ||
( $svn_changed_tags && in_array( ( $stable_tag ?: 'trunk' ), $svn_changed_tags, true ) )
) {
if ( $last_modified ) {
$plugin->post_modified = $plugin->post_modified_gmt = $last_modified;
} else {
$plugin->post_modified = $plugin->post_modified_gmt = current_time( 'mysql' );
}
}
// Plugins should move from 'approved' to 'publish' on first parse
// `export_and_parse_plugin()` will throw an exception in the case where plugin files cannot be found,
// so by this time the plugin should be live.
if ( 'approved' === $plugin->post_status ) {
$plugin->post_status = 'publish';
// The post date should be set to when the plugin is first set live.
$plugin->post_date = $plugin->post_date_gmt = current_time( 'mysql' );
}
wp_update_post( $plugin );
// Set categories if there aren't any yet. wp-admin takes precedent.
if ( ! wp_get_object_terms( $plugin->ID, 'plugin_category', array( 'fields' => 'ids' ) ) ) {
wp_set_object_terms( $plugin->ID, Tag_To_Category::map( $readme->tags ), 'plugin_category' );
}
// Set tags from the readme
wp_set_object_terms( $plugin->ID, $readme->tags, 'plugin_tags' );
// Update the contributors list
wp_set_object_terms( $plugin->ID, $readme->contributors, 'plugin_contributors' );
// Update the committers list
Tools::sync_plugin_committers_with_taxonomy( $plugin->post_name );
if ( in_array( 'adopt-me', $readme->tags ) ) {
wp_set_object_terms( $plugin->ID, 'adopt-me', 'plugin_section' );
} else {
wp_remove_object_terms( $plugin->ID, 'adopt-me', 'plugin_section' );
}
// Update all readme meta
foreach ( $this->readme_fields as $readme_field ) {
update_post_meta( $plugin->ID, $readme_field, wp_slash( $readme->$readme_field ) );
}
// Store the plugin headers we need. Note that 'Version', 'RequiresWP', and 'RequiresPHP' are handled below.
foreach ( $this->plugin_headers as $plugin_header => $meta_field ) {
update_post_meta( $plugin->ID, $meta_field, ( isset( $headers->$plugin_header ) ? wp_slash( $headers->$plugin_header ) : '' ) );
}
// Update the Requires, Requires PHP, and Tested up to fields, prefering those from the Plugin Headers.
// Unfortunately the value within $headers is not always a well-formed value.
$requires = $readme->requires;
$requires_php = $readme->requires_php;
$tested = $readme->tested;
if ( $headers->RequiresWP && preg_match( '!^[\d.]{3,}$!', $headers->RequiresWP ) ) {
$requires = $headers->RequiresWP;
}
if ( $headers->RequiresPHP && preg_match( '!^[\d.]{3,}$!', $headers->RequiresPHP ) ) {
$requires_php = $headers->RequiresPHP;
}
if ( $headers->TestedUpTo && preg_match( '!^[\d.]{3,}$!', $headers->TestedUpTo ) ) {
$tested = $headers->TestedUpTo;
}
// Sanitize the tested version.
if ( function_exists( 'wporg_get_version_equivalents' ) ) {
foreach ( wporg_get_version_equivalents() as $latest_compatible_version => $compatible_with ) {
if ( in_array( $tested, $compatible_with, true ) ) {
$tested = $latest_compatible_version;
break;
}
}
}
// Keep a log of all plugin names used by the plugin over time.
$plugin_names = get_post_meta( $plugin->ID, 'plugin_name_history', true ) ?: [];
if ( ! isset( $plugin_names[ $headers->Name ] ) ) {
// [ 'Plugin Name' => '1.2.3', 'Plugin New Name' => '4.5.6' ]
$plugin_names[ $headers->Name ] = $headers->Version;
update_post_meta( $plugin->ID, 'plugin_name_history', wp_slash( $plugin_names ) );
}
update_post_meta( $plugin->ID, 'requires_plugins', wp_slash( $requires_plugins ) );
update_post_meta( $plugin->ID, 'requires', wp_slash( $requires ) );
update_post_meta( $plugin->ID, 'requires_php', wp_slash( $requires_php ) );
update_post_meta( $plugin->ID, 'tested', wp_slash( $tested ) );
update_post_meta( $plugin->ID, 'tagged_versions', wp_slash( array_keys( $tagged_versions ) ) );
update_post_meta( $plugin->ID, 'sections', wp_slash( array_keys( $readme->sections ) ) );
update_post_meta( $plugin->ID, 'assets_screenshots', wp_slash( $assets['screenshot'] ) );
update_post_meta( $plugin->ID, 'assets_icons', wp_slash( $assets['icon'] ) );
update_post_meta( $plugin->ID, 'assets_banners', wp_slash( $assets['banner'] ) );
update_post_meta( $plugin->ID, 'last_updated', wp_slash( $plugin->post_modified_gmt ) );
// Calculate the 'plugin color' from the average color of the banner if provided. This is used for fallback icons.
$banner_average_color = '';
if ( $first_banner = reset( $assets['banner'] ) ) {
// The Banners are not stored locally, which is why a URL is used here
$banner_average_color = Tools::get_image_average_color( Template::get_asset_url( $plugin, $first_banner, false /* no CDN */ ) );
}
update_post_meta( $plugin->ID, 'assets_banners_color', wp_slash( $banner_average_color ) );
// Store the content of blueprint files, if they're available and valid.
if ( isset( $assets['blueprint'] ) && count( $assets['blueprint'] ) > 0 ) {
update_post_meta( $plugin->ID, 'assets_blueprints', wp_slash( $assets['blueprint'] ) );
} else {
delete_post_meta( $plugin->ID, 'assets_blueprints' );
// TODO: maybe if ( $touches_stable_tag )?
add_post_meta( $plugin->ID, '_missing_blueprint_notice', 1, true );
}
// Store the block data, if known
if ( count( $blocks ) ) {
$changed = update_post_meta( $plugin->ID, 'all_blocks', $blocks );
if ( $changed || count ( get_post_meta( $plugin->ID, 'block_name' ) ) !== count ( $blocks ) ) {
delete_post_meta( $plugin->ID, 'block_name' );
delete_post_meta( $plugin->ID, 'block_title' );
foreach ( $blocks as $block ) {
add_post_meta( $plugin->ID, 'block_name', $block->name, false );
add_post_meta( $plugin->ID, 'block_title', $block->title, false );
}
}
} else {
delete_post_meta( $plugin->ID, 'all_blocks' );
delete_post_meta( $plugin->ID, 'block_name' );
delete_post_meta( $plugin->ID, 'block_title' );
}
// Only store block_files for plugins in the block directory
if ( count( $block_files ) && has_term( 'block', 'plugin_section', $plugin->ID ) ) {
update_post_meta( $plugin->ID, 'block_files', $block_files );
} else {
delete_post_meta( $plugin->ID, 'block_files' );
}
// Dashboard widgets: assign the section term and store widget names.
if ( $dashboard_widgets ) {
wp_add_object_terms( $plugin->ID, 'dashboard-widgets', 'plugin_section' );
delete_post_meta( $plugin->ID, 'dashboard_widget_name' );
foreach ( $dashboard_widgets as $widget_name ) {
if ( '' === $widget_name ) {
continue;
}
add_post_meta( $plugin->ID, 'dashboard_widget_name', $widget_name, false );
}
} else {
wp_remove_object_terms( $plugin->ID, 'dashboard-widgets', 'plugin_section' );
delete_post_meta( $plugin->ID, 'dashboard_widget_name' );
}
// Add the release to storage.
if ( 'trunk' != $stable_tag ) {
Plugin_Directory::add_release(
$plugin,
[
'tag' => $stable_tag,
'version' => $version,
'committer' => [ $last_committer ],
'revision' => [ $last_revision ]
]
);
} elseif ( 'trunk' === $stable_tag && version_compare( $version, $plugin->version, '>' ) ) {
// This is a new version, released from trunk.
Plugin_Directory::add_release(
$plugin,
[
'tag' => "trunk@{$version}",
'version' => $version,
'committer' => [ $last_committer ],
'revision' => [ $last_revision ]
]
);
}
$this->rebuild_affected_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags, $svn_revision_triggered );
// If we've got a new version, store the last version in the plugin meta.
if ( $version && $version !== $plugin->version ) {
update_post_meta( $plugin->ID, 'last_version', wp_slash( $plugin->version ) );
update_post_meta( $plugin->ID, 'last_stable_tag', wp_slash( $current_stable_tag ) );
update_post_meta( $plugin->ID, 'last_version_date', wp_slash( $plugin->version_date ) );
// Keep the date of the last version change, this often differs from the last_updated/post_modified dates.
update_post_meta( $plugin->ID, 'version_date', wp_slash( current_time( 'mysql' ) ) );
}
// Finally, set the new version live.
update_post_meta( $plugin->ID, 'stable_tag', wp_slash( $stable_tag ) );
update_post_meta( $plugin->ID, 'version', wp_slash( $version ) );
// Update the list of tags last, as it controls which ZIPs are present in the 'Previous versions' section and info API.
update_post_meta( $plugin->ID, 'tags', wp_slash( $tagged_versions ) );
// Ensure that the API gets the updated data
API_Update_Updater::update_single_plugin( $plugin->post_name );
Plugins_Info_API::flush_plugin_information_cache( $plugin->post_name );
/**
* Action that fires after a plugin is imported.
*
* @param WP_Post $plugin The plugin updated.
* @param string $stable_tag The new stable tag for the plugin.
* @param string $old_stable_tag The previous stable tag for the plugin.
* @param array $changed_tags The list of SVN tags/trunk affected to trigger the import.
* @param int $svn_revision The SVN revision that triggered the import.
* @param array $warnings The list of warnings generated during the import process.
*/
do_action( 'wporg_plugins_imported', $plugin, $stable_tag, $current_stable_tag, $svn_changed_tags, $svn_revision_triggered, $this->warnings );
return true;
}
/**
* (Re)build plugin ZIPs affected by this commit.
*
* @param string $plugin_slug The plugin slug.
* @param string $stable_tag The new stable tag.
* @param string $current_stable_tag The current stable tag.
* @param array $svn_changed_tags The list of SVN tags modified since last import.
* @param string $svn_revision_triggered The SVN revision which triggered the rebuild.
*
* @return bool
*/
protected function rebuild_affected_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags, $svn_revision_triggered = 0 ) {
$versions_to_build = $svn_changed_tags;
// Ensure that the stable zip is built/rebuilt if need be.
if ( $stable_tag != $current_stable_tag && ! in_array( $stable_tag, $versions_to_build ) ) {
$versions_to_build[] = $stable_tag;
}
$plugin = Plugin_Directory::get_plugin_post( $plugin_slug );
// Don't rebuild release-confirmation-required tags.
if ( $plugin->release_confirmation ) {
foreach ( $versions_to_build as $i => $tag ) {
// Trunk should always be built, and will never be set as the stable tag when confirmations are enabled.
if ( 'trunk' === $tag ) {
continue;
}
$release = Plugin_Directory::get_release( $plugin, $tag );
if (
// If the release isn't known, skip.
! $release ||
// If the release isn't confirmed AND confirmations were required, skip.
( ! $release['confirmed'] && $release['confirmations_required'] ) ||
// If the release has had its ZIPs built, skip if it required confirmations.
( $release['zips_built'] && $release['confirmations_required'] )
) {
unset( $versions_to_build[ $i ] );
}
}
if ( $versions_to_build ) {
echo "Building ZIPs for {$plugin_slug}: " . implode( ', ', $versions_to_build ) . "\n";
}
}
if ( ! $versions_to_build ) {
return false;
}
// Rebuild/Build $build_zips
try {
// This will rebuild the ZIP.
$zip_builder = new Builder();
$built_versions = $zip_builder->build(
$plugin_slug,
array_unique( $versions_to_build ),
$svn_revision_triggered ?
"{$plugin_slug}: ZIP build triggered by https://plugins.trac.wordpress.org/changeset/{$svn_revision_triggered}" :
"{$plugin_slug}: ZIP build triggered by " . php_uname( 'n' ),
$stable_tag
);
} catch ( Exception $e ) {
$failed_versions = array_unique( $versions_to_build );
$error = preg_replace( '/[\r\n\t]+/', ' ', $e->getMessage() );
$this->warnings['zip_build_failed'] = [
'versions' => $failed_versions,
'message' => $error,
];
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Routed to the error log via E_USER_WARNING; raw is fine.
trigger_error( sprintf( '%s: ZIP build failed for %s: %s', $plugin_slug, implode( ', ', $failed_versions ), $error ), E_USER_WARNING );
return false;
}
// Mark only the ZIPs that actually built, each with its export revision.
Plugin_Directory::mark_zips_built( $plugin, $built_versions );
return true;
}
/**
* Export a plugin and determine all the information about the current state of the plugin.
*
* - Creates a /trunk/ export of the plugin.
* - Creates a /stable/ export of the stable_tag if specified, falling back to /trunk/.
* - Handles readme.md & readme.txt prefering the latter.
* - Searches for Screenshots in /$stable/ and in /assets/ (listed remotely).
*
* @throws \Exception
*
* @param string $plugin_slug The slug of the plugin to parse.
*
* @return array {
* 'readme', 'stable_tag', 'plugin_headers', 'assets', 'tagged_versions'
* }
*/
protected function export_and_parse_plugin( $plugin_slug ) {
$tmp_dir = Filesystem::temp_directory( "process-{$plugin_slug}" );
// We assume the stable tag is trunk to start with.
$stable_tag = 'trunk';
// Find the trunk readme file, list remotely to avoid checking out the entire directory.
$trunk_files = SVN::ls( self::PLUGIN_SVN_BASE . "/{$plugin_slug}/trunk" ) ?: array();
// Find the list of tagged versions of the plugin.
$tagged_versions = [];
$tagged_versions_raw = SVN::ls( "https://plugins.svn.wordpress.org/{$plugin_slug}/tags/", true ) ?: [];
foreach ( $tagged_versions_raw as $entry ) {
// Discard files
if ( 'dir' !== $entry['kind'] ) {
continue;
}
$tag = $entry['filename'];
// Prefix the 0 for plugin versions like 0.1
if ( '.' == substr( $tag, 0, 1 ) ) {
$tag = "0{$tag}";
}
$tagged_versions[ $tag ] = [
'tag' => $entry['filename'],
'author' => $entry['author'],
'date' => $entry['date'],
'revision' => (int) ( $entry['revision'] ?? 0 ),
];
}
// Not all plugins utilise `trunk`, some just tag versions.
if ( ! $trunk_files ) {
if ( ! $tagged_versions ) {
throw new Exception( 'Plugin has no files in trunk, nor tags.' );
}
$stable_tag = array_reduce( array_keys( $tagged_versions ), function( $a, $b ) {
return version_compare( $a, $b, '>' ) ? $a : $b;
} );
}
// A plugin historically doesn't have to have a readme.
$trunk_readme_files = preg_grep( '!^readme.(txt|md)$!i', $trunk_files );
if ( $trunk_readme_files ) {
$trunk_readme_file = reset( $trunk_readme_files );
// Preference readme.txt over readme.md if both exist.
foreach ( $trunk_readme_files as $f ) {
if ( '.txt' == strtolower( substr( $f, -4 ) ) ) {
$trunk_readme_file = $f;
break;
}
}
$trunk_readme_file = self::PLUGIN_SVN_BASE . "/{$plugin_slug}/trunk/{$trunk_readme_file}";
$trunk_readme = new Readme_Parser( $trunk_readme_file );
$stable_tag = $trunk_readme->stable_tag;
}
$svn_info = false;
if ( $stable_tag && 'trunk' != $stable_tag ) {
$stable_url = self::PLUGIN_SVN_BASE . "/{$plugin_slug}/tags/{$stable_tag}";
$svn_info = SVN::info( $stable_url );
if ( ! $svn_info['result'] && '0.' === substr( $stable_tag, 0, 2 ) ) {
// Handle tags which we store as 0.blah but are in /tags/.blah
$_stable_tag = substr( $stable_tag, 1 );
$stable_url = self::PLUGIN_SVN_BASE . "/{$plugin_slug}/tags/{$_stable_tag}";
$svn_info = SVN::info( $stable_url );
}
// Verify that the tag has files, falling back to trunk if not.
if ( ! SVN::ls( $stable_url ) ) {
$svn_info = false;
}
}
// Fall back to using `trunk` as stable, if the tag doesn't exist.
if ( ! $svn_info || ! $svn_info['result'] ) {
if ( 'trunk' !== $stable_tag ) {
$this->warnings['stable_tag_invalid_trunk_fallback'] = $stable_tag;
$this->warnings['stable_tag_invalid'] = true;
}
$stable_tag = 'trunk';
$stable_url = self::PLUGIN_SVN_BASE . "/{$plugin_slug}/trunk";
$svn_info = SVN::info( $stable_url );
}
if ( ! $svn_info['result'] ) {
throw new Exception( 'Could not find stable SVN URL: ' . ( $svn_info['errors'] ? implode( ' ', reset( $svn_info['errors'] ) ) : 'Unknown error' ) );
}
$last_modified = false;
if ( preg_match( '/^([0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{1,2}:[0-9]{2}:[0-9]{2})/', $svn_info['result']['Last Changed Date'] ?? '', $m ) ) {
$last_modified = $m[0];
}
$last_committer = $svn_info['result']['Last Changed Author'] ?? '';
$last_revision = $svn_info['result']['Last Changed Rev'] ?? 0;
/*
* Before we check out the plugin, ensure that it has *files* in the folder.
*
* Some plugins accidentally copy their entire SVN repo into the tagged folder, which
* causes a recursive checkout many multiple gigabytes in size, causing issues for WordPress.org.
*/
if ( ! wp_list_filter( SVN::ls( $stable_url, true ), [ 'kind' => 'file' ] ) ) {
throw new Exception( "Could not create SVN export of {$stable_url}: Path appears not to have any files." );
}
$svn_export = SVN::export(
$stable_url,
$tmp_dir . '/export',
array(
'ignore-externals',
)
);
if ( ! $svn_export['result'] || empty( $svn_export['revision'] ) ) {
// Catch the case where exporting a tag finds nothing, but there was nothing in trunk either.
if ( ! $trunk_files ) {
throw new Exception( 'Plugin has no files in trunk, nor tags.' );
}
throw new Exception( 'Could not create SVN export: ' . ( $svn_export['errors'] ? implode( ' ', reset( $svn_export['errors'] ) ) : 'Unknown error' ) );
}
// The readme may not actually exist, but that's okay.
$readme = $this->find_readme_file( $tmp_dir . '/export' );
$readme = new Readme_Parser( $readme );
// There must be valid plugin headers though.
$plugin_headers = $this->find_plugin_headers( "$tmp_dir/export" );
if ( ! $plugin_headers ) {
throw new Exception( 'Could not find the plugin headers.' );
}
// Now we look in the /assets/ folder for banners, screenshots, and icons.
$assets = array(
'screenshot' => array(),
'banner' => array(),
'icon' => array(),
'blueprint' => array(),
);
$asset_limits = array(
'screenshot' => 10 * MB_IN_BYTES,
'banner' => 4 * MB_IN_BYTES,
'icon' => 1 * MB_IN_BYTES,
'blueprint' => 100 * KB_IN_BYTES,
);
// Previously-imported asset metadata, used to skip re-reading any file whose
// SVN revision hasn't changed.
$prior_assets = array(
'screenshot' => get_post_meta( $this->plugin->ID, 'assets_screenshots', true ) ?: array(),
'banner' => get_post_meta( $this->plugin->ID, 'assets_banners', true ) ?: array(),
'icon' => get_post_meta( $this->plugin->ID, 'assets_icons', true ) ?: array(),
);
$svn_blueprints_folder = null;
$svn_assets_folder = SVN::ls( self::PLUGIN_SVN_BASE . "/{$plugin_slug}/assets/", true /* verbose */ );
if ( $svn_assets_folder ) { // /assets/ may not exist.
foreach ( $svn_assets_folder as $asset ) {
if ( 'blueprints' === $asset['filename'] ) {
$svn_blueprints_folder = self::PLUGIN_SVN_BASE . "/{$plugin_slug}/assets/blueprints/";
continue;
}
// screenshot-0(-rtl)(-de_DE).(png|jpg|jpeg|gif) || banner-772x250.PNG || icon.svg
if ( ! preg_match( '!^(?P<type>screenshot|banner|icon)(?:-(?P<resolution>\d+(?:\D\d+)?)(-rtl)?(?:-(?P<locale>[a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?))?\.(png|jpg|jpeg|gif)|\.svg)$!iu', $asset['filename'], $m ) ) {
continue;
}
$type = strtolower( $m['type'] );
// Don't import zero-byte or oversize assets.
if ( ! $asset['filesize'] || $asset['filesize'] > $asset_limits[ $type ] ) {
continue;
}
$filename = $asset['filename'];
$revision = $asset['revision'];
$location = 'assets';
$resolution = isset( $m['resolution'] ) ? $m['resolution'] : false;
$locale = isset( $m['locale'] ) ? $m['locale'] : false;
// Ensure the resolution key is in the expected 123x123 format.
// Resolution is also the screenshot number, in which case it's stringy numeric only.
if ( $resolution && 'screenshot' === $type ) {
$resolution = (string)( (int) $resolution );
} else if ( $resolution ) {
$resolution = preg_replace( '/[^0-9]/u', 'x', $resolution );
}
$record = compact( 'filename', 'revision', 'resolution', 'location', 'locale' );
$record = self::enrich_asset_dimensions(
$record,