Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 85 additions & 1 deletion features/plugin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ Feature: Manage WordPress plugins

When I run `wp plugin list --name=hello-dolly --field=version`
And save STDOUT as {PLUGIN_VERSION}

When I run `wp plugin list --name=hello-dolly --field=update_version`
And save STDOUT as {UPDATE_VERSION}

Expand Down Expand Up @@ -720,3 +720,87 @@ Feature: Manage WordPress plugins
Then STDOUT should be a table containing rows:
| name | auto_update |
| hello | on |

Scenario: Listing plugins should include tested_up_to from the 'tested up to' header
Given a WP install
And a wp-content/plugins/foo/foo.php file:
"""
<?php
/**
* Plugin Name: Foo
* Description: A plugin for foo
* Author: Matt
*/
"""
And a wp-content/plugins/foo/readme.txt file:
"""
=== Foo ===
Contributors: matt
Donate link: https://example.com/
Tags: tag1, tag2
Requires at least: 4.7
Tested up to: 3.4
Stable tag: 4.3
Requires PHP: 7.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
"""
And I run `wp plugin activate foo`

When I run `wp plugin list`
Then STDOUT should be a table containing rows:
| name | status | update | version | update_version | auto_update |
| foo | active | none | | | off |

When I run `wp plugin list --fields=name,tested_up_to`
Then STDOUT should be a table containing rows:
| name | tested_up_to |
| foo | 3.4 |

And I run `wp plugin list --name=foo --field=tested_up_to`
Then STDOUT should be:
"""
3.4
"""

Scenario: Listing plugins should include tested_up_to from the 'tested' header
Given a WP install
And a wp-content/plugins/foo/foo.php file:
"""
<?php
/**
* Plugin Name: Foo
* Description: A plugin for foo
* Author: Matt
*/
"""
And a wp-content/plugins/foo/readme.txt file:
"""
=== Foo ===
Tested: 5.5
Contributors: matt
Donate link: https://example.com/
Tags: tag1, tag2
Requires at least: 4.7
Stable tag: 4.3
Requires PHP: 7.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
"""
And I run `wp plugin activate foo`

When I run `wp plugin list`
Then STDOUT should be a table containing rows:
| name | status | update | version | update_version | auto_update |
| foo | active | none | | | off |

When I run `wp plugin list --fields=name,tested_up_to`
Then STDOUT should be a table containing rows:
| name | tested_up_to |
| foo | 5.5 |

And I run `wp plugin list --name=foo --field=tested_up_to`
Then STDOUT should be:
"""
5.5
"""
41 changes: 41 additions & 0 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use WP_CLI\Utils;
use WP_CLI\WpOrgApi;

use function WP_CLI\Utils\normalize_path;

/**
* Manages plugins, including installs, activations, and updates.
*
Expand Down Expand Up @@ -51,6 +53,9 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
'status' => false,
'last_updated' => false,
];
protected $check_headers = [
'tested_up_to' => false,
];

protected $obj_fields = array(
'name',
Expand Down Expand Up @@ -265,6 +270,7 @@ protected function get_all_items() {
'description' => $mu_description,
'file' => $file,
'auto_update' => false,
'tested_up_to' => '',
'wporg_status' => $wporg_info['status'],
'wporg_last_updated' => $wporg_info['last_updated'],
);
Expand All @@ -286,6 +292,7 @@ protected function get_all_items() {
'file' => $name,
'auto_update' => false,
'author' => $item_data['Author'],
'tested_up_to' => '',
'wporg_status' => '',
'wporg_last_updated' => '',
];
Expand Down Expand Up @@ -735,10 +742,39 @@ protected function get_item_list() {
'file' => $file,
'auto_update' => in_array( $file, $auto_updates, true ),
'author' => $details['Author'],
'tested_up_to' => '',
'wporg_status' => $wporg_info['status'],
'wporg_last_updated' => $wporg_info['last_updated'],
];

if ( $this->check_headers['tested_up_to'] ) {
$plugin_readme = normalize_path( dirname( WP_PLUGIN_DIR . '/' . $file ) . '/readme.txt' );

if ( file_exists( $plugin_readme ) && is_readable( $plugin_readme ) ) {
$readme_obj = new SplFileObject( $plugin_readme );
$readme_obj->setFlags( SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY );
$readme_line = 0;

// Reading the whole file can exhaust the memory, so only read the first 100 lines of the file,
// as the "Tested up to" header should be near the top.
while ( $readme_line < 100 && ! $readme_obj->eof() ) {
$line = $readme_obj->fgets();

// Similar to WP.org, it matches for both "Tested up to" and "Tested" header in the readme file.
preg_match( '/^tested(:| up to:) (.*)$/i', strtolower( $line ), $matches );

if ( isset( $matches[2] ) && ! empty( $matches[2] ) ) {
$items[ $file ]['tested_up_to'] = $matches[2];
break;
}

++$readme_line;
}

$file_obj = null;
}
}

if ( null === $update_info ) {
// Get info for all plugins that don't have an update.
$plugin_update_info = isset( $all_update_info->no_update[ $file ] ) ? $all_update_info->no_update[ $file ] : null;
Expand Down Expand Up @@ -1260,6 +1296,7 @@ public function delete( $args, $assoc_args = array() ) {
* * description
* * file
* * author
* * tested_up_to
* * wporg_status
* * wporg_last_updated
*
Expand Down Expand Up @@ -1303,6 +1340,8 @@ public function list_( $_, $assoc_args ) {
$fields = explode( ',', $fields );
$this->check_wporg['status'] = in_array( 'wporg_status', $fields, true );
$this->check_wporg['last_updated'] = in_array( 'wporg_last_updated', $fields, true );

$this->check_headers['tested_up_to'] = in_array( 'tested_up_to', $fields, true );
}

$field = Utils\get_flag_value( $assoc_args, 'field' );
Expand All @@ -1312,6 +1351,8 @@ public function list_( $_, $assoc_args ) {
$this->check_wporg['last_updated'] = true;
}

$this->check_headers['tested_up_to'] = 'tested_up_to' === $field || $this->check_headers['tested_up_to'];

parent::_list( $_, $assoc_args );
}

Expand Down