remote install

This commit is contained in:
Pau Capó 2017-03-15 01:40:42 +01:00
parent 996b5bc50a
commit 4bd2231777
2 changed files with 238 additions and 91 deletions

View file

@ -2,9 +2,12 @@
defined( 'ABSPATH' ) or exit; defined( 'ABSPATH' ) or exit;
include_once(ABSPATH.'wp-admin/includes/class-wp-upgrader.php');
class Gitea_Options { class Gitea_Options {
private static $_instance; private static $_instance;
var $install_log = false;
public static function getInstance() { public static function getInstance() {
if (!(self::$_instance instanceof self)) { if (!(self::$_instance instanceof self)) {
@ -31,6 +34,7 @@ class Gitea_Options {
if (isset($_POST['gitea_options'])) { if (isset($_POST['gitea_options'])) {
update_option('gitea_options', $_POST['gitea_options']); update_option('gitea_options', $_POST['gitea_options']);
} }
} }
function options_page() { function options_page() {
@ -42,15 +46,33 @@ class Gitea_Options {
); );
?> ?>
<div class="wrap"> <div class="wrap">
<form action="<?=admin_url('options-general.php?page=gitea-updater')?>" method="post">
<h1>Gitea Updater</h1> <h1>Gitea Updater</h1>
<?php if (isset($_POST['gitea_install'])) : ?>
<div class="box install">
<div class="title">
<h3>Install Results</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">
<div class="title">
<h3>Access Tokens</h3>
</div>
<div class="inner">
<p>The access token from a repository always have priority over the global.</p>
<?php foreach ($repositories as $repository => $packages) : ?> <?php foreach ($repositories as $repository => $packages) : ?>
<br> <br>
<table class="widefat striped gitea"> <table class="widefat striped gitea">
<tr> <tr>
<td colspan="3"> <td colspan="3" class="repository">
<a href="<?=$repository?>"><strong><?=parse_url($repository)['host']?></strong></a> <a href="<?=$repository?>"><strong><?=parse_url($repository)['host']?></strong></a>
</td> </td>
<td> <td>
@ -60,19 +82,19 @@ class Gitea_Options {
<?php foreach ($packages as $type => $items) : ?> <?php foreach ($packages as $type => $items) : ?>
<tr> <tr>
<td></td> <td class="empty"></td>
<td colspan="3"> <td colspan="3" class="type">
<?=$titles[$type]?> <?=$titles[$type]?>
</td> </td>
</tr> </tr>
<?php foreach ($items as $slug => $package) : ?> <?php foreach ($items as $slug => $package) : ?>
<tr> <tr>
<td></td> <td class="empty"></td>
<td></td> <td class="empty"></td>
<td class="package"> <td class="package">
<a href="<?=$package['url']?>"><?=$package['name']?></a> <a href="<?=$package['url']?>"><?=$package['name']?></a>
</td> </td>
<td> <td class="input">
<input type="password" name="gitea_options[repo_token][<?=$package['url']?>]" value="<?=@$options['repo_token'][$package['url']]?>" placeholder="Access Token"> <input type="password" name="gitea_options[repo_token][<?=$package['url']?>]" value="<?=@$options['repo_token'][$package['url']]?>" placeholder="Access Token">
</td> </td>
</li> </li>
@ -82,28 +104,139 @@ class Gitea_Options {
</table> </table>
<?php endforeach; ?> <?php endforeach; ?>
<?php /*submit_button();*/ ?>
<p class="submit"> <p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary" value="<?=__('Save Changes')?>"> <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> <a href="<?=admin_url('options-general.php?page=gitea-updater&force-check=1')?>" class="button button-primary"><?=__('Reset Cache')?></a>
</p> </p>
</form>
<?=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> </div>
</form>
<form action="<?=admin_url('options-general.php?page=gitea-updater')?>" method="post" class="box install">
<div class="title">
<h3>Install from URL</h3>
</div>
<div class="inner">
<table class="form-table">
<tr>
<th>
<label>Type</label>
</th>
<td>
<select name="gitea_install[type]">
<option value="plugin"><?=__('Plugins')?></option>
<option value="theme"><?=__('Themes')?></option>
</select>
</td>
</tr>
<tr>
<th>
<label>Repository URL</label>
</th>
<td>
<input type="text" name="gitea_install[url]" value="https://git.paucapo.com/wp/pau-cleanwordpress.git">
</td>
</tr>
<tr>
<th>
<label>Access Token</label>
</th>
<td>
<input type="text" name="gitea_install[access_token]" value="9ebc78834767d2ce0f95ec8a5ab597bc1fbc9ed6">
</td>
</tr>
<tr>
<th></th>
<td>
<input type="submit" name="submit" id="submit" class="button button-primary" value="<?=__('Install Now')?>">
</td>
</tr>
</table>
</div>
</form>
<?php endif; ?>
</div>
<div class="times">
plugins: <?=date('d/m/Y H:i:s', get_option('gitea_plugins_updated'))?>
themes: <?=date('d/m/Y H:i:s', get_option('gitea_themes_updated'))?>
</div>
<textarea style="width:100%" rows="2" onfocus="this.rows=30;" onblur="this.rows=2;" readonly><?php var_dump($repositories); ?></textarea>
<style> <style>
.wrap {
overflow: hidden;
}
.box {
margin: 20px 0;
background: #FFFFFF;
border: 1px solid #E5E5E5;
position: relative;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
}
.box .title {
border-bottom: 1px solid #EEEEEE;
margin: 0;
padding: 15px;
background: #FFFFFF;
}
.box .title h3 {
font-size: 14px;
line-height: 1em;
margin: 0;
padding: 0;
}
.box .inner {
padding: 15px;
}
table.gitea { table.gitea {
min-width: 700px; width: 100%;
width: auto;
}
table.gitea input {
min-width: 400px;
} }
table.gitea td { table.gitea td {
vertical-align: middle; vertical-align: middle;
box-sizing: border-box;
}
table.gitea td.repository {
width: 210px;
}
table.gitea td.type {
width: 190px;
} }
table.gitea td.package { table.gitea td.package {
min-width: 250px; width: 170px;
}
table.gitea td.empty {
width: 1px;
}
table.gitea input {
width: 70%;
}
@media screen and (max-width: 782px) {
table.gitea td {
display: block;
}
table.gitea td.empty {
display: none;
}
table.gitea input {
width: 100%;
}
}
.times {
color: #555d66;
float: right;
padding: 10px;
font-style: italic;
}
.form-table input[type=text] {
width: 75%;
} }
</style> </style>
<?php <?php
@ -116,10 +249,12 @@ class Gitea_Options {
$repositories = array(); $repositories = array();
foreach ($plugins as $slug => $plugin) { foreach ($plugins as $slug => $plugin) {
$plugin['url'] = $plugin['gitea_host'].$plugin['gitea_repo'];
$repositories[$plugin['gitea_host']]['plugin'][$slug] = $plugin; $repositories[$plugin['gitea_host']]['plugin'][$slug] = $plugin;
} }
foreach ($themes as $slug => $theme) { foreach ($themes as $slug => $theme) {
$theme['url'] = $theme['gitea_host'].$theme['gitea_repo'];
$repositories[$plugin['gitea_host']]['theme'][$slug] = $theme; $repositories[$plugin['gitea_host']]['theme'][$slug] = $theme;
} }
@ -131,5 +266,44 @@ class Gitea_Options {
return (array)get_option('gitea_options'); 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'], '/'));
$updater = Gitea_Updater::getInstance();
$package = $updater->get_gitea_url($host, $repo, '/archive/master.zip', $data['access_token']);
if ($data['type'] == 'plugin') {
add_filter('install_plugin_complete_actions', array($this, 'installed'), 10, 3);
$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, 'installed'), 10, 3);
} elseif ($data['type'] == 'theme') {
add_filter('install_theme_complete_actions', array($this, 'installed'), 10, 3);
$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, 'installed'), 10, 3);
}
}
function installed($install_actions, $api, $file) {
$return = '<a href="' . admin_url('options-general.php?page=gitea-updater') . '" target="_parent">' . __( 'Return' ) . '</a>';
if (isset($install_actions['plugins_page'])) $install_actions['plugins_page'] = $return;
if (isset($install_actions['themes_page'])) $install_actions['themes_page'] = $return;
return $install_actions;
}
} }

View file

@ -43,11 +43,6 @@ class Gitea_Updater {
Gitea_Options::getInstance(); Gitea_Options::getInstance();
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'));
// plugins checks // plugins checks
add_filter('plugins_api', array($this, 'plugins_api'), 10, 3); 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')); add_filter('pre_set_site_transient_update_plugins', array($this, 'pre_set_site_transient_update_plugins'));
@ -56,7 +51,7 @@ class Gitea_Updater {
add_filter('pre_set_site_transient_update_themes', array($this, 'pre_set_site_transient_update_themes')); add_filter('pre_set_site_transient_update_themes', array($this, 'pre_set_site_transient_update_themes'));
// rename directories // rename directories
add_filter('upgrader_source_selection', array($this, 'upgrader_source_selection'), 10, 4); // add_filter('upgrader_source_selection', array($this, 'upgrader_source_selection'), 10, 4);
// extra plugins and themes headers (Gitea Host and Gitea URI) // extra plugins and themes headers (Gitea Host and Gitea URI)
add_filter('extra_plugin_headers', array($this, 'extra_headers')); add_filter('extra_plugin_headers', array($this, 'extra_headers'));
@ -64,15 +59,6 @@ class Gitea_Updater {
} }
function admin_init() {
// cached data
// $this->plugins = (array)get_option('gitea_plugins');
// $this->themes = (array)get_option('gitea_themes');
}
function get_gitea_all() { function get_gitea_all() {
$this->get_gitea_plugins(); $this->get_gitea_plugins();
$this->get_gitea_themes(); $this->get_gitea_themes();
@ -89,11 +75,12 @@ class Gitea_Updater {
// this is a gitea plugin // this is a gitea plugin
$slug = trim(dirname($plugin_slug), '/'); $slug = trim(dirname($plugin_slug), '/');
$file = basename($plugin_slug);
$host = $this->get_gitea_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']); $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/'.$file);
if ($url != false) { if ($url != false) {
$new_version = $this->get_gitea_version($url, 'plugin'); $new_version = $this->get_gitea_version($url, 'plugin');
$new_version = $new_version ? $new_version : $local_version; $new_version = $new_version ? $new_version : $local_version;
@ -105,7 +92,7 @@ class Gitea_Updater {
$gitea = array( $gitea = array(
'plugin' => $plugin_slug, 'plugin' => $plugin_slug,
'slug' => $slug, 'slug' => $slug,
'file' => basename($plugin_slug), 'file' => $file,
'name' => $plugin['Name'], 'name' => $plugin['Name'],
'gitea_host' => $host, 'gitea_host' => $host,
'gitea_repo' => $repo, 'gitea_repo' => $repo,
@ -122,6 +109,7 @@ class Gitea_Updater {
} }
update_option('gitea_plugins', $this->plugins); update_option('gitea_plugins', $this->plugins);
update_option('gitea_plugins_updated', time());
} }
@ -168,6 +156,7 @@ class Gitea_Updater {
} }
update_option('gitea_themes', $this->themes); update_option('gitea_themes', $this->themes);
update_option('gitea_themes_updated', time());
} }
@ -191,9 +180,13 @@ class Gitea_Updater {
return !empty($token) ? $token : false; return !empty($token) ? $token : false;
} }
function get_gitea_url($host, $repo, $args = '') { function get_gitea_url($host, $repo, $args = '', $access_token = false) {
if ($access_token === false)
$access_token = $this->get_gitea_token($host, $repo); $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;
} }
@ -255,14 +248,14 @@ class Gitea_Updater {
unset($transient->response[$git_plugin['plugin']]); unset($transient->response[$git_plugin['plugin']]);
if (version_compare($git_plugin['local_version'], $git_plugin['new_version'], '<')) { if (version_compare($git_plugin['local_version'], $git_plugin['new_version'], '<')) {
$object = new stdClass; // $object = new stdClass;
$object->slug = $git_plugin['slug']; // $object->slug = $git_plugin['slug'];
$object->plugin = $git_plugin['plugin']; // $object->plugin = $git_plugin['plugin'];
$object->new_version = $git_plugin['new_version']; // $object->new_version = $git_plugin['new_version'];
$object->url = $git_plugin['url']; // $object->url = $git_plugin['url'];
$object->package = $git_plugin['package']; // $object->package = $git_plugin['package'];
$transient->response[$git_plugin['plugin']] = $object; $transient->response[$git_plugin['plugin']] = (object)$git_plugin;
} }
} }
@ -288,26 +281,6 @@ class Gitea_Updater {
return $transient; return $transient;
} }
public function admin_pages_update_transient() {
// force update data on 'plugins.php', 'themes.php', 'update-core.php'
global $pagenow;
$admin_pages = array('plugins.php', 'themes.php', 'update-core.php');
$is_admin_page = in_array($pagenow, $admin_pages) ? true : false;
$transient = 'update_'.rtrim($pagenow, '.php');
$transient = 'update_update-core' === $transient ? 'update_core' : $transient;
if ($is_admin_page && current_user_can($transient)) {
$current = get_site_transient($transient);
$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'));
}
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;