theme_slug . '-welcome-screen', get_template_directory_uri() . '/inc/welcome/css/welcome.css' );
wp_enqueue_script( $this->theme_slug . '-welcome-screen', get_template_directory_uri() . '/inc/welcome/js/welcome.js', array( 'jquery' ) );
wp_localize_script( $this->theme_slug . '-welcome-screen', 'SmWelcomeObject', array(
'admin_nonce' => wp_create_nonce( 'plugin_installer_nonce'),
'activate_nonce' => wp_create_nonce( 'plugin_activate_nonce'),
'deactivate_nonce' => wp_create_nonce( 'plugin_deactivate_nonce'),
'ajaxurl' => esc_url( admin_url( 'admin-ajax.php' ) ),
'activate_btn' => $this->strings['activate_btn'],
'installed_btn' => $this->strings['installed_btn'],
'demo_installing' => $this->strings['demo_installing'],
'demo_installed' => $this->strings['demo_installed'],
'demo_confirm' => $this->strings['demo_confirm'],
) );
}
/** Plugin API **/
public function call_plugin_api( $plugin ) {
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
$call_api = plugins_api( 'plugin_information', array(
'slug' => $plugin,
'fields' => array(
'downloaded' => false,
'rating' => false,
'description' => false,
'short_description' => true,
'donate_link' => false,
'tags' => false,
'sections' => true,
'homepage' => true,
'added' => false,
'last_updated' => false,
'compatibility' => false,
'tested' => false,
'requires' => false,
'downloadlink' => false,
'icons' => true
)
) );
return $call_api;
}
/** Check For Icon **/
public function check_for_icon( $arr ) {
if ( ! empty( $arr['svg'] ) ) {
$plugin_icon_url = $arr['svg'];
} elseif ( ! empty( $arr['2x'] ) ) {
$plugin_icon_url = $arr['2x'];
} elseif ( ! empty( $arr['1x'] ) ) {
$plugin_icon_url = $arr['1x'];
} else {
$plugin_icon_url = $arr['default'];
}
return $plugin_icon_url;
}
/** Check if Plugin is active or not **/
public function get_plugin_active($plugin) {
$folder_name = $plugin['slug'];
$file_name = $plugin['filename'];
$class = '';
if( isset($plugin['class']) ){
$class = $plugin['class'];
}
$function = '';
if( isset($plugin['function']) ){
$function = $plugin['function'];
}
$status = 'install';
$path = WP_PLUGIN_DIR.'/'.esc_attr($folder_name).'/'.esc_attr($file_name);
if( file_exists( $path ) ) {
if($class){
$status = class_exists( $class ) ? 'inactive' : 'active';
}elseif($function){
$status = function_exists( $function ) ? 'inactive' : 'active';
}
}
return $status;
}
/** Generate Url for the Plugin Button **/
public function generate_plugin_url($status, $plugin) {
$folder_name = $plugin['slug'];
$file_name = $plugin['filename'];
switch ( $status ) {
case 'install':
return wp_nonce_url(
add_query_arg(
array(
'action' => 'install-plugin',
'plugin' => esc_attr($folder_name)
),
network_admin_url( 'update.php' )
),
'install-plugin_' . esc_attr($folder_name)
);
break;
case 'inactive':
return '#';
break;
case 'active':
return '#';
break;
}
}
/* ========== Plugin Installation Ajax =========== */
public function plugin_installer_callback(){
if ( ! current_user_can('install_plugins') ) {
wp_die( esc_html__( 'Sorry, you are not allowed to install plugins on this site.', 'accesspress-basic' ) );
}
$nonce = isset( $_POST["nonce"] ) ? sanitize_text_field( wp_unslash( $_POST["nonce"] ) ) : '';
$plugin = isset( $_POST["plugin"] ) ? sanitize_text_field( wp_unslash( $_POST["plugin"] ) ) : '';
$plugin_file = isset( $_POST["plugin_file"] ) ? sanitize_text_field( wp_unslash( $_POST["plugin_file"] ) ) : '';
$download_link = isset( $_POST["download_link"] ) ? sanitize_text_field( wp_unslash( $_POST["download_link"] ) ) : '';
// Check our nonce, if they don't match then bounce!
if (! wp_verify_nonce( $nonce, 'plugin_installer_nonce' )) {
wp_die( esc_html__( 'Error - unable to verify nonce, please try again.', 'accesspress-basic') );
}
// Include required libs for installation
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
// Get Plugin Info
$api = $this->call_plugin_api($plugin);
$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$upgrader->install($api->download_link);
$plugin_file = esc_html($plugin).'/'.esc_html($plugin_file);
if($api->name) {
if($plugin_file) {
activate_plugin($plugin_file);
echo 'success';
die();
}
}else{
$upgrader->install($download_link);
$plugin_file = ABSPATH . 'wp-content/plugins/'.esc_html($plugin).'/'.esc_html($plugin_file);
if(file_exists($plugin_file)) {
activate_plugin($plugin_file);
echo "success";
die();
}
}
echo 'fail';
die();
}
/** Plugin Offline Installation Ajax **/
public function plugin_offline_installer_callback() {
$plugin = array();
$file_location = $plugin['location'] = isset( $_POST['file_location'] ) ? sanitize_text_field( wp_unslash( $_POST['file_location'] ) ) : '';
$file = isset( $_POST['file'] ) ? sanitize_text_field( wp_unslash( $_POST['file'] ) ) : '';
$host_type = isset( $_POST['host_type'] ) ? sanitize_text_field( wp_unslash( $_POST['host_type'] ) ) : '';
$plugin_class = $plugin['class'] = isset( $_POST['class_name'] ) ? sanitize_text_field( wp_unslash( $_POST['class_name'] ) ) : '';
$plugin_slug = $plugin['slug'] = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
$plugin_directory = ABSPATH . 'wp-content/plugins/';
$plugin_file = $plugin_slug . '/' . $file;
if( $host_type == 'remote' ) {
$file_location = $this->get_local_dir_path($plugin);
}
$zip = new ZipArchive();
if ($zip->open($file_location) === TRUE) {
$zip->extractTo($plugin_directory);
$zip->close();
activate_plugin($plugin_file);
if( $host_type == 'remote' ) {
unlink($file_location);
}
echo 'success';
die();
} else {
echo 'failed';
}
die();
}
/** Plugin Offline Activation Ajax **/
public function plugin_activation_callback() {
$plugin = isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : '';
$plugin_file = isset( $_POST['plugin_file'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_file'] ) ) : '';
$plugin_file = ABSPATH . 'wp-content/plugins/'.esc_html($plugin).'/'.esc_html($plugin_file);
if(file_exists($plugin_file)) {
activate_plugin($plugin_file);
echo "success";
} else {
echo esc_html__('Plugin Does not Exists' , 'accesspress-basic');
}
die();
}
/** Plugin Offline Activation Ajax **/
public function plugin_deactivation_callback() {
$plugin = isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : '';
$plugin_file = isset( $_POST['plugin_file'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_file'] ) ) : '';
$plugin_file = ABSPATH . 'wp-content/plugins/'.esc_html($plugin).'/'.esc_html($plugin_file);
if(file_exists($plugin_file)) {
deactivate_plugins($plugin_file);
echo "success";
} else {
echo esc_html__('Plugin Does not Exists' , 'accesspress-basic');
}
die();
}
public function all_required_plugins_installed() {
$companion_plugins = $this->companion_plugins;
$show_success_notice = false;
foreach($companion_plugins as $plugin) {
$path = WP_PLUGIN_DIR.'/'.esc_attr($plugin['slug']).'/'.esc_attr($plugin['filename']);
if(file_exists($path)) {
if(class_exists($plugin['class'])) {
$show_success_notice = true;
} else {
$show_success_notice = false;
break;
}
} else {
$show_success_notice = false;
break;
}
}
return $show_success_notice;
}
public function get_local_dir_path($plugin) {
$upload_dir = wp_upload_dir();
$file_location = $file_location = $upload_dir['path'] . '/' . $plugin['slug'].'.zip';
if( file_exists( $file_location ) || class_exists( $plugin['class'] ) ) {
return $file_location;
}
$url = wp_nonce_url(admin_url('themes.php?page=' . $this->theme_slug . '-welcome§ion=actions_required'),'remote-file-installation');
if (false === ($creds = request_filesystem_credentials($url, '', false, false, null) ) ) {
return; // stop processing here
}
if ( ! WP_Filesystem($creds) ) {
request_filesystem_credentials($url, '', true, false, null);
return;
}
global $wp_filesystem;
$file = $wp_filesystem->get_contents( $plugin['location'] );
$wp_filesystem->put_contents( $file_location, $file, FS_CHMOD_FILE );
return $file_location;
}
}
endif;
?>