Theme   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 5
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A themeDeleteDefaults() 0 12 2
A themeDelete() 0 4 1
A themeActivate() 0 4 1
C themeInstall() 0 37 7
1
<?php
2
namespace PressCLI\WPCLI;
3
4
use PressCLI\WPCLI\CLI;
5
use PressCLI\ThemeInstall\Zip;
6
use PressCLI\Option\Configuration;
7
use PressCLI\ThemeInstall\StyleCSS;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
trait Theme
11
{
12
    /**
13
     * Deletes all unused default themes.
14
     */
15
    public static function themeDeleteDefaults()
16
    {
17
        $themes = [
18
            'twentyfifteen',
19
            'twentyfourteen',
20
            'twentysixteen',
21
        ];
22
23
        foreach ($themes as $theme) {
24
            self::themeDelete($theme);
25
        }
26
    }
27
28
    /**
29
     * Deletes a theme.
30
     *
31
     * @param  string $theme Theme name to delete.
32
     */
33
    public static function themeDelete($theme)
34
    {
35
        CLI::execCommand('theme', ['delete', $theme]);
36
    }
37
38
    /**
39
     * Activates a theme.
40
     *
41
     * @param  string $theme Theme name to activate.
42
     */
43
    protected static function themeActivate($theme)
44
    {
45
        CLI::execCommand('theme', ['activate', $theme]);
46
    }
47
48
    /**
49
     * Installs a theme.
50
     *
51
     * @param  OutputInterface $output
52
     */
53
    public static function themeInstall(OutputInterface $output)
54
    {
55
        $config = Configuration::get();
56
        $url = isset($config['theme']['url']) ? $config['theme']['url'] : '';
57
        $name = isset($config['theme']['name']) ? $config['theme']['name'] : '';
58
        $directory = getcwd() . '/wp-content/themes';
59
60
        // Make sure we have the required configuration.
61
        if (!$url || !$name) {
62
            return;
63
        }
64
65
        // Make sure the theme does not already exist.
66
        if (file_exists("{$directory}/{$name}")) {
67
            $output->writeln("Warning: Theme already exists in {$directory}/{$name}.");
68
69
            // Activate theme.
70
            self::themeActivate($name);
71
72
            return;
73
        }
74
75
        // Download the theme to the themes directory.
76
        $theme = Zip::execute($url, $directory, $name);
77
        if (!$theme) {
78
            $output->writeln("<error>Error: Theme could not be downloaded from {$url}!</error>");
79
            return;
80
        }
81
82
        $output->writeln("Success: Theme successfully installed!");
83
84
        // Set style.css
85
        StyleCSS::set();
86
87
        // Activate theme.
88
        self::themeActivate($name);
89
    }
90
}
91