Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ThemeService
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 retry
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 initialize
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2namespace NewfoldLabs\WP\Module\Onboarding\Services;
3
4use NewfoldLabs\WP\Module\Installer\Services\ThemeInstaller;
5use NewfoldLabs\WP\Module\Installer\Data\Themes as ThemeInstallerData;
6use NewfoldLabs\WP\Module\Onboarding\Data\Themes;
7
8/**
9 * Class for providing theme related services.
10 */
11class ThemeService {
12    /**
13     * Number of retry attempts made.
14     *
15     * @var int
16     */
17    private static $retries = 0;
18
19    /**
20     * Maximum number of retries allowed.
21     *
22     * @var int
23     */
24    private static $max_retries = 3;
25
26    /**
27     * Retry the theme installation.
28     *
29     * @return bool True if the retry was successful, false otherwise.
30     */
31    private static function retry(): bool {
32        ++self::$retries;
33        if ( self::$retries < self::$max_retries ) {
34            return self::initialize();
35        }
36
37        return false;
38    }
39
40    /**
41     * Initialize the theme installation.
42     *
43     * @return bool True if the installation was successful, false otherwise.
44     */
45    public static function initialize(): bool {
46        // Get the default sitegen theme to be installed.
47        $init_themes   = Themes::get_init( true );
48        $sitegen_theme = $init_themes['sitegen']['default'][0];
49
50        // If the sitegen theme is NOT installed or activated...
51        if ( ! ThemeInstaller::exists( $sitegen_theme['slug'], $sitegen_theme['activate'] ) ) {
52            // Get the sitegen theme installer data.
53            $themes_installer_data        = ThemeInstallerData::get()['nfd_slugs'];
54            $sitegen_theme_installer_data = $themes_installer_data[ $sitegen_theme['slug'] ];
55
56            // If the sitegen theme is installed but not activated.
57            if ( ThemeInstaller::is_theme_installed( $sitegen_theme_installer_data['stylesheet'] ) ) {
58                // Activate the sitegen theme.
59                \switch_theme( $sitegen_theme_installer_data['stylesheet'] );
60                return true;
61            }
62
63            // Install and activate the sitegen theme.
64            $installer_response = ThemeInstaller::install_from_zip(
65                $sitegen_theme_installer_data['url'],
66                true,
67                $sitegen_theme_installer_data['stylesheet']
68            );
69
70            // If the installation fails, retry.
71            if ( is_wp_error( $installer_response ) ) {
72                return self::retry();
73            }
74        }
75
76        return true;
77    }
78}