-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathPlugin_Header_Fields_Check.php
More file actions
568 lines (528 loc) · 17.9 KB
/
Copy pathPlugin_Header_Fields_Check.php
File metadata and controls
568 lines (528 loc) · 17.9 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
<?php
/**
* Class Plugin_Header_Fields_Check.
*
* @package plugin-check
*/
namespace WordPress\Plugin_Check\Checker\Checks\Plugin_Repo;
use Exception;
use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Static_Check;
use WordPress\Plugin_Check\Traits\Amend_Check_Result;
use WordPress\Plugin_Check\Traits\License_Utils;
use WordPress\Plugin_Check\Traits\Stable_Check;
use WordPress\Plugin_Check\Traits\Version_Utils;
/**
* Check for plugin header fields.
*
* @since 1.2.0
*/
class Plugin_Header_Fields_Check implements Static_Check {
use Amend_Check_Result;
use License_Utils;
use Stable_Check;
use Version_Utils;
/**
* Gets the categories for the check.
*
* Every check must have at least one category.
*
* @since 1.2.0
*
* @return array The categories for the check.
*/
public function get_categories() {
return array( Check_Categories::CATEGORY_PLUGIN_REPO );
}
/**
* Amends the given result by running the check on the associated plugin.
*
* @since 1.2.0
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
*
* @throws Exception Thrown when the check fails with a critical error (unrelated to any errors detected as part of the check).
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function run( Check_Result $result ) {
$plugin_main_file = $result->plugin()->main_file();
$labels = array(
'Name' => 'Plugin Name',
'PluginURI' => 'Plugin URI',
'Version' => 'Version',
'Description' => 'Description',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path',
'Network' => 'Network',
'RequiresWP' => 'Requires at least',
'RequiresPHP' => 'Requires PHP',
'UpdateURI' => 'Update URI',
'RequiresPlugins' => 'Requires Plugins',
'License' => 'License',
'LicenseURI' => 'License URI',
);
$restricted_labels = array(
'RestrictedLabel' => 'Restricted Label',
); // Reserved for future use.
$plugin_header = $this->get_plugin_data( $plugin_main_file, array_merge( $labels, $restricted_labels ) );
if ( ! empty( $plugin_header['Name'] ) ) {
if ( in_array( $plugin_header['Name'], array( 'Plugin Name', 'My Basics Plugin' ), true ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header in the plugin file is not valid.', 'plugin-check' ),
esc_html( $labels['Name'] )
),
'plugin_header_invalid_plugin_name',
$plugin_main_file,
0,
0,
'',
7
);
} else {
$valid_chars_count = preg_match_all( '/[a-z0-9]/i', $plugin_header['Name'] );
if ( intval( $valid_chars_count ) < 5 ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header in the plugin file is not valid. It needs to contain at least 5 latin letters (a-Z) and/or numbers. This is necessary because the initial plugin slug is generated from the name.', 'plugin-check' ),
esc_html( $labels['Name'] )
),
'plugin_header_unsupported_plugin_name',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
7
);
}
}
}
if ( ! empty( $plugin_header['PluginURI'] ) ) {
if ( true !== $this->is_valid_url( $plugin_header['PluginURI'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header in the plugin file is not valid.', 'plugin-check' ),
esc_html( $labels['PluginURI'] )
),
'plugin_header_invalid_plugin_uri',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
7
);
} elseif ( preg_match( '/\/\/(example\.com|example\.net|example\.org)\//', $plugin_header['PluginURI'], $matches ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: domain */
__( 'The "%1$s" header in the plugin file is not valid. Discouraged domain "%2$s" found. This is the homepage of the plugin, which should be a unique URL, preferably on your own website. ', 'plugin-check' ),
esc_html( $labels['PluginURI'] ),
esc_html( $matches[1] ),
),
'plugin_header_invalid_plugin_uri_domain',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
7
);
}
}
if ( empty( $plugin_header['Description'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header is missing in the plugin file.', 'plugin-check' ),
esc_html( $labels['Description'] )
),
'plugin_header_missing_plugin_description',
$plugin_main_file,
0,
0,
__( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' ),
7
);
} else {
if (
str_contains( $plugin_header['Description'], 'This is a short description of what the plugin does' )
|| str_contains( $plugin_header['Description'], 'Here is a short description of the plugin' )
|| str_contains( $plugin_header['Description'], 'Handle the basics with this plugin' )
) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header in the plugin file should not contain default text.', 'plugin-check' ),
esc_html( $labels['Description'] )
),
'plugin_header_invalid_plugin_description',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
7
);
}
}
if ( empty( $plugin_header['Version'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header is missing in the plugin file.', 'plugin-check' ),
esc_html( $labels['Version'] )
),
'plugin_header_missing_plugin_version',
$plugin_main_file,
0,
0,
__( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' ),
7
);
} else {
if ( ! preg_match( '/^[a-z0-9.-]+$/i', $plugin_header['Version'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header in the plugin file should only contain numbers, letters, periods, and hyphens.', 'plugin-check' ),
esc_html( $labels['Version'] )
),
'plugin_header_invalid_plugin_version',
$plugin_main_file,
0,
0,
__( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' ),
7
);
}
}
if ( ! empty( $plugin_header['AuthorURI'] ) ) {
if ( true !== $this->is_valid_url( $plugin_header['AuthorURI'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header in the plugin file is not valid.', 'plugin-check' ),
esc_html( $labels['AuthorURI'] )
),
'plugin_header_invalid_author_uri',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
7
);
}
}
if ( ! empty( $plugin_header['Network'] ) ) {
if ( 'true' !== strtolower( $plugin_header['Network'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header in the plugin file is not valid. Can only be set to true, and should be left out when not needed.', 'plugin-check' ),
esc_html( $labels['Network'] )
),
'plugin_header_invalid_network',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
7
);
}
}
if ( ! empty( $plugin_header['RequiresWP'] ) ) {
$latest_wp_version = $this->get_wordpress_stable_version();
if ( ! preg_match( '!^\d+\.\d(\.\d+)?$!', $plugin_header['RequiresWP'] ) ) {
$previous_wp_version = $this->get_wordpress_relative_major_version( $latest_wp_version, -1 );
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: Example version 6.7, 3: Example version 6.6 */
__( 'The "%1$s" header in the plugin file should only contain a WordPress version such as "%2$s" or "%3$s".', 'plugin-check' ),
esc_html( $labels['RequiresWP'] ),
esc_html( $latest_wp_version ),
esc_html( $previous_wp_version )
),
'plugin_header_invalid_requires_wp',
$plugin_main_file,
0,
0,
'',
7
);
} else {
$acceptable_min_wp_version = $this->get_wordpress_relative_major_version( $latest_wp_version, 1 );
if ( version_compare( $plugin_header['RequiresWP'], $acceptable_min_wp_version, '>' ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: currently used version */
__( '<strong>%1$s: %2$s.</strong><br>The "%1$s" value in your plugin header is not valid. This version of WordPress does not exist (yet).', 'plugin-check' ),
esc_html( $labels['RequiresWP'] ),
esc_html( $plugin_header['RequiresWP'] )
),
'plugin_header_nonexistent_requires_wp',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
7
);
}
}
}
if ( ! empty( $plugin_header['RequiresPHP'] ) ) {
if ( ! preg_match( '!^\d+(\.\d+){1,2}$!', $plugin_header['RequiresPHP'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: 1: plugin header field; 2: Example version 5.2.4. 3: Example version 7.0. */
__( 'The "%1$s" header in the plugin file should only contain a PHP version such as "%2$s" or "%3$s".', 'plugin-check' ),
esc_html( $labels['RequiresPHP'] ),
'5.2.4',
'7.0'
),
'plugin_header_invalid_requires_php',
$plugin_main_file,
0,
0,
'',
7
);
}
}
if ( ! empty( $plugin_header['RequiresPlugins'] ) ) {
if ( ! preg_match( '/^[a-z0-9-]+(?:,\s*[a-z0-9-]+)*$/', $plugin_header['RequiresPlugins'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header in the plugin file must contain a comma-separated list of WordPress.org-formatted slugs.', 'plugin-check' ),
esc_html( $labels['RequiresPlugins'] )
),
'plugin_header_invalid_requires_plugins',
$plugin_main_file,
0,
0,
'',
7
);
}
}
if ( empty( $plugin_header['License'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( '<strong>Missing "%s" in Plugin Header.</strong><br>Please update your Plugin Header with a valid GPLv2 (or later) compatible license.', 'plugin-check' ),
esc_html( $labels['License'] )
),
'plugin_header_no_license',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared',
9
);
} else {
$plugin_license = $this->get_normalized_license( $plugin_header['License'] );
if ( ! $this->is_license_gpl_compatible( $plugin_license ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: license */
__( '<strong>Invalid %1$s: %2$s.</strong><br>Please update your Plugin Header with a valid GPLv2 (or later) compatible license.', 'plugin-check' ),
esc_html( $labels['License'] ),
esc_html( $plugin_header['License'] )
),
'plugin_header_invalid_license',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared',
9
);
}
}
$found_headers = array();
foreach ( $restricted_labels as $restricted_key => $restricted_label ) {
if ( array_key_exists( $restricted_key, $plugin_header ) && ! empty( $plugin_header[ $restricted_key ] ) ) {
$found_headers[ $restricted_key ] = $restricted_label;
}
}
if ( ! empty( $found_headers ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: %s: header fields */
__( 'Restricted plugin header field(s) found: %s', 'plugin-check' ),
"'" . implode( "', '", array_values( $found_headers ) ) . "'"
),
'plugin_header_restricted_fields',
$plugin_main_file,
0,
0,
'',
7
);
}
if ( ! $result->plugin()->is_single_file_plugin() ) {
if ( ! empty( $plugin_header['TextDomain'] ) ) {
if ( ! preg_match( '/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $plugin_header['TextDomain'] ) ) {
$this->add_result_error_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: text domain */
__( 'The "%1$s" header in the plugin file should only contain lowercase letters, numbers, and hyphens. Found "%2$s".', 'plugin-check' ),
esc_html( $labels['TextDomain'] ),
esc_html( $plugin_header['TextDomain'] )
),
'textdomain_invalid_format',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#text-domains',
7
);
} else {
$plugin_slug = $result->plugin()->slug();
if ( $plugin_slug !== $plugin_header['TextDomain'] ) {
$this->add_result_warning_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: plugin header text domain, 3: plugin slug */
__( 'The "%1$s" header in the plugin file does not match the slug. Found "%2$s", expected "%3$s".', 'plugin-check' ),
esc_html( $labels['TextDomain'] ),
esc_html( $plugin_header['TextDomain'] ),
esc_html( $plugin_slug )
),
'textdomain_mismatch',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/',
6
);
}
}
}
if ( ! empty( $plugin_header['DomainPath'] ) ) {
if ( ! str_starts_with( $plugin_header['DomainPath'], '/' ) ) {
$this->add_result_warning_for_file(
$result,
sprintf(
/* translators: %s: plugin header field */
__( 'The "%s" header in the plugin file must start with forward slash.', 'plugin-check' ),
esc_html( $labels['DomainPath'] )
),
'plugin_header_invalid_domain_path',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#domain-path',
6
);
}
$domain_path = trim( $plugin_header['DomainPath'], '/' );
$target_path = wp_normalize_path( $result->plugin()->path() . $domain_path );
if ( ! is_dir( $target_path ) ) {
$this->add_result_warning_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: domain path */
__( 'The "%1$s" header in the plugin file must point to an existing folder. Found: "%2$s"', 'plugin-check' ),
esc_html( $labels['DomainPath'] ),
$domain_path
),
'plugin_header_nonexistent_domain_path',
$plugin_main_file,
0,
0,
'https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#domain-path',
6
);
}
}
}
}
/**
* Checks if URL is valid.
*
* @since 1.2.0
*
* @param string $url URL.
* @return bool true if the URL is valid, otherwise false.
*/
private function is_valid_url( $url ) {
if ( filter_var( $url, FILTER_VALIDATE_URL ) !== $url || ! str_starts_with( $url, 'http' ) ) {
return false;
}
// Detect duplicated protocol (e.g., "https://http://example.com/").
$parsed_url = wp_parse_url( $url );
if ( isset( $parsed_url['scheme'] ) && str_contains( substr( $url, strlen( $parsed_url['scheme'] ) + 3 ), '://' ) ) {
return false;
}
return true;
}
/**
* Parses the plugin contents to retrieve plugin's metadata.
*
* @since 1.2.0
*
* @param string $plugin_file Absolute path to the main plugin file.
* @param array $default_headers List of headers, in the format `array( 'HeaderKey' => 'Header Name' )`.
* @return string[] Array of file header values keyed by header name.
*/
private function get_plugin_data( $plugin_file, $default_headers ) {
$plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
// If no text domain is defined fall back to the plugin slug.
if ( ! $plugin_data['TextDomain'] ) {
$plugin_slug = dirname( plugin_basename( $plugin_file ) );
if ( '.' !== $plugin_slug && ! str_contains( $plugin_slug, '/' ) ) {
$plugin_data['TextDomain'] = $plugin_slug;
}
}
return $plugin_data;
}
/**
* Gets the description for the check.
*
* Every check must have a short description explaining what the check does.
*
* @since 1.2.0
*
* @return string Description.
*/
public function get_description(): string {
return __( 'Checks adherence to the Headers requirements.', 'plugin-check' );
}
/**
* Gets the documentation URL for the check.
*
* Every check must have a URL with further information about the check.
*
* @since 1.2.0
*
* @return string The documentation URL.
*/
public function get_documentation_url(): string {
return __( 'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/', 'plugin-check' );
}
}