-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathclass-gf-cli-root.php
More file actions
384 lines (325 loc) · 10.4 KB
/
class-gf-cli-root.php
File metadata and controls
384 lines (325 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
defined( 'ABSPATH' ) || die();
/**
* Manage Gravity Forms.
*
* @class GF_CLI
* @version 1.0
* @package GravityForms/CLI
* @category CLI
* @author Rocketgenius
* @copyright Copyright (c) 2016-2018, Rocketgenius
*/
class GF_CLI_Root extends WP_CLI_Command {
/**
* Returns the version of Gravity Forms.
*
* @since 1.0-beta-5
* @access public
*
* [<slug>]
* : The slug of the plugin. Default: gravityforms
*
* ## EXAMPLES
*
* wp gf version
* wp gf version gravityformspolls
*/
public function version( $args, $assoc_args ) {
$slug = isset( $args[0] ) ? $args[0] : 'gravityforms';
if ( $slug == 'gravityforms' ) {
if ( class_exists( 'GFForms' ) ) {
WP_CLI::log( GFForms::$version );
} else {
WP_CLI::error( 'Gravity Forms is not installed. Use the wp gf install command.' );
}
} else {
$addon_class_names = GFAddOn::get_registered_addons();
$addon_found = false;
foreach ( $addon_class_names as $addon_class_name ) {
/* @var GFAddon $addon */
$addon = call_user_func( array( $addon_class_name, 'get_instance' ) );
if ( $addon->get_slug() == $slug ) {
WP_CLI::log( $addon->get_version() );
$addon_found = true;
break;
}
}
if ( ! $addon_found ) {
WP_CLI::error( 'Invalid pluging slug: ' . $slug );
}
}
}
/**
* Installs Gravity Forms or a Gravity Forms official add-on.
*
* A valid key is required either in the GF_LICENSE_KEY constant or the --key option.
*
* @since 1.0
* @access public
*
* [<slug>]
* : The slug of the add-on. Default: gravityforms
*
* [--key=<key>]
* : The license key if not already available in the GF_LICENSE_KEY constant.
*
* [--version=<version>]
* : The version to be installed. Accepted values: auto-update, hotfix. Default: hotfix.
*
* [--force]
* : If set, the command will overwrite any installed version of the plugin, without prompting for confirmation.
*
* [--activate]
* : If set, the plugin will be activated immediately after install.
*
* [--activate-network]
* : If set, the plugin will be network activated immediately after install
*
*
* ## EXAMPLES
*
* wp gf install
* wp gf install --force
* wp gf install --key=[A valid Gravity Forms License Key]
* wp gf install gravityformspolls key=[1234ABC]
* @synopsis [<slug>] [--key=<key>] [--version=<version>] [--force] [--activate] [--activate-network]
*/
public function install( $args, $assoc_args ) {
$slug = isset( $args[0] ) ? $args[0] : 'gravityforms';
$key = isset( $assoc_args['key'] ) ? $assoc_args['key'] : $key = $this->get_key();
if ( empty( $key ) ) {
WP_CLI::error( 'A valid license key must be specified either in the GF_LICENSE_KEY constant or the --key option.' );
}
$this->save_key( $key );
$key = md5( $key );
$plugin_info = $this->get_plugin_info( $slug, $key );
$version = isset( $assoc_args['version'] ) ? $assoc_args['version'] : 'hotfix';
if ( $version == 'hotfix' ) {
$download_url = isset( $plugin_info['download_url_latest'] ) ? $plugin_info['download_url_latest'] : '';
} else {
$download_url = isset( $plugin_info['download_url'] ) ? $plugin_info['download_url'] : '';
}
if ( $plugin_info && ! empty( $download_url ) ) {
$download_url .= '&key=' . $key;
$force = WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
$activate = WP_CLI\Utils\get_flag_value( $assoc_args, 'activate', false );
$activate_network = WP_CLI\Utils\get_flag_value( $assoc_args, 'activate-network', false );
$command = sprintf( 'plugin install "%s"', $download_url );
$install_assoc_args = array();
if ( $force ) {
$install_assoc_args['force'] = true;
$command .= ' --force';
}
if ( $activate ) {
$install_assoc_args['activate'] = true;
$command .= ' --activate';
}
if ( $activate_network ) {
$install_assoc_args['activate-network'] = true;
$command .= ' --activate-network';
}
$options = array(
'return' => false,
'launch' => true,
'exit_error' => true,
);
WP_CLI::runcommand( $command, $options );
} else {
WP_CLI::error( 'There was a problem retrieving the download URL, please check the key.' );
}
}
/**
* Updates Gravity Forms or a Gravity Forms official add-on.
*
* A valid key is required either in the Gravity Forms settings, the GF_LICENSE_KEY constant or the --key option.
*
* @since 1.0
* @access public
*
* [<slug>]
* : The slug of the add-on. Default: gravityforms
*
* [--key=<key>]
* : The license key if not already available in the GF_LICENSE_KEY constant.
*
* [--version=<version>]
* : The version to be installed. Accepted values: auto-update, hotfix. Default: hotfix.
*
*
* ## EXAMPLES
*
* wp gf update
* wp gf install --key=[A valid Gravity Forms License Key]
* wp gf install gravityformspolls key=[1234ABC]
* @synopsis [<slug>] [--key=<key>] [--version=<version>]
*/
public function update( $args, $assoc_args ) {
if ( ! class_exists( 'GFForms' ) ) {
WP_CLI::error( 'Gravity Forms is not active.' );
}
$slug = isset( $args[0] ) ? $args[0] : 'gravityforms';
$key = isset( $assoc_args['key'] ) ? $assoc_args['key'] : $key = $this->get_key();
if ( empty( $key ) ) {
$key = GFCommon::get_key();
} else {
$this->save_key( $key );
$key = GFCommon::get_key();
}
if ( empty( $key ) ) {
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.' );
}
$plugin_info = $this->get_plugin_info( $slug, $key );
$version = isset( $assoc_args['version'] ) ? $assoc_args['version'] : 'hotfix';
if ( $version == 'hotfix' ) {
$available_version = isset( $plugin_info['version_latest'] ) ? $plugin_info['version_latest'] : '';
$download_url = isset( $plugin_info['download_url_latest'] ) ? $plugin_info['download_url_latest'] : '';
} else {
$available_version = isset( $plugin_info['version'] ) ? $plugin_info['version'] : '';
$download_url = isset( $plugin_info['download_url'] ) ? $plugin_info['download_url'] : '';
}
if ( version_compare( GFForms::$version, $available_version, '>=' ) ) {
WP_CLI::success( 'Plugin already updated' );
return;
}
if ( $plugin_info && ! empty( $download_url ) ) {
$download_url .= '&key=' . $key;
$command = sprintf( 'plugin install "%s" --force', $download_url );
$options = array(
'return' => false,
'launch' => true,
'exit_error' => true,
);
WP_CLI::runcommand( $command, $options );
$setup_command = 'gf setup ' . $slug . ' --force';
WP_CLI::runcommand( $setup_command, $options );
} else {
WP_CLI::error( 'There was a problem retrieving the download URL, please check the key.' );
}
}
/**
* Runs the setup for Gravity Forms or a Gravity Forms official add-on.
*
*
* @since 1.0
* @access public
*
* [<slug>]
* : The slug of the add-on. Default: gravityforms
*
* [--force]
* : If set, the command will run the setup regardless of whether it has been run before.
*
* ## EXAMPLES
*
* wp gf setup
* wp gf setup --force
* wp gf setup gravityformspolls
* wp gf setup gravityformspolls --force
*
* @synopsis [<slug>] [--force]
*/
public function setup( $args, $assoc_args ) {
$slug = isset( $args[0] ) ? $args[0] : 'gravityforms';
$force = WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
if ( $slug == 'gravityforms' ) {
$versions = gf_upgrade()->get_versions();
if ( empty( $versions['current_version'] ) ) {
// First setup
gf_upgrade()->maybe_upgrade();
WP_CLI::success( 'setup ' . $slug );
} else {
// Re-running setup
if ( $force ) {
gf_upgrade()->upgrade( $versions['previous_db_version'], true );
WP_CLI::success( 'Database upgraded.' );
} else {
WP_CLI::error( 'Use the --force flag to force the database setup.' );
}
}
} else {
$addon_class_names = GFAddOn::get_registered_addons();
foreach ( $addon_class_names as $addon_class_name ) {
$addon = call_user_func( array( $addon_class_name, 'get_instance' ) );
if ( $addon->get_slug() == $slug ) {
$addon->setup();
WP_CLI::success( 'setup ' . $slug );
break;
}
}
}
}
/**
* Checks for available updates for Gravity Forms or a Gravity Forms official add-on.
*
* A valid key is required either in the GF_LICENSE_KEY constant or the --key option.
*
* @since 1.0-beta-2
* @access public
*
* [<slug>]
* : The slug of the add-on. Default: gravityforms
*
* [--format=<format>]
* : Accepted values: table, csv, json, count. Default: table.
*
* ## EXAMPLES
*
* wp gf check-update
* wp gf check-update gravityformspolls
*
* @synopsis [<slug>] [--format=<format>]
* @alias check-update
*/
public function check_update( $args, $assoc_args ) {
$slug = isset( $args[0] ) ? $args[0] : 'gravityforms';
$plugin_info = $this->get_plugin_info( $slug );
$versions = array(
array(
'type' => 'auto-update',
'version' => $plugin_info['version'],
),
array(
'type' => 'hotfix',
'version' => $plugin_info['version_latest'],
),
);
$fields = array(
'type',
'version',
);
$format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table';
WP_CLI\Utils\format_items( $format, $versions, $fields );
}
private function save_key( $key ) {
$current_key = get_option( 'rg_gforms_key' );
if ( empty( $key ) ) {
delete_option( 'rg_gforms_key' );
} else if ( $current_key != $key ) {
$key = trim( $key );
update_option( 'rg_gforms_key', md5( $key ) );
}
}
private function get_key() {
global $gf_license_key;
$license_key = defined( 'GF_LICENSE_KEY' ) && empty( $gf_license_key ) ? GF_LICENSE_KEY : $gf_license_key;
return $license_key;
}
private function get_plugin_info( $slug, $key = '' ) {
$gravity_manager_url = defined( 'GRAVITY_MANAGER_URL' ) && GRAVITY_MANAGER_URL ? GRAVITY_MANAGER_URL : 'https://gravityapi.com/wp-content/plugins/gravitymanager';
$url = $gravity_manager_url . "/api.php?op=get_plugin&slug={$slug}&key={$key}";
$options = array(
'timeout' => 30,
);
$headers = array(
'Accept' => 'text/plain',
);
$response = \WP_CLI\Utils\http_request( 'GET', $url, null, $headers, $options );
if ( ! $response->success || 200 != $response->status_code ) {
WP_CLI::error( 'There was a problem getting the download URL' );
}
$body = trim( $response->body );
$plugin_info = unserialize( $body );
return $plugin_info;
}
}