Skip to content

Commit 4a1c2c7

Browse files
committed
Refactor readme parser
1 parent b7d6b58 commit 4a1c2c7

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
}
1818
],
1919
"require": {
20-
"afragen/wordpress-plugin-readme-parser": "dev-master",
2120
"composer/semver": "^1.4 || ^2 || ^3",
2221
"wp-cli/wp-cli": "^2.10"
2322
},

src/Plugin_Command.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

3-
use WordPressdotorg\Plugin_Directory\Readme\Parser;
43
use WP_CLI\ParsePluginNameInput;
54
use WP_CLI\Utils;
65
use WP_CLI\WpOrgApi;
76

7+
use function WP_CLI\Utils\normalize_path;
8+
89
/**
910
* Manages plugins, including installs, activations, and updates.
1011
*
@@ -747,12 +748,30 @@ protected function get_item_list() {
747748
];
748749

749750
if ( $this->check_headers['tested_up_to'] ) {
750-
// Include information from the plugin readme.txt headers.
751-
$plugin_readme = WP_PLUGIN_DIR . '/' . $name . '/readme.txt';
751+
$plugin_readme = normalize_path( WP_PLUGIN_DIR . '/' . $name . '/readme.txt' );
752+
753+
if ( file_exists( $plugin_readme ) && is_readable( $plugin_readme ) ) {
754+
$readme_obj = new SplFileObject( $plugin_readme );
755+
$readme_obj->setFlags( SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY );
756+
$readme_line = 0;
757+
758+
// Reading the whole file can exhaust the memory, so only read the first 100 lines of the file,
759+
// as the "Tested up to" header should be near the top.
760+
while ( $readme_line < 100 && ! $readme_obj->eof() ) {
761+
$line = $readme_obj->fgets();
762+
763+
// Similar to WP.org, it matches for both "Tested up to" and "Tested" header in the readme file.
764+
preg_match( '/^tested(:| up to:) (.*)$/i', strtolower( $line ), $matches );
765+
766+
if ( isset( $matches[2] ) && ! empty( $matches[2] ) ) {
767+
$items[ $file ]['tested_up_to'] = $matches[2];
768+
break;
769+
}
770+
771+
++$readme_line;
772+
}
752773

753-
if ( file_exists( $plugin_readme ) ) {
754-
$readme_parser = new Parser( $plugin_readme );
755-
$items[ $file ]['tested_up_to'] = $readme_parser->tested ? $readme_parser->tested : '';
774+
$file_obj = null;
756775
}
757776
}
758777

0 commit comments

Comments
 (0)