355 lines
10 KiB
PHP
355 lines
10 KiB
PHP
<?php
|
|
/*
|
|
* Plugin Name: Gitea Updater
|
|
* Plugin URI: http://www.paucapo.com
|
|
* Description: Plugins updater
|
|
* Version: 0.2.2
|
|
* Author: Pau Capó
|
|
* Author URI: http://www.paucapo.com
|
|
* Text Domain: gitea
|
|
* Gitea Host: https://git.paucapo.com
|
|
* Gitea URI: wp/gitea-updater
|
|
*/
|
|
|
|
/*
|
|
# TODO:
|
|
** branches?
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) or exit;
|
|
|
|
|
|
require 'gitea-options.php';
|
|
|
|
class Gitea_Updater {
|
|
|
|
private static $_instance;
|
|
|
|
public static function getInstance() {
|
|
if (!(self::$_instance instanceof self)) {
|
|
self::$_instance = new self();
|
|
}
|
|
return self::$_instance;
|
|
}
|
|
|
|
public $plugins = array();
|
|
public $themes = array();
|
|
|
|
function __construct() {
|
|
|
|
add_action('plugins_loaded', array($this, 'plugins_loaded'));
|
|
|
|
Gitea_Options::getInstance();
|
|
|
|
add_action('admin_init', array($this, 'admin_init'));
|
|
|
|
// plugins checks
|
|
add_filter('plugins_api', array($this, 'plugins_api'), 10, 3);
|
|
add_filter('pre_set_site_transient_update_plugins', array($this, 'pre_set_site_transient_update_plugins'));
|
|
|
|
// themes checks
|
|
add_filter('pre_set_site_transient_update_themes', array($this, 'pre_set_site_transient_update_themes'));
|
|
|
|
// extra plugins and themes headers (Gitea Host and Gitea URI)
|
|
add_filter('extra_plugin_headers', array($this, 'extra_headers'));
|
|
add_filter('extra_theme_headers', array($this, 'extra_headers'));
|
|
|
|
}
|
|
|
|
function admin_init() {
|
|
if (!get_option('gitea_plugins') || !get_option('gitea_plugins'))
|
|
$this->get_all();
|
|
}
|
|
|
|
function plugins_loaded() {
|
|
load_plugin_textdomain('gitea', FALSE, basename( dirname( __FILE__ ) ) . '/languages/');
|
|
}
|
|
|
|
function get_all() {
|
|
$this->get_plugins();
|
|
$this->get_themes();
|
|
}
|
|
|
|
function get_plugins() {
|
|
$this->plugins = array();
|
|
$plugins = get_plugins();
|
|
|
|
foreach ($plugins as $plugin_slug => $plugin) {
|
|
|
|
if (!empty($plugin['Gitea URI']) && !empty($plugin['Gitea Host'])) {
|
|
|
|
// this is a gitea plugin
|
|
|
|
$slug = trim(dirname($plugin_slug), '/');
|
|
$file = basename($plugin_slug);
|
|
$host = $this->get_host($plugin['Gitea Host']);
|
|
$repo = $plugin['Gitea URI'];
|
|
$local_version = strtolower($plugin['Version']);
|
|
|
|
$url = $this->get_url($host, $repo, '/raw/master/'.$file);
|
|
if ($url != false) {
|
|
$new_version = $this->get_version($url, 'plugin');
|
|
}
|
|
$new_version = isset($new_version) && $new_version ? $new_version : $local_version;
|
|
|
|
// here we have the remote version from gitea/repo/plugin_file
|
|
|
|
// building the plugin data
|
|
$gitea = array(
|
|
'plugin' => $plugin_slug,
|
|
'slug' => $slug,
|
|
'file' => $file,
|
|
'name' => $plugin['Name'],
|
|
'gitea_host' => $host,
|
|
'gitea_repo' => $repo,
|
|
'description' => $plugin['Description'],
|
|
'url' => $host.$repo,
|
|
'local_version' => $local_version,
|
|
'new_version' => $new_version,
|
|
'package' => $this->get_url($host, $repo, '/archive/master.zip'),
|
|
);
|
|
|
|
$this->plugins[$slug] = $gitea;
|
|
|
|
}
|
|
}
|
|
|
|
update_option('gitea_plugins', $this->plugins);
|
|
update_option('gitea_plugins_updated', time());
|
|
|
|
}
|
|
|
|
function get_themes() {
|
|
$this->themes = array();
|
|
$themes = wp_get_themes();
|
|
|
|
foreach ( $themes as $theme ) {
|
|
|
|
if ($theme->get('Gitea Host') != '' && $theme->get('Gitea URI')) {
|
|
|
|
// this is a gitea theme
|
|
|
|
$slug = $theme->stylesheet;
|
|
$host = $this->get_host($theme->get('Gitea Host'));
|
|
$repo = $theme->get('Gitea URI');
|
|
$local_version = strtolower($theme->get('Version'));
|
|
|
|
$url = $this->get_url($host, $repo, '/raw/master/style.css');
|
|
if ($url != false) {
|
|
$new_version = $this->get_version($url, 'plugin');
|
|
$new_version = $new_version ? $new_version : $local_version;
|
|
}
|
|
|
|
// here we have the remote version from gitea/repo/style.css
|
|
|
|
// building the theme data
|
|
$gitea = array(
|
|
'slug' => $slug,
|
|
'name' => $theme->get('Name'),
|
|
'gitea_host' => $host,
|
|
'gitea_repo' => $repo,
|
|
'description' => $theme->get('Description'),
|
|
'author' => $theme->get('Author'),
|
|
'url' => $theme->get('AuthorURI'),//$host.$repo,
|
|
'local_version' => $local_version,
|
|
'new_version' => $new_version,
|
|
'package' => $this->get_url($host, $repo, '/archive/master.zip'),
|
|
);
|
|
|
|
$this->themes[$slug] = $gitea;
|
|
}
|
|
|
|
}
|
|
|
|
update_option('gitea_themes', $this->themes);
|
|
update_option('gitea_themes_updated', time());
|
|
|
|
}
|
|
|
|
function get_host($host) {
|
|
return rtrim($host, '/').'/';
|
|
}
|
|
|
|
function get_token($host, $repo) {
|
|
$options = Gitea_Options::get();
|
|
$token = '';
|
|
|
|
// get repo token
|
|
if (isset($options['repo_token'][$host.$repo]))
|
|
$token = $options['repo_token'][$host.$repo];
|
|
|
|
// get host token if repo is empty
|
|
if (empty($token) && isset($options['host_token'][$host]))
|
|
$token = $options['host_token'][$host];
|
|
|
|
// return token or false if empty
|
|
return !empty($token) ? $token : false;
|
|
}
|
|
|
|
function get_url($host, $repo, $args = '', $access_token = false) {
|
|
|
|
if ($access_token === false) {
|
|
$access_token = $this->get_token($host, $repo);
|
|
if (!$access_token) return false;
|
|
}
|
|
|
|
return $host.'api/v1/repos/'.$repo.$args.'?access_token='.$access_token;
|
|
}
|
|
|
|
function get_file($url) {
|
|
$request = wp_remote_get($url);
|
|
|
|
if (is_wp_error($request) || 200 != wp_remote_retrieve_response_code($request)) {
|
|
return false;
|
|
}
|
|
|
|
// in $request['body'] we have the content from the file
|
|
return $request;
|
|
}
|
|
|
|
function get_version($url, $type) {
|
|
|
|
$request = $this->get_file($url);
|
|
|
|
if (!$request) return false;
|
|
|
|
// get the headers
|
|
$headers = $this->get_file_headers($request['body'], $type);
|
|
|
|
return isset($headers['Version']) ? $headers['Version'] : false;
|
|
}
|
|
|
|
function plugins_api($default = false, $action, $args) {
|
|
if ('plugin_information' != $action)
|
|
return $default;
|
|
|
|
if (!isset($this->plugins[$args->slug]))
|
|
return $default;
|
|
|
|
// plugin information (view notes befor update)
|
|
|
|
$plugin = $this->plugins[$args->slug];
|
|
|
|
return (object) array(
|
|
'name' => $plugin['name'],
|
|
'slug' => $plugin['plugin'],
|
|
'version' => $plugin['new_version'],
|
|
'sections' => array(
|
|
'description' => $plugin['description']
|
|
),
|
|
'download_link' => $plugin['package'],
|
|
'homepage' => $plugin['url'],
|
|
);
|
|
|
|
}
|
|
|
|
function pre_set_site_transient_update_plugins($transient) {
|
|
// check if some plugin needs update
|
|
|
|
$this->get_plugins();
|
|
|
|
foreach ($this->plugins as $plugin => $git_plugin) {
|
|
|
|
if (isset($transient->response[$git_plugin['plugin']]))
|
|
unset($transient->response[$git_plugin['plugin']]);
|
|
if (version_compare($git_plugin['local_version'], $git_plugin['new_version'], '<')) {
|
|
$transient->response[$git_plugin['plugin']] = (object)$git_plugin;
|
|
}
|
|
|
|
}
|
|
|
|
return $transient;
|
|
}
|
|
|
|
function pre_set_site_transient_update_themes($transient) {
|
|
// check if some theme needs update
|
|
|
|
$this->get_themes();
|
|
|
|
foreach ($this->themes as $theme => $git_theme) {
|
|
|
|
if (isset($transient->response[$theme]))
|
|
unset($transient->response[$theme]);
|
|
if (version_compare($git_theme['local_version'], $git_theme['new_version'], '<')) {
|
|
$transient->response[$theme] = (array)$git_theme;
|
|
}
|
|
|
|
}
|
|
|
|
return $transient;
|
|
}
|
|
|
|
function get_file_headers($contents, $type) {
|
|
|
|
$gitea_headers = array(
|
|
'GiteaHost' => 'Gitea Host',
|
|
'GiteaUri' => 'Gitea URI',
|
|
);
|
|
|
|
$default_plugin_headers = array(
|
|
'Name' => 'Plugin Name',
|
|
'PluginURI' => 'Plugin URI',
|
|
'Version' => 'Version',
|
|
'Description' => 'Description',
|
|
'Author' => 'Author',
|
|
'AuthorURI' => 'Author URI',
|
|
'TextDomain' => 'Text Domain',
|
|
'DomainPath' => 'Domain Path',
|
|
'Network' => 'Network',
|
|
);
|
|
|
|
$default_theme_headers = array(
|
|
'Name' => 'Theme Name',
|
|
'ThemeURI' => 'Theme URI',
|
|
'Description' => 'Description',
|
|
'Author' => 'Author',
|
|
'AuthorURI' => 'Author URI',
|
|
'Version' => 'Version',
|
|
'Template' => 'Template',
|
|
'Status' => 'Status',
|
|
'Tags' => 'Tags',
|
|
'TextDomain' => 'Text Domain',
|
|
'DomainPath' => 'Domain Path',
|
|
);
|
|
|
|
if (false !== strpos($type, 'plugin')) {
|
|
$all_headers = $default_plugin_headers;
|
|
}
|
|
|
|
if (false !== strpos($type, 'theme')) {
|
|
$all_headers = $default_theme_headers;
|
|
}
|
|
|
|
$file_data = str_replace("\r", "\n", $contents);
|
|
|
|
$all_headers = array_merge($gitea_headers, (array) $all_headers);
|
|
$all_headers = array_unique($all_headers);
|
|
|
|
foreach ($all_headers as $field => $regex) {
|
|
if (preg_match( '/^[ \t\/*#@]*'.preg_quote($regex, '/').':(.*)$/mi', $file_data, $match) && $match[1]) {
|
|
$all_headers[$field] = _cleanup_header_comment($match[1]);
|
|
} else {
|
|
$all_headers[$field] = '';
|
|
}
|
|
}
|
|
|
|
$all_headers = array_filter($all_headers,
|
|
function($e) use (&$all_headers) {
|
|
return !empty($e);
|
|
});
|
|
|
|
return $all_headers;
|
|
}
|
|
|
|
function extra_headers($headers) {
|
|
$headers[] = 'Gitea URI';
|
|
$headers[] = 'Gitea Host';
|
|
return $headers;
|
|
}
|
|
|
|
}
|
|
|
|
Gitea_Updater::getInstance();
|
|
|