reformat code to wordpress standard

This commit is contained in:
Pau Capó 2022-02-02 20:46:47 +01:00
parent c597bf3ae4
commit 7bd47309e1
2 changed files with 430 additions and 446 deletions

View file

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

View file

@ -3,7 +3,7 @@
* Plugin Name: Gitea Updater * Plugin Name: Gitea Updater
* Plugin URI: http://www.paucapo.com * Plugin URI: http://www.paucapo.com
* Description: Plugins updater * Description: Plugins updater
* Version: 0.3.9 * Version: 0.3.10
* Author: Pau Capó * Author: Pau Capó
* Author URI: http://www.paucapo.com * Author URI: http://www.paucapo.com
* Text Domain: gitea * Text Domain: gitea
@ -11,423 +11,414 @@
* Gitea URI: wp/gitea-updater * Gitea URI: wp/gitea-updater
*/ */
defined('ABSPATH') or exit; defined( 'ABSPATH' ) or exit;
require 'gitea-options.php'; require 'gitea-options.php';
class Gitea_Updater class Gitea_Updater {
{
private static $_instance; private static $_instance;
public static function getInstance() public static function getInstance() {
{ if ( ! ( self::$_instance instanceof self ) ) {
if (!(self::$_instance instanceof self)) { self::$_instance = new self();
self::$_instance = new self(); }
}
return self::$_instance;
}
private $cache = 86400; // 1 day in seconds return self::$_instance;
private $id = ''; }
public $plugins = array(); private $cache = 86400; // 1 day in seconds
public $themes = array(); private $id = '';
function __construct() public $plugins = array();
{ public $themes = array();
$this->id = str_replace(array('http://', 'https://'), '', get_site_url()); function __construct() {
add_action('plugins_loaded', array($this, 'plugins_loaded')); $this->id = str_replace( array( 'http://', 'https://' ), '', get_site_url() );
Gitea_Options::getInstance(); add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
add_action('admin_init', array($this, 'admin_init')); Gitea_Options::getInstance();
// plugins checks add_action( 'admin_init', array( $this, 'admin_init' ) );
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 // plugins checks
add_filter('pre_set_site_transient_update_themes', array($this, 'pre_set_site_transient_update_themes')); 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' ) );
// extra plugins and themes headers (Gitea Host and Gitea URI) // themes checks
add_filter('extra_plugin_headers', array($this, 'extra_headers')); add_filter( 'pre_set_site_transient_update_themes', array( $this, 'pre_set_site_transient_update_themes' ) );
add_filter('extra_theme_headers', array($this, 'extra_headers'));
} // 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_updated') === false || get_option('gitea_themes_updated') === false) {
$this->get_all();
}
}
function plugins_loaded() function admin_init() {
{ if ( get_option( 'gitea_plugins_updated' ) === false || get_option( 'gitea_themes_updated' ) === false ) {
load_plugin_textdomain('gitea', FALSE, basename(dirname(__FILE__)) . '/languages/'); $this->get_all();
} }
}
function get_all() function plugins_loaded() {
{ load_plugin_textdomain( 'gitea', false, basename( dirname( __FILE__ ) ) . '/languages/' );
$this->get_plugins(); }
$this->get_themes();
}
function is_force_check() function get_all() {
{ $this->get_plugins();
return isset($_GET['force-check']); $this->get_themes();
} }
function is_iwp() function is_force_check() {
{ return isset( $_GET['force-check'] );
foreach ($_POST as $key => $value) { }
if (substr($key, 0, 5) == '_IWP_') {
return true;
}
}
return false;
}
function update($type) function is_iwp() {
{ foreach ( $_POST as $key => $value ) {
// disable cache in case of a force-check from admin site if ( substr( $key, 0, 5 ) == '_IWP_' ) {
if ($this->is_force_check()) { return true;
$this->cache = 30; }
} }
// small hack for multiple checks when asked throw IWP
if ($this->is_iwp()) {
$this->cache = 300;
}
$last_update = get_option('gitea_' . $type . '_updated');
return ($last_update + $this->cache) < time(); return false;
} }
function get_plugins() function update( $type ) {
{ // disable cache in case of a force-check from admin site
if ( $this->is_force_check() ) {
$this->cache = 30;
}
// small hack for multiple checks when asked throw IWP
if ( $this->is_iwp() ) {
$this->cache = 300;
}
$last_update = get_option( 'gitea_' . $type . '_updated' );
$this->plugins = get_option('gitea_plugins'); return ( $last_update + $this->cache ) < time();
if (!is_array($this->plugins)) { }
$this->plugins = array();
}
$plugins = get_plugins(); function get_plugins() {
foreach ($plugins as $plugin_slug => $plugin) { $this->plugins = get_option( 'gitea_plugins' );
if ( ! is_array( $this->plugins ) ) {
$this->plugins = array();
}
if (!empty($plugin['Gitea URI']) && !empty($plugin['Gitea Host'])) { $plugins = get_plugins();
// this is a gitea plugin foreach ( $plugins as $plugin_slug => $plugin ) {
$slug = trim(dirname($plugin_slug), '/');
$local_version = strtolower($plugin['Version']);
if (isset($this->plugins[$slug]) && !$this->update('plugins')) { if ( ! empty( $plugin['Gitea URI'] ) && ! empty( $plugin['Gitea Host'] ) ) {
$this->plugins[$slug]['local_version'] = $local_version;
continue;
}
$file = basename($plugin_slug); // this is a gitea plugin
$host = $this->get_host($plugin['Gitea Host']); $slug = trim( dirname( $plugin_slug ), '/' );
$repo = $plugin['Gitea URI']; $local_version = strtolower( $plugin['Version'] );
$url = $this->get_url($host, $repo, '/raw/master/' . $file); if ( isset( $this->plugins[ $slug ] ) && ! $this->update( 'plugins' ) ) {
if ($url != false) { $this->plugins[ $slug ]['local_version'] = $local_version;
$new_version = $this->get_version($url, 'plugin'); continue;
} }
$new_version = isset($new_version) && $new_version ? $new_version : $local_version;
// here we have the remote version from gitea/repo/plugin_file $file = basename( $plugin_slug );
$host = $this->get_host( $plugin['Gitea Host'] );
$repo = $plugin['Gitea URI'];
// building the plugin data $url = $this->get_url( $host, $repo, '/raw/master/' . $file );
$gitea = array( if ( $url != false ) {
'plugin' => $plugin_slug, $new_version = $this->get_version( $url, 'plugin' );
'slug' => $slug, }
'file' => $file, $new_version = isset( $new_version ) && $new_version ? $new_version : $local_version;
'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; // 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' ),
);
update_option('gitea_plugins', $this->plugins); $this->plugins[ $slug ] = $gitea;
if ($this->update('plugins')) {
update_option('gitea_plugins_updated', time());
}
} }
}
function get_themes() update_option( 'gitea_plugins', $this->plugins );
{ if ( $this->update( 'plugins' ) ) {
update_option( 'gitea_plugins_updated', time() );
}
$this->themes = array(); }
$themes = wp_get_themes();
foreach ($themes as $theme) { function get_themes() {
$headers = $this->get_file_headers(file_get_contents($theme->get_template_directory().'/style.css'), 'theme'); $this->themes = array();
if (empty($headers['GiteaHost']) || empty($headers['GiteaURI'])) { $themes = wp_get_themes();
foreach ( $themes as $theme ) {
$headers = $this->get_file_headers( file_get_contents( $theme->get_template_directory() . '/style.css' ), 'theme' );
if ( empty( $headers['GiteaHost'] ) || empty( $headers['GiteaURI'] ) ) {
continue; continue;
} }
// this is a gitea theme // this is a gitea theme
$slug = $theme->get_stylesheet(); $slug = $theme->get_stylesheet();
var_dump($slug); var_dump( $slug );
$local_version = strtolower($headers['Version']); $local_version = strtolower( $headers['Version'] );
if (isset($this->themes[$slug]) && !$this->update('themes')) { if ( isset( $this->themes[ $slug ] ) && ! $this->update( 'themes' ) ) {
$this->themes[$slug]['local_version'] = $local_version; $this->themes[ $slug ]['local_version'] = $local_version;
continue; continue;
} }
$host = $this->get_host($headers['GiteaHost']); $host = $this->get_host( $headers['GiteaHost'] );
$repo = $headers['GiteaURI']; $repo = $headers['GiteaURI'];
$url = $this->get_url($host, $repo, '/raw/master/style.css'); $url = $this->get_url( $host, $repo, '/raw/master/style.css' );
$new_version = $local_version; $new_version = $local_version;
if ($url != false) { if ( $url != false ) {
$new_version = $this->get_version($url, 'theme'); $new_version = $this->get_version( $url, 'theme' );
$new_version = $new_version ? $new_version : $local_version; $new_version = $new_version ? $new_version : $local_version;
} }
// 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' => $headers['Name'], 'name' => $headers['Name'],
'gitea_host' => $host, 'gitea_host' => $host,
'gitea_repo' => $repo, 'gitea_repo' => $repo,
'description' => $headers['Description'], 'description' => $headers['Description'],
'author' => $headers['Author'], 'author' => $headers['Author'],
'url' => $headers['AuthorURI'], 'url' => $headers['AuthorURI'],
'local_version' => $local_version, 'local_version' => $local_version,
'new_version' => $new_version, 'new_version' => $new_version,
'package' => $this->get_url($host, $repo, '/archive/master.zip'), 'package' => $this->get_url( $host, $repo, '/archive/master.zip' ),
); );
$this->themes[$slug] = $gitea; $this->themes[ $slug ] = $gitea;
} }
update_option('gitea_themes', $this->themes); update_option( 'gitea_themes', $this->themes );
if ($this->update('themes')) { if ( $this->update( 'themes' ) ) {
update_option('gitea_themes_updated', time()); update_option( 'gitea_themes_updated', time() );
} }
} }
function get_host($host) function get_host( $host ) {
{ return rtrim( $host, '/' ) . '/';
return rtrim($host, '/') . '/'; }
}
function get_token($host, $repo) function get_token( $host, $repo ) {
{ $options = Gitea_Options::get();
$options = Gitea_Options::get(); $token = '';
$token = '';
// get repo token // get repo token
if (isset($options['repo_token'][$host . $repo])) if ( isset( $options['repo_token'][ $host . $repo ] ) ) {
$token = $options['repo_token'][$host . $repo]; $token = $options['repo_token'][ $host . $repo ];
}
// get host token if repo is empty // get host token if repo is empty
if (empty($token) && isset($options['host_token'][$host])) if ( empty( $token ) && isset( $options['host_token'][ $host ] ) ) {
$token = $options['host_token'][$host]; $token = $options['host_token'][ $host ];
}
// return token or false if empty // return token or false if empty
return !empty($token) ? $token : false; return ! empty( $token ) ? $token : false;
} }
function get_url($host, $repo, $args = '', $access_token = false) function get_url( $host, $repo, $args = '', $access_token = false ) {
{
if ($access_token === false) { if ( $access_token === false ) {
$access_token = $this->get_token($host, $repo); $access_token = $this->get_token( $host, $repo );
if (!$access_token) return false; if ( ! $access_token ) {
} return false;
}
}
$add = '&id=' . $this->id; $add = '&id=' . $this->id;
if ($this->is_iwp()) if ( $this->is_iwp() ) {
$add .= '&from=iwp'; $add .= '&from=iwp';
elseif ($this->is_force_check()) } elseif ( $this->is_force_check() ) {
$add .= '&from=force-check'; $add .= '&from=force-check';
}
return $host . 'api/v1/repos/' . $repo . $args . '?access_token=' . $access_token . $add; return $host . 'api/v1/repos/' . $repo . $args . '?access_token=' . $access_token . $add;
} }
function get_file($url) function get_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 ) ) {
return false; return false;
} }
// in $request['body'] we have the content from the file // in $request['body'] we have the content from the file
return $request; return $request;
} }
function get_version($url, $type) function get_version( $url, $type ) {
{
$request = $this->get_file($url); $request = $this->get_file( $url );
if (!$request) return false; if ( ! $request ) {
return false;
}
// get the headers // get the headers
$headers = $this->get_file_headers($request['body'], $type); $headers = $this->get_file_headers( $request['body'], $type );
return isset($headers['Version']) ? $headers['Version'] : false; return isset( $headers['Version'] ) ? $headers['Version'] : false;
} }
function plugins_api($default = false, $action, $args) function plugins_api( $default = false, $action, $args ) {
{ if ( 'plugin_information' != $action ) {
if ('plugin_information' != $action) return $default;
return $default; }
if (!isset($this->plugins[$args->slug])) if ( ! isset( $this->plugins[ $args->slug ] ) ) {
return $default; return $default;
}
// plugin information (view notes befor update) // plugin information (view notes befor update)
$plugin = $this->plugins[$args->slug]; $plugin = $this->plugins[ $args->slug ];
return (object)array( return (object) array(
'name' => $plugin['name'], 'name' => $plugin['name'],
'slug' => $plugin['plugin'], 'slug' => $plugin['plugin'],
'version' => $plugin['new_version'], 'version' => $plugin['new_version'],
'sections' => array( 'sections' => array(
'description' => $plugin['description'] 'description' => $plugin['description']
), ),
'download_link' => $plugin['package'], 'download_link' => $plugin['package'],
'homepage' => $plugin['url'], 'homepage' => $plugin['url'],
); );
} }
function pre_set_site_transient_update_plugins($transient) function pre_set_site_transient_update_plugins( $transient ) {
{ // check if some plugin needs update
// check if some plugin needs update
$this->get_plugins(); $this->get_plugins();
foreach ($this->plugins as $plugin => $git_plugin) { foreach ( $this->plugins as $plugin => $git_plugin ) {
if (isset($transient->response[$git_plugin['plugin']])) if ( isset( $transient->response[ $git_plugin['plugin'] ] ) ) {
unset($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; if ( version_compare( $git_plugin['local_version'], $git_plugin['new_version'], '<' ) ) {
} $transient->response[ $git_plugin['plugin'] ] = (object) $git_plugin;
}
} }
return $transient; return $transient;
} }
function pre_set_site_transient_update_themes($transient) function pre_set_site_transient_update_themes( $transient ) {
{ // check if some theme needs update
// check if some theme needs update
$this->get_themes(); $this->get_themes();
foreach ($this->themes as $theme => $git_theme) { foreach ( $this->themes as $theme => $git_theme ) {
if (isset($transient->response[$theme])) if ( isset( $transient->response[ $theme ] ) ) {
unset($transient->response[$theme]); unset( $transient->response[ $theme ] );
if (version_compare($git_theme['local_version'], $git_theme['new_version'], '<')) { }
$transient->response[$theme] = (array)$git_theme; if ( version_compare( $git_theme['local_version'], $git_theme['new_version'], '<' ) ) {
} $transient->response[ $theme ] = (array) $git_theme;
}
} }
return $transient; return $transient;
} }
function get_file_headers($contents, $type) function get_file_headers( $contents, $type ) {
{
$gitea_headers = array( $gitea_headers = array(
'GiteaHost' => 'Gitea Host', 'GiteaHost' => 'Gitea Host',
'GiteaUri' => 'Gitea URI', 'GiteaUri' => 'Gitea URI',
); );
$default_plugin_headers = array( $default_plugin_headers = array(
'Name' => 'Plugin Name', 'Name' => 'Plugin Name',
'PluginURI' => 'Plugin URI', 'PluginURI' => 'Plugin URI',
'Version' => 'Version', 'Version' => 'Version',
'Description' => 'Description', 'Description' => 'Description',
'Author' => 'Author', 'Author' => 'Author',
'AuthorURI' => 'Author URI', 'AuthorURI' => 'Author URI',
'TextDomain' => 'Text Domain', 'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path', 'DomainPath' => 'Domain Path',
'Network' => 'Network', 'Network' => 'Network',
); );
$default_theme_headers = array( $default_theme_headers = array(
'Name' => 'Theme Name', 'Name' => 'Theme Name',
'ThemeURI' => 'Theme URI', 'ThemeURI' => 'Theme URI',
'Description' => 'Description', 'Description' => 'Description',
'Author' => 'Author', 'Author' => 'Author',
'AuthorURI' => 'Author URI', 'AuthorURI' => 'Author URI',
'Version' => 'Version', 'Version' => 'Version',
'Template' => 'Template', 'Template' => 'Template',
'Status' => 'Status', 'Status' => 'Status',
'Tags' => 'Tags', 'Tags' => 'Tags',
'TextDomain' => 'Text Domain', 'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path', 'DomainPath' => 'Domain Path',
); );
if (false !== strpos($type, 'plugin')) { if ( false !== strpos( $type, 'plugin' ) ) {
$all_headers = $default_plugin_headers; $all_headers = $default_plugin_headers;
} } else if ( false !== strpos( $type, 'theme' ) ) {
else if (false !== strpos($type, 'theme')) { $all_headers = $default_theme_headers;
$all_headers = $default_theme_headers; } else {
}
else {
return []; return [];
} }
$file_data = str_replace("\r", "\n", $contents); $file_data = str_replace( "\r", "\n", $contents );
$all_headers = array_merge($gitea_headers, (array)$all_headers); $all_headers = array_merge( $gitea_headers, (array) $all_headers );
$all_headers = array_unique($all_headers); $all_headers = array_unique( $all_headers );
foreach ($all_headers as $field => $regex) { foreach ( $all_headers as $field => $regex ) {
if (preg_match('/^[ \t\/*#@]*' . preg_quote($regex, '/') . ':(.*)$/mi', $file_data, $match) && $match[1]) { if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) {
$all_headers[$field] = _cleanup_header_comment($match[1]); $all_headers[ $field ] = _cleanup_header_comment( $match[1] );
} else { } else {
$all_headers[$field] = ''; $all_headers[ $field ] = '';
} }
} }
$all_headers = array_filter($all_headers, $all_headers = array_filter( $all_headers,
function ($e) use (&$all_headers) { function ( $e ) use ( &$all_headers ) {
return !empty($e); return ! empty( $e );
}); } );
return $all_headers; return $all_headers;
} }
function extra_headers($headers) function extra_headers( $headers ) {
{ $headers[] = 'Gitea URI';
$headers[] = 'Gitea URI'; $headers[] = 'Gitea Host';
$headers[] = 'Gitea Host';
return $headers; return $headers;
} }
} }