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
<?php
/**
* Addon Install class file
*
* @package LifterLMS/CLI
*
* @since 0.0.1
* @version 0.0.1
*/
namespace LifterLMS\CLI\Commands\AddOn;
/**
* AddOn Installation command
*
* @since 0.0.1
*/
trait Install {
/**
* Install one of more add-ons.
*
* ## OPTIONS
*
* [<slug>...]
* : The slug of one or more add-on to install.
*
* [--key=<key>]
* : If set, will attempt to activate and use the provided license key.
*
* [--activate]
* : If set, the add-on(s) will be activated immediately after install.
*
* [--all]
* : If set, all of the add-ons available to the site will be installed.
* All existing license keys stored on the site will be queried for the list of available add-ons.
*
* [--type=<type>]
* : When using '--all', determines the type of add-on to be installed.
* ---
* default: 'all'
* options:
* - all
* - plugin
* - theme
* ---
*
* @since 0.0.1
*
* @param array $args Indexed array of positional command arguments.
* @param array $assoc_args Associative array of command options.
* @return null
*/
public function install( $args, $assoc_args ) {
// If a key is provided, activate it first.
if ( ! empty( $assoc_args['key'] ) ) {
\WP_CLI::runcommand( "llms license activate {$assoc_args['key']}" );
}
if ( ! empty( $assoc_args['all'] ) ) {
$args = $this->get_available_addons( 'uninstalled', true, $assoc_args['type'] );
if ( empty( $args ) ) {
return \WP_CLI::warning( 'No add-ons to install.' );
}
}
$results = $this->loop( $args, $assoc_args, 'install_one' );
\WP_CLI\Utils\report_batch_operation_results( 'add-on', 'install', count( $args ), $results['successes'], $results['errors'] );
}
/**
* Loop callback function for install()
*
* Ensures add-on can be installed and actually installs (and maybe activates) the add-on.
*
* @since 0.0.1
*
* @param string $slug Add-on slug.
* @param LLMS_Add_On $addon Add-on object.
* @param array $assoc_args Associative array of command options.
* @return null|true Returns `null` if an error is encountered and `true` on success.
*/
private function install_one( $slug, $addon, $assoc_args ) {
if ( $addon->is_installed() ) {
return \WP_CLI::warning( sprintf( 'Add-on "%s" is already installed.', $slug ) );
}
\WP_CLI::log( sprintf( 'Installing add-on: %s...', $addon->get( 'title' ) ) );
$res = $addon->install();
if ( is_wp_error( $res ) ) {
return \WP_CLI::warning( $res );
}
\WP_CLI::log( $res );
if ( ! empty( $assoc_args['activate'] ) ) {
$this->chain_command( 'activate', array( $slug ) );
}
return true;
}
}