Skip to content

Commit ca887dc

Browse files
committed
Show requires fields optionally
1 parent e491918 commit ca887dc

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

features/plugin-get.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ Feature: Get WordPress plugin
2626
| version | 1.0.0 |
2727
| status | inactive |
2828

29+
When I run `wp plugin get foo --format=json`
30+
Then STDOUT should be:
31+
"""
32+
{"name":"foo","title":"Sample Plugin","author":"John Doe","version":"1.0.0","description":"Description for sample plugin.","status":"inactive"}
33+
"""
34+
2935
@require-wp-6.5
3036
Scenario: Get Requires Plugins header of plugin
3137
Given a WP install
@@ -45,3 +51,24 @@ Feature: Get WordPress plugin
4551
"""
4652
jetpack, woocommerce
4753
"""
54+
55+
@require-wp-5.3
56+
Scenario: Get Requires PHP and Requires WP header of plugin
57+
Given a WP install
58+
And a wp-content/plugins/foo.php file:
59+
"""
60+
<?php
61+
/**
62+
* Plugin Name: Foo
63+
* Description: Foo plugin
64+
* Author: John Doe
65+
* Requires at least: 6.2
66+
* Requires PHP: 7.4
67+
*/
68+
"""
69+
70+
When I run `wp plugin get foo --fields=requires_wp,requires_php`
71+
Then STDOUT should be a table containing rows:
72+
| Field | Value |
73+
| requires_wp | 6.2 |
74+
| requires_php | 7.4 |

src/Plugin_Command.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -947,27 +947,46 @@ public function install( $args, $assoc_args ) {
947947
* {"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"}
948948
*/
949949
public function get( $args, $assoc_args ) {
950+
$default_fields = array(
951+
'name',
952+
'title',
953+
'author',
954+
'version',
955+
'description',
956+
'status',
957+
);
958+
950959
$plugin = $this->fetcher->get_check( $args[0] );
951960
$file = $plugin->file;
952961

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

955964
$plugin_obj = (object) [
956-
'name' => Utils\get_plugin_name( $file ),
957-
'title' => $plugin_data['Name'],
958-
'author' => $plugin_data['Author'],
959-
'version' => $plugin_data['Version'],
960-
'description' => wordwrap( $plugin_data['Description'] ),
961-
'status' => $this->get_status( $file ),
965+
'name' => Utils\get_plugin_name( $file ),
966+
'title' => $plugin_data['Name'],
967+
'author' => $plugin_data['Author'],
968+
'version' => $plugin_data['Version'],
969+
'description' => wordwrap( $plugin_data['Description'] ),
970+
'status' => $this->get_status( $file ),
971+
'requires_wp' => '',
972+
'requires_php' => '',
973+
'requires_plugins' => '',
962974
];
963975

964-
if ( isset( $plugin_data['RequiresPlugins'] ) && ! empty( $plugin_data['RequiresPlugins'] ) ) {
965-
$plugin_obj->requires_plugins = $plugin_data['RequiresPlugins'];
976+
$require_fields = [
977+
'requires_wp' => 'RequiresWP',
978+
'requires_php' => 'RequiresPHP',
979+
'requires_plugins' => 'RequiresPlugins',
980+
];
981+
982+
foreach ( $require_fields as $field_key => $data_key ) {
983+
if ( isset( $plugin_data[ $data_key ] ) && ! empty( $plugin_data[ $data_key ] ) ) {
984+
$plugin_obj->{$field_key} = $plugin_data[ $data_key ];
985+
}
966986
}
967987

968988
if ( empty( $assoc_args['fields'] ) ) {
969-
$plugin_array = get_object_vars( $plugin_obj );
970-
$assoc_args['fields'] = array_keys( $plugin_array );
989+
$assoc_args['fields'] = $default_fields;
971990
}
972991

973992
$formatter = $this->get_formatter( $assoc_args );

0 commit comments

Comments
 (0)