plugin_page_setup = Helpers::get_plugin_page_setup_data(); $this->plugin_page = add_menu_page( $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( 'academyst/plugin_page_display_callback_function', array( $this, 'display_plugin_page' ) ), 'dashicons-welcome-learn-more', 2 ); register_importer( $this->plugin_page_setup['menu_slug'], $this->plugin_page_setup['page_title'], $this->plugin_page_setup['menu_title'], Helpers::apply_filters( 'academyst/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 ACADEMY_STARTER_ROOT_DIR_PATH . 'views/install-plugins.php'; return; } if ( isset( $_GET['step'] ) && 'create-content' === $_GET['step'] ) { require_once ACADEMY_STARTER_ROOT_DIR_PATH . 'views/create-content.php'; return; } if ( isset( $_GET['step'] ) && 'import' === $_GET['step'] ) { require_once ACADEMY_STARTER_ROOT_DIR_PATH . 'views/import.php'; return; } require_once ACADEMY_STARTER_ROOT_DIR_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( 'academyst-main-js', ACADEMY_STARTER_ASSETS_URI . 'js/main.js', array( 'jquery' ), ACADEMY_STARTER_VERSION ); // Get theme data. $theme = wp_get_theme(); wp_localize_script( 'academyst-main-js', 'academyst', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'site_url' => get_option( 'siteurl' ), 'ajax_nonce' => wp_create_nonce( 'academyst-ajax-verification' ), 'import_files' => $this->import_files, 'wp_customize_on' => Helpers::apply_filters( 'academyst/enable_wp_customize_save_hooks', false ), 'theme_screenshot' => $theme->get_screenshot(), 'missing_plugins' => $this->plugin_installer->get_missing_plugins(), 'plugin_url' => ACADEMY_STARTER_PLUGIN_ROOT_URI, 'import_url' => $this->get_plugin_settings_url( [ 'step' => 'import' ] ), 'texts' => array( 'missing_preview_image' => esc_html__( 'No preview image defined for this import.', 'academy-starter-templates' ), 'dialog_title' => esc_html__( 'Are you sure?', 'academy-starter-templates' ), 'dialog_no' => esc_html__( 'Cancel', 'academy-starter-templates' ), 'dialog_yes' => esc_html__( 'Yes, import!', 'academy-starter-templates' ), 'selected_import_title' => esc_html__( 'Selected demo import:', 'academy-starter-templates' ), 'installing' => esc_html__( 'Installing...', 'academy-starter-templates' ), 'importing' => esc_html__( 'Importing...', 'academy-starter-templates' ), 'successful_import' => esc_html__( 'Successfully Imported!', 'academy-starter-templates' ), 'install_plugin' => esc_html__( 'Install Plugin', 'academy-starter-templates' ), 'installed' => esc_html__( 'Installed', 'academy-starter-templates' ), 'import_failed' => esc_html__( 'Import Failed', 'academy-starter-templates' ), 'import_failed_subtitle' => esc_html__( 'Whoops, there was a problem importing your content.', 'academy-starter-templates' ), '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.', 'academy-starter-templates' ), 'content_filetype_warn' => esc_html__( 'Invalid file type detected! Please select an XML file for the Content Import.', 'academy-starter-templates' ), 'widgets_filetype_warn' => esc_html__( 'Invalid file type detected! Please select a JSON or WIE file for the Widgets Import.', 'academy-starter-templates' ), 'customizer_filetype_warn' => esc_html__( 'Invalid file type detected! Please select a DAT file for the Customizer Import.', 'academy-starter-templates' ), 'redux_filetype_warn' => esc_html__( 'Invalid file type detected! Please select a JSON file for the Redux Import.', 'academy-starter-templates' ), ), ) ); wp_enqueue_style( 'academyst-main-css', ACADEMY_STARTER_ASSETS_URI . 'css/main.css', array(), ACADEMY_STARTER_VERSION ); }//end if } /** * 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.', 'academy-starter-templates' ) ); } // 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', 'academy-starter-templates' ); // Save the initial import data as a transient, so the next import call (in new AJAX call) can use that data. Helpers::set_academyst_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() { // Set a higher memory limit for large imports. ini_set( 'memory_limit', Helpers::apply_filters( 'academyst/import_memory_limit', '350M' ) ); // Set a higher execution time limit for large imports. ini_set( 'max_execution_time', Helpers::apply_filters( 'academyst/import_execution_time_limit', 600 ) ); // Verify nonce and user capability. Helpers::verify_ajax_call(); $user_existing_importer_data = $this->use_existing_importer_data(); if( ! $user_existing_importer_data ) { Helpers::set_demo_import_start_time(); $this->log_file_path = Helpers::get_log_path(); $this->selected_index = empty( $_POST['selected'] ) ? 0 : absint( $_POST['selected'] ); if( ! empty( $_FILES ) ) { $this->selected_import_files = Helpers::process_uploaded_files( $_FILES, $this->log_file_path ); $this->import_files[ $this->selected_index ]['import_file_name'] = esc_html__( 'Manually uploaded files', 'academy-starter-templates' ); } elseif ( ! empty( $this->import_files[ $this->selected_index ] ) ) { $this->selected_import_files = Helpers::download_import_files( $this->import_files[ $this->selected_index ] ); if( is_wp_error( $this->selected_import_files ) ) { Helpers::log_error_and_send_ajax_response( $this->selected_import_files->get_error_message(), $this->log_file_path, esc_html__( 'Downloaded files', 'academy-starter-templates' ) ); } $log_added = Helpers::append_to_file( sprintf( __( 'The import files for: %s were successfully downloaded!', 'academy-starter-templates' ), $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', 'academy-starter-templates' ) ); } else { wp_send_json_error( esc_html__( 'No import files specified!', 'academy-starter-templates' ) ); } } Helpers::set_academyst_import_data_transient( $this->get_current_importer_data() ); if ( ! $this->before_import_executed ) { $this->before_import_executed = true; // Run any actions hooked to run before content import. Helpers::do_action( 'academyst/before_content_import_execution', $this->selected_import_files, $this->import_files, $this->selected_index ); } if ( ! empty( $this->selected_import_files['content'] ) ) { if( is_plugin_active('wordpress-importer/wordpress-importer.php') ) { Helper::get_wordpress_importer(); $wp_import = new \WP_Import(); $wp_import->fetch_attachments = true; ob_start(); $wp_import->import( $this->selected_import_files['content'] ); ob_get_clean(); } else { wp_send_json_error( esc_html__( 'Content importer class not available.', 'academy-starter-templates' ) ); } } Helpers::do_action( 'academyst/after_content_import_execution', $this->selected_import_files, $this->import_files, $this->selected_index ); Helpers::set_academyst_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( 'academyst/after_all_import_execution' ) ) { wp_send_json( array( 'status' => 'afterAllImportAJAX' ) ); } // 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( 'academyst/customizer_import_execution', $this->selected_import_files ); } // Request the after all import AJAX call. if ( false !== Helpers::has_action( 'academyst/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( 'academyst/after_all_import_execution', $this->selected_import_files, $this->import_files, $this->selected_index ); } // 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( 'academyst_importer_data' ); // Display final messages (success or warning messages). $response['title'] = esc_html__( 'Import Complete!', 'academy-starter-templates' ); $response['subtitle'] = '

