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

@ -4,42 +4,39 @@ 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; 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_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
} }
function admin_menu() function admin_menu() {
{
$this->page = add_options_page( 'Gitea Updater', 'Gitea Updater', 'manage_options', 'gitea-updater', array( $this, 'options_page' ) ); $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' ) ); add_action( 'load-' . $this->page, array( $this, 'options_post' ) );
} }
function enqueue_assets($hook) function enqueue_assets( $hook ) {
{ if ( $hook != $this->page ) {
if ($hook != $this->page) return; return;
}
wp_enqueue_style( 'gitea-updater', plugins_url( '/assets/style.css', __FILE__ ) ); 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_script( 'gitea-js', plugins_url( '/assets/script.js', __FILE__ ), array( 'jquery' ), null, true );
} }
function options_post() function options_post() {
{
if ( isset( $_GET['force-check'] ) ) { if ( isset( $_GET['force-check'] ) ) {
Gitea_Updater::getInstance()->get_all(); Gitea_Updater::getInstance()->get_all();
@ -52,8 +49,7 @@ class Gitea_Options
} }
function options_page() function options_page() {
{
$options = (array) get_option( 'gitea_options' ); $options = (array) get_option( 'gitea_options' );
$repositories = $this->get_repositories(); $repositories = $this->get_repositories();
$types = array( $types = array(
@ -99,7 +95,7 @@ class Gitea_Options
<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>
@ -115,7 +111,7 @@ class Gitea_Options
<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>
@ -203,8 +199,7 @@ class Gitea_Options
<?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' );
@ -224,13 +219,11 @@ class Gitea_Options
} }
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'], '/' ) );
@ -260,10 +253,10 @@ class Gitea_Options
} }
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
@ -16,16 +16,15 @@ 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; return self::$_instance;
} }
@ -35,8 +34,7 @@ class Gitea_Updater
public $plugins = array(); public $plugins = array();
public $themes = array(); public $themes = array();
function __construct() function __construct() {
{
$this->id = str_replace( array( 'http://', 'https://' ), '', get_site_url() ); $this->id = str_replace( array( 'http://', 'https://' ), '', get_site_url() );
@ -59,41 +57,36 @@ class Gitea_Updater
} }
function admin_init() function admin_init() {
{
if ( get_option( 'gitea_plugins_updated' ) === false || get_option( 'gitea_themes_updated' ) === false ) { if ( get_option( 'gitea_plugins_updated' ) === false || get_option( 'gitea_themes_updated' ) === false ) {
$this->get_all(); $this->get_all();
} }
} }
function plugins_loaded() function plugins_loaded() {
{ load_plugin_textdomain( 'gitea', false, basename( dirname( __FILE__ ) ) . '/languages/' );
load_plugin_textdomain('gitea', FALSE, basename(dirname(__FILE__)) . '/languages/');
} }
function get_all() function get_all() {
{
$this->get_plugins(); $this->get_plugins();
$this->get_themes(); $this->get_themes();
} }
function is_force_check() function is_force_check() {
{
return isset( $_GET['force-check'] ); return isset( $_GET['force-check'] );
} }
function is_iwp() function is_iwp() {
{
foreach ( $_POST as $key => $value ) { foreach ( $_POST as $key => $value ) {
if ( substr( $key, 0, 5 ) == '_IWP_' ) { if ( substr( $key, 0, 5 ) == '_IWP_' ) {
return true; return true;
} }
} }
return false; return false;
} }
function update($type) function update( $type ) {
{
// disable cache in case of a force-check from admin site // disable cache in case of a force-check from admin site
if ( $this->is_force_check() ) { if ( $this->is_force_check() ) {
$this->cache = 30; $this->cache = 30;
@ -107,8 +100,7 @@ class Gitea_Updater
return ( $last_update + $this->cache ) < time(); return ( $last_update + $this->cache ) < time();
} }
function get_plugins() function get_plugins() {
{
$this->plugins = get_option( 'gitea_plugins' ); $this->plugins = get_option( 'gitea_plugins' );
if ( ! is_array( $this->plugins ) ) { if ( ! is_array( $this->plugins ) ) {
@ -169,8 +161,7 @@ class Gitea_Updater
} }
function get_themes() function get_themes() {
{
$this->themes = array(); $this->themes = array();
$themes = wp_get_themes(); $themes = wp_get_themes();
@ -230,47 +221,48 @@ class Gitea_Updater
} }
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 ) ) {
@ -281,12 +273,13 @@ class Gitea_Updater
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 );
@ -294,13 +287,14 @@ class Gitea_Updater
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)
@ -319,16 +313,16 @@ class Gitea_Updater
} }
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'], '<' ) ) { if ( version_compare( $git_plugin['local_version'], $git_plugin['new_version'], '<' ) ) {
$transient->response[ $git_plugin['plugin'] ] = (object) $git_plugin; $transient->response[ $git_plugin['plugin'] ] = (object) $git_plugin;
} }
@ -338,16 +332,16 @@ class Gitea_Updater
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'], '<' ) ) { if ( version_compare( $git_theme['local_version'], $git_theme['new_version'], '<' ) ) {
$transient->response[ $theme ] = (array) $git_theme; $transient->response[ $theme ] = (array) $git_theme;
} }
@ -357,8 +351,7 @@ class Gitea_Updater
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',
@ -393,11 +386,9 @@ class Gitea_Updater
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 [];
} }
@ -422,10 +413,10 @@ class Gitea_Updater
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;
} }