up = Helpers::get_plugin_page_setup_data(); $this->plugin_page = add_submenu_page( $this->plugin_page_setup['parent_slug'], $this->plugin_page_setup['page_title'], $this->plugin_page_setup['menu_title'], $this->plugin_page_setup['capability'], $this->plugin_page_setup['menu_slug'], Helpers::apply_filters( 'ocdi/plugin_page_display_callback_function', array( $this, 'display_plugin_page' ) ) ); // Register the old default settings page, so we can redirect to the new one and not break any existing links. add_submenu_page( '', $this->plugin_page_setup['page_title'], $this->plugin_page_setup['menu_title'], $this->plugin_page_setup['capability'], 'pt-one-click-demo-import' ); register_importer( $this->plugin_page_setup['menu_slug'], $this->plugin_page_setup['page_title'], $this->plugin_page_setup['menu_title'], Helpers::apply_filters( 'ocdi/plugin_page_display_callback_function', array( $this, 'display_plugin_page' ) ) ); } /** * Plugin page display. * Output (HTML) is in another file. */ public function display_plugin_page() { if ( isset( $_GET['step'] ) && 'install-plugins' === $_GET['step'] ) { require_once OCDI_PATH . 'views/install-plugins.php'; return; } if ( isset( $_GET['step'] ) && 'create-content' === $_GET['step'] ) { require_once OCDI_PATH . 'views/create-content.php'; return; } if ( isset( $_GET['step'] ) && 'import' === $_GET['step'] ) { require_once OCDI_PATH . 'views/import.php'; return; } require_once OCDI_PATH . 'views/plugin-page.php'; } /** * Enqueue admin scripts (JS and CSS) * * @param string $hook holds info on which admin page you are currently loading. */ public function admin_enqueue_scripts( $hook ) { // Enqueue the scripts only on the plugin page. if ( $this->plugin_page === $hook || ( 'admin.php' === $hook && $this->plugin_page_setup['menu_slug'] === esc_attr( $_GET['import'] ) ) ) { wp_enqueue_script( 'ocdi-main-js', OCDI_URL . 'assets/js/main.js' , array( 'jquery' ), OCDI_VERSION ); // Get theme data. $theme = wp_get_theme(); wp_localize_script( 'ocdi-main-js', 'ocdi', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'ajax_nonce' => wp_create_nonce( 'ocdi-ajax-verification' ), 'import_files' => $this->import_files, 'wp_customize_on' => Helpers::apply_filters( 'ocdi/enable_wp_customize_save_hooks', false ), 'theme_screenshot' => $theme->get_screenshot(), 'missing_plugins' => $this->plugin_installer->get_missing_plugins(), 'plugin_url' => OCDI_URL, 'import_url' => $this->get_plugin_settings_url( [ 'step' => 'import' ] ), 'texts' => array( 'missing_preview_image' => esc_html__( 'No preview image defined for this import.', 'one-click-demo-import' ), 'dialog_title' => esc_html__( 'Are you sure?', 'one-click-demo-import' ), 'dialog_no' => esc_html__( 'Cancel', 'one-click-demo-import' ), 'dialog_yes' => esc_html__( 'Yes, import!', 'one-click-demo-import' ), 'selected_import_title' => esc_html__( 'Selected demo import:', 'one-click-demo-import' ), 'installing' => esc_html__( 'Installing...', 'one-click-demo-import' ), 'importing' => esc_html__( 'Importing...', 'one-click-demo-import' ), 'successful_import' => esc_html__( 'Successfully Imported!', 'one-click-demo-import' ), 'install_plugin' => esc_html__( 'Install Plugin', 'one-click-demo-import' ), 'installed' => esc_html__( 'Installed', 'one-click-demo-import' ), 'import_failed' => esc_html__( 'Import Failed', 'one-click-demo-import' ), 'import_failed_subtitle' => esc_html__( 'Whoops, there was a problem importing your content.', 'one-click-demo-import' ), 'plugin_install_failed' => esc_html__( 'Looks like some of the plugins failed to install. Please try again. If this issue persists, please manually install the failing plugins and come back to this step to import the theme demo data.', 'one-click-demo-import' ), 'content_filetype_warn' => esc_html__( 'Invalid file type detected! Please select an XML file for the Content Import.', 'one-click-demo-import' ), 'widgets_filetype_warn' => esc_html__( 'Invalid file type detected! Please select a JSON or WIE file for the Widgets Import.', 'one-click-demo-import' ), 'customizer_filetype_warn' => esc_html__( 'Invalid file type detected! Please select a DAT file for the Customizer Import.', 'one-click-demo-import' ), 'redux_filetype_warn' => esc_html__( 'Invalid file type detected! Please select a JSON file for the Redux Import.', 'one-click-demo-import' ), ), ) ); wp_enqueue_style( 'ocdi-main-css', OCDI_URL . 'assets/css/main.css', array() , OCDI_VERSION ); } } /** * AJAX callback method for uploading the manual import files. */ public function upload_manual_import_files_callback() { Helpers::verify_ajax_call(); if ( empty( $_FILES ) ) { wp_send_json_error( esc_html__( 'Manual import files are missing! Please select the import files and try again.', 'one-click-demo-import' ) ); } // Create a date and time string to use for demo and log file names. Helpers::set_demo_import_start_time(); // Define log file path. $this->log_file_path = Helpers::get_log_path(); $this->selected_index = 0; // Get paths for the uploaded files. $this->selected_import_files = Helpers::process_uploaded_files( $_FILES, $this->log_file_path ); // Set the name of the import files, because we used the uploaded files. $this->import_files[ $this->selected_index ]['import_file_name'] = esc_html__( 'Manually uploaded files', 'one-click-demo-import' ); // Save the initial import data as a transient, so the next import call (in new AJAX call) can use that data. Helpers::set_ocdi_import_data_transient( $this->get_current_importer_data() ); wp_send_json_success(); } /** * Main AJAX callback function for: * 1). prepare import files (uploaded or predefined via filters) * 2). execute 'before content import' actions (before import WP action) * 3). import content * 4). execute 'after content import' actions (before widget import WP action, widget import, customizer import, after import WP action) */ public function import_demo_data_ajax_callback() { // Try to update PHP memory limit (so that it does not run out of it). ini_set( 'memory_limit', Helpers::apply_filters( 'ocdi/import_memory_limit', '350M' ) ); // Verify if the AJAX call is valid (checks nonce and current_user_can). Helpers::verify_ajax_call(); // Is this a new AJAX call to continue the previous import? $use_existing_importer_data = $this->use_existing_importer_data(); if ( ! $use_existing_importer_data ) { // Create a date and time string to use for demo and log file names. Helpers::set_demo_import_start_time(); // Define log file path. $this->log_file_path = Helpers::get_log_path(); // Get selected file index or set it to 0. $this->selected_index = empty( $_POST['selected'] ) ? 0 : absint( $_POST['selected'] ); /** * 1). Prepare import files. * Manually uploaded import files or predefined import files via filter: ocdi/import_files */ if ( ! empty( $_FILES ) ) { // Using manual file uploads? // Get paths for the uploaded files. $this->selected_import_files = Helpers::process_uploaded_files( $_FILES, $this->log_file_path ); // Set the name of the import files, because we used the uploaded files. $this->import_files[ $this->selected_index ]['import_file_name'] = esc_html__( 'Manually uploaded files', 'one-click-demo-import' ); } elseif ( ! empty( $this->import_files[ $this->selected_index ] ) ) { // Use predefined import files from wp filter: ocdi/import_files. // Download the import files (content, widgets and customizer files). $this->selected_import_files = Helpers::download_import_files( $this->import_files[ $this->selected_index ] ); // Check Errors. if ( is_wp_error( $this->selected_import_files ) ) { // Write error to log file and send an AJAX response with the error. Helpers::log_error_and_send_ajax_response( $this->selected_import_files->get_error_message(), $this->log_file_path, esc_html__( 'Downloaded files', 'one-click-demo-import' ) ); } // Add this message to log file. $log_added = Helpers::append_to_file( sprintf( /* translators: %s - the name of the selected import. */ __( 'The import files for: %s were successfully downloaded!', 'one-click-demo-import' ), $this->import_files[ $this->selected_index ]['import_file_name'] ) . Helpers::import_file_info( $this->selected_import_files ), $this->log_file_path, esc_html__( 'Downloaded files' , 'one-click-demo-import' ) ); } else { // Send JSON Error response to the AJAX call. wp_send_json( esc_html__( 'No import files specified!', 'one-click-demo-import' ) ); } } // Save the initial import data as a transient, so other import parts (in new AJAX calls) can use that data. Helpers::set_ocdi_import_data_transient( $this->get_current_importer_data() ); if ( ! $this->before_import_executed ) { $this->before_import_executed = true; /** * 2). Execute the actions hooked to the 'ocdi/before_content_import_execution' action: * * Default actions: * 1 - Before content import WP action (with priority 10). */ Helpers::do_action( 'ocdi/before_content_import_execution', $this->selected_import_files, $this->import_files, $this->selected_index ); } /** * 3). Import content (if the content XML file is set for this import). * Returns any errors greater then the "warning" logger level, that will be displayed on front page. */ if ( ! empty( $this->selected_import_files['content'] ) ) { $this->append_to_frontend_error_messages( $this->importer->import_content( $this->selected_import_files['content'] ) ); } /** * 4). Execute the actions hooked to the 'ocdi/after_content_import_execution' action: * * Default actions: * 1 - Before widgets import setup (with priority 10). * 2 - Import widgets (with priority 20). * 3 - Import Redux data (with priority 30). */ Helpers::do_action( 'ocdi/after_content_import_execution', $this->selected_import_files, $this->import_files, $this->selected_index ); // Save the import data as a transient, so other import parts (in new AJAX calls) can use that data. Helpers::set_ocdi_import_data_transient( $this->get_current_importer_data() ); // Request the customizer import AJAX call. if ( ! empty( $this->selected_import_files['customizer'] ) ) { wp_send_json( array( 'status' => 'customizerAJAX' ) ); } // Request the after all import AJAX call. if ( false !== Helpers::has_action( 'ocdi/after_all_import_execution' ) ) { wp_send_json( array( 'status' => 'afterAllImportAJAX' ) ); } // Update terms count. $this->update_terms_count(); // Send a JSON response with final report. $this->final_response(); } /** * AJAX callback for importing the customizer data. * This request has the wp_customize set to 'on', so that the customizer hooks can be called * (they can only be called with the $wp_customize instance). But if the $wp_customize is defined, * then the widgets do not import correctly, that's why the customizer import has its own AJAX call. */ public function import_customizer_data_ajax_callback() { // Verify if the AJAX call is valid (checks nonce and current_user_can). Helpers::verify_ajax_call(); // Get existing import data. if ( $this->use_existing_importer_data() ) { /** * Execute the customizer import actions. * * Default actions: * 1 - Customizer import (with priority 10). */ Helpers::do_action( 'ocdi/customizer_import_execution', $this->selected_import_files ); } // Request the after all import AJAX call. if ( false !== Helpers::has_action( 'ocdi/after_all_import_execution' ) ) { wp_send_json( array( 'status' => 'afterAllImportAJAX' ) ); } // Send a JSON response with final report. $this->final_response(); } /** * AJAX callback for the after all import action. */ public function after_all_import_data_ajax_callback() { // Verify if the AJAX call is valid (checks nonce and current_user_can). Helpers::verify_ajax_call(); // Get existing import data. if ( $this->use_existing_importer_data() ) { /** * Execute the after all import actions. * * Default actions: * 1 - after_import action (with priority 10). */ Helpers::do_action( 'ocdi/after_all_import_execution', $this->selected_import_files, $this->import_files, $this->selected_index ); } // Update terms count. $this->update_terms_count(); // Send a JSON response with final report. $this->final_response(); } /** * Send a JSON response with final report. */ private function final_response() { // Delete importer data transient for current import. delete_transient( 'ocdi_importer_data' ); delete_transient( 'ocdi_importer_data_failed_attachment_imports' ); delete_transient( 'ocdi_import_menu_mapping' ); delete_transient( 'ocdi_import_posts_with_nav_block' ); // Display final messages (success or warning messages). $response['title'] = esc_html__( 'Import Complete!', 'one-click-demo-import' ); $response['subtitle'] = '
' . esc_html__( 'Congrats, your demo was imported successfully. You can now begin editing your site.', 'one-click-demo-import' ) . '
'; $response['message'] = ''; if ( ! empty( $this->frontend_error_messages ) ) { $response['subtitle'] = '' . esc_html__( 'Your import completed, but some things may not have imported properly.', 'one-click-demo-import' ) . '
'; $response['subtitle'] .= sprintf( wp_kses( /* translators: %s - link to the log file. */ __( 'View error log for more information.
', 'one-click-demo-import' ), array( 'p' => [], 'a' => [ 'href' => [], 'target' => [], ], ) ), Helpers::get_log_url( $this->log_file_path ) ); $response['message'] = '' . $this->frontend_error_messages_display() . '