This commit is contained in:
Pau Capó 2017-03-14 21:06:42 +01:00
parent 6632c1ab14
commit 996b5bc50a
2 changed files with 47 additions and 26 deletions

View file

@ -18,16 +18,22 @@ class Gitea_Options {
}
function admin_menu() {
add_options_page('Gitea Updater', 'Gitea Updater', 'manage_options', 'gitea-updater', array($this, 'options_page'));
$options = add_options_page('Gitea Updater', 'Gitea Updater', 'manage_options', 'gitea-updater', array($this, 'options_page'));
add_action('load-'.$options, array($this, 'options_post'));
}
function options_page() {
function options_post() {
if (isset($_GET['force-check'])) {
Gitea_Updater::getInstance()->get_gitea_all();
set_site_transient('update_plugins', null);
set_site_transient('update_themes', null);
}
if (isset($_POST['gitea_options'])) {
update_option('gitea_options', $_POST['gitea_options']);
}
}
function options_page() {
$options = (array)get_option('gitea_options');
$repositories = $this->get_repositories();
$titles = array(
@ -82,7 +88,8 @@ class Gitea_Options {
<a href="<?=admin_url('options-general.php?page=gitea-updater&force-check=1')?>" class="button button-primary"><?=__('Reset Cache')?></a>
</p>
</form>
<textarea style="width:100%" rows="2" onfocus="this.rows=30;" onblur="this.rows=2;" readonly><?php var_dump($options); ?></textarea>
<?=date('d/m/Y H:i:s', get_option('gitea_last_checked'))?>
<textarea style="width:100%" rows="2" onfocus="this.rows=30;" onblur="this.rows=2;" readonly><?php var_dump(get_plugin_updates()); ?></textarea>
</div>
<style>
table.gitea {
@ -120,5 +127,9 @@ class Gitea_Options {
}
static function get() {
return (array)get_option('gitea_options');
}
}

View file

@ -46,7 +46,7 @@ class Gitea_Updater {
add_action('admin_init', array($this, 'admin_init'));
// force check in core update, plugins and themes pages
add_action('admin_init', array($this, 'admin_pages_update_transient'));
// add_action('admin_init', array($this, 'admin_pages_update_transient'));
// plugins checks
add_filter('plugins_api', array($this, 'plugins_api'), 10, 3);
@ -62,31 +62,14 @@ class Gitea_Updater {
add_filter('extra_plugin_headers', array($this, 'extra_headers'));
add_filter('extra_theme_headers', array($this, 'extra_headers'));
// remote management
add_action('wp_update_plugins', array($this, 'get_gitea_plugins'));
add_action('wp_update_themes', array($this, 'get_gitea_themes'));
add_action('wp_ajax_nopriv_ithemes_sync_request', array($this, 'get_gitea_all'));
add_action('update_option_auto_updater.lock', array($this, 'get_gitea_all'));
}
function admin_init() {
// check interval data
$now = strtotime('now');
$last_checked = (int) get_option('gitea_last_checked');
$check_interval = apply_filters('gitea_check_interval', (60 * 60 * 10));
// cached data
$this->plugins = (array)get_option('gitea_plugins');
$this->themes = (array)get_option('gitea_themes');
// if is time... update data
if (($now - $last_checked) > $check_interval) {
$this->get_gitea_all();
update_option('gitea_last_checked', $now);
}
// $this->plugins = (array)get_option('gitea_plugins');
// $this->themes = (array)get_option('gitea_themes');
}
@ -193,7 +176,19 @@ class Gitea_Updater {
}
function get_gitea_token($host, $repo) {
return '9ebc78834767d2ce0f95ec8a5ab597bc1fbc9ed6';
$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_gitea_url($host, $repo, $args = '') {
@ -252,10 +247,22 @@ class Gitea_Updater {
function pre_set_site_transient_update_plugins($transient) {
// check if some plugin needs update using the cached data
$this->get_gitea_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;
$object = new stdClass;
$object->slug = $git_plugin['slug'];
$object->plugin = $git_plugin['plugin'];
$object->new_version = $git_plugin['new_version'];
$object->url = $git_plugin['url'];
$object->package = $git_plugin['package'];
$transient->response[$git_plugin['plugin']] = $object;
}
}
@ -266,8 +273,12 @@ class Gitea_Updater {
function pre_set_site_transient_update_themes($transient) {
// check if some theme needs update
$this->get_gitea_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;
}
@ -289,7 +300,6 @@ class Gitea_Updater {
if ($is_admin_page && current_user_can($transient)) {
$current = get_site_transient($transient);
$this->get_gitea_all();
$current = $this->pre_set_site_transient_update_plugins($current);
$current = $this->pre_set_site_transient_update_themes($current);
set_site_transient($transient, $current);