Viewing File: /usr/local/cpanel/3rdparty/wpinstaller/src/WpDownloader/Update.php

<?php

namespace WpDownloader;

use WpDownloader\Distribution;
use WpDownloader\Log;

class Update
{
    # ToDo: Refactor this whole class to remove this code duplication

    public $saveToDirectory = '/usr/local/cpanel/3rdparty/wpinstaller/files';
    public $forceRecompile = false; // Set to true to force re-compilation of core even if there are no updates.
    public $forceDownload = false; // Force re-download of already cached and up-to-date plugins
    public $pluginConfigurationFilePath = __DIR__.'/../../config/plugins.json';

    public $components;

    protected function loadComponents()
    {
        $pluginData = Utility::loadJsonFile($this->pluginConfigurationFilePath);

        $pluginSourceClassMap = [
            'wordpress-org'       => 'SlugPlugin',
            'github-com'          => 'GithubPlugin',
            'bluehost-com'        => 'BluehostPlugin',
        ];

        $pluginComponents = [];
        foreach ($pluginData as $source => $plugins) {
            $pluginClass = $pluginSourceClassMap[$source];

            foreach ($plugins as $plugin) {
                $plugin['type'] = $pluginClass;
                array_push($pluginComponents, $plugin);
            }
        }
        $this->components = array_merge([['type' => 'Core']], $pluginComponents);

        return $this->components;
    }

    public function prepareNewVersion()
    {
        $this->loadComponents();

        $updatedComponents = [];
        $loadedComponents = [];

        foreach ($this->components as $componentData) {
            $handlerClass =  "\\WpDownloader\\Component\\{$componentData['type']}";
            $loadedComponents[] = $component = new $handlerClass;
            $component->cleanupTmp();
            $component->setData($componentData);

            if ($this->syncComponent($component)) {
                $updatedComponents[] = $component->getSlug();
            }
        }

        if (count($updatedComponents) < 1) {
            Log::write("No components have changed.", Log::INFO);

            if ($this->forceRecompile) {
                Log::write("Force recompile was set. Re-compiling anyway.", Log::INFO);
            } else {
                Log::write("No new version being prepared.", Log::INFO);
                return;
            }
        }

        $distribution = $this->buildDistribution($loadedComponents);

        Log::write("Compiled Distro");
    }

    protected function syncComponent($component)
    {
        $slug = $component->getSlug();
        $component->localPath = "{$this->saveToDirectory}/{$slug}";
        $component->tempPath  = "{$this->saveToDirectory}/.{$slug}-tmp";

        if (!$this->forceDownload && !$component->hasUpdateAvailable()) {
            Log::write("Nothing is being updated for {$slug}.", Log::INFO);
            return false;
        }

        Log::write("Downloading update for {$slug}.", Log::INFO);

        $downloadSuccess = $component->downloadLatestVersion();

        return $downloadSuccess;
    }

    protected function buildDistribution($components)
    {
        $distribution = new Distribution();

        $distribution->localPath = "{$this->saveToDirectory}/dist";
        $distribution->tempPath  = "{$this->saveToDirectory}/.dist-tmp";

        $distribution->setComponents($components);

        $distribution->build();

        return $distribution;
    }
}
Back to Directory File Manager