settings almost done!

This commit is contained in:
Pau Capó 2017-03-14 16:42:46 +01:00
parent 86156ba3cd
commit e19b894737
2 changed files with 169 additions and 126 deletions

View file

@ -4,56 +4,122 @@ defined( 'ABSPATH' ) or exit;
class Gitea_Updater_Settings { class Gitea_Updater_Settings {
private static $_instance;
public static function getInstance() {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
function __construct() { function __construct() {
add_action('admin_menu', array($this, 'admin_menu')); add_action('admin_menu', array($this, 'admin_menu'));
add_action('admin_init', array($this, 'admin_init'));
} }
function admin_menu() { function admin_menu() {
add_options_page('Gitea Upater', 'Gitea Upater', 'manage_options', 'gitea-upater', array($this, 'options_page')); add_options_page('Gitea Updater', 'Gitea Updater', 'manage_options', 'gitea-updater', array($this, 'options_page'));
}
function admin_init() {
register_setting('gitea_options_page', 'gitea_options');
// add_settings_section(
// 'gitea_options_section',
// __('Your section description', 'gitea'),
// '__return_false',
// 'gitea_options_page'
// );
// add_settings_field(
// 'gitea_acces_token',
// __('Access token', 'gitea'),
// array($this, 'access_token'),
// 'gitea_options_page',
// 'gitea_options_section'
// );
}
function access_token() {
$options = get_option('gitea_options');
?>
<input type='text' name='gitea_options[gitea_acces_token]' value='<?php echo $options['gitea_acces_token']; ?>'>
<?php
} }
function options_page() { function options_page() {
if (isset($_GET['force-check'])) {
Gitea_Updater::getInstance()->get_gitea_all();
}
if (isset($_POST['gitea_options'])) {
update_option('gitea_options', $_POST['gitea_options']);
}
$options = (array)get_option('gitea_options');
$repositories = $this->get_repositories();
$titles = array(
'plugin' => __('Plugins'),
'theme' => __('Themes'),
);
?> ?>
<form action='options.php' method='post'> <div class="wrap">
<form action="<?=admin_url('options-general.php?page=gitea-updater')?>" method="post">
<?php /* settings_fields('gitea_options_page'); */ ?>
<h2>Gitea Upater</h2> <h1>Gitea Updater</h1>
<?php <?php foreach ($repositories as $repository => $packages) : ?>
settings_fields('gitea_options_page'); <br>
// do_settings_sections('gitea_options_page'); <table class="widefat striped gitea">
?> <tr>
<td colspan="3">
<a href="<?=$repository?>"><strong><?=parse_url($repository)['host']?></strong></a>
</td>
<td>
<input type="password" name="gitea_options[host_token][<?=$repository?>]" value="<?=@$options['host_token'][$repository]?>" placeholder="Global Access Token">
</td>
</tr>
<?php submit_button(); ?> <?php foreach ($packages as $type => $items) : ?>
</form> <tr>
<td></td>
<td colspan="3">
<?=$titles[$type]?>
</td>
</tr>
<?php foreach ($items as $slug => $package) : ?>
<tr>
<td></td>
<td></td>
<td class="package">
<a href="<?=$package['url']?>"><?=$package['name']?></a>
</td>
<td>
<input type="password" name="gitea_options[repo_token][<?=$package['url']?>]" value="<?=@$options['repo_token'][$package['url']]?>" placeholder="Access Token">
</td>
</li>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
<?php endforeach; ?>
<?php /*submit_button();*/ ?>
<p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary" value="<?=__('Save Changes')?>">
<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>
</div>
<style>
table.gitea {
min-width: 700px;
width: auto;
}
table.gitea input {
min-width: 400px;
}
table.gitea td {
vertical-align: middle;
}
table.gitea td.package {
min-width: 250px;
}
</style>
<?php <?php
} }
function get_repositories() {
$plugins = (array)get_option('gitea_plugins');
$themes = (array)get_option('gitea_themes');
$repositories = array();
foreach ($plugins as $slug => $plugin) {
$repositories[$plugin['gitea_host']]['plugin'][$slug] = $plugin;
}
foreach ($themes as $slug => $theme) {
$repositories[$plugin['gitea_host']]['theme'][$slug] = $theme;
}
return $repositories;
}
} }

View file

@ -6,7 +6,7 @@ Description: Plugins updater
Version: 0.1.1 Version: 0.1.1
Author: Pau Capó Author: Pau Capó
Author URI: http://www.paucapo.com Author URI: http://www.paucapo.com
Gitea Host: https://git.paucapo.com/ Gitea Host: https://git.paucapo.com
Gitea URI: wp/gitea-updater Gitea URI: wp/gitea-updater
*/ */
@ -25,7 +25,14 @@ require 'gitea-updater-settings.php';
class Gitea_Updater { class Gitea_Updater {
public $settings = false; private static $_instance;
public static function getInstance() {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
public $plugins = array(); public $plugins = array();
public $themes = array(); public $themes = array();
@ -34,7 +41,7 @@ class Gitea_Updater {
function __construct() { function __construct() {
$this->settings = new Gitea_Updater_Settings(); Gitea_Updater_Settings::getInstance();
add_action('admin_init', array($this, 'admin_init')); add_action('admin_init', array($this, 'admin_init'));
@ -56,10 +63,10 @@ class Gitea_Updater {
add_filter('extra_theme_headers', array($this, 'extra_headers')); add_filter('extra_theme_headers', array($this, 'extra_headers'));
// remote management // remote management
add_action('wp_update_plugins', array($this, 'forced_meta_update_plugins')); add_action('wp_update_plugins', array($this, 'get_gitea_plugins'));
add_action('wp_update_themes', array($this, 'forced_meta_update_themes')); add_action('wp_update_themes', array($this, 'get_gitea_themes'));
add_action('wp_ajax_nopriv_ithemes_sync_request', array($this, 'forced_meta_update_remote_management')); add_action('wp_ajax_nopriv_ithemes_sync_request', array($this, 'get_gitea_all'));
add_action('update_option_auto_updater.lock', array($this, 'forced_meta_update_remote_management')); add_action('update_option_auto_updater.lock', array($this, 'get_gitea_all'));
} }
@ -69,7 +76,7 @@ class Gitea_Updater {
// check interval data // check interval data
$now = strtotime('now'); $now = strtotime('now');
$last_checked = (int) get_option('gitea_last_checked'); $last_checked = (int) get_option('gitea_last_checked');
$check_interval = apply_filters('gitea_check_interval', (60 * 60 * 0)); $check_interval = apply_filters('gitea_check_interval', (60 * 60 * 10));
// cached data // cached data
$this->plugins = (array)get_option('gitea_plugins'); $this->plugins = (array)get_option('gitea_plugins');
@ -77,19 +84,21 @@ class Gitea_Updater {
// if is time... update data // if is time... update data
if (($now - $last_checked) > $check_interval) { if (($now - $last_checked) > $check_interval) {
$this->plugins = $this->get_gitea_plugins(); $this->get_gitea_all();
update_option('gitea_plugins', $this->plugins);
$this->themes = $this->get_gitea_themes();
update_option('gitea_themes', $this->plugins);
update_option('gitea_last_checked', $now); update_option('gitea_last_checked', $now);
} }
} }
function get_gitea_all() {
$this->get_gitea_plugins();
$this->get_gitea_themes();
$time_end = microtime(true);
}
function get_gitea_plugins() { function get_gitea_plugins() {
$plugin_data = array(); $this->plugins = array();
$plugins = get_plugins(); $plugins = get_plugins();
foreach ($plugins as $plugin_slug => $plugin) { foreach ($plugins as $plugin_slug => $plugin) {
@ -99,16 +108,16 @@ class Gitea_Updater {
// this is a gitea plugin // this is a gitea plugin
$slug = trim(dirname($plugin_slug), '/'); $slug = trim(dirname($plugin_slug), '/');
$host = $plugin['Gitea Host']; $host = $this->get_gitea_host($plugin['Gitea Host']);
$repo = $plugin['Gitea URI']; $repo = $plugin['Gitea URI'];
$local_version = strtolower($plugin['Version']);
$url = $this->get_gitea_url($host, $repo, '/raw/master/'.basename($plugin_slug)); $url = $this->get_gitea_url($host, $repo, '/raw/master/'.basename($plugin_slug));
if ($url == false) if ($url != false) {
continue; $new_version = $this->get_gitea_version($url, 'plugin');
$new_version = $new_version ? $new_version : $local_version;
}
$version = $this->get_gitea_version($url, 'plugin');
if ($version == false)
continue;
// here we have the remote version from gitea/repo/plugin_file // here we have the remote version from gitea/repo/plugin_file
// building the plugin data // building the plugin data
@ -117,24 +126,26 @@ class Gitea_Updater {
'slug' => $slug, 'slug' => $slug,
'file' => basename($plugin_slug), 'file' => basename($plugin_slug),
'name' => $plugin['Name'], 'name' => $plugin['Name'],
'gitea_host' => $plugin['Gitea Host'], 'gitea_host' => $host,
'gitea_repo' => $plugin['Gitea URI'], 'gitea_repo' => $repo,
'description' => $plugin['Description'], 'description' => $plugin['Description'],
'url' => $host.$repo, 'url' => $host.$repo,
'local_version' => strtolower($plugin['Version']), 'local_version' => $local_version,
'new_version' => $version, 'new_version' => $new_version,
'package' => $this->get_gitea_url($host, $repo, '/archive/master.zip'), 'package' => $this->get_gitea_url($host, $repo, '/archive/master.zip'),
); );
$plugin_data[$slug] = $gitea; $this->plugins[$slug] = $gitea;
} }
} }
return $plugin_data; update_option('gitea_plugins', $this->plugins);
} }
function get_gitea_themes() { function get_gitea_themes() {
$theme_data = array(); $this->themes = array();
$themes = wp_get_themes(); $themes = wp_get_themes();
foreach ( $themes as $theme ) { foreach ( $themes as $theme ) {
@ -144,51 +155,56 @@ class Gitea_Updater {
// this is a gitea theme // this is a gitea theme
$slug = $theme->stylesheet; $slug = $theme->stylesheet;
$host = $theme->get('Gitea Host'); $host = $this->get_gitea_host($theme->get('Gitea Host'));
$repo = $theme->get('Gitea URI'); $repo = $theme->get('Gitea URI');
$local_version = strtolower($theme->get('Version'));
$url = $this->get_gitea_url($host, $repo, '/raw/master/style.css'); $url = $this->get_gitea_url($host, $repo, '/raw/master/style.css');
if ($url == false) if ($url != false) {
continue; $new_version = $this->get_gitea_version($url, 'plugin');
$new_version = $new_version ? $new_version : $local_version;
}
$version = $this->get_gitea_version($url, 'theme');
if ($version == false)
continue;
// here we have the remote version from gitea/repo/style.css // here we have the remote version from gitea/repo/style.css
// building the theme data // building the theme data
$gitea = array( $gitea = array(
'slug' => $slug, 'slug' => $slug,
'name' => $theme->get('Name'), 'name' => $theme->get('Name'),
'gitea_host' => $theme->get('Gitea Host'), 'gitea_host' => $host,
'gitea_repo' => $theme->get('Gitea URI'), 'gitea_repo' => $repo,
'description' => $theme->get('Description'), 'description' => $theme->get('Description'),
'author' => $theme->get('Author'), 'author' => $theme->get('Author'),
'url' => $theme->get('AuthorURI'),//$host.$repo, 'url' => $theme->get('AuthorURI'),//$host.$repo,
'local_version' => strtolower($theme->get('Version')), 'local_version' => $local_version,
'new_version' => $version, 'new_version' => $new_version,
'package' => $this->get_gitea_url($host, $repo, '/archive/master.zip'), 'package' => $this->get_gitea_url($host, $repo, '/archive/master.zip'),
); );
$theme_data[$slug] = $gitea; $this->themes[$slug] = $gitea;
} }
} }
return $theme_data; update_option('gitea_themes', $this->themes);
} }
function get_gitea_token($host) { function get_gitea_host($host) {
return rtrim($host, '/').'/';
}
function get_gitea_token($host, $repo) {
return '9ebc78834767d2ce0f95ec8a5ab597bc1fbc9ed6'; return '9ebc78834767d2ce0f95ec8a5ab597bc1fbc9ed6';
} }
function get_gitea_url($host, $repo, $args = '') { function get_gitea_url($host, $repo, $args = '') {
$access_token = $this->get_gitea_token($host); $access_token = $this->get_gitea_token($host, $repo);
if (!$access_token) return false; if (!$access_token) return false;
return $host.'api/v1/repos/'.$repo.$args.'?access_token='.$access_token; return $host.'api/v1/repos/'.$repo.$args.'?access_token='.$access_token;
} }
function get_gite_file($url) { function get_gitea_file($url) {
$request = wp_remote_get($url); $request = wp_remote_get($url);
if (is_wp_error($request) || 200 != wp_remote_retrieve_response_code($request)) { if (is_wp_error($request) || 200 != wp_remote_retrieve_response_code($request)) {
@ -201,7 +217,7 @@ class Gitea_Updater {
function get_gitea_version($url, $type) { function get_gitea_version($url, $type) {
$request = $this->get_gite_file($url); $request = $this->get_gitea_file($url);
if (!$request) return false; if (!$request) return false;
@ -273,57 +289,17 @@ class Gitea_Updater {
$transient = 'update_'.rtrim($pagenow, '.php'); $transient = 'update_'.rtrim($pagenow, '.php');
$transient = 'update_update-core' === $transient ? 'update_core' : $transient; $transient = 'update_update-core' === $transient ? 'update_core' : $transient;
if ($is_admin_page) { if ($is_admin_page && current_user_can($transient)) {
$this->make_update_transient_current($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);
} }
remove_filter('admin_init', array($this, 'admin_pages_update_transient')); remove_filter('admin_init', array($this, 'admin_pages_update_transient'));
} }
function make_update_transient_current($transient) {
if (!in_array($transient, array('update_plugins', 'update_themes', 'update_core'))) {
return;
}
if (current_user_can($transient)) {
$current = get_site_transient($transient);
switch ($transient) {
case 'update_plugins':
$this->forced_meta_update_plugins(true);
$current = $this->pre_set_site_transient_update_plugins($current);
break;
case 'update_themes':
$this->forced_meta_update_themes(true);
$current = $this->pre_set_site_transient_update_themes($current);
break;
case 'update_core':
$this->forced_meta_update_plugins(true);
$current = $this->pre_set_site_transient_update_plugins($current);
$this->forced_meta_update_themes(true);
$current = $this->pre_set_site_transient_update_themes($current);
break;
}
set_site_transient($transient, $current);
}
}
public function forced_meta_update_plugins($true = false) {
if ($this->remote_managemnet || $true) {
$this->plugins = $this->get_gitea_plugins();
}
}
public function forced_meta_update_themes($true = false) {
if ($this->remote_managemnet || $true) {
$this->themes = $this->get_gitea_themes();
}
}
public function forced_meta_update_remote_management() {
$this->forced_meta_update_plugins();
$this->forced_meta_update_themes();
}
function upgrader_source_selection($source, $remote_source, $upgrader, $hook_extra = null) { function upgrader_source_selection($source, $remote_source, $upgrader, $hook_extra = null) {
global $wp_filesystem; global $wp_filesystem;
@ -407,4 +383,5 @@ class Gitea_Updater {
} }
new Gitea_Updater(); Gitea_Updater::getInstance();