File: /www/wwwroot/q.autos58.cn/wp-content/plugins/link-manager/includes/class-lm-updater.php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class LM_Updater {
/** @var string */
private $plugin_slug;
/** @var string */
private $plugin_basename;
public function __construct() {
$this->plugin_slug = 'link-manager';
$this->plugin_basename = plugin_basename( LM_PLUGIN_FILE );
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_for_update' ) );
add_filter( 'plugins_api', array( $this, 'plugin_info' ), 10, 3 );
add_action( 'upgrader_process_complete', array( $this, 'post_update' ), 10, 2 );
}
/**
* Check the panel for a newer plugin version.
*
* @param object $transient
* @return object
*/
public function check_for_update( $transient ) {
if ( empty( $transient->checked ) ) {
return $transient;
}
$panel_url = get_option( 'lm_panel_url', '' );
if ( empty( $panel_url ) ) {
return $transient;
}
$token = get_option( 'lm_site_token', '' );
$response = wp_remote_get(
trailingslashit( $panel_url ) . 'api/plugin/version',
array(
'timeout' => 10,
'headers' => array(
'X-Site-Token' => $token,
),
)
);
if ( is_wp_error( $response ) ) {
return $transient;
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $body ) || empty( $body['version'] ) ) {
return $transient;
}
$remote_version = $body['version'];
if ( version_compare( LM_VERSION, $remote_version, '<' ) ) {
$transient->response[ $this->plugin_basename ] = (object) array(
'slug' => $this->plugin_slug,
'plugin' => $this->plugin_basename,
'new_version' => $remote_version,
'url' => $panel_url,
'package' => trailingslashit( $panel_url ) . 'api/plugin/download?token=' . urlencode( $token ),
'tested' => $body['tested'] ?? '',
'requires' => $body['requires'] ?? '5.8',
);
}
return $transient;
}
/**
* Provide plugin information for the WordPress plugin details modal.
*
* @param false|object|array $result
* @param string $action
* @param object $args
* @return false|object
*/
public function plugin_info( $result, $action, $args ) {
if ( $action !== 'plugin_information' || ( $args->slug ?? '' ) !== $this->plugin_slug ) {
return $result;
}
$panel_url = get_option( 'lm_panel_url', '' );
if ( empty( $panel_url ) ) {
return $result;
}
$token = get_option( 'lm_site_token', '' );
$response = wp_remote_get(
trailingslashit( $panel_url ) . 'api/plugin/version',
array(
'timeout' => 10,
'headers' => array(
'X-Site-Token' => $token,
),
)
);
if ( is_wp_error( $response ) ) {
return $result;
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $body ) ) {
return $result;
}
return (object) array(
'name' => 'LinkManager',
'slug' => $this->plugin_slug,
'version' => $body['version'] ?? LM_VERSION,
'author' => 'LinkManager',
'homepage' => $panel_url,
'download_link' => trailingslashit( $panel_url ) . 'api/plugin/download?token=' . urlencode( $token ),
'requires' => $body['requires'] ?? '5.8',
'tested' => $body['tested'] ?? '',
'sections' => array(
'description' => $body['description'] ?? 'Link network management plugin.',
'changelog' => $body['changelog'] ?? '',
),
);
}
/**
* Run post-update tasks after the plugin has been upgraded.
*
* @param WP_Upgrader $upgrader
* @param array $options
*/
public function post_update( $upgrader, $options ) {
if (
$options['action'] !== 'update' ||
$options['type'] !== 'plugin' ||
empty( $options['plugins'] )
) {
return;
}
if ( ! in_array( $this->plugin_basename, $options['plugins'], true ) ) {
return;
}
// Clear update transient so WP re-checks.
delete_site_transient( 'update_plugins' );
// Re-run heartbeat to notify panel of new version.
$heartbeat = new LM_Heartbeat();
$heartbeat->send_heartbeat();
}
}