<?php
namespace WpDownloader\Component;
use WpDownloader\WpApi;
use WpDownloader\Log;
use WpDownloader\Utility;
class SlugPlugin extends Plugin
{
public $slug; // Used to look up plugin info
public function getLatestVersion()
{
return WpApi::getLatestPluginVersionNumber($this->slug);
}
public function afterSetData()
{
if (!isset($this->componentData['slug'])) {
Log::throwException('Attempted to set data for a "Slug Plugin" without specifying the slug.');
}
$this->slug = $this->componentData['slug'];
}
public function getActivationFile()
{
return "{$this->componentData['slug']}/{$this->componentData['file']}";
}
public function downloadLatestVersion()
{
$latestVersion = WpApi::getPluginData($this->slug);
$downloadUrl = $latestVersion['download_link'];
$version = $latestVersion['version'];
$fileName = basename($downloadUrl);
$localFile = $this->downloadFile($downloadUrl, "{$this->temporaryDirectory}/{$fileName}");
if (!$localFile || !file_exists($localFile)) {
Log::throwException("Downloaded failed or local file {$localFile} does not exist.");
}
$this->extractArchive($localFile);
$this->commitTempToPersistent();
$this->appendLocalData(['version' => $version]);
return $version;
}
}