<?php

if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Theme_Setup
{
    public const THEME_SLUG = 'generatepress';

    public const INSTALL_HOOK = 'puffergo_install_generatepress';

    public function __construct()
    {
        add_action(self::INSTALL_HOOK, array($this, 'run_scheduled_install'));
    }

    public static function is_generatepress_present()
    {
        return wp_get_theme(self::THEME_SLUG)->exists();
    }

    public function run_scheduled_install()
    {
        if (self::is_generatepress_present()) {
            return;
        } $this->install_generatepress();
    }

    public function install_generatepress()
    {
        $before = self::is_generatepress_present() ? 'installed' : 'not_installed';

        if ('installed' === $before) {
            return $this->result($before, 'installed', 'already_installed', __('GeneratePress is already installed.', 'puffergo'));
        } require_once ABSPATH . 'wp-admin/includes/theme-install.php';
        require_once ABSPATH . 'wp-admin/includes/file.php';
        require_once ABSPATH . 'wp-admin/includes/misc.php';
        require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
        require_once ABSPATH . 'wp-admin/includes/theme.php';
        $api = themes_api('theme_information', array('slug' => self::THEME_SLUG, 'fields' => array('sections' => false, ), ));

        if (is_wp_error($api) || empty($api->download_link)) {
            $message = is_wp_error($api) ? $api->get_error_message() : __('No download link returned for GeneratePress.', 'puffergo');
            puffergo_log('GeneratePress lookup failed: ' . $message);

            return $this->result($before, 'not_installed', 'install_failed', $message);
        } $skin = new Automatic_Upgrader_Skin();
        $upgrader = new Theme_Upgrader($skin);
        $installed = $upgrader->install($api->download_link);

        if (is_wp_error($installed)) {
            puffergo_log('GeneratePress install failed: ' . $installed->get_error_message());

            return $this->result($before, 'not_installed', 'install_failed', $installed->get_error_message());
        }

if (false === $installed) {
            puffergo_log('GeneratePress install failed: upgrader returned false (filesystem credentials may be required).');

            return $this->result($before, 'not_installed', 'install_failed', __('Theme installation failed.', 'puffergo'));
        }

if (! self::is_generatepress_present()) {
            puffergo_log('GeneratePress install reported success but theme is not present.');

            return $this->result($before, 'not_installed', 'install_failed', __('GeneratePress did not install correctly.', 'puffergo'));
        } puffergo_log('GeneratePress installed on activation.');

        return $this->result($before, 'installed', 'installed', __('GeneratePress installed successfully.', 'puffergo'));
    }

    private function result($status_before, $status_after, $result, $message)
    {
        return array('slug' => self::THEME_SLUG, 'status_before' => $status_before, 'status_after' => $status_after, 'result' => $result, 'message' => $message, );
    }
}
