@@ -227,24 +227,34 @@ protected function parse_readme_contents( $contents ) {
227
227
if ( 'plugin name ' == strtolower ( $ this ->name ) ) {
228
228
$ this ->warnings ['invalid_plugin_name_header ' ] = true ;
229
229
230
- $ this ->name = $ line = $ this ->get_first_nonwhitespace ( $ contents );
230
+ $ this ->name = false ;
231
+ $ line = $ this ->get_first_nonwhitespace ( $ contents );
231
232
232
- // Ensure that the line read wasn't an actual header or description.
233
- if ( strlen ( $ line ) > 50 || preg_match ( '~^( ' . implode ( '| ' , array_keys ( $ this ->valid_headers ) ) . ')\s*:~i ' , $ line ) ) {
234
- $ this ->name = false ;
235
- array_unshift ( $ contents , $ line );
233
+ // Ensure that the line read doesn't look like a description.
234
+ if ( strlen ( $ line ) < 50 && ! $ this ->parse_possible_header ( $ line , true /* only valid headers */ ) ) {
235
+ $ this ->name = $ this ->sanitize_text ( trim ( $ line , "#= \t\0\x0B" ) );
236
236
}
237
237
}
238
238
239
+ // It's possible to leave the plugin name header off entirely.
240
+ if ( $ this ->parse_possible_header ( $ this ->name , true /* only valid headers */ ) ) {
241
+ array_unshift ( $ contents , $ line );
242
+
243
+ $ this ->warnings ['invalid_plugin_name_header ' ] = true ;
244
+ $ this ->name = false ;
245
+ }
246
+
239
247
// Parse headers.
240
248
$ headers = array ();
241
249
242
250
$ line = $ this ->get_first_nonwhitespace ( $ contents );
243
251
$ last_line_was_blank = false ;
244
252
do {
245
- $ value = null ;
253
+ $ value = null ;
254
+ $ header = $ this ->parse_possible_header ( $ line );
255
+
246
256
// If it doesn't look like a header value, maybe break to the next section.
247
- if ( ! str_contains ( $ line , ' : ' ) || str_starts_with ( $ line , ' # ' ) || str_starts_with ( $ line , ' = ' ) ) {
257
+ if ( ! $ header ) {
248
258
if ( empty ( $ line ) ) {
249
259
// Some plugins have line-breaks within the headers...
250
260
$ last_line_was_blank = true ;
@@ -255,12 +265,10 @@ protected function parse_readme_contents( $contents ) {
255
265
}
256
266
}
257
267
258
- $ bits = explode ( ': ' , trim ( $ line ), 2 );
259
- list ( $ key , $ value ) = $ bits ;
260
- $ key = strtolower ( trim ( $ key , " \t*- \r\n" ) );
268
+ list ( $ key , $ value ) = $ header ;
261
269
262
270
if ( isset ( $ this ->valid_headers [ $ key ] ) ) {
263
- $ headers [ $ this ->valid_headers [ $ key ] ] = trim ( $ value ) ;
271
+ $ headers [ $ this ->valid_headers [ $ key ] ] = $ value ;
264
272
} elseif ( $ last_line_was_blank ) {
265
273
// If we skipped over a blank line, and then ended up with an unexpected header, assume we parsed too far and ended up in the Short Description.
266
274
// This final line will be added back into the stack after the loop for further parsing.
@@ -521,6 +529,31 @@ protected function trim_length( $desc, $length = 150 ) {
521
529
return trim ( $ desc );
522
530
}
523
531
532
+ /**
533
+ * Parse a line to see if it's a header.
534
+ *
535
+ * @access protected
536
+ *
537
+ * @param string $line The line from the readme to parse.
538
+ * @param bool $only_valid Whether to only return a valid known header.
539
+ * @return false|array
540
+ */
541
+ protected function parse_possible_header ( $ line , $ only_valid = false ) {
542
+ if ( ! str_contains ( $ line , ': ' ) || str_starts_with ( $ line , '# ' ) || str_starts_with ( $ line , '= ' ) ) {
543
+ return false ;
544
+ }
545
+
546
+ list ( $ key , $ value ) = explode ( ': ' , $ line , 2 );
547
+ $ key = strtolower ( trim ( $ key , " \t*- \r\n" ) );
548
+ $ value = trim ( $ value , " \t*- \r\n" );
549
+
550
+ if ( $ only_valid && ! isset ( $ this ->valid_headers [ $ key ] ) ) {
551
+ return false ;
552
+ }
553
+
554
+ return [ $ key , $ value ];
555
+ }
556
+
524
557
/**
525
558
* @access protected
526
559
*
0 commit comments