Skip to content

Commit

Permalink
Merge pull request #414 from ernilambar/update/plugin-get-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Apr 25, 2024
2 parents 96a2d33 + 033353d commit e81579d
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 10 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,28 @@ wp plugin get <plugin> [--field=<field>] [--fields=<fields>] [--format=<format>]
- yaml
---

**AVAILABLE FIELDS**

These fields will be displayed by default for the plugin:

* name
* title
* author
* version
* description
* status

These fields are optionally available:

* requires_wp
* requires_php
* requires_plugins

**EXAMPLES**

# Get plugin details.
$ wp plugin get bbpress --format=json
{"name":"bbpress","title":"bbPress","author":"The bbPress Contributors","version":"2.6-alpha","description":"bbPress is forum software with a twist from the creators of WordPress.","status":"active"}
{"name":"bbpress","title":"bbPress","author":"The bbPress Contributors","version":"2.6.9","description":"bbPress is forum software with a twist from the creators of WordPress.","status":"active"}



Expand Down
74 changes: 74 additions & 0 deletions features/plugin-get.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
Feature: Get WordPress plugin

Scenario: Get plugin info
Given a WP install
And a wp-content/plugins/foo.php file:
"""
/**
* Plugin Name: Sample Plugin
* Description: Description for sample plugin.
* Requires at least: 6.0
* Requires PHP: 5.6
* Version: 1.0.0
* Author: John Doe
* Author URI: https://example.com/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: sample-plugin
*/
"""

When I run `wp plugin get foo --fields=name,author,version,status`
Then STDOUT should be a table containing rows:
| Field | Value |
| name | foo |
| author | John Doe |
| version | 1.0.0 |
| status | inactive |

When I run `wp plugin get foo --format=json`
Then STDOUT should be:
"""
{"name":"foo","title":"Sample Plugin","author":"John Doe","version":"1.0.0","description":"Description for sample plugin.","status":"inactive"}
"""

@require-wp-6.5
Scenario: Get Requires Plugins header of plugin
Given a WP install
And a wp-content/plugins/foo.php file:
"""
<?php
/**
* Plugin Name: Foo
* Description: Foo plugin
* Author: John Doe
* Requires Plugins: jetpack, woocommerce
*/
"""

When I run `wp plugin get foo --field=requires_plugins`
Then STDOUT should be:
"""
jetpack, woocommerce
"""

@require-wp-5.3
Scenario: Get Requires PHP and Requires WP header of plugin
Given a WP install
And a wp-content/plugins/foo.php file:
"""
<?php
/**
* Plugin Name: Foo
* Description: Foo plugin
* Author: John Doe
* Requires at least: 6.2
* Requires PHP: 7.4
*/
"""

When I run `wp plugin get foo --fields=requires_wp,requires_php`
Then STDOUT should be a table containing rows:
| Field | Value |
| requires_wp | 6.2 |
| requires_php | 7.4 |
47 changes: 38 additions & 9 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -941,29 +941,58 @@ public function install( $args, $assoc_args ) {
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for the plugin:
*
* * name
* * title
* * author
* * version
* * description
* * status
*
* These fields are optionally available:
*
* * requires_wp
* * requires_php
* * requires_plugins
*
* ## EXAMPLES
*
* # Get plugin details.
* $ wp plugin get bbpress --format=json
* {"name":"bbpress","title":"bbPress","author":"The bbPress Contributors","version":"2.6-alpha","description":"bbPress is forum software with a twist from the creators of WordPress.","status":"active"}
* {"name":"bbpress","title":"bbPress","author":"The bbPress Contributors","version":"2.6.9","description":"bbPress is forum software with a twist from the creators of WordPress.","status":"active"}
*/
public function get( $args, $assoc_args ) {
$default_fields = array(
'name',
'title',
'author',
'version',
'description',
'status',
);

$plugin = $this->fetcher->get_check( $args[0] );
$file = $plugin->file;

$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $file, false, false );

$plugin_obj = (object) [
'name' => Utils\get_plugin_name( $file ),
'title' => $plugin_data['Name'],
'author' => $plugin_data['Author'],
'version' => $plugin_data['Version'],
'description' => wordwrap( $plugin_data['Description'] ),
'status' => $this->get_status( $file ),
'name' => Utils\get_plugin_name( $file ),
'title' => $plugin_data['Name'],
'author' => $plugin_data['Author'],
'version' => $plugin_data['Version'],
'description' => wordwrap( $plugin_data['Description'] ),
'status' => $this->get_status( $file ),
'requires_wp' => ! empty( $plugin_data['RequiresWP'] ) ? $plugin_data['RequiresWP'] : '',
'requires_php' => ! empty( $plugin_data['RequiresPHP'] ) ? $plugin_data['RequiresPHP'] : '',
'requires_plugins' => ! empty( $plugin_data['RequiresPlugins'] ) ? $plugin_data['RequiresPlugins'] : '',
];

if ( empty( $assoc_args['fields'] ) ) {
$plugin_array = get_object_vars( $plugin_obj );
$assoc_args['fields'] = array_keys( $plugin_array );
$assoc_args['fields'] = $default_fields;
}

$formatter = $this->get_formatter( $assoc_args );
Expand Down

0 comments on commit e81579d

Please sign in to comment.