Skip to content

Commit

Permalink
v 0.5.3
Browse files Browse the repository at this point in the history
- Add autoupdate
  • Loading branch information
Darklg committed Apr 15, 2024
1 parent 895afda commit 317b1c5
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 2 deletions.
1 change: 1 addition & 0 deletions inc/WPUBaseUpdate/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deny from all
16 changes: 16 additions & 0 deletions inc/WPUBaseUpdate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
WPU Base Update
---

Update your Github WordPress plugin from the plugins page admin.

## Insert in the INIT hook

```php
include dirname( __FILE__ ) . '/inc/WPUBaseUpdate/WPUBaseUpdate.php';
$this->settings_update = new \PLUGINID\WPUBaseUpdate(
'WordPressUtilities',
'PLUGINID',
$this->plugin_version);
```

Please ensure that this code is available before when !is_admin() ( for API purposes )
257 changes: 257 additions & 0 deletions inc/WPUBaseUpdate/WPUBaseUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
<?php
namespace wpuerrorlogs;

/*
Class Name: WPU Base Update
Description: A class to handle plugin update from github
Version: 0.4.5
Class URI: https://github.com/WordPressUtilities/wpubaseplugin
Author: Darklg
Author URI: https://darklg.me/
License: MIT License
License URI: https://opensource.org/licenses/MIT
Thanks: https://gist.github.com/danielbachhuber/7684646
*/

defined('ABSPATH') || die;

class WPUBaseUpdate {

public $current_version;
private $github_username;
private $github_project;
private $github_path;
private $transient_name;
private $transient_expiration;
private $plugin_id;
private $plugin_dir;
private $details;

public function __construct($github_username = false, $github_project = false, $current_version = false, $details = array()) {
$this->init($github_username, $github_project, $current_version, $details);
}

public function init($github_username = false, $github_project = false, $current_version = false, $details = array()) {
if (!$github_username || !$github_project || !$current_version) {
return;
}

/* Settings */
$this->github_username = $github_username;
$this->github_project = $github_project;
$this->current_version = $current_version;
$this->github_path = $this->github_username . '/' . $this->github_project;
$this->transient_name = strtolower($this->github_username . '_' . $this->github_project . '_info_plugin_update');
$this->transient_expiration = HOUR_IN_SECONDS;
$this->plugin_id = $this->github_project . '/' . $this->github_project . '.php';
$this->plugin_dir = (defined('WP_PLUGIN_DIR') ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins') . '/' . $this->plugin_id;

$gitpath = dirname($this->plugin_dir) . '/.git';
if (is_dir($gitpath) || file_exists($gitpath)) {
return;
}

if (!is_array($details)) {
$details = array();
}

$this->details = $details;

/* Hook on plugin update */
add_filter('site_transient_update_plugins', array($this,
'filter_update_plugins'
));
add_filter('transient_update_plugins', array($this,
'filter_update_plugins'
));
add_filter('upgrader_post_install', array($this,
'upgrader_post_install'
), 10, 3);
add_filter('plugins_api', array(&$this,
'plugins_api'
), 20, 3);

}

public function filter_update_plugins($update_plugins) {

if (!is_object($update_plugins)) {
return $update_plugins;
}

if (!isset($update_plugins->response) || !is_array($update_plugins->response)) {
$update_plugins->response = array();
}

$plugin_info = $this->get_new_plugin_info();
if ($plugin_info !== false && is_array($plugin_info)) {
$update_plugins->response[$this->plugin_id] = (object) $plugin_info;
}

return $update_plugins;
}

public function get_new_plugin_info() {
$plugin_info = false;
$body_json = $this->get_plugin_update_info();
if (!is_array($body_json)) {
return false;
}

foreach ($body_json as $plugin_version) {
/* Skip older versions */
if (version_compare($plugin_version->name, $this->current_version) <= 0) {
continue;
}

if (is_array($plugin_info)) {
/* Update only changelog if other plugin info have been filled by a previous commit */
$plugin_info['sections']['changelog'] .= $this->get_commit_info($plugin_version->commit->url, $plugin_version->commit->sha);
continue;
}

/* Add plugin details */
$plugin_info = array(
'name' => $this->github_project,
'slug' => 'github-' . $this->github_project,
'version' => $plugin_version->name,
'new_version' => $plugin_version->name,
'plugin' => $this->plugin_id,
'destination_name' => $this->github_project,
'url' => 'https://github.com/' . $this->github_path,
'trunk' => $plugin_version->zipball_url,
'download_link' => $plugin_version->zipball_url,
'package' => $plugin_version->zipball_url,
'sections' => array()
);

/* Fetch plugin data */
$plugin_data = array();
if (file_exists($this->plugin_dir)) {
if (!function_exists('get_plugin_data')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_data = get_plugin_data($this->plugin_dir);
}

if (isset($plugin_data['Author'])) {
$plugin_info['author'] = $plugin_data['Author'];
}
if (isset($plugin_data['Name'])) {
$plugin_info['name'] = $plugin_data['Name'];
}
if (isset($plugin_data['Description'])) {
$plugin_info['sections']['description'] = $plugin_data['Description'];
}

$plugin_info['sections']['changelog'] = $this->get_commit_info($plugin_version->commit->url, $plugin_version->commit->sha);

$_plugin_vars = array('tested', 'requires', 'homepage', 'donate_link', 'author_profile');
foreach ($_plugin_vars as $_plugin_var) {
if (isset($this->details[$_plugin_var]) && $this->details[$_plugin_var]) {
$plugin_info[$_plugin_var] = $this->details[$_plugin_var];
}
}

/* Future info */
// $plugin_info['banners'] = array(
// 'low' => 'http://placehold.it/772x250',
// 'high' => 'http://placehold.it/1544x500'
// );

}

return $plugin_info;
}

public function plugins_api($res, $action, $args) {

if ($action !== 'plugin_information') {
return false;
}

if ('github-' . $this->github_project !== $args->slug) {
return $res;
}

$plugin_info = $this->get_new_plugin_info();
if ($plugin_info !== false && is_array($plugin_info)) {
return (object) $plugin_info;
}

return false;

}

/* Retrieve tag infos from github */
public function get_plugin_update_info() {
if (false === ($plugin_update_body = get_transient($this->transient_name))) {
$plugin_update_body = wp_remote_retrieve_body(wp_remote_get('https://api.github.com/repos/' . $this->github_path . '/tags'));
set_transient($this->transient_name, $plugin_update_body, $this->transient_expiration);
}
return json_decode($plugin_update_body);
}

/* Retrieve commit infos from github */
public function get_plugin_commits_info() {
$transient_id = $this->transient_name . '_commits';
$url = 'https://api.github.com/repos/' . $this->github_path . '/commits';
if (false === ($plugin_update_body = get_transient($transient_id))) {
$plugin_update_body = wp_remote_retrieve_body(wp_remote_get($url));
set_transient($transient_id, $plugin_update_body, $this->transient_expiration);
}
return json_decode($plugin_update_body);
}

private function get_commit_info($commit, $sha) {

$commit_info = false;
/* Try to obtain commit info from global commit infos */
$commits = $this->get_plugin_commits_info();
if (is_array($commits)) {
foreach ($commits as $_commit) {
if ($_commit->sha != $sha) {
continue;
}
return $this->get_nice_commit_diff($_commit);
}
}

/* No luck : obtain infos from Commit API */
$transient_id = $this->github_project . '_commit_info_' . $sha . $this->current_version;
if (!$commit_info && false === ($commit_info = get_transient($transient_id))) {
$commit_info = wp_remote_retrieve_body(wp_remote_get($commit));
set_transient($transient_id, $commit_info, YEAR_IN_SECONDS);
}
return $this->get_nice_commit_diff(json_decode($commit_info));
}

private function get_nice_commit_diff($commit_info) {
$info = '';
if (is_object($commit_info) && isset($commit_info->commit->author->date)) {
$commit_time = strtotime($commit_info->commit->author->date);
$info .= '<p>';
$info .= '<strong>' . date_i18n('Y-m-d H:i', $commit_time) . '</strong> / ';
$info .= nl2br($commit_info->commit->message);
$info .= '</p>';
}
return $info;
}

public function upgrader_post_install($true, $hook_extra, $result) {
/* Only for this plugin */
$base_plugin_name = $this->github_username . '-' . $this->github_project;
if (substr($result['destination_name'], 0, strlen($base_plugin_name)) === $base_plugin_name) {
$new_destination = $result['local_destination'] . '/' . $this->github_project . '/';

/* Move files to the correct destination */
rename($result['destination'], $new_destination);

/* Set result vars */
$result['destination'] = $new_destination;
$result['remote_destination'] = $new_destination;
$result['destination_name'] = $this->github_project;
}
return $result;
}
}
1 change: 1 addition & 0 deletions inc/WPUBaseUpdate/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php /* Silence */
11 changes: 9 additions & 2 deletions wpuerrorlogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin URI: https://github.com/WordPressUtilities/wpuerrorlogs
Update URI: https://github.com/WordPressUtilities/wpuerrorlogs
Description: Make sense of your log files
Version: 0.5.2
Version: 0.5.3
Author: Darklg
Author URI: https://github.com/Darklg
Text Domain: wpuerrorlogs
Expand All @@ -21,7 +21,7 @@
}

class WPUErrorLogs {
private $plugin_version = '0.5.2';
private $plugin_version = '0.5.3';
private $plugin_settings = array(
'id' => 'wpuerrorlogs',
'name' => 'WPU Error Logs'
Expand All @@ -41,6 +41,13 @@ public function plugins_loaded() {
}
$this->plugin_description = __('Make sense of your log files', 'wpuerrorlogs');

# UPDATE
require_once __DIR__ . '/inc/WPUBaseUpdate/WPUBaseUpdate.php';
$this->settings_update = new \wpuerrorlogs\WPUBaseUpdate(
'WordPressUtilities',
'wpuerrorlogs',
$this->plugin_version);

# TOOLBOX
require_once __DIR__ . '/inc/WPUBaseToolbox/WPUBaseToolbox.php';
$this->basetoolbox = new \wpuerrorlogs\WPUBaseToolbox();
Expand Down

0 comments on commit 317b1c5

Please sign in to comment.