Skip to content

Commit

Permalink
Merge pull request #413 from tfirdaus/feat/241-add-tested-up-to-head
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Apr 26, 2024
2 parents 5dcbb5e + c94e23f commit ae6adca
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
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 @@ -1289,6 +1325,7 @@ public function delete( $args, $assoc_args = array() ) {
* * description
* * file
* * author
* * tested_up_to
* * wporg_status
* * wporg_last_updated
*
Expand Down Expand Up @@ -1332,6 +1369,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 @@ -1341,6 +1380,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

0 comments on commit ae6adca

Please sign in to comment.