Skip to content

Commit

Permalink
Show requires fields optionally
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Apr 25, 2024
1 parent e491918 commit ca887dc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
27 changes: 27 additions & 0 deletions features/plugin-get.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Feature: Get WordPress plugin
| 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
Expand All @@ -45,3 +51,24 @@ Feature: Get WordPress plugin
"""
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 |
39 changes: 29 additions & 10 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -947,27 +947,46 @@ public function install( $args, $assoc_args ) {
* {"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"}
*/
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' => '',
'requires_php' => '',
'requires_plugins' => '',
];

if ( isset( $plugin_data['RequiresPlugins'] ) && ! empty( $plugin_data['RequiresPlugins'] ) ) {
$plugin_obj->requires_plugins = $plugin_data['RequiresPlugins'];
$require_fields = [
'requires_wp' => 'RequiresWP',
'requires_php' => 'RequiresPHP',
'requires_plugins' => 'RequiresPlugins',
];

foreach ( $require_fields as $field_key => $data_key ) {
if ( isset( $plugin_data[ $data_key ] ) && ! empty( $plugin_data[ $data_key ] ) ) {
$plugin_obj->{$field_key} = $plugin_data[ $data_key ];
}
}

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 ca887dc

Please sign in to comment.