From ca887dc711b13a04ba31106d52a1de33dbca46a7 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Thu, 25 Apr 2024 14:53:49 +0545 Subject: [PATCH] Show requires fields optionally --- features/plugin-get.feature | 27 +++++++++++++++++++++++++ src/Plugin_Command.php | 39 +++++++++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/features/plugin-get.feature b/features/plugin-get.feature index 403bbb64..cdb3a75a 100644 --- a/features/plugin-get.feature +++ b/features/plugin-get.feature @@ -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 @@ -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: + """ + 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 );