Passed
Push — master ( a5e8db...945884 )
by Danny
02:33
created

Plugin::pluginInstallAll()   C

Complexity

Conditions 9
Paths 98

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 13
nc 98
nop 1
dl 0
loc 22
rs 6.412
c 0
b 0
f 0
1
<?php
2
namespace PressCLI\WPCLI;
3
4
use PressCLI\WPCLI\CLI;
5
use PressCLI\Option\Configuration;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
trait Plugin
9
{
10
    /**
11
     * Deletes all unused default plugins.
12
     */
13
    public static function pluginDeleteDefaults()
14
    {
15
        $plugins = [
16
            'akismet',
17
            'hello',
18
        ];
19
20
        foreach ($plugins as $plugin) {
21
            self::pluginDelete($plugin);
22
        }
23
    }
24
25
    /**
26
     * Deletes a plugin.
27
     *
28
     * @param  string $plugin Theme name to delete.
29
     */
30
    public static function pluginDelete($plugin)
31
    {
32
        CLI::execCommand('plugin', ['delete', $plugin]);
33
    }
34
35
    /**
36
     * Installs all of the plugins from the configuration.
37
     *
38
     * @param  OutputInterface $output
39
     */
40
    public static function pluginInstallAll(OutputInterface $output)
41
    {
42
        $config = Configuration::get();
43
        $plugins = isset($config['plugins']) ? $config['plugins'] : [];
44
        foreach ($plugins as $plugin) {
45
            $version = isset($plugin['version']) ? $plugin['version'] : '';
46
            $activate = isset($plugin['activate']) ? (bool) $plugin['activate'] : false;
47
            $location = isset($plugin['location']) ? $plugin['location'] : '';
48
49
            // Let the user know which custom plugin is being installed.
50
            if ($location) {
51
                $locVersion = $version ? "($version)" : '';
52
                $output->writeln(trim("Installing {$plugin['plugin']} {$locVersion}"));
53
            }
54
55
            // Install the plugin.
56
            $install = $location ? $location : $plugin['plugin'];
57
            self::pluginInstall($install, $activate, $version);
58
59
            $output->writeln('');
60
        }
61
    }
62
63
    /**
64
     * Installs and optionally activates plugin.
65
     *
66
     * @param string  $plugin   A plugin slug, the path to a local zip file, or URL to a remote zip file.
67
     * @param boolean $activate Optional, if true the plugin will be activated immediately after install.
68
     * @param string  $version  Optional, version of plugin to install. Default stable.
69
     */
70
    public static function pluginInstall($plugin, $activate = true, $version = null)
71
    {
72
        $options = [ 'force' => '' ];
73
74
        if ($activate) {
75
            $options['activate'] = '';
76
        }
77
78
        if ($version) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
79
            $options['version'] = $version;
80
        }
81
82
        CLI::execCommand('plugin', ['install', $plugin], $options);
83
    }
84
}
85