bug fix on admin_init and code cleanup

This commit is contained in:
Pau Capó 2018-03-03 23:10:05 +01:00
parent bf32f439e6
commit d1275eb680
5 changed files with 592 additions and 538 deletions

View file

@ -1,246 +1,271 @@
<?php
defined( 'ABSPATH' ) or exit;
defined('ABSPATH') or exit;
include_once(ABSPATH.'wp-admin/includes/class-wp-upgrader.php');
include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
class Gitea_Options {
class Gitea_Options
{
private static $_instance;
var $page = false;
private static $_instance;
var $page = false;
public static function getInstance() {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
public static function getInstance()
{
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
function __construct() {
add_action('admin_menu', array($this, 'admin_menu'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_assets'));
}
function __construct()
{
add_action('admin_menu', array($this, 'admin_menu'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_assets'));
}
function admin_menu() {
$this->page = add_options_page('Gitea Updater', 'Gitea Updater', 'manage_options', 'gitea-updater', array($this, 'options_page'));
add_action('load-'.$this->page, array($this, 'options_post'));
}
function admin_menu()
{
$this->page = add_options_page('Gitea Updater', 'Gitea Updater', 'manage_options', 'gitea-updater', array($this, 'options_page'));
add_action('load-' . $this->page, array($this, 'options_post'));
}
function enqueue_assets($hook) {
if ($hook != $this->page) return;
function enqueue_assets($hook)
{
if ($hook != $this->page) return;
wp_enqueue_style('gitea-updater', plugins_url('/assets/style.css', __FILE__));
wp_enqueue_script('gitea-js', plugins_url('/assets/script.js', __FILE__), array('jquery'), null, true);
}
wp_enqueue_style('gitea-updater', plugins_url('/assets/style.css', __FILE__));
wp_enqueue_script('gitea-js', plugins_url('/assets/script.js', __FILE__), array('jquery'), null, true);
}
function options_post() {
function options_post()
{
if (isset($_GET['force-check'])) {
Gitea_Updater::getInstance()->get_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']);
}
if (isset($_GET['force-check'])) {
Gitea_Updater::getInstance()->get_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();
$types = array(
'plugin' => __('Plugins'),
'theme' => __('Themes'),
);
?>
<div class="wrap">
function options_page()
{
$options = (array)get_option('gitea_options');
$repositories = $this->get_repositories();
$types = array(
'plugin' => __('Plugins'),
'theme' => __('Themes'),
);
?>
<div class="wrap">
<h1>Gitea Updater</h1>
<h1>Gitea Updater</h1>
<?php if (isset($_POST['gitea_install'])) : ?>
<div class="box install">
<div class="title">
<h3><?=__('Install Results', 'gitea')?></h3>
</div>
<div class="inner">
<?php $this->install($_POST['gitea_install']); ?>
</div>
</div>
<?php else : ?>
<?php if (isset($_POST['gitea_install'])) : ?>
<div class="box install">
<div class="title">
<h3><?= __('Install Results', 'gitea') ?></h3>
</div>
<div class="inner">
<?php $this->install($_POST['gitea_install']); ?>
</div>
</div>
<?php else : ?>
<form action="<?=admin_url('options-general.php?page=gitea-updater')?>" method="post" class="box access">
<form action="<?= admin_url('options-general.php?page=gitea-updater') ?>" method="post"
class="box access">
<div class="title">
<h3><?=__('Access Tokens', 'gitea')?></h3>
</div>
<div class="inner">
<p><?=__('The access token from a repository always have priority over the global.', 'gitea')?></p>
</div>
<div class="title">
<h3><?= __('Access Tokens', 'gitea') ?></h3>
</div>
<div class="inner">
<p><?= __('The access token from a repository always have priority over the global.', 'gitea') ?></p>
</div>
<ul class="gitea">
<?php $r = 0; foreach ($repositories as $repository => $packages) : $r++;?>
<li class="repo-row repo-<?=$r?>">
<ul>
<li class="repository">
<strong><a href="#" class="repo-toggle" data-repo="<?=$r?>"><?=parse_url($repository)['host']?></a></strong>
<a href="<?=$repository?>"><span class="dashicons dashicons-external"></span></a>
</li>
<li>
<input type="password" class="token" name="gitea_options[host_token][<?=$repository?>]" value="<?=@$options['host_token'][$repository]?>" placeholder="<?=__('Global Access Token', 'gitea')?>">
</li>
</ul>
<ul class="striped packages">
<?php foreach ($packages as $type => $items) : ?>
<?php foreach ($items as $slug => $package) : ?>
<li>
<ul>
<li class="package">
<span class="dashicons dashicons-admin-<?=($type == 'plugin' ? 'plugins' : 'appearance')?>"></span>
<a href="<?=$package['url']?>"><?=$package['name']?></a>
<ul class="gitea">
<?php $r = 0;
foreach ($repositories as $repository => $packages) : $r++; ?>
<li class="repo-row repo-<?= $r ?>">
<ul>
<li class="repository">
<strong><a href="#" class="repo-toggle"
data-repo="<?= $r ?>"><?= parse_url($repository)['host'] ?></a></strong>
<a href="<?= $repository ?>"><span class="dashicons dashicons-external"></span></a>
</li>
<li class="input">
<input type="password" class="token" name="gitea_options[repo_token][<?=$package['url']?>]" value="<?=@$options['repo_token'][$package['url']]?>" placeholder="<?=__('Access Token', 'gitea')?>">
<li>
<input type="password" class="token"
name="gitea_options[host_token][<?= $repository ?>]"
value="<?= @$options['host_token'][$repository] ?>"
placeholder="<?= __('Global Access Token', 'gitea') ?>">
</li>
</ul>
</li>
<?php endforeach; ?>
</ul>
<ul class="striped packages">
<?php foreach ($packages as $type => $items) : ?>
<?php foreach ($items as $slug => $package) : ?>
<li>
<ul>
<li class="package">
<span class="dashicons dashicons-admin-<?= ($type == 'plugin' ? 'plugins' : 'appearance') ?>"></span>
<a href="<?= $package['url'] ?>"><?= $package['name'] ?></a>
</li>
<li class="input">
<input type="password" class="token"
name="gitea_options[repo_token][<?= $package['url'] ?>]"
value="<?= @$options['repo_token'][$package['url']] ?>"
placeholder="<?= __('Access Token', 'gitea') ?>">
</li>
</ul>
</li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
</li>
</ul>
<?php endforeach; ?>
</ul>
<div class="inner">
<p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary"
value="<?= __('Save Changes', 'gitea') ?>">
<a href="<?= admin_url('options-general.php?page=gitea-updater&force-check=1') ?>"
class="button button-primary"><?= __('Reset Cache', 'gitea') ?></a>
</p>
<div class="inner">
<p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary" value="<?=__('Save Changes', 'gitea')?>">
<a href="<?=admin_url('options-general.php?page=gitea-updater&force-check=1')?>" class="button button-primary"><?=__('Reset Cache', 'gitea')?></a>
</p>
</div>
</div>
</form>
</form>
<form action="<?= admin_url('options-general.php?page=gitea-updater') ?>" method="post"
class="box install">
<div class="title">
<h3><?= __('Install from URL', 'gitea') ?></h3>
</div>
<div class="inner">
<form action="<?=admin_url('options-general.php?page=gitea-updater')?>" method="post" class="box install">
<div class="title">
<h3><?=__('Install from URL', 'gitea')?></h3>
</div>
<div class="inner">
<p><?= __('Only with <code>http://</code> or <code>https://</code> links, It\'s not working with <code>ssh://</code> links!', 'gitea') ?></p>
<p><?=__('Only with <code>http://</code> or <code>https://</code> links, It\'s not working with <code>ssh://</code> links!', 'gitea')?></p>
<table class="form-table">
<tr>
<th>
<label><?= __('Type', 'gitea') ?></label>
</th>
<td>
<select name="gitea_install[type]">
<option value="plugin"><?= __('Plugins', 'gitea') ?></option>
<option value="theme"><?= __('Themes', 'gitea') ?></option>
</select>
</td>
</tr>
<tr>
<th>
<label><?= __('Repository URL', 'gitea') ?></label>
</th>
<td>
<input type="text" name="gitea_install[url]"
placeholder="<?= __('https://host/org/repo or https://host/org/repo.git', 'gitea') ?>">
</td>
</tr>
<tr>
<th>
<label><?= __('Access Token', 'gitea') ?></label>
</th>
<td>
<input type="password" class="token" name="gitea_install[access_token]">
</td>
</tr>
<tr>
<th></th>
<td>
<input type="submit" name="submit" id="submit" class="button button-primary"
value="<?= __('Install Now', 'gitea') ?>">
</td>
</tr>
</table>
</div>
</form>
<?php endif; ?>
<table class="form-table">
<tr>
<th>
<label><?=__('Type', 'gitea')?></label>
</th>
<td>
<select name="gitea_install[type]">
<option value="plugin"><?=__('Plugins', 'gitea')?></option>
<option value="theme"><?=__('Themes', 'gitea')?></option>
</select>
</td>
</tr>
<tr>
<th>
<label><?=__('Repository URL', 'gitea')?></label>
</th>
<td>
<input type="text" name="gitea_install[url]" placeholder="<?=__('https://host/org/repo or https://host/org/repo.git', 'gitea')?>">
</td>
</tr>
<tr>
<th>
<label><?=__('Access Token', 'gitea')?></label>
</th>
<td>
<input type="password" class="token" name="gitea_install[access_token]">
</td>
</tr>
<tr>
<th></th>
<td>
<input type="submit" name="submit" id="submit" class="button button-primary" value="<?=__('Install Now', 'gitea')?>">
</td>
</tr>
</table>
</div>
</form>
<?php endif; ?>
</div>
</div>
<div class="times">
<?= __('Plugins', 'gitea') ?>: <?= date('d/m/Y H:i:s', get_option('gitea_plugins_updated')) ?>
<br>
<?= __('Themes', 'gitea') ?>: <?= date('d/m/Y H:i:s', get_option('gitea_themes_updated')) ?>
</div>
<div class="times">
<?=__('Plugins', 'gitea')?>: <?=date('d/m/Y H:i:s', get_option('gitea_plugins_updated'))?>
<br>
<?=__('Themes', 'gitea')?>: <?=date('d/m/Y H:i:s', get_option('gitea_themes_updated'))?>
</div>
<?php /* <textarea style="width:100%" rows="2" onfocus="this.rows=30;" onblur="this.rows=2;" readonly><?php var_dump($repositories); ?></textarea> */ ?>
<?php /* <textarea style="width:100%" rows="2" onfocus="this.rows=30;" onblur="this.rows=2;" readonly><?php var_dump($repositories); ?></textarea> */ ?>
<?php
}
<?php
}
function get_repositories()
{
$plugins = (array)get_option('gitea_plugins');
$themes = (array)get_option('gitea_themes');
function get_repositories() {
$plugins = (array)get_option('gitea_plugins');
$themes = (array)get_option('gitea_themes');
$repositories = array();
$repositories = array();
foreach ($plugins as $slug => $plugin) {
$plugin['url'] = $plugin['gitea_host'] . $plugin['gitea_repo'];
$repositories[$plugin['gitea_host']]['plugin'][$slug] = $plugin;
}
foreach ($plugins as $slug => $plugin) {
$plugin['url'] = $plugin['gitea_host'].$plugin['gitea_repo'];
$repositories[$plugin['gitea_host']]['plugin'][$slug] = $plugin;
}
foreach ($themes as $slug => $theme) {
$theme['url'] = $theme['gitea_host'] . $theme['gitea_repo'];
$repositories[$theme['gitea_host']]['theme'][$slug] = $theme;
}
foreach ($themes as $slug => $theme) {
$theme['url'] = $theme['gitea_host'].$theme['gitea_repo'];
$repositories[$theme['gitea_host']]['theme'][$slug] = $theme;
}
return $repositories;
return $repositories;
}
}
static function get()
{
return (array)get_option('gitea_options');
}
static function get() {
return (array)get_option('gitea_options');
}
function install($data)
{
$url = parse_url($data['url']);
$host = $url['scheme'] . '://' . $url['host'] . '/';
$repo = str_replace('.git', '', trim($url['path'], '/'));
function install($data) {
$url = parse_url($data['url']);
$host = $url['scheme'].'://'.$url['host'].'/';
$repo = str_replace('.git', '', trim($url['path'], '/'));
$updater = Gitea_Updater::getInstance();
$package = $updater->get_url($host, $repo, '/archive/master.zip', $data['access_token']);
$updater = Gitea_Updater::getInstance();
$package = $updater->get_url($host, $repo, '/archive/master.zip', $data['access_token']);
if ($data['type'] == 'plugin') {
if ($data['type'] == 'plugin') {
add_filter('install_plugin_complete_actions', array($this, 'install_actions'));
add_filter('install_plugin_complete_actions', array($this, 'install_actions'));
$upgrader = new Plugin_Upgrader(new Plugin_Installer_Skin(compact('title', 'url', 'nonce', 'plugin', 'api')));
$upgrader->install($package);
$upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact('title', 'url', 'nonce', 'plugin', 'api') ) );
$upgrader->install($package);
remove_filter('install_plugin_complete_actions', array($this, 'install_actions'));
remove_filter('install_plugin_complete_actions', array($this, 'install_actions'));
} elseif ($data['type'] == 'theme') {
} elseif ($data['type'] == 'theme') {
add_filter('install_theme_complete_actions', array($this, 'install_actions'));
add_filter('install_theme_complete_actions', array($this, 'install_actions'));
$upgrader = new Theme_Upgrader(new Theme_Installer_Skin(compact('title', 'url', 'nonce', 'theme', 'api')));
$upgrader->install($package);
$upgrader = new Theme_Upgrader( new Theme_Installer_Skin( compact('title', 'url', 'nonce', 'theme', 'api') ) );
$upgrader->install($package);
remove_filter('install_theme_complete_actions', array($this, 'install_actions'));
remove_filter('install_theme_complete_actions', array($this, 'install_actions'));
}
}
}
}
function install_actions($install_actions) {
unset($install_actions['plugins_page'], $install_actions['themes_page']);
$install_actions['gitea_page'] = '<a href="' . admin_url('options-general.php?page=gitea-updater') . '" target="_parent">' . __( 'Return', 'gitea' ) . '</a>';
return $install_actions;
}
function install_actions($install_actions)
{
unset($install_actions['plugins_page'], $install_actions['themes_page']);
$install_actions['gitea_page'] = '<a href="' . admin_url('options-general.php?page=gitea-updater') . '" target="_parent">' . __('Return', 'gitea') . '</a>';
return $install_actions;
}
}