'application/xml'
) );
return $mimes;
}
/**
* Get demos data to add them in the Demo Import and Pro Demos plugins
*
*
*/
public static function get_demos_data() {
if( !current_user_can('install_plugins') )
die('Access Denied. You do not have required capability.');
// $git_url = 'https://raw.githubusercontent.com/WPaccesskeys/WPaccesskeys.github.io/master/theme-demos/'.get_template().'-demos/config.json';
$config_url = 'https://accesspressthemes.com/theme-demos/'.get_template().'-demos/config.json';
$config_url = apply_filters('adi_config_location', $config_url );
$data = ADI_Demos_Helpers::get_remote( $config_url );
if ( is_wp_error( $data ) ) {
return $data;
}
$data = json_decode( $data,true );
if($data == ''){
$data = array();
}
// Return
return apply_filters( 'adi_demos_data', $data );
}
/**
* Get the category list of all categories used in the predefined demo imports array.
*
*
*/
public static function get_demo_all_categories( $demo_imports ) {
$categories = array();
foreach ( $demo_imports as $item ) {
if ( ! empty( $item['categories'] ) && is_array( $item['categories'] ) ) {
foreach ( $item['categories'] as $category ) {
$categories[ sanitize_key( $category ) ] = $category;
}
}
}
if ( empty( $categories ) ) {
return false;
}
return $categories;
}
/**
* Return the concatenated string of demo import item categories.
* These should be separated by comma and sanitized properly.
*
*
*/
public static function get_demo_item_categories( $item ) {
$sanitized_categories = array();
if ( isset( $item['categories'] ) ) {
foreach ( $item['categories'] as $category ) {
$sanitized_categories[] = sanitize_key( $category );
}
}
if ( ! empty( $sanitized_categories ) ) {
return implode( ',', $sanitized_categories );
}
return false;
}
/**
* Demos popup
*
*
*/
public static function popup() {
global $pagenow;
?>
$plugin ) {
$api = array(
'slug' => isset( $plugin['slug'] ) ? $plugin['slug'] : '',
'init' => isset( $plugin['init'] ) ? $plugin['init'] : '',
'name' => isset( $plugin['name'] ) ? $plugin['name'] : '',
'class' => isset( $plugin['class'] ) ? $plugin['class'] : '',
);
if ( ! is_wp_error( $api ) ) { // confirm error free
// Installed but Inactive.
if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin['init'] ) && is_plugin_inactive( $plugin['init'] ) ) {
$button_classes = 'button activate-now button-primary';
$button_text = esc_html__( 'Activate', 'access-demo-importer' );
// Not Installed.
} elseif ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin['init'] ) ) {
$button_classes = 'button install-now';
$button_text = esc_html__( 'Install Now', 'access-demo-importer' );
// Active.
} else {
$button_classes = 'button disabled';
$button_text = esc_html__( 'Activated', 'access-demo-importer' );
} ?>
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();
}
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=demo-importer'),'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;
}
/**
* Required plugins activate
*
*
*/
public function ajax_required_plugins_activate() {
check_ajax_referer( 'adi_activate_req_plugin_nonce', 'security' );
if ( ! current_user_can( 'install_plugins' ) || ! isset( $_POST['init'] ) || ! $_POST['init'] ) {
wp_send_json_error(
array(
'success' => false,
'message' => __( 'No plugin specified', 'access-demo-importer' ),
)
);
}
$plugin_init = ( isset( $_POST['init'] ) ) ? sanitize_text_field( $_POST['init'] ) : '';
$activate = activate_plugin( $plugin_init, '', false, true );
if ( is_wp_error( $activate ) ) {
wp_send_json_error(
array(
'success' => false,
'message' => $activate->get_error_message(),
)
);
}
wp_send_json_success(
array(
'success' => true,
'message' => __( 'Plugin Successfully Activated', 'access-demo-importer' ),
)
);
}
/**
* Returns an array containing all the importable content
*
*
*/
public function ajax_get_import_data() {
check_ajax_referer( 'adi_import_data_nonce', 'security' );
if ( ! current_user_can( 'import' ) )
die('Access denied. You do not have import capability.');
echo json_encode(
array(
array(
'input_name' => 'adi_import_xml',
'action' => 'adi_ajax_import_xml',
'method' => 'ajax_import_xml',
'loader' => esc_html__( 'Importing XML Data', 'access-demo-importer' )
),
array(
'input_name' => 'adi_theme_settings',
'action' => 'adi_ajax_import_theme_settings',
'method' => 'ajax_import_theme_settings',
'loader' => esc_html__( 'Importing Customizer Settings', 'access-demo-importer' )
),
array(
'input_name' => 'adi_import_widgets',
'action' => 'adi_ajax_import_widgets',
'method' => 'ajax_import_widgets',
'loader' => esc_html__( 'Importing Widgets', 'access-demo-importer' )
),
array(
'input_name' => 'adi_import_sliders',
'action' => 'adi_ajax_importSliderRev',
'method' => 'ajax_importSliderRev',
'loader' => esc_html__( 'Importing Revolution Slider', 'access-demo-importer' )
),
// array(
// 'input_name' => 'adi_import_smartsliders',
// 'action' => 'adi_ajax_importSliderSmart',
// 'method' => 'ajax_importSliderSmart',
// 'loader' => esc_html__( 'Importing Smart Slider', 'access-demo-importer' )
// ),
array(
'input_name' => 'adi_import_theme_options',
'action' => 'adi_ajax_importThemeOptions',
'method' => 'ajax_importThemeOptions',
'loader' => esc_html__( 'Importing Theme Settings', 'access-demo-importer' )
),
)
);
die();
}
/**
* Import XML file
*
*
*/
public function ajax_import_xml() {
if ( !current_user_can('import') || ! wp_verify_nonce( $_POST['adi_import_demo_data_nonce'], 'adi_import_demo_data_nonce' ) ) {
die( 'This action was stopped for security purposes.' );
}
// Get the selected demo
$demo_type = sanitize_text_field(wp_unslash($_POST['adi_import_demo']));
// Get demos data
$demo = ADI_Demos::get_demos_data()[ $demo_type ];
// Content file
$xml_file = isset( $demo['xml_file'] ) ? $demo['xml_file'] : '';
// Delete the default post and page
$sample_page = get_page_by_path( 'sample-page', OBJECT, 'page' );
$hello_world_post = get_page_by_path( 'hello-world', OBJECT, 'post' );
if ( ! is_null( $sample_page ) ) {
wp_delete_post( $sample_page->ID, true );
}
if ( ! is_null( $hello_world_post ) ) {
wp_delete_post( $hello_world_post->ID, true );
}
// Import Posts, Pages, Images, Menus.
$result = $this->process_xml( $xml_file );
if ( is_wp_error( $result ) ) {
echo json_encode( $result->errors );
} else {
echo 'successful import';
}
die();
}
/**
* Import customizer settings
*
*
*/
public function ajax_import_theme_settings() {
if ( !current_user_can('edit_theme_options') || ! wp_verify_nonce( $_POST['adi_import_demo_data_nonce'], 'adi_import_demo_data_nonce' ) ) {
die( 'This action was stopped for security purposes.' );
}
// Include settings importer
include ADI_PATH . 'inc/importers/class-settings-importer.php';
// Get the selected demo
$demo_type = sanitize_text_field(wp_unslash($_POST['adi_import_demo']));
// Get demos data
$demo = ADI_Demos::get_demos_data()[ $demo_type ];
// Settings file
$theme_settings = isset( $demo['theme_settings'] ) ? $demo['theme_settings'] : '';
// Import settings.
$settings_importer = new ADI_Settings_Importer();
$result = $settings_importer->process_import_file( $theme_settings );
if ( is_wp_error( $result ) ) {
echo json_encode( $result->errors );
} else {
echo 'successful import';
}
die();
}
/**
* Import widgets
*
*
*/
public function ajax_import_widgets() {
if ( !current_user_can('edit_theme_options') || ! wp_verify_nonce( $_POST['adi_import_demo_data_nonce'], 'adi_import_demo_data_nonce' ) ) {
die( 'This action was stopped for security purposes.' );
}
// Include widget importer
include ADI_PATH . 'inc/importers/class-widget-importer.php';
// Get the selected demo
$demo_type = sanitize_text_field(wp_unslash($_POST['adi_import_demo']));
// Get demos data
$demo = ADI_Demos::get_demos_data()[ $demo_type ];
// Widgets file
$widgets_file = isset( $demo['widgets_file'] ) ? $demo['widgets_file'] : '';
// Import settings.
$widgets_importer = new ADI_Widget_Importer();
$result = $widgets_importer->process_import_file( $widgets_file );
if ( is_wp_error( $result ) ) {
echo json_encode( $result->errors );
} else {
echo 'successful import';
}
die();
}
/*
* Import Slider Revolution
*/
public function ajax_importSliderRev() {
if ( !current_user_can('import') || ! wp_verify_nonce( $_POST['adi_import_demo_data_nonce'], 'adi_import_demo_data_nonce' ) ) {
die( 'This action was stopped for security purposes.' );
}
// Get the selected demo
$demo_type = sanitize_text_field(wp_unslash($_POST['adi_import_demo']));
$demo = ADI_Demos::get_demos_data()[ $demo_type ];
$slider_file = isset( $demo['rev_slider'] ) ? $demo['rev_slider'] : '';
$response = ADI_Demos_Helpers::get_remote( $slider_file );
// No sample data found
if ( $response === false ) {
return new WP_Error( 'xml_import_error', __( 'Can not retrieve slider data. The server may be down at the moment please try again later. If you still have issues contact the theme developer for assistance.', 'access-demo-importer' ) );
}
$upload_dir = wp_upload_dir();
$zip_dir = $upload_dir['basedir'].'/adi-temp-data';
if ( ! file_exists( $zip_dir ) ) {
wp_mkdir_p( $zip_dir );
}
$slider_temp_zip = $zip_dir.'/revslider_temp.zip';
if ( ! file_exists( $slider_temp_zip ) ) {
$zip = new ZipArchive();
if( $zip->open( $slider_temp_zip, ZIPARCHIVE::CREATE ) ){
file_put_contents( $slider_temp_zip, $response );
}
} else {
file_put_contents( $slider_temp_zip, $response );
}
if ( class_exists( 'RevSlider' ) ) {
$slider = new RevSlider();
$slider->importSliderFromPost( true, true, $slider_temp_zip );
echo 'successful import';
file_put_contents( $slider_temp_zip, '' );
} else {
echo 'It looks like you don\'t have Slider Revolution installed and activated. Sliders were not imported!
';
}
die();
}
public function ajax_importSliderSmart() {
if ( !current_user_can('import') || ! wp_verify_nonce( $_POST['adi_import_demo_data_nonce'], 'adi_import_demo_data_nonce' ) ) {
die( 'This action was stopped for security purposes.' );
}
// Include form importer
include ADI_PATH . '/inc/importers/class-smartslider-importer.php';
// Get the selected demo
$demo_type = sanitize_text_field(wp_unslash($_POST['adi_import_demo']));
$demo = ADI_Demos::get_demos_data()[ $demo_type ];
$slider_file = isset( $demo['smart_slider'] ) ? $demo['smart_slider'] : '';
$response = ADI_Demos_Helpers::get_remote( $slider_file );
// No sample data found
if ( $response === false ) {
return new WP_Error( 'xml_import_error', __( 'Cannot retrieve slider data. The server may be down at the moment please try again later. If you still have issues contact the theme developer for assistance.', 'access-demo-importer' ) );
}
// Import settings.
$slider_importer = new ADI_SmartSlider_Importer();
$result = $slider_importer->process_import_file( $slider_file );
// Clear sample data content from temp slider file
$temp_slider = ADI_PATH.'/temp-data/slider.ss3';
file_put_contents( $temp_slider, '' );
if ( is_wp_error( $result ) ) {
echo json_encode( $result->errors );
} else {
echo 'successful import';
}
die();
}
/**
* Import Theme Options
*/
public function ajax_importThemeOptions() {
if ( !current_user_can('edit_theme_options') || ! wp_verify_nonce( $_POST['adi_import_demo_data_nonce'], 'adi_import_demo_data_nonce' ) ) {
die( 'This action was stopped for security purposes. You do not have capabitlity to edit theme options' );
}
$demo_type = sanitize_text_field(wp_unslash($_POST['adi_import_demo']));
$demo = ADI_Demos::get_demos_data()[ $demo_type ];
$import_redux = isset( $demo['import_redux'] ) ? $demo['import_redux'] : '';
$option_filepath = '';
if($import_redux){
$option_filepath = isset( $import_redux['file_url'] ) ? $import_redux['file_url'] : '';
$option_name = isset( $import_redux['option_name'] ) ? $import_redux['option_name'] : '';
}
$json_file = wp_remote_get( $option_filepath );
if ( $json_file[ 'response' ][ 'code' ] == '200' && !empty( $json_file[ 'body' ] ) ) {
if ( update_option( $option_name, json_decode( $json_file[ 'body' ], true ), '', 'yes' ) ) {
echo 'successful import';
}
} else {
return esc_html__( 'Theme Options could not be imported', 'access-demo-importer' );
}
die();
}
/**
* After import
*
*
*/
public function ajax_after_import() {
if ( !current_user_can('install_plugins') || !current_user_can('edit_theme_options') || !current_user_can('manage_options') || ! wp_verify_nonce( $_POST['adi_import_demo_data_nonce'], 'adi_import_demo_data_nonce' ) ) {
die( 'This action was stopped for security purposes.' );
}
// If XML file is imported
if ( $_POST['adi_import_is_xml'] === 'true' ) {
// Get the selected demo
$demo_type = sanitize_text_field(wp_unslash($_POST['adi_import_demo']));
// Get demos data
$demo = ADI_Demos::get_demos_data()[ $demo_type ];
// Elementor width setting
$elementor_width = isset( $demo['elementor_width'] ) ? $demo['elementor_width'] : '';
// Reading settings
$homepage_title = isset( $demo['home_title'] ) ? $demo['home_title'] : '';
$blog_title = isset( $demo['blog_title'] ) ? $demo['blog_title'] : '';
// Posts to show on the blog page
$posts_to_show = isset( $demo['posts_to_show'] ) ? $demo['posts_to_show'] : '';
// If shop demo
$shop_demo = isset( $demo['is_shop'] ) ? $demo['is_shop'] : false;
// Product image size
$image_size = isset( $demo['woo_image_size'] ) ? $demo['woo_image_size'] : '';
$thumbnail_size = isset( $demo['woo_thumb_size'] ) ? $demo['woo_thumb_size'] : '';
$crop_width = isset( $demo['woo_crop_width'] ) ? $demo['woo_crop_width'] : '';
$crop_height = isset( $demo['woo_crop_height'] ) ? $demo['woo_crop_height'] : '';
// Assign WooCommerce pages if WooCommerce Exists
if ( class_exists( 'WooCommerce' ) && true == $shop_demo ) {
$woopages = array(
'woocommerce_shop_page_id' => 'Shop',
'woocommerce_cart_page_id' => 'Cart',
'woocommerce_checkout_page_id' => 'Checkout',
'woocommerce_pay_page_id' => 'Checkout → Pay',
'woocommerce_thanks_page_id' => 'Order Received',
'woocommerce_myaccount_page_id' => 'My Account',
'woocommerce_edit_address_page_id' => 'Edit My Address',
'woocommerce_view_order_page_id' => 'View Order',
'woocommerce_change_password_page_id' => 'Change Password',
'woocommerce_logout_page_id' => 'Logout',
'woocommerce_lost_password_page_id' => 'Lost Password'
);
foreach ( $woopages as $woo_page_name => $woo_page_title ) {
$woopage = get_page_by_title( $woo_page_title );
if ( isset( $woopage ) && $woopage->ID ) {
update_option( $woo_page_name, $woopage->ID );
}
}
// We no longer need to install pages
delete_option( '_wc_needs_pages' );
delete_transient( '_wc_activation_redirect' );
// Get products image size
update_option( 'woocommerce_single_image_width', $image_size );
update_option( 'woocommerce_thumbnail_image_width', $thumbnail_size );
update_option( 'woocommerce_thumbnail_cropping', 'custom' );
update_option( 'woocommerce_thumbnail_cropping_custom_width', $crop_width );
update_option( 'woocommerce_thumbnail_cropping_custom_height', $crop_height );
}
// Set imported menus to registered theme locations
$locations = get_theme_mod( 'nav_menu_locations' );
$menus = wp_get_nav_menus();
$config_menus = isset( $demo['menus'] ) ? $demo['menus'] : '';
if( $config_menus ){
foreach( $config_menus as $menu_id => $config_menu ){
$locations[$menu_id] = $config_menu;
}
}
if ( $menus ) {
foreach ( $menus as $menu ) {
if ( $menu->name == 'Top Menu' ) {
$locations['topbar_menu'] = $menu->term_id;
} else if ( $menu->name == 'Footer Menu' ) {
$locations['footer_menu'] = $menu->term_id;
} else if ( $menu->name == 'Sticky Footer' ) {
$locations['sticky_footer_menu'] = $menu->term_id;
}
}
}
// Set menus to locations
set_theme_mod( 'nav_menu_locations', $locations );
// Disable Elementor default settings
update_option( 'elementor_disable_color_schemes', 'yes' );
update_option( 'elementor_disable_typography_schemes', 'yes' );
if ( ! empty( $elementor_width ) ) {
update_option( 'elementor_container_width', $elementor_width );
}
// Assign front page and posts page (blog page).
$home_page = get_page_by_title( $homepage_title );
$blog_page = get_page_by_title( $blog_title );
if( empty($homepage_title) ){
update_option( 'show_on_front', 'posts' );
}else{
update_option( 'show_on_front', 'page' );
}
if ( is_object( $home_page ) ) {
update_option( 'page_on_front', $home_page->ID );
}
if ( is_object( $blog_page ) ) {
update_option( 'page_for_posts', $blog_page->ID );
}
// Posts to show on the blog page
if ( ! empty( $posts_to_show ) ) {
update_option( 'posts_per_page', $posts_to_show );
}
}
wp_redirect(admin_url('themes.php?page=demo-importer'));
die();
}
/**
* Import XML data
*
*
*/
public function process_xml( $file ) {
if( !current_user_can('install_plugins') )
die('Access Denied. You do not have required capability to install plugins.');
$response = ADI_Demos_Helpers::get_remote( $file );
// No sample data found
if ( $response === false ) {
return new WP_Error( 'xml_import_error', __( 'Can not retrieve sample data xml file. The server may be down at the moment please try again later. If you still have issues contact the theme developer for assistance.', 'access-demo-importer' ) );
}
// Write sample data content to temp xml file
$temp_xml = ADI_PATH .'temp-data/temp.xml';
// $temp_xml = 'https://accesspressthemes.com/theme-demos/temp-data/temp.xml';
file_put_contents( $temp_xml, $response );
// Set temp xml to attachment url for use
$attachment_url = $temp_xml;
$file_headers = @get_headers($attachment_url);
if($file_headers[0] == 'HTTP/1.0 404 Not Found'){
return new WP_Error( 'xml_import_error', __( 'The xml import file could not be accessed. Please try again or contact the theme developer.', 'access-demo-importer' ) );
} else if ($file_headers[0] == 'HTTP/1.0 302 Found' && $file_headers[7] == 'HTTP/1.0 404 Not Found'){
return new WP_Error( 'xml_import_error', __( 'The xml import file could not be accessed. Please try again or contact the theme developer.', 'access-demo-importer' ) );
} else {
$this->import_xml( $attachment_url );
}
// If file exists lets import it
// if ( file_exists( $attachment_url ) ) {
// $this->import_xml( $attachment_url );
// } else {
// // Import file can't be imported - we should die here since this is core for most people.
// return new WP_Error( 'xml_import_error', __( 'The xml import file could not be accessed. Please try again or contact the theme developer.', 'access-demo-importer' ) );
// }
}
/**
* Import XML file
*
*
*/
private function import_xml( $file ) {
if( !current_user_can('import') )
die('You do not have required capability to import.');
// Make sure importers constant is defined
if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) {
define( 'WP_LOAD_IMPORTERS', true );
}
// Import file location
$import_file = ABSPATH . 'wp-admin/includes/import.php';
// Include import file
if ( ! file_exists( $import_file ) ) {
return;
}
// Include import file
require_once( $import_file );
// Define error var
$importer_error = false;
if ( ! class_exists( 'WP_Importer' ) ) {
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
if ( file_exists( $class_wp_importer ) ) {
require_once $class_wp_importer;
} else {
$importer_error = __( 'Can not retrieve class-wp-importer.php', 'access-demo-importer' );
}
}
if ( ! class_exists( 'WP_Import' ) ) {
$class_wp_import = ADI_PATH . 'inc/importers/class-wordpress-importer.php';
if ( file_exists( $class_wp_import ) ) {
require_once $class_wp_import;
} else {
$importer_error = __( 'Can not retrieve wordpress-importer.php', 'access-demo-importer' );
}
}
// Display error
if ( $importer_error ) {
return new WP_Error( 'xml_import_error', $importer_error );
} else {
// No error, lets import things...
if ( ! is_file( $file ) ) {
$importer_error = __( 'Sample data file appears corrupt or can not be accessed.', 'access-demo-importer' );
return new WP_Error( 'xml_import_error', $importer_error );
} else {
$importer = new WP_Import();
$importer->fetch_attachments = true;
$importer->import( $file );
// Clear sample data content from temp xml file
$temp_xml = ADI_PATH .'temp-data/temp.xml';
// $temp_xml = 'https://accesspressthemes.com/theme-demos/temp-data/temp.xml';
file_put_contents( $temp_xml, '' );
}
}
}
}
}
new ADI_Demos();