223 lines
No EOL
6.9 KiB
PHP
223 lines
No EOL
6.9 KiB
PHP
<?php
|
|
/*
|
|
Plugin Name: Pau-Updater
|
|
Plugin URI: http://www.paucapo.com
|
|
Description: Plugins updater
|
|
Version: 0.1
|
|
Author: Pau Capó
|
|
Author URI: http://www.paucapo.com
|
|
Gitea Host: https://git.paucapo.com/
|
|
Gitea URI: wp/pau-updater
|
|
*/
|
|
|
|
/*
|
|
TODO:
|
|
* access_token saved for host
|
|
* branches?
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) or exit;
|
|
|
|
class Pau_Updater {
|
|
public $update_data = array();
|
|
public $active_plugins = array();
|
|
|
|
|
|
function __construct() {
|
|
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, 'set_update_data' ) );
|
|
add_filter( 'upgrader_source_selection', array( $this, 'upgrader_source_selection' ), 10, 4 );
|
|
add_filter( 'extra_plugin_headers', array( $this, 'extra_plugin_headers' ) );
|
|
}
|
|
|
|
|
|
function admin_init() {
|
|
$now = strtotime( 'now' );
|
|
$last_checked = (int) get_option( 'ghu_last_checked' );
|
|
$check_interval = 0;//apply_filters( 'ghu_check_interval', ( 60 * 60 * 12 ) );
|
|
$this->update_data = (array) get_option( 'ghu_update_data' );
|
|
$active = (array) get_option( 'active_plugins' );
|
|
|
|
foreach ( $active as $slug ) {
|
|
$this->active_plugins[ $slug ] = true;
|
|
}
|
|
|
|
// transient expiration
|
|
if ( ( $now - $last_checked ) > $check_interval ) {
|
|
$this->update_data = $this->get_gitea_updates();
|
|
|
|
update_option( 'ghu_update_data', $this->update_data );
|
|
update_option( 'ghu_last_checked', $now );
|
|
}
|
|
}
|
|
|
|
function get_gitea_updates() {
|
|
$plugin_data = array();
|
|
$plugins = get_plugins();
|
|
foreach ( $plugins as $slug => $info ) {
|
|
if ( isset( $this->active_plugins[ $slug ] ) && ! empty( $info['Gitea URI'] ) && ! empty( $info['Gitea Host'] ) ) {
|
|
$temp = array(
|
|
'plugin' => $slug,
|
|
'slug' => trim( dirname( $slug ), '/' ),
|
|
'file' => basename($slug),
|
|
'name' => $info['Name'],
|
|
'gitea_host' => $info['Gitea Host'],
|
|
'gitea_repo' => $info['Gitea URI'],
|
|
'description' => $info['Description'],
|
|
);
|
|
|
|
|
|
// TODO
|
|
$access_token = '9ebc78834767d2ce0f95ec8a5ab597bc1fbc9ed6';
|
|
|
|
$url = $temp['gitea_host'].'api/v1/repos/'.$temp['gitea_repo'].'/raw/master/'.$temp['file'].'?access_token='.$access_token;
|
|
$request = wp_remote_get( $url );
|
|
|
|
// WP error or rate limit exceeded
|
|
if ( is_wp_error( $request ) || 200 != wp_remote_retrieve_response_code( $request ) ) {
|
|
break;
|
|
}
|
|
|
|
$headers = $this->get_file_headers($request['body'], 'plugin');
|
|
|
|
$temp['new_version'] = $headers['Version'];
|
|
$temp['url'] = $temp['gitea_host'].$temp['gitea_repo'];
|
|
$temp['package'] = $temp['gitea_host'].'api/v1/repos/'.$temp['gitea_repo'].'/archive/master.zip?access_token='.$access_token;
|
|
|
|
$plugin_data[ $slug ] = $temp;
|
|
}
|
|
}
|
|
|
|
return $plugin_data;
|
|
}
|
|
|
|
function get_file_headers( $contents, $type ) {
|
|
|
|
$gitea_headers = array(
|
|
'GiteaHost' => 'Gitea Host',
|
|
'GiteaUri' => 'Gitea URI',
|
|
);
|
|
|
|
$default_plugin_headers = array(
|
|
'Name' => 'Plugin Name',
|
|
'PluginURI' => 'Plugin URI',
|
|
'Version' => 'Version',
|
|
'Description' => 'Description',
|
|
'Author' => 'Author',
|
|
'AuthorURI' => 'Author URI',
|
|
'TextDomain' => 'Text Domain',
|
|
'DomainPath' => 'Domain Path',
|
|
'Network' => 'Network',
|
|
);
|
|
|
|
$default_theme_headers = array(
|
|
'Name' => 'Theme Name',
|
|
'ThemeURI' => 'Theme URI',
|
|
'Description' => 'Description',
|
|
'Author' => 'Author',
|
|
'AuthorURI' => 'Author URI',
|
|
'Version' => 'Version',
|
|
'Template' => 'Template',
|
|
'Status' => 'Status',
|
|
'Tags' => 'Tags',
|
|
'TextDomain' => 'Text Domain',
|
|
'DomainPath' => 'Domain Path',
|
|
);
|
|
|
|
if ( false !== strpos( $type, 'plugin' ) ) {
|
|
$all_headers = $default_plugin_headers;
|
|
}
|
|
|
|
if ( false !== strpos( $type, 'theme' ) ) {
|
|
$all_headers = $default_theme_headers;
|
|
}
|
|
|
|
$file_data = str_replace( "\r", "\n", $contents );
|
|
|
|
$all_headers = array_merge( $gitea_headers, (array) $all_headers );
|
|
$all_headers = array_unique( $all_headers );
|
|
|
|
foreach ( $all_headers as $field => $regex ) {
|
|
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) {
|
|
$all_headers[ $field ] = _cleanup_header_comment( $match[1] );
|
|
} else {
|
|
$all_headers[ $field ] = '';
|
|
}
|
|
}
|
|
|
|
$all_headers = array_filter( $all_headers,
|
|
function( $e ) use ( &$all_headers ) {
|
|
return ! empty( $e );
|
|
} );
|
|
|
|
return $all_headers;
|
|
}
|
|
|
|
function plugins_api( $default = false, $action, $args ) {
|
|
if ( 'plugin_information' == $action ) {
|
|
if ( isset( $this->update_data[ $args->slug ] ) ) {
|
|
$plugin = $this->update_data[ $args->slug ];
|
|
|
|
return (object) array(
|
|
'name' => $plugin['name'],
|
|
'slug' => $plugin['plugin'],
|
|
'version' => $plugin['new_version'],
|
|
'requires' => '4.4',
|
|
'tested' => get_bloginfo( 'version' ),
|
|
'last_updated' => date( 'Y-m-d' ),
|
|
'sections' => array(
|
|
'description' => $plugin['description']
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
return $default;
|
|
}
|
|
|
|
|
|
function set_update_data( $transient ) {
|
|
if ( empty( $transient->checked ) ) {
|
|
return $transient;
|
|
}
|
|
|
|
foreach ( $this->update_data as $plugin => $info ) {
|
|
if ( isset( $this->active_plugins[ $plugin ] ) ) {
|
|
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
|
|
$version = $plugin_data['Version'];
|
|
|
|
if ( version_compare( $version, $info['new_version'], '<' ) ) {
|
|
$transient->response[ $plugin ] = (object) $info;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $transient;
|
|
}
|
|
|
|
|
|
function upgrader_source_selection( $source, $remote_source, $upgrader, $hook_extra = null ) {
|
|
global $wp_filesystem;
|
|
|
|
$plugin = isset( $hook_extra['plugin'] ) ? $hook_extra['plugin'] : false;
|
|
if ( isset( $this->update_data[ $plugin ] ) && $plugin ) {
|
|
$new_source = trailingslashit( $remote_source ) . dirname( $plugin );
|
|
$wp_filesystem->move( $source, $new_source );
|
|
return trailingslashit( $new_source );
|
|
}
|
|
|
|
return $source;
|
|
}
|
|
|
|
function extra_plugin_headers( $headers ) {
|
|
$headers[] = 'Gitea URI';
|
|
$headers[] = 'Gitea Host';
|
|
return $headers;
|
|
}
|
|
|
|
}
|
|
|
|
new Pau_Updater(); |