Skip to content

Instantly share code, notes, and snippets.

@nztim
Created May 6, 2019 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nztim/100c22f39da2c6d0eef8af4f086cf439 to your computer and use it in GitHub Desktop.
Save nztim/100c22f39da2c6d0eef8af4f086cf439 to your computer and use it in GitHub Desktop.
<?php namespace NZTim\Connect\WordPress;
use WP_Filesystem_Base;
/** Adapted from https://www.smashingmagazine.com/2015/08/deploy-wordpress-plugins-with-github-using-transients/ */
class Updater
{
/** @var string */
protected $pluginFile;
/** @var array */
protected $pluginData;
/** @var string */
protected $basename;
/** @var bool */
protected $active;
/** @var string */
private $user;
/** @var string */
private $repo;
/** @var array */
private $githubResponse;
public function __construct(string $pluginFile, string $user, string $repo)
{
$this->pluginFile = $pluginFile;
$this->user = $user;
$this->repo = $repo;
}
public static function addAction(string $pluginFile, string $user, string $repo)
{
$instance = new static($pluginFile, $user, $repo);
add_action('admin_init', [$instance, 'setPluginProperties']);
add_filter('pre_set_site_transient_update_plugins', [$instance, 'modifyTransient'], 10, 1);
add_filter('plugins_api', [$instance, 'pluginPopup'], 10, 3);
add_filter('upgrader_post_install', [$instance, 'afterInstall'], 10, 3);
}
public function setPluginProperties()
{
$this->pluginData = get_plugin_data($this->pluginFile);
$this->basename = plugin_basename($this->pluginFile);
$this->active = is_plugin_active($this->basename);
}
public function modifyTransient($transient)
{
if (property_exists($transient, 'checked')) {
$checked = $transient->checked;
if ($checked) {
$this->getRepositoryInfo();
$outdated = version_compare($this->githubResponse['tag_name'], $checked[$this->basename], 'gt');
if ($outdated) {
$new = $this->githubResponse['zipball_url'];
$slug = current(explode('/', $this->basename));
$plugin = [
'url' => $this->pluginData["PluginURI"],
'slug' => $slug,
'package' => $new,
'new_version' => $this->githubResponse['tag_name'],
];
$transient->response[$this->basename] = (object) $plugin;
}
}
}
return $transient;
}
protected function getRepositoryInfo()
{
if (is_null($this->githubResponse)) {
$url = sprintf('https://api.github.com/repos/%s/%s/releases', $this->user, $this->repo); // Build URI
$response = json_decode(wp_remote_retrieve_body(wp_remote_get($url)), true);
if (is_array($response)) {
$response = current($response);
}
$this->githubResponse = $response;
}
}
public function pluginPopup($result, $action, $args)
{
if ($args->slug == current(explode('/', $this->basename))) {
$this->getRepositoryInfo();
$plugin = [
'name' => $this->pluginData["Name"],
'slug' => $this->basename,
'version' => $this->githubResponse['tag_name'],
'author' => $this->pluginData["AuthorName"],
'author_profile' => $this->pluginData["AuthorURI"],
'last_updated' => $this->githubResponse['published_at'],
'homepage' => $this->pluginData["PluginURI"],
'short_description' => $this->pluginData["Description"],
'sections' => [
'Description' => $this->pluginData["Description"],
'Updates' => $this->githubResponse['body'],
],
'download_link' => $this->githubResponse['zipball_url'],
];
return (object) $plugin;
}
return $result;
}
public function afterInstall($response, $hook_extra, $result)
{
/** @var WP_Filesystem_Base $wp_filesystem */
global $wp_filesystem;
$installDir = plugin_dir_path($this->pluginFile);
$wp_filesystem->move($result['destination'], $installDir);
$result['destination'] = $installDir;
if ($this->active) {
activate_plugin($this->basename);
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment