213 lines
5.9 KiB
PHP
213 lines
5.9 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Updater
|
|
*
|
|
* Handles automatic updates from IGNY8 API
|
|
*
|
|
* @package Igny8Bridge
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Igny8_Updater {
|
|
|
|
/**
|
|
* Plugin slug
|
|
*
|
|
* @var string
|
|
*/
|
|
private $plugin_slug = 'igny8-wp-bridge';
|
|
|
|
/**
|
|
* Plugin file
|
|
*
|
|
* @var string
|
|
*/
|
|
private $plugin_file;
|
|
|
|
/**
|
|
* Current version
|
|
*
|
|
* @var string
|
|
*/
|
|
private $version;
|
|
|
|
/**
|
|
* API URL
|
|
*
|
|
* @var string
|
|
*/
|
|
private $api_url;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param string $plugin_file Plugin file path
|
|
* @param string $version Current version
|
|
* @param string $api_url API URL
|
|
*/
|
|
public function __construct($plugin_file, $version, $api_url) {
|
|
$this->plugin_file = $plugin_file;
|
|
$this->version = $version;
|
|
$this->api_url = trailingslashit($api_url);
|
|
|
|
// Hook into WordPress update system
|
|
add_filter('pre_set_site_transient_update_plugins', array($this, 'check_for_updates'));
|
|
add_filter('plugins_api', array($this, 'plugin_info'), 10, 3);
|
|
|
|
// Add custom styling for plugin details popup
|
|
add_action('admin_enqueue_scripts', array($this, 'enqueue_plugin_details_styles'));
|
|
}
|
|
|
|
/**
|
|
* Check for plugin updates
|
|
*
|
|
* @param object $transient Update transient
|
|
* @return object Modified transient
|
|
*/
|
|
public function check_for_updates($transient) {
|
|
if (empty($transient->checked)) {
|
|
return $transient;
|
|
}
|
|
|
|
// Get update info from IGNY8 API
|
|
$update_info = $this->get_update_info();
|
|
|
|
if (!$update_info || !isset($update_info['latest_version'])) {
|
|
return $transient;
|
|
}
|
|
|
|
$plugin_basename = plugin_basename($this->plugin_file);
|
|
$latest_version = $update_info['latest_version'];
|
|
|
|
// Only show update if latest version is actually newer than current
|
|
if (version_compare($this->version, $latest_version, '<')) {
|
|
$transient->response[$plugin_basename] = (object) array(
|
|
'slug' => $this->plugin_slug,
|
|
'new_version' => $latest_version,
|
|
'package' => $update_info['download_url'] ?? '',
|
|
'url' => $update_info['info_url'] ?? '',
|
|
'tested' => $update_info['tested'] ?? '',
|
|
'requires_php' => $update_info['requires_php'] ?? '',
|
|
);
|
|
} else {
|
|
// Remove from response if version is current or newer
|
|
unset($transient->response[$plugin_basename]);
|
|
}
|
|
|
|
return $transient;
|
|
}
|
|
|
|
/**
|
|
* Provide plugin information for update screen
|
|
*
|
|
* @param false|object|array $result The result object or array
|
|
* @param string $action The type of information being requested
|
|
* @param object $args Plugin API arguments
|
|
* @return false|object Modified result
|
|
*/
|
|
public function plugin_info($result, $action, $args) {
|
|
if ($action !== 'plugin_information') {
|
|
return $result;
|
|
}
|
|
|
|
if (!isset($args->slug) || $args->slug !== $this->plugin_slug) {
|
|
return $result;
|
|
}
|
|
|
|
// Get plugin info from IGNY8 API
|
|
$info = $this->get_plugin_info();
|
|
|
|
if (!$info) {
|
|
return $result;
|
|
}
|
|
|
|
return (object) array(
|
|
'name' => $info['name'] ?? 'IGNY8 WordPress Bridge',
|
|
'slug' => $this->plugin_slug,
|
|
'version' => $info['version'] ?? $this->version,
|
|
'author' => '<a href="https://igny8.com">IGNY8</a>',
|
|
'author_profile' => 'https://igny8.com',
|
|
'homepage' => 'https://igny8.com',
|
|
'sections' => array(
|
|
'description' => $info['description'] ?? '',
|
|
'changelog' => $info['changelog'] ?? '',
|
|
),
|
|
'download_link' => $info['download_url'] ?? '',
|
|
'tested' => $info['tested'] ?? '',
|
|
'requires_php' => $info['requires_php'] ?? '7.4',
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Enqueue custom styles for plugin details popup
|
|
*/
|
|
public function enqueue_plugin_details_styles() {
|
|
// No custom styles needed
|
|
}
|
|
|
|
/**
|
|
* Get update information from IGNY8 API
|
|
*
|
|
* @return array|false Update info or false on failure
|
|
*/
|
|
private function get_update_info() {
|
|
$url = $this->api_url . $this->plugin_slug . '/check-update/';
|
|
|
|
$response = wp_remote_get($url, array(
|
|
'timeout' => 10,
|
|
'headers' => array(
|
|
'X-IGNY8-Site-ID' => get_option('igny8_site_id'),
|
|
'X-IGNY8-API-Key' => get_option('igny8_api_key'),
|
|
),
|
|
'body' => array(
|
|
'current_version' => $this->version,
|
|
),
|
|
));
|
|
|
|
if (is_wp_error($response)) {
|
|
return false;
|
|
}
|
|
|
|
$code = wp_remote_retrieve_response_code($response);
|
|
if ($code !== 200) {
|
|
return false;
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
$data = json_decode($body, true);
|
|
|
|
return $data ?: false;
|
|
}
|
|
|
|
/**
|
|
* Get plugin information from IGNY8 API
|
|
*
|
|
* @return array|false Plugin info or false on failure
|
|
*/
|
|
private function get_plugin_info() {
|
|
$url = $this->api_url . $this->plugin_slug . '/info/';
|
|
|
|
$response = wp_remote_get($url, array(
|
|
'timeout' => 10,
|
|
));
|
|
|
|
if (is_wp_error($response)) {
|
|
return false;
|
|
}
|
|
|
|
$code = wp_remote_retrieve_response_code($response);
|
|
if ($code !== 200) {
|
|
return false;
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
$data = json_decode($body, true);
|
|
|
|
return $data ?: false;
|
|
}
|
|
}
|