Importer.php (5389B)
1 <?php 2 /** 3 * Class for declaring the content importer used in the One Click Demo Import plugin 4 * 5 * @package ocdi 6 */ 7 8 namespace OCDI; 9 10 class Importer { 11 /** 12 * The importer class object used for importing content. 13 * 14 * @var object 15 */ 16 private $importer; 17 18 /** 19 * Time in milliseconds, marking the beginning of the import. 20 * 21 * @var float 22 */ 23 private $microtime; 24 25 /** 26 * The instance of the OCDI\Logger class. 27 * 28 * @var object 29 */ 30 public $logger; 31 32 /** 33 * The instance of the One Click Demo Import class. 34 * 35 * @var object 36 */ 37 private $ocdi; 38 39 /** 40 * Constructor method. 41 * 42 * @param array $importer_options Importer options. 43 * @param object $logger Logger object used in the importer. 44 */ 45 public function __construct( $importer_options = array(), $logger = null ) { 46 // Include files that are needed for WordPress Importer v2. 47 $this->include_required_files(); 48 49 // Set the WordPress Importer v2 as the importer used in this plugin. 50 // More: https://github.com/humanmade/WordPress-Importer. 51 $this->importer = new WXRImporter( $importer_options ); 52 53 // Set logger to the importer. 54 $this->logger = $logger; 55 if ( ! empty( $this->logger ) ) { 56 $this->set_logger( $this->logger ); 57 } 58 59 // Get the OCDI (main plugin class) instance. 60 $this->ocdi = OneClickDemoImport::get_instance(); 61 } 62 63 64 /** 65 * Include required files. 66 */ 67 private function include_required_files() { 68 if ( ! class_exists( '\WP_Importer' ) ) { 69 require ABSPATH . '/wp-admin/includes/class-wp-importer.php'; 70 } 71 } 72 73 74 /** 75 * Imports content from a WordPress export file. 76 * 77 * @param string $data_file path to xml file, file with WordPress export data. 78 */ 79 public function import( $data_file ) { 80 $this->importer->import( $data_file ); 81 } 82 83 84 /** 85 * Set the logger used in the import 86 * 87 * @param object $logger logger instance. 88 */ 89 public function set_logger( $logger ) { 90 $this->importer->set_logger( $logger ); 91 } 92 93 94 /** 95 * Get all protected variables from the WXR_Importer needed for continuing the import. 96 */ 97 public function get_importer_data() { 98 return $this->importer->get_importer_data(); 99 } 100 101 102 /** 103 * Sets all protected variables from the WXR_Importer needed for continuing the import. 104 * 105 * @param array $data with set variables. 106 */ 107 public function set_importer_data( $data ) { 108 $this->importer->set_importer_data( $data ); 109 } 110 111 112 /** 113 * Import content from an WP XML file. 114 * 115 * @param string $import_file_path Path to the import file. 116 */ 117 public function import_content( $import_file_path ) { 118 $this->microtime = microtime( true ); 119 120 // Increase PHP max execution time. Just in case, even though the AJAX calls are only 25 sec long. 121 set_time_limit( apply_filters( 'pt-ocdi/set_time_limit_for_demo_data_import', 300 ) ); 122 123 // Disable import of authors. 124 add_filter( 'wxr_importer.pre_process.user', '__return_false' ); 125 126 // Check, if we need to send another AJAX request and set the importing author to the current user. 127 add_filter( 'wxr_importer.pre_process.post', array( $this, 'new_ajax_request_maybe' ) ); 128 129 // Disables generation of multiple image sizes (thumbnails) in the content import step. 130 if ( ! apply_filters( 'pt-ocdi/regenerate_thumbnails_in_content_import', true ) ) { 131 add_filter( 'intermediate_image_sizes_advanced', '__return_null' ); 132 } 133 134 // Import content. 135 if ( ! empty( $import_file_path ) ) { 136 ob_start(); 137 $this->import( $import_file_path ); 138 $message = ob_get_clean(); 139 } 140 141 // Return any error messages for the front page output (errors, critical, alert and emergency level messages only). 142 return $this->logger->error_output; 143 } 144 145 146 /** 147 * Check if we need to create a new AJAX request, so that server does not timeout. 148 * 149 * @param array $data current post data. 150 * @return array 151 */ 152 public function new_ajax_request_maybe( $data ) { 153 $time = microtime( true ) - $this->microtime; 154 155 // We should make a new ajax call, if the time is right. 156 if ( $time > apply_filters( 'pt-ocdi/time_for_one_ajax_call', 25 ) ) { 157 $response = array( 158 'status' => 'newAJAX', 159 'message' => 'Time for new AJAX request!: ' . $time, 160 ); 161 162 // Add any output to the log file and clear the buffers. 163 $message = ob_get_clean(); 164 165 // Add any error messages to the frontend_error_messages variable in OCDI main class. 166 if ( ! empty( $message ) ) { 167 $this->ocdi->append_to_frontend_error_messages( $message ); 168 } 169 170 // Add message to log file. 171 $log_added = Helpers::append_to_file( 172 __( 'New AJAX call!' , 'pt-ocdi' ) . PHP_EOL . $message, 173 $this->ocdi->get_log_file_path(), 174 '' 175 ); 176 177 // Set the current importer stat, so it can be continued on the next AJAX call. 178 $this->set_current_importer_data(); 179 180 // Send the request for a new AJAX call. 181 wp_send_json( $response ); 182 } 183 184 // Set importing author to the current user. 185 // Fixes the [WARNING] Could not find the author for ... log warning messages. 186 $current_user_obj = wp_get_current_user(); 187 $data['post_author'] = $current_user_obj->user_login; 188 189 return $data; 190 } 191 192 193 /** 194 * Set current state of the content importer, so we can continue the import with new AJAX request. 195 */ 196 private function set_current_importer_data() { 197 $data = array_merge( $this->ocdi->get_current_importer_data(), $this->get_importer_data() ); 198 199 Helpers::set_ocdi_import_data_transient( $data ); 200 } 201 }