Skip to content

Commit dc5b7c1

Browse files
authored
Fix update command using GF version for add-on slugs (#32)
* Add GF_CLI_Root::get_addon() * Fix update command using GF version for add-on slugs * bump version; update change log
1 parent 269d5b1 commit dc5b7c1

File tree

4 files changed

+54
-33
lines changed

4 files changed

+54
-33
lines changed

change_log.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
= 1.3.2 =
2+
- Fixed an issue where the version comparison performed when using `wp gf update` with an add-on slug uses the Gravity Forms version number.
3+
4+
15
= 1.3.1 =
26
- Added support for using `--version=beta` with the `wp gf install` and `wp gf update` commands. Add-On beta releases are not currently supported.
37
- Fixed a fatal error which can occur when using the `wp gf version` command with an add-on slug when Gravity Forms is not active or not installed.

cli.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: Gravity Forms CLI
44
Plugin URI: https://gravityforms.com
55
Description: Manage Gravity Forms with the WP CLI.
6-
Version: 1.3.1
6+
Version: 1.3.2
77
Author: Rocketgenius
88
Author URI: https://gravityforms.com
99
License: GPL-2.0+
@@ -30,7 +30,7 @@
3030
defined( 'ABSPATH' ) || die();
3131

3232
// Defines the current version of the CLI add-on
33-
define( 'GF_CLI_VERSION', '1.3.1' );
33+
define( 'GF_CLI_VERSION', '1.3.2' );
3434

3535
define( 'GF_CLI_MIN_GF_VERSION', '1.9.17.8' );
3636

includes/class-gf-cli-root.php

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,8 @@ public function version( $args, $assoc_args ) {
3838
if ( $slug == 'gravityforms' ) {
3939
WP_CLI::log( GFForms::$version );
4040
} else {
41-
$addon_class_names = GFAddOn::get_registered_addons();
42-
$addon_found = false;
43-
foreach ( $addon_class_names as $addon_class_name ) {
44-
/* @var GFAddon $addon */
45-
$addon = call_user_func( array( $addon_class_name, 'get_instance' ) );
46-
if ( $addon->get_slug() == $slug ) {
47-
WP_CLI::log( $addon->get_version() );
48-
$addon_found = true;
49-
break;
50-
}
51-
}
52-
53-
if ( ! $addon_found ) {
54-
WP_CLI::error( 'Invalid plugin slug: ' . $slug );
55-
}
41+
$addon = $this->get_addon( $slug );
42+
WP_CLI::log( $addon->get_version() );
5643
}
5744
}
5845

@@ -199,24 +186,32 @@ public function update( $args, $assoc_args ) {
199186
WP_CLI::error( 'A valid license key must be saved in the settings or specified in the GF_LICENSE_KEY constant or the --key option.' );
200187
}
201188

189+
if ( $slug === 'gravityforms' ) {
190+
$current_version = GFForms::$version;
191+
} else {
192+
$addon = $this->get_addon( $slug );
193+
$current_version = $addon->get_version();
194+
}
195+
202196
$version = isset( $assoc_args['version'] ) ? $assoc_args['version'] : 'hotfix';
203197
$plugin_info = $version === 'beta' ? $this->get_beta_plugin_info( $slug, $key ) : $this->get_plugin_info( $slug, $key );
204198

205199
if ( $version == 'hotfix' ) {
206200
$available_version = isset( $plugin_info['version_latest'] ) ? $plugin_info['version_latest'] : '';
207-
$download_url = isset( $plugin_info['download_url_latest'] ) ? $plugin_info['download_url_latest'] : '';
201+
$download_url = isset( $plugin_info['download_url_latest'] ) ? $plugin_info['download_url_latest'] : '';
208202
} else {
209203
$available_version = isset( $plugin_info['version'] ) ? $plugin_info['version'] : '';
210-
$download_url = isset( $plugin_info['download_url'] ) ? $plugin_info['download_url'] : '';
211-
}
212-
213-
if ( version_compare( GFForms::$version, $available_version, '>=' ) ) {
214-
WP_CLI::success( 'Plugin already updated' );
215-
return;
204+
$download_url = isset( $plugin_info['download_url'] ) ? $plugin_info['download_url'] : '';
216205
}
217206

218207
if ( $plugin_info && ! empty( $download_url ) ) {
219208

209+
if ( version_compare( $current_version, $available_version, '>=' ) ) {
210+
WP_CLI::success( 'Plugin already updated' );
211+
212+
return;
213+
}
214+
220215
$download_url .= '&key=' . $key;
221216

222217
$command = sprintf( 'plugin install "%s" --force', $download_url );
@@ -282,15 +277,9 @@ public function setup( $args, $assoc_args ) {
282277
}
283278
}
284279
} else {
285-
$addon_class_names = GFAddOn::get_registered_addons();
286-
foreach ( $addon_class_names as $addon_class_name ) {
287-
$addon = call_user_func( array( $addon_class_name, 'get_instance' ) );
288-
if ( $addon->get_slug() == $slug ) {
289-
$addon->setup();
290-
WP_CLI::success( 'setup ' . $slug );
291-
break;
292-
}
293-
}
280+
$addon = $this->get_addon( $slug );
281+
$addon->setup();
282+
WP_CLI::success( 'setup ' . $slug );
294283
}
295284
}
296285

@@ -437,4 +426,29 @@ private function get_slug( $args, $beta_check = false ) {
437426
return $slug;
438427
}
439428

429+
/**
430+
* Returns the add-on instance for the given slug.
431+
*
432+
* @since 1.4
433+
*
434+
* @param string $slug The add-on slug.
435+
*
436+
* @return GFAddon
437+
* @throws \WP_CLI\ExitException
438+
*/
439+
private function get_addon( $slug ) {
440+
$addon_class_names = GFAddOn::get_registered_addons();
441+
442+
foreach ( $addon_class_names as $addon_class_name ) {
443+
/* @var GFAddon $addon */
444+
$addon = call_user_func( array( $addon_class_name, 'get_instance' ) );
445+
if ( $addon->get_slug() == $slug ) {
446+
447+
return $addon;
448+
}
449+
}
450+
451+
WP_CLI::error( 'Invalid slug or plugin not active: ' . $slug );
452+
}
453+
440454
}

readme.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ If you have any ideas for improvements please submit your idea at https://www.gr
185185

186186
== ChangeLog ==
187187

188+
= 1.3.2 =
189+
- Fixed an issue where the version comparison performed when using `wp gf update` with an add-on slug uses the Gravity Forms version number.
190+
188191
= 1.3.1 =
189192
- Added support for using `--version=beta` with the `wp gf install` and `wp gf update` commands. Add-On beta releases are not currently supported.
190193
- Fixed a fatal error which can occur when using the `wp gf version` command with an add-on slug when Gravity Forms is not active or not installed.

0 commit comments

Comments
 (0)