<?php
namespace Jet_Dashboard;
use Jet_Dashboard\Dashboard as Dashboard;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Define plugin updater class.
*
* @since 1.0.0
*/
class Plugin_Manager {
/**
* [$jet_banners_url description]
* @var string
*/
public $jet_banners_url = 'https://account.crocoblock.com/free-download/images/jetbanners/';
/**
* [$jet_changelog_url description]
* @var string
*/
public $jet_changelog_url = 'https://crocoblock.com/wp-content/uploads/jet-changelog/%s.json';
/**
* [$remote_plugin_data description]
* @var boolean
*/
public $remote_plugin_data = false;
/**
* [$user_plugins description]
* @var boolean
*/
public $user_plugins = false;
/**
* [$update_plugins description]
* @var boolean
*/
public $update_plugins = false;
/**
* [$registered_plugins description]
* @var boolean
*/
public $registered_plugins = false;
/**
* [$registered_plugins description]
* @var boolean
*/
public $registered_plugins_data = false;
/**
* Init class parameters.
*
* @since 1.0.0
* @param array $attr Input attributes array.
* @return void
*/
public function __construct() {
$registered_plugins = Dashboard::get_instance()->get_registered_plugins();
if ( ! empty( $registered_plugins ) ) {
foreach ( $registered_plugins as $plugin_file => $plugin_data ) {
add_filter( 'in_plugin_update_message-' . $plugin_file , array( $this, 'in_plugin_update_message' ), 10, 2 );
}
}
/**
* Need for test update - set_site_transient( 'update_plugins', null );
*/
add_action( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
add_action( 'admin_init', array( $this, 'generate_register_plugin_data' ) );
add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 3 );
add_filter( 'http_request_args', array( $this, 'allow_unsafe_urls' ) );
add_action( 'activated_plugin', array( $this, 'activate_plugin_handle' ), 10, 2 );
add_action( 'wp_ajax_jet_dashboard_plugin_action', array( $this, 'plugin_action' ) );
add_action( 'wp_ajax_jet_dashboard_wizard_plugin_action', array( $this, 'wizard_plugin_action' ) );
}
/**
* [admin_init description]
* @return [type] [description]
*/
public function generate_register_plugin_data() {
$registered_plugins = Dashboard::get_instance()->get_registered_plugins();
if ( ! empty( $registered_plugins ) ) {
foreach ( $registered_plugins as $plugin_file => $plugin_data ) {
$plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_file );
$this->registered_plugins_data[ $plugin_file ] = array(
'name' => $plugin_info['Name'],
'author' => $plugin_info['Author'],
'plugin_url' => $plugin_info['PluginURI'],
'requires' => '5.2',
'tested' => '',
'banners' => array(
'high' => sprintf( 'https://account.crocoblock.com/free-download/images/jetbanners/%s.png', $plugin_data['slug'] ),
'low' => sprintf( 'https://account.crocoblock.com/free-download/images/jetbanners/%s.png', $plugin_data['slug'] ),
),
'version' => false,
'changelog' => false,
'slug' => $plugin_data['slug'],
'transient_key' => $plugin_data['slug'] . '_plugin_info_data'
);
}
}
}
/**
* [plugins_api_filter description]
* @param [type] $_data [description]
* @param string $_action [description]
* @param [type] $_args [description]
* @return [type] [description]
*/
public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
if ( 'plugin_information' !== $_action ) {
return $_data;
}
if ( ! isset( $_args->slug ) ) {
return $_data;
}
$registered_plugin_data = false;
foreach ( $this->registered_plugins_data as $plugin_file => $plugin_data ) {
if ( $plugin_data['slug'] === $_args->slug ) {
$registered_plugin_data = $plugin_data;
break;
}
}
if ( ! $registered_plugin_data ) {
return $_data;
}
$plugin_api_data = get_site_transient( $registered_plugin_data['transient_key'] );
if ( empty( $plugin_api_data ) ) {
$changelog_remote_response = Dashboard::get_instance()->data_manager->changelog_remote_query( $registered_plugin_data['slug'] );
if ( ! $changelog_remote_response ) {
return $_data;
}
$plugin_api_data = new \stdClass();
$plugin_api_data->name = $registered_plugin_data['name'];
$plugin_api_data->slug = $registered_plugin_data['slug'];
$plugin_api_data->author = $registered_plugin_data['author'];
$plugin_api_data->homepage = $registered_plugin_data['plugin_url'];
$plugin_api_data->requires = $registered_plugin_data['requires'];
$plugin_api_data->tested = $registered_plugin_data['tested'];
$plugin_api_data->banners = $registered_plugin_data['banners'];
$plugin_api_data->version = $changelog_remote_response->current_version;
$plugin_api_data->sections = array(
'changelog' => $changelog_remote_response->changelog,
);
// Expires in 1 day
set_site_transient( $registered_plugin_data['transient_key'], $plugin_api_data, DAY_IN_SECONDS );
}
$_data = $plugin_api_data;
return $_data;
}
/**
* [changelog_remote_query description]
* @param [type] $slug [description]
* @return [type] [description]
*/
public function changelog_remote_query( $slug ) {
$response = wp_remote_get( sprintf( $this->jet_changelog_url, $slug ) );
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' ) {
return false;
}
$response = json_decode( $response['body'] );
return $response;
}
/**
* [plugin_row_meta description]
* @param [type] $plugin_meta [description]
* @param [type] $plugin_file [description]
* @param [type] $plugin_data [description]
* @return [type] [description]
*/
public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data ) {
if ( array_key_exists( $plugin_file, $this->registered_plugins_data ) && empty( $plugin_data['update'] ) ) {
$plugin_meta['view-details'] = sprintf( '<a href="%s" class="thickbox open-plugin-details-modal" aria-label="%s" data-title="%s">%s</a>',
esc_url( network_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $this->registered_plugins_data[ $plugin_file ]['slug'] . '&TB_iframe=true&width=600&height=550' ) ),
esc_attr( sprintf( __( 'More information about %s', 'jet-tricks' ), $this->registered_plugins_data[ $plugin_file ]['name'] ) ),
esc_attr( $this->registered_plugins_data[ $plugin_file ]['name'] ),
'View details'
);
}
return $plugin_meta;
}
/**
* [activate_plugin_handle description]
* @param [type] $plugin [description]
* @param [type] $network_wide [description]
* @return [type] [description]
*/
public function activate_plugin_handle( $plugin, $network_wide ) {
$jet_plugin_list = $this->get_remote_jet_plugin_list();
$is_jet_plugin = array_search( $plugin, array_column( $jet_plugin_list, 'slug' ) );
if ( ! $is_jet_plugin ) {
return false;
}
$query_url = add_query_arg(
array(
'action' => 'plugin_activate_action',
'license' => Utils::get_plugin_license_key( $plugin ),
'plugin' => $plugin,
'site_url' => urlencode( Utils::get_site_url() ),
),
Utils::get_api_url()
);
wp_remote_post( $query_url, array(
'timeout' => 30,
//'blocking' => false
) );
return false;
}
/**
* [plugin_row_meta description]
* @param [type] $plugin_meta [description]
* @param [type] $plugin_file [description]
* @param [type] $plugin_data [description]
* @return [type] [description]
*/
public function in_plugin_update_message( $plugin_data, $response ) {
if ( ! $response->package ) {
echo sprintf( ' <strong><a class="" href="%1$s">%2$s</a><strong>', Dashboard::get_instance()->get_dashboard_page_url( 'license-page', 'license-manager' ), 'Activate your license for automatic updates.' );
}
}
/**
* Process update.
*
* @since 1.0.0
* @param object $data Update data.
* @return object
*/
public function check_update( $data ) {
delete_site_transient( 'jet_dashboard_remote_jet_plugin_list' );
$registered_plugins = Dashboard::get_instance()->get_registered_plugins();
foreach ( $registered_plugins as $plugin_slug => $plugin_data ) {
$new_update_version = $this->check_new_update_version( $plugin_data );
if ( $new_update_version ) {
// Delete plugin api transient data
if ( ! empty( $this->registered_plugins_data ) && isset( $this->registered_plugins_data[ $plugin_data['file'] ] ) ) {
delete_site_transient( $this->registered_plugins_data[ $plugin_data['file'] ]['transient_key'] );
}
$update = new \stdClass();
$update->slug = $plugin_data['slug'];
$update->plugin = $plugin_data['file'];
$update->new_version = $new_update_version;
$update->url = false;
$update->package = Utils::package_url( $plugin_data['file'] );
$data->response[ $plugin_data['file'] ] = $update;
}
}
return $data;
}
/**
* [check_update description]
* @return [type] [description]
*/
public function check_new_update_version( $plugin_data = false ) {
$remote_plugin_data = $this->get_remote_jet_plugin_list();
if ( ! $remote_plugin_data || ! is_array( $remote_plugin_data ) ) {
return false;
}
$new_version = '1.0.0';
foreach ( $remote_plugin_data as $key => $plugin ) {
if ( $plugin_data['file'] === $plugin['slug'] ) {
$new_version = $plugin['version'];
break;
}
}
if ( version_compare( $plugin_data['version'], $new_version, '<' ) ) {
return $new_version;
}
Loading ...