Skip to content

Commit ae6adca

Browse files
authored
Merge pull request #413 from tfirdaus/feat/241-add-tested-up-to-head
2 parents 5dcbb5e + c94e23f commit ae6adca

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

features/plugin.feature

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ Feature: Manage WordPress plugins
649649

650650
When I run `wp plugin list --name=hello-dolly --field=version`
651651
And save STDOUT as {PLUGIN_VERSION}
652-
652+
653653
When I run `wp plugin list --name=hello-dolly --field=update_version`
654654
And save STDOUT as {UPDATE_VERSION}
655655

@@ -720,3 +720,87 @@ Feature: Manage WordPress plugins
720720
Then STDOUT should be a table containing rows:
721721
| name | auto_update |
722722
| hello | on |
723+
724+
Scenario: Listing plugins should include tested_up_to from the 'tested up to' header
725+
Given a WP install
726+
And a wp-content/plugins/foo/foo.php file:
727+
"""
728+
<?php
729+
/**
730+
* Plugin Name: Foo
731+
* Description: A plugin for foo
732+
* Author: Matt
733+
*/
734+
"""
735+
And a wp-content/plugins/foo/readme.txt file:
736+
"""
737+
=== Foo ===
738+
Contributors: matt
739+
Donate link: https://example.com/
740+
Tags: tag1, tag2
741+
Requires at least: 4.7
742+
Tested up to: 3.4
743+
Stable tag: 4.3
744+
Requires PHP: 7.0
745+
License: GPLv2 or later
746+
License URI: https://www.gnu.org/licenses/gpl-2.0.html
747+
"""
748+
And I run `wp plugin activate foo`
749+
750+
When I run `wp plugin list`
751+
Then STDOUT should be a table containing rows:
752+
| name | status | update | version | update_version | auto_update |
753+
| foo | active | none | | | off |
754+
755+
When I run `wp plugin list --fields=name,tested_up_to`
756+
Then STDOUT should be a table containing rows:
757+
| name | tested_up_to |
758+
| foo | 3.4 |
759+
760+
And I run `wp plugin list --name=foo --field=tested_up_to`
761+
Then STDOUT should be:
762+
"""
763+
3.4
764+
"""
765+
766+
Scenario: Listing plugins should include tested_up_to from the 'tested' header
767+
Given a WP install
768+
And a wp-content/plugins/foo/foo.php file:
769+
"""
770+
<?php
771+
/**
772+
* Plugin Name: Foo
773+
* Description: A plugin for foo
774+
* Author: Matt
775+
*/
776+
"""
777+
And a wp-content/plugins/foo/readme.txt file:
778+
"""
779+
=== Foo ===
780+
Tested: 5.5
781+
Contributors: matt
782+
Donate link: https://example.com/
783+
Tags: tag1, tag2
784+
Requires at least: 4.7
785+
Stable tag: 4.3
786+
Requires PHP: 7.0
787+
License: GPLv2 or later
788+
License URI: https://www.gnu.org/licenses/gpl-2.0.html
789+
"""
790+
And I run `wp plugin activate foo`
791+
792+
When I run `wp plugin list`
793+
Then STDOUT should be a table containing rows:
794+
| name | status | update | version | update_version | auto_update |
795+
| foo | active | none | | | off |
796+
797+
When I run `wp plugin list --fields=name,tested_up_to`
798+
Then STDOUT should be a table containing rows:
799+
| name | tested_up_to |
800+
| foo | 5.5 |
801+
802+
And I run `wp plugin list --name=foo --field=tested_up_to`
803+
Then STDOUT should be:
804+
"""
805+
5.5
806+
"""

src/Plugin_Command.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use WP_CLI\Utils;
55
use WP_CLI\WpOrgApi;
66

7+
use function WP_CLI\Utils\normalize_path;
8+
79
/**
810
* Manages plugins, including installs, activations, and updates.
911
*
@@ -51,6 +53,9 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
5153
'status' => false,
5254
'last_updated' => false,
5355
];
56+
protected $check_headers = [
57+
'tested_up_to' => false,
58+
];
5459

5560
protected $obj_fields = array(
5661
'name',
@@ -265,6 +270,7 @@ protected function get_all_items() {
265270
'description' => $mu_description,
266271
'file' => $file,
267272
'auto_update' => false,
273+
'tested_up_to' => '',
268274
'wporg_status' => $wporg_info['status'],
269275
'wporg_last_updated' => $wporg_info['last_updated'],
270276
);
@@ -286,6 +292,7 @@ protected function get_all_items() {
286292
'file' => $name,
287293
'auto_update' => false,
288294
'author' => $item_data['Author'],
295+
'tested_up_to' => '',
289296
'wporg_status' => '',
290297
'wporg_last_updated' => '',
291298
];
@@ -735,10 +742,39 @@ protected function get_item_list() {
735742
'file' => $file,
736743
'auto_update' => in_array( $file, $auto_updates, true ),
737744
'author' => $details['Author'],
745+
'tested_up_to' => '',
738746
'wporg_status' => $wporg_info['status'],
739747
'wporg_last_updated' => $wporg_info['last_updated'],
740748
];
741749

750+
if ( $this->check_headers['tested_up_to'] ) {
751+
$plugin_readme = normalize_path( dirname( WP_PLUGIN_DIR . '/' . $file ) . '/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+
}
773+
774+
$file_obj = null;
775+
}
776+
}
777+
742778
if ( null === $update_info ) {
743779
// Get info for all plugins that don't have an update.
744780
$plugin_update_info = isset( $all_update_info->no_update[ $file ] ) ? $all_update_info->no_update[ $file ] : null;
@@ -1289,6 +1325,7 @@ public function delete( $args, $assoc_args = array() ) {
12891325
* * description
12901326
* * file
12911327
* * author
1328+
* * tested_up_to
12921329
* * wporg_status
12931330
* * wporg_last_updated
12941331
*
@@ -1332,6 +1369,8 @@ public function list_( $_, $assoc_args ) {
13321369
$fields = explode( ',', $fields );
13331370
$this->check_wporg['status'] = in_array( 'wporg_status', $fields, true );
13341371
$this->check_wporg['last_updated'] = in_array( 'wporg_last_updated', $fields, true );
1372+
1373+
$this->check_headers['tested_up_to'] = in_array( 'tested_up_to', $fields, true );
13351374
}
13361375

13371376
$field = Utils\get_flag_value( $assoc_args, 'field' );
@@ -1341,6 +1380,8 @@ public function list_( $_, $assoc_args ) {
13411380
$this->check_wporg['last_updated'] = true;
13421381
}
13431382

1383+
$this->check_headers['tested_up_to'] = 'tested_up_to' === $field || $this->check_headers['tested_up_to'];
1384+
13441385
parent::_list( $_, $assoc_args );
13451386
}
13461387

0 commit comments

Comments
 (0)