first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,220 @@
<?php
namespace Jet_Dashboard;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Define Jet_Dashboard_License_Manager class
*/
class License_Manager {
/**
* [$slug description]
* @var boolean
*/
public $license_data_key = 'jet-license-data';
/**
* [$sys_messages description]
* @var array
*/
public $sys_messages = [];
/**
* Init page
*/
public function __construct() {
$this->sys_messages = apply_filters( 'jet_dashboard_license_sys_messages', array(
'internal' => 'Internal error. Please, try again later',
'server_error' => 'Server error. Please, try again later',
) );
add_action( 'wp_ajax_jet_license_action', array( $this, 'jet_license_action' ) );
$this->license_expire_check();
$this->maybe_theme_core_license_exist();
}
/**
* [maybe_theme_core_license_exist description]
* @return [type] [description]
*/
public function maybe_theme_core_license_exist() {
$jet_theme_core_key = get_option( 'jet_theme_core_license', false );
if ( ! $jet_theme_core_key ) {
return false;
}
$jet_theme_core_license_sync = get_option( 'jet_theme_core_sync', 'false' );
if ( filter_var( $jet_theme_core_license_sync, FILTER_VALIDATE_BOOLEAN ) ) {
return false;
}
$license_list = Utils::get_license_data( 'license-list', [] );
if ( array_key_exists( $jet_theme_core_key, $license_list ) ) {
return false;
}
$responce = $this->license_action_query( 'activate_license', $jet_theme_core_key );
$responce_data = isset( $responce['data'] ) ? $responce['data'] : [];
$license_list[ $jet_theme_core_key ] = array(
'licenseStatus' => 'active',
'licenseKey' => $jet_theme_core_key,
'licenseDetails' => $responce_data,
);
update_option( 'jet_theme_core_sync', 'true' );
if ( 'error' === $responce['status'] ) {
Utils::set_license_data( 'license-list', $license_list );
return false;
}
Utils::set_license_data( 'license-list', $license_list );
}
/**
* [license_expire_check description]
* @return [type] [description]
*/
public function license_expire_check() {
$jet_dashboard_license_expire_check = get_site_transient( 'jet_dashboard_license_expire_check' );
if ( $jet_dashboard_license_expire_check ) {
return false;
}
Utils::license_data_expire_sync();
set_site_transient( 'jet_dashboard_license_expire_check', 'true', HOUR_IN_SECONDS * 12 );
}
/**
* Proccesing subscribe form ajax
*
* @return void
*/
public function jet_license_action() {
$data = ( ! empty( $_POST['data'] ) ) ? $_POST['data'] : false;
if ( ! $data ) {
wp_send_json(
array(
'status' => 'error',
'message' => $this->sys_messages['server_error'],
'data' => [],
)
);
}
$license_action = $data['action'];
$license_key = $data['license'];
if ( empty( $license_key ) && isset( $data['plugin'] ) ) {
$license_key = Utils::get_plugin_license_key( $data['plugin'] );
}
$responce = $this->license_action_query( $license_action . '_license', $license_key );
$responce_data = [];
if ( 'error' === $responce['status'] ) {
wp_send_json(
array(
'status' => 'error',
'message' => $responce['message'],
'data' => isset( $responce['data'] ) ? $responce['data'] : [],
)
);
}
if ( isset( $responce['data'] ) ) {
$responce_data = $responce['data'];
}
switch ( $license_action ) {
case 'activate':
$this->update_license_list( $license_key, $responce_data );
break;
case 'deactivate':
$license_list = Utils::get_license_data( 'license-list', [] );
unset( $license_list[ $license_key ] );
Utils::set_license_data( 'license-list', $license_list );
break;
}
$responce_data['license_key'] = $license_key;
set_site_transient( 'update_plugins', null );
wp_send_json(
array(
'status' => 'success',
'message' => $responce['message'],
'data' => $responce_data,
)
);
}
/**
* [update_license_list description]
* @param boolean $responce [description]
* @return [type] [description]
*/
public function update_license_list( $license_key = '', $responce = false ) {
$license_list = Utils::get_license_data( 'license-list', [] );
$license_list[ $license_key ] = array(
'licenseStatus' => 'active',
'licenseKey' => $license_key,
'licenseDetails' => $responce,
);
Utils::set_license_data( 'license-list', $license_list );
}
/**
* Remote request to updater API.
*
* @since 1.0.0
* @return array|bool
*/
public function license_action_query( $action = '', $license = '' ) {
$query_url = add_query_arg(
array(
'action' => $action,
'license' => $license,
'site_url' => urlencode( Utils::get_site_url() ),
),
Utils::get_api_url()
);
$response = wp_remote_get( $query_url );
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' ) {
return false;
}
return json_decode( $response['body'], true );
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace Jet_Dashboard;
use Jet_Dashboard\Dashboard as Dashboard;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Define Jet_Dashboard_License_Manager class
*/
class Module_Manager {
/**
* Modules map
*
* @var array
*/
private $registered_modules = array();
/**
* [__construct description]
*/
public function __construct() {
add_action( 'init', array( $this, 'init_module' ), -996 );
}
/**
* [$modules description]
* @var array
*/
public function register_modules( $modules = array() ) {
if ( empty( $modules ) ) {
return;
}
foreach ( $modules as $module_slug => $module_class ) {
if ( ! array_key_exists( $module_slug, $this->registered_modules ) ) {
$this->registered_modules[ $module_slug ] = $module_class;
}
}
}
/**
* [get_registeredregistered_modules description]
* @param array $modules [description]
* @return [type] [description]
*/
public function get_registered_modules() {
return $this->registered_modules;
}
/**
* Initialize modules on aproppriate AJAX or on module page
*
* @return [type] [description]
*/
public function init_module() {
if ( wp_doing_ajax() ) {
$this->maybe_load_module_on_ajax();
} else {
$this->maybe_load_module();
}
}
/**
* Maybe load on ajax request
*
* @return [type] [description]
*/
public function maybe_load_module_on_ajax() {
$action = ! empty( $_REQUEST['action'] ) ? $_REQUEST['action'] : false;
if ( ! $action ) {
return;
}
$parts = explode( '/', $action );
if ( empty( $parts[1] ) || Dashboard::get_instance()->dashboard_slug !== $parts[0] ) {
return;
}
$module = $parts[1];
$this->load_module( $module );
}
/**
* Maybe load on regular request
*
* @return [type] [description]
*/
public function maybe_load_module() {
if ( ! Dashboard::get_instance()->is_dashboard_page() ) {
return;
}
$module = Dashboard::get_instance()->get_subpage();
$this->load_module( $module );
}
/**
* Load module by slug
*
* @param [type] $module [description]
* @return [type] [description]
*/
public function load_module( $module ) {
if ( ! isset( $this->registered_modules[ $module ] ) ) {
return;
}
$class_name = $this->registered_modules[ $module ];
return new $class_name();
}
}

View File

@@ -0,0 +1,114 @@
<?php
namespace Jet_Dashboard\Base;
use Jet_Dashboard\Dashboard as Dashboard;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
abstract class Module {
abstract public function get_slug();
public function __construct() {
$this->init();
add_action(
'jet-dashboard/before-enqueue-assets/' . $this->get_slug(),
array( $this, 'assets' )
);
add_action( 'wp_ajax_jet_dashboard/' . $this->get_slug(), array( $this, 'process_ajax' ) );
}
/**
* Initialize module-specific parts
*
* @return [type] [description]
*/
public function init() {}
/**
* Register module assets
*
* @return [type] [description]
*/
public function assets() {
$this->enqueue_module_assets();
add_filter( 'jet-dashboard/js-page-config', array( $this, 'page_config' ), 10, 2 );
add_filter( 'jet-dashboard/js-page-templates', array( $this, 'page_templates' ), 10, 2 );
}
/**
* Process ajax
*
* @return [type] [description]
*/
public function process_ajax() {
$handler = isset( $_REQUEST['handler'] ) ? $_REQUEST['handler'] : false;
if ( ! $handler || ! is_callable( array( $this, $handler ) ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array(
'message' => 'You are not allowed to access this',
) );
}
$nonce = isset( $_REQUEST['nonce'] ) ? esc_attr( $_REQUEST['nonce'] ) : false;
if ( ! $nonce || ! wp_verify_nonce( $nonce, Dashboard::get_instance()->get_dashboard_page_url() ) ) {
wp_send_json_error( array(
'message' => 'Nonce verfictaion failed',
) );
}
call_user_func( array( $this, $handler ) );
}
/**
* Enqueue module-specific assets
*
* @return void
*/
public function enqueue_module_assets() {}
/**
* Modify page config
*
* @param [type] $config [description]
* @return [type] [description]
*/
public function page_config( $config = array(), $subpage = '' ) {
return $config;
}
/**
* Add page templates
*
* @param [type] $config [description]
* @return [type] [description]
*/
public function page_templates( $templates = array(), $subpage = '' ) {
return $templates;
}
/**
* Returns link to current page
*
* @return [type] [description]
*/
public function get_page_link() {
return Dashboard::get_instance()->get_dashboard_page_url( $this->get_slug() );
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace Jet_Dashboard\Modules\License;
use Jet_Dashboard\Base\Module as Module_Base;
use Jet_Dashboard\Dashboard as Dashboard;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
class Module extends Module_Base {
/**
* [init description]
* @return [type] [description]
*/
public function init() {
add_action( 'admin_menu', array( $this, 'register_license_page' ), 22 );
}
/**
* [register_license_page description]
* @return [type] [description]
*/
public function register_license_page() {
}
/**
* Returns module slug
*
* @return void
*/
public function get_slug() {
return 'license-page';
}
/**
* Enqueue module-specific assets
*
* @return void
*/
public function enqueue_module_assets() {
wp_enqueue_script(
'jet-dashboard-license-page',
Dashboard::get_instance()->get_dashboard_url() . 'assets/js/license-page.js',
array( 'cx-vue-ui' ),
Dashboard::get_instance()->get_dashboard_version(),
true
);
}
/**
* License page config
*
* @param array $config [description]
* @param string $subpage [description]
* @return [type] [description]
*/
public function page_config( $config = array(), $subpage = '' ) {
$config['headerTitle'] = 'License Manager';
$config['page'] = 'license-page';
$config['wrapperCss'] = 'license-page';
return $config;
}
/**
* [page_templates description]
* @param array $templates [description]
* @param string $subpage [description]
* @return [type] [description]
*/
public function page_templates( $templates = array(), $subpage = '' ) {
$templates['license-page'] = 'license/main';
$templates['license-item'] = 'license/license-item';
$templates['plugin-item-installed'] = 'license/plugin-item-installed';
$templates['plugin-item-avaliable'] = 'license/plugin-item-avaliable';
$templates['plugin-item-more'] = 'license/plugin-item-more';
return $templates;
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Jet_Dashboard\Modules\Welcome;
use Jet_Dashboard\Base\Module as Module_Base;
use Jet_Dashboard\Dashboard as Dashboard;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
class Module extends Module_Base {
/**
* Returns module slug
*
* @return void
*/
public function get_slug() {
return 'welcome-page';
}
/**
* Enqueue module-specific assets
*
* @return void
*/
public function enqueue_module_assets() {
wp_enqueue_script(
'jet-dashboard-welcome-page',
Dashboard::get_instance()->get_dashboard_url() . 'assets/js/welcome-page.js',
array( 'cx-vue-ui' ),
Dashboard::get_instance()->get_dashboard_version(),
true
);
}
/**
* License page config
*
* @param array $config [description]
* @param string $subpage [description]
* @return [type] [description]
*/
public function page_config( $config = array(), $subpage = '' ) {
$config['headerTitle'] = 'Welcome';
$config['page'] = 'welcome-page';
$config['wrapperCss'] = 'welcome-page';
return $config;
}
/**
* Add welcome component template
*
* @param array $templates [description]
* @param string $subpage [description]
* @return [type] [description]
*/
public function page_templates( $templates = array(), $subpage = '' ) {
$templates['welcome-page'] = 'welcome/main';
return $templates;
}
}

View File

@@ -0,0 +1,855 @@
<?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() {
add_action( 'wp_ajax_jet_dashboard_plugin_action', array( $this, 'plugin_action' ) );
$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 );
}
/**
* [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 = $this->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;
}
/**
* [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( '&nbsp;<strong><a class="" href="%1$s">%2$s</a><strong>', Dashboard::get_instance()->get_dashboard_page_url(), '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;
}
return false;
}
/**
* Remote request to updater API.
*
* @since 1.0.0
* @return array|bool
*/
public function get_remote_jet_plugin_list() {
$remote_jet_plugin_list = get_site_transient( 'jet_dashboard_remote_jet_plugin_list' );
if ( $remote_jet_plugin_list ) {
return $remote_jet_plugin_list;
}
$query_url = add_query_arg(
array(
'action' => 'get_plugins_data',
),
Utils::get_api_url()
);
$response = wp_remote_get( $query_url );
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' ) {
return false;
}
$response = json_decode( $response['body'], true );
if ( 'error' === $response['status'] ) {
return false;
}
if ( ! isset( $response['data'] ) ) {
return false;
}
$remote_jet_plugin_list = $response['data'];
set_site_transient( 'jet_dashboard_remote_jet_plugin_list', $remote_jet_plugin_list, HOUR_IN_SECONDS * 12 );
return $remote_jet_plugin_list;
}
/**
* [get_plugin_list description]
* @return [type] [description]
*/
public function get_plugin_data_list() {
$jet_plugin_list = $this->get_remote_jet_plugin_list();
$user_plugin_list = $this->get_user_plugins();
$registered_plugins = Dashboard::get_instance()->get_registered_plugins();
$plugins_list = [];
if ( ! empty( $jet_plugin_list ) ) {
foreach ( $jet_plugin_list as $key => $plugin_data ) {
$plugin_slug = $plugin_data['slug'];
if ( array_key_exists( $plugin_slug, $user_plugin_list ) ) {
$plugin_data = wp_parse_args( $plugin_data, $user_plugin_list[ $plugin_slug ] );
} else {
$plugin_data = wp_parse_args( $plugin_data, array(
'version' => $plugin_data['version'],
'currentVersion' => $plugin_data['version'],
'updateAvaliable' => false,
'isActivated' => false,
'isInstalled' => false,
) );
}
$plugin_data['licenseControl'] = array_key_exists( $plugin_slug, $registered_plugins ) ? true : false;
$plugins_list[ $plugin_data['slug'] ] = $plugin_data;
}
}
return $plugins_list;
}
/**
* [get_plugin_data description]
* @return [type] [description]
*/
public function get_user_plugins() {
if ( ! $this->user_plugins ) {
$this->user_plugins = get_plugins();
}
$plugin_list = array();
if ( $this->user_plugins ) {
foreach ( $this->user_plugins as $plugin_file => $plugin_data ) {
$current_version = $plugin_data['Version'];
$latest_version = $this->get_latest_version( $plugin_file );
$plugin_list[ $plugin_file ] = array(
'version' => $latest_version,
'currentVersion' => $current_version,
'updateAvaliable' => version_compare( $latest_version, $current_version, '>' ),
'isActivated' => is_plugin_active( $plugin_file ),
'isInstalled' => true,
);
}
}
return $plugin_list;
}
/**
* [get_installed_plugin_data description]
* @param [type] $plugin [description]
* @return [type] [description]
*/
public function get_installed_plugin_data( $plugin_file ) {
if ( ! $this->user_plugins ) {
$this->user_plugins = get_plugins();
}
$plugin_data = $this->user_plugins[ $plugin_file ];
$current_version = $plugin_data['Version'];
$latest_version = $this->get_latest_version( $plugin_file );
return array(
'version' => $latest_version,
'currentVersion' => $current_version,
'updateAvaliable' => version_compare( $latest_version, $current_version, '>' ),
'isActivated' => is_plugin_active( $plugin_file ),
'isInstalled' => true,
);
}
/**
* Get latest version for passed plugin
*
* @param [type] $remote_plugin_data [description]
* @return [type] [description]
*/
public function get_latest_version( $plugin_file ) {
if ( ! $this->update_plugins ) {
$this->update_plugins = get_site_transient( 'update_plugins' );
}
$no_update = isset( $this->update_plugins->no_update ) ? $this->update_plugins->no_update : false;
$to_update = isset( $this->update_plugins->response ) ? $this->update_plugins->response : false;
if ( $to_update && ! empty( $to_update ) && array_key_exists( $plugin_file, $to_update ) ) {
$version = $to_update[ $plugin_file ]->new_version;
} elseif ( ! empty( $no_update ) && array_key_exists( $plugin_file, $no_update ) ) {
$version = $no_update[ $plugin_file ]->new_version;
} elseif ( array_key_exists( $plugin_file, $this->user_plugins ) ) {
$version = $this->user_plugins[ $plugin_file ]['Version'];
} else {
$version = '1.0.0';
}
return $version;
}
/**
* [install_plugin description]
* @param [type] $plugin [description]
* @param boolean $plugin_url [description]
* @return [type] [description]
*/
public function plugin_action() {
$data = ( ! empty( $_POST['data'] ) ) ? $_POST['data'] : false;
if ( ! $data ) {
wp_send_json(
array(
'status' => 'error',
'message' => $this->sys_messages['server_error']
)
);
}
$action = $data['action'];
$plugin = $data['plugin'];
switch ( $action ) {
case 'install':
$this->install_plugin( $plugin );
break;
case 'activate':
$this->activate_plugin( $plugin );
break;
case 'deactivate':
$this->deactivate_plugin( $plugin );
break;
case 'update':
$this->update_plugin( $plugin );
break;
}
wp_send_json(
array(
'status' => 'success',
'message' => 'Success',
'data' => [],
)
);
}
/**
* Perform plugin installtion by passed plugin slug and plugin package URL (optional)
*
* @param [type] $plugin [description]
* @param boolean $plugin_url [description]
* @return [type] [description]
*/
public function install_plugin( $plugin_file ) {
$status = array();
if ( ! current_user_can( 'install_plugins' ) ) {
wp_send_json(
array(
'status' => 'error',
'message' => 'Sorry, you are not allowed to install plugins on this site.'
)
);
}
if ( ! $plugin_file ) {
wp_send_json(
array(
'status' => 'error',
'message' => 'Plugin slug is required'
)
);
}
$package = Utils::package_url( $plugin_file );
include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
$skin = new \WP_Ajax_Upgrader_Skin();
$upgrader = new \Plugin_Upgrader( $skin );
$result = $upgrader->install( $package );
if ( is_wp_error( $result ) ) {
$status['errorCode'] = $result->get_error_code();
$status['errorMessage'] = $result->get_error_message();
wp_send_json(
array(
'status' => 'error',
'message' => $result->get_error_message(),
'data' => [],
)
);
} elseif ( is_wp_error( $skin->result ) ) {
$status['errorCode'] = $skin->result->get_error_code();
$status['errorMessage'] = $skin->result->get_error_message();
wp_send_json(
array(
'status' => 'error',
'message' => $skin->result->get_error_message(),
'data' => [],
)
);
} elseif ( $skin->get_errors()->get_error_code() ) {
$status['errorMessage'] = $skin->get_error_messages();
wp_send_json(
array(
'status' => 'error',
'message' => $skin->get_error_messages(),
'data' => [],
)
);
} elseif ( is_null( $result ) ) {
global $wp_filesystem;
$status['errorMessage'] = 'Unable to connect to the filesystem. Please confirm your credentials.';
// Pass through the error from WP_Filesystem if one was raised.
if ( $wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
}
wp_send_json(
array(
'status' => 'error',
'message' => $status['errorMessage'],
'data' => [],
)
);
}
wp_send_json(
array(
'status' => 'success',
'message' => 'The plugin has been Installed',
'data' => $this->get_installed_plugin_data( $plugin_file ),
)
);
}
/**
* Performs plugin activation
*
* @param [type] $plugin [description]
* @return [type] [description]
*/
public function activate_plugin( $plugin_file ) {
$status = array();
if ( ! current_user_can( 'activate_plugins' ) ) {
wp_send_json(
array(
'status' => 'error',
'message' => 'Sorry, you are not allowed to install plugins on this site.'
)
);
}
if ( ! $plugin_file ) {
wp_send_json(
array(
'status' => 'error',
'message' => 'Plugin slug is required'
)
);
}
$activate = null;
if ( ! is_plugin_active( $plugin_file ) ) {
$activate = activate_plugin( $plugin_file );
}
if ( is_wp_error( $activate ) ) {
wp_send_json(
array(
'status' => 'error',
'message' => $activate->get_error_message(),
)
);
}
wp_send_json(
array(
'status' => 'success',
'message' => 'The plugin has been activated',
'data' => $this->get_installed_plugin_data( $plugin_file ),
)
);
}
/**
* Performs plugin activation
*
* @param [type] $plugin [description]
* @return [type] [description]
*/
public function deactivate_plugin( $plugin_file ) {
$status = array();
if ( ! current_user_can( 'activate_plugins' ) ) {
wp_send_json(
array(
'status' => 'error',
'message' => 'Sorry, you are not allowed to install plugins on this site.'
)
);
}
if ( ! $plugin_file ) {
wp_send_json(
array(
'status' => 'error',
'message' => 'Plugin slug is required'
)
);
}
$deactivate_handler = null;
if ( is_plugin_active( $plugin_file ) ) {
$deactivate_handler = deactivate_plugins( $plugin_file );
}
if ( is_wp_error( $deactivate_handler ) ) {
wp_send_json(
array(
'status' => 'error',
'message' => $deactivate_handler->get_error_message(),
)
);
}
wp_send_json(
array(
'status' => 'success',
'message' => 'The plugin has been deactivated',
'data' => $this->get_installed_plugin_data( $plugin_file ),
)
);
}
/**
* [update_plugin description]
* @param [type] $plugin_slug [description]
* @return [type] [description]
*/
public function update_plugin( $plugin_file ) {
if ( ! $plugin_file ) {
wp_send_json(
array(
'status' => 'error',
'message' => 'Plugin slug is required'
)
);
}
$plugin = plugin_basename( sanitize_text_field( wp_unslash( $plugin_file ) ) );
$slug = dirname( $plugin );
$status = array(
'update' => 'plugin',
'slug' => $slug,
'oldVersion' => '',
'newVersion' => '',
);
if ( ! current_user_can( 'update_plugins' ) || 0 !== validate_file( $plugin ) ) {
wp_send_json(
array(
'status' => 'error',
'message' => 'Sorry, you are not allowed to update plugins for this site.',
)
);
}
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
wp_update_plugins();
$skin = new \WP_Ajax_Upgrader_Skin();
$upgrader = new \Plugin_Upgrader( $skin );
$result = $upgrader->bulk_upgrade( array( $plugin ) );
$upgrade_messages = [];
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$upgrade_messages = $skin->get_upgrade_messages();
}
if ( is_wp_error( $skin->result ) ) {
wp_send_json(
array(
'status' => 'error',
'message' => $skin->result->get_error_message(),
'debug' => $upgrade_messages,
)
);
} elseif ( $skin->get_errors()->get_error_code() ) {
wp_send_json(
array(
'status' => 'error',
'message' => $skin->get_error_messages(),
'debug' => $upgrade_messages,
)
);
} elseif ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) {
$plugin_update_data = current( $result );
/*
* If the `update_plugins` site transient is empty (e.g. when you update
* two plugins in quick succession before the transient repopulates),
* this may be the return.
*
* Preferably something can be done to ensure `update_plugins` isn't empty.
* For now, surface some sort of error here.
*/
if ( true === $plugin_update_data ) {
wp_send_json(
array(
'status' => 'error',
'message' => 'Plugin update failed.',
'debug' => $upgrade_messages,
)
);
}
$plugin_data = get_plugins( '/' . $result[ $plugin ]['destination_name'] );
$plugin_data = reset( $plugin_data );
wp_send_json(
array(
'status' => 'success',
'message' => 'The plugin has been updated',
'data' => $this->get_installed_plugin_data( $plugin_file ),
)
);
} elseif ( false === $result ) {
global $wp_filesystem;
$errorMessage = 'Unable to connect to the filesystem. Please confirm your credentials.';
// Pass through the error from WP_Filesystem if one was raised.
if ( $wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
$errorMessage = esc_html( $wp_filesystem->errors->get_error_message() );
}
wp_send_json(
array(
'status' => 'error',
'message' => $errorMessage,
)
);
}
wp_send_json(
array(
'status' => 'error',
'message' => 'Plugin update failed.',
)
);
}
}