' . esc_html__( 'Congrats, your demo was imported successfully. You can now begin editing your site.', 'academy-starter-templates' ) . '

'; $response['message'] = '' . esc_attr__( 'Successful Import', 'academy-starter-templates' ) . ''; if ( ! empty( $this->frontend_error_messages ) ) { $response['subtitle'] = '

' . esc_html__( 'Your import completed, but some things may not have imported properly.', 'academy-starter-templates' ) . '

'; $response['subtitle'] .= sprintf( wp_kses( /* translators: %s - link to the log file. */ __( '

View error log for more information.

', 'academy-starter-templates' ), array( 'p' => [], 'a' => [ 'href' => [], 'target' => [], ], ) ), Helpers::get_log_url( $this->log_file_path ) ); $response['message'] = '

' . $this->frontend_error_messages_display() . '

'; } wp_send_json( $response ); } /** * Get content importer data, so we can continue the import with this new AJAX request. * * @return boolean */ private function use_existing_importer_data() { if ( $data = get_transient( 'academyst_importer_data' ) ) { $this->frontend_error_messages = empty( $data['frontend_error_messages'] ) ? array() : $data['frontend_error_messages']; $this->log_file_path = empty( $data['log_file_path'] ) ? '' : $data['log_file_path']; $this->selected_index = empty( $data['selected_index'] ) ? 0 : $data['selected_index']; $this->selected_import_files = empty( $data['selected_import_files'] ) ? array() : $data['selected_import_files']; $this->import_files = empty( $data['import_files'] ) ? array() : $data['import_files']; $this->before_import_executed = empty( $data['before_import_executed'] ) ? false : $data['before_import_executed']; $this->importer->set_importer_data( $data ); return true; } return false; } /** * Get the current state of selected data. * * @return array */ public function get_current_importer_data() { return array( 'frontend_error_messages' => $this->frontend_error_messages, 'log_file_path' => $this->log_file_path, 'selected_index' => $this->selected_index, 'selected_import_files' => $this->selected_import_files, 'import_files' => $this->import_files, 'before_import_executed' => $this->before_import_executed, ); } /** * Getter function to retrieve the private log_file_path value. * * @return string The log_file_path value. */ public function get_log_file_path() { return $this->log_file_path; } /** * Setter function to append additional value to the private frontend_error_messages value. * * @param string $additional_value The additional value that will be appended to the existing frontend_error_messages. */ public function append_to_frontend_error_messages( $text ) { $lines = array(); if ( ! empty( $text ) ) { $text = str_replace( '
', PHP_EOL, $text ); $lines = explode( PHP_EOL, $text ); } foreach ( $lines as $line ) { if ( ! empty( $line ) && ! in_array( $line, $this->frontend_error_messages ) ) { $this->frontend_error_messages[] = $line; } } } /** * Display the frontend error messages. * * @return string Text with HTML markup. */ public function frontend_error_messages_display() { $output = ''; if ( ! empty( $this->frontend_error_messages ) ) { foreach ( $this->frontend_error_messages as $line ) { $output .= esc_html( $line ); $output .= '
'; } } return $output; } /** * Get data from filters, after the theme has loaded and instantiate the importer. */ public function setup_plugin_with_filter_data() { if ( ! ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) ) { return; } // Get info of import data files and filter it. $this->import_files = Helpers::validate_import_file_info( Helpers::apply_filters( 'academyst/import_files', array() ) ); /** * Register all default actions (before content import, widget, customizer import and other actions) * to the 'before_content_import_execution' and the 'academyst/after_content_import_execution' action hook. */ $import_actions = new ImportActions(); $import_actions->register_hooks(); // Importer options array. $importer_options = Helpers::apply_filters( 'academyst/importer_options', array( 'fetch_attachments' => true, ) ); // Logger options for the logger used in the importer. $logger_options = Helpers::apply_filters( 'academyst/logger_options', array( 'logger_min_level' => 'warning', ) ); // Configure logger instance and set it to the importer. $logger = new Logger(); $logger->min_level = $logger_options['logger_min_level']; // Create importer instance with proper parameters. $this->importer = new Importer( $importer_options, $logger ); // Prepare registered plugins and register AJAX callbacks. $this->plugin_installer = new PluginInstaller(); $this->plugin_installer->init(); } /** * Getter for $plugin_page_setup. * * @return array */ public function get_plugin_page_setup() { return $this->plugin_page_setup; } /** * Output the begining of the container div for all notices, but only on OCDI pages. */ public function start_notice_output_capturing() { $screen = get_current_screen(); if ( false === strpos( $screen->base, $this->plugin_page_setup['menu_slug'] ) ) { return; } echo '
'; } /** * Output the ending of the container div for all notices, but only on OCDI pages. */ public function finish_notice_output_capturing() { if ( is_network_admin() ) { return; } $screen = get_current_screen(); if ( false === strpos( $screen->base, $this->plugin_page_setup['menu_slug'] ) ) { return; } echo '
'; } /** * Get the URL of the plugin settings page. * * @return string */ public function get_plugin_settings_url( $query_parameters = [] ) { if ( empty( $this->plugin_page_setup ) ) { $this->plugin_page_setup = Helpers::get_plugin_page_setup_data(); } $parameters = array_merge( array( 'page' => $this->plugin_page_setup['menu_slug'] ), $query_parameters ); $url = menu_page_url( $this->plugin_page_setup['parent_slug'], false ); if ( empty( $url ) ) { $url = self_admin_url( $this->plugin_page_setup['parent_slug'] ); } return add_query_arg( $parameters, $url ); } /** * Redirect from the old default OCDI settings page URL to the new one. */ public function redirect_from_old_default_admin_page() { global $pagenow; if ( $pagenow == 'themes.php' && isset( $_GET['page'] ) && $_GET['page'] == 'pt-one-click-demo-import' ) { wp_safe_redirect( $this->get_plugin_settings_url() ); exit; } } }