View File

@@ -0,0 +1,223 @@
<?php
namespace Jet_Dashboard;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Define Jet_Dashboard_Utils class
*/
class Utils {
/**
* [$api_url description]
* @var string
*/
public static $api_url = 'https://api.crocoblock.com';
/**
* [$settings description]
* @var null
*/
public static $license_data = null;
/**
* [$license_data_key description]
* @var string
*/
public static $license_data_key = 'jet-license-data';
/**
* [get_api_url description]
* @return [type] [description]
*/
public static function get_api_url() {
return apply_filters( 'jet-dashboard/license/api-url', self::$api_url );
}
/**
* [get_site_url description]
* @return [type] [description]
*/
public static function get_site_url() {
$urlParts = parse_url( site_url( '/' ) );
$site_url = $urlParts['host'] . $urlParts['path'];
$site_url = preg_replace('#^https?://#', '', rtrim( $site_url ));
return $site_url;
}
/**
* [get description]
* @param [type] $setting [description]
* @param boolean $default [description]
* @return [type] [description]
*/
public static function get_license_data( $setting = false, $default = false ) {
if ( ! $setting ) {
return get_option( self::$license_data_key, array() );
}
if ( null === self::$license_data ) {
self::$license_data = get_option( self::$license_data_key, array() );
}
return isset( self::$license_data[ $setting ] ) ? self::$license_data[ $setting ] : $default;
}
/**
* [set_license_data description]
* @param [type] $setting [description]
* @param boolean $value [description]
*/
public static function set_license_data( $setting = false, $value = false ) {
$current_license_data = get_option( self::$license_data_key, array() );
$current_license_data[ $setting ] = $value;
update_option( self::$license_data_key, $current_license_data );
}
/**
* [get_license_list description]
* @return [type] [description]
*/
public static function get_license_list() {
$license_list = self::get_license_data( 'license-list', [] );
return $license_list;
}
/**
* [license_data_expire_sync description]
* @return [type] [description]
*/
public static function license_data_expire_sync() {
$license_list = self::get_license_data( 'license-list', [] );
if ( ! empty( $license_list ) ) {
foreach ( $license_list as $license_key => $license_data ) {
$license_details = $license_data['licenseDetails'];
$is_expired = ( 'expired' === $license_data['licenseStatus'] ) ? true : false;
if ( ! empty( $license_details ) ) {
$is_expired = self::license_expired_check( $license_details['expire'] );
}
if ( $is_expired ) {
$license_list[$license_key]['licenseStatus'] = 'expired';
}
}
}
self::set_license_data( 'license-list', $license_list );
}
/**
* [get_plugin_license_key description]
* @param boolean $setting [description]
* @param boolean $value [description]
* @return [type] [description]
*/
public static function get_plugin_license_key( $plugin_slug ) {
$license_list = self::get_license_data( 'license-list', [] );
$plugin_license_key = false;
if ( ! empty( $license_list ) ) {
foreach ( $license_list as $license_key => $license_data ) {
if ( 'expired' === $license_data['licenseStatus'] ) {
continue;
}
$license_details = $license_data['licenseDetails'];
if ( empty( $license_details ) ) {
continue;
}
$is_expired = self::license_expired_check( $license_details['expire'] );
if ( $is_expired ) {
$license_list[$license_key]['licenseStatus'] = 'expired';
continue;
}
$license_plugins = $license_details['plugins'];
if ( array_key_exists( $plugin_slug, $license_plugins ) ) {
$plugin_license_key = $license_key;
break;
}
}
}
if ( $plugin_license_key ) {
return $plugin_license_key;
}
return false;
}
/**
* [package_url description]
* @param [type] $key [description]
* @return [type] [description]
*/
public static function package_url( $plugin_slug = false ) {
$license_key = self::get_plugin_license_key( $plugin_slug );
if ( ! $license_key ) {
return false;
}
return add_query_arg(
array(
'action' => 'get_plugin_update',
'license' => self::get_plugin_license_key( $plugin_slug ),
'plugin' => $plugin_slug,
'site_url' => urlencode( self::get_site_url() ),
),
self::get_api_url()
);
}
/**
* [if_license_expire_check description]
* @param boolean $expire_date [description]
* @return [type] [description]
*/
public static function license_expired_check( $expire_date = false ) {
if ( '0000-00-00 00:00:00' === $expire_date || 'lifetime' === $expire_date ) {
return false;
}
$current_time = time( 'Y-m-d H:i:s' );
$expire_time = strtotime( $expire_date );
if ( $current_time > $expire_time ) {
return true;
}
return false;
}
}