balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

WPCLICommands.php (9514B)


      1 <?php
      2 /**
      3  * The class for WP-CLI commands for One Click Demo Import plugin.
      4  *
      5  * @package ocdi
      6  */
      7 
      8 namespace OCDI;
      9 
     10 use WP_CLI;
     11 
     12 class WPCLICommands extends \WP_CLI_Command {
     13 
     14 	/**
     15 	 * @var object Main OCDI class object.
     16 	 */
     17 	private $ocdi;
     18 
     19 	public function __construct() {
     20 		parent::__construct();
     21 
     22 		$this->ocdi = OneClickDemoImport::get_instance();
     23 
     24 		Helpers::set_demo_import_start_time();
     25 
     26 		$this->ocdi->log_file_path = Helpers::get_log_path();
     27 	}
     28 
     29 	/**
     30 	 * List all predefined demo imports.
     31 	 */
     32 	public function list_predefined() {
     33 		if ( empty( $this->ocdi->import_files ) ) {
     34 			WP_CLI::error( esc_html__( 'There are no predefined demo imports for currently active theme!', 'pt-ocdi' ) );
     35 		}
     36 
     37 		WP_CLI::success( esc_html__( 'Here are the predefined demo imports:', 'pt-ocdi' ) );
     38 
     39 		foreach ( $this->ocdi->import_files as $index => $import_file ) {
     40 			WP_CLI::log( sprintf(
     41 				'%d -> %s [content: %s, widgets: %s, customizer: %s, redux: %s]',
     42 				$index,
     43 				$import_file['import_file_name'],
     44 				empty( $import_file['import_file_url'] ) && empty( $import_file['local_import_file'] ) ? 'no' : 'yes',
     45 				empty( $import_file['import_widget_file_url'] ) && empty( $import_file['local_import_widget_file'] ) ? 'no' : 'yes',
     46 				empty( $import_file['import_customizer_file_url'] ) && empty( $import_file['local_import_customizer_file'] ) ? 'no' : 'yes',
     47 				empty( $import_file['import_redux'] ) && empty( $import_file['local_import_redux'] ) ? 'no' : 'yes'
     48 			) );
     49 		}
     50 	}
     51 
     52 	/**
     53 	 * Import content/widgets/customizer settings with the OCDI plugin.
     54 	 *
     55 	 * ## OPTIONS
     56 	 *
     57 	 * [--content=<file>]
     58 	 * : Content file (XML), that will be used to import the content.
     59 	 *
     60 	 * [--widgets=<file>]
     61 	 * : Widgets file (JSON or WIE), that will be used to import the widgets.
     62 	 *
     63 	 * [--customizer=<file>]
     64 	 * : Customizer file (DAT), that will be used to import the customizer settings.
     65 	 *
     66 	 * [--predefined=<index>]
     67 	 * : The index of the predefined demo imports (use the 'list_predefined' command to check the predefined demo imports)
     68 	 */
     69 	public function import( $args, $assoc_args ) {
     70 		if ( ! $this->any_import_options_set( $assoc_args ) ) {
     71 			WP_CLI::error( esc_html__( 'At least one of the possible options should be set! Check them with --help', 'pt-ocdi' ) );
     72 		}
     73 
     74 		if ( isset( $assoc_args['predefined'] ) ) {
     75 			$this->import_predefined( $assoc_args['predefined'] );
     76 		}
     77 
     78 		if ( ! empty( $assoc_args['content'] ) ) {
     79 			$this->import_content( $assoc_args['content'] );
     80 		}
     81 
     82 		if ( ! empty( $assoc_args['widgets'] ) ) {
     83 			$this->import_widgets( $assoc_args['widgets'] );
     84 		}
     85 
     86 		if ( ! empty( $assoc_args['customizer'] ) ) {
     87 			$this->import_customizer( $assoc_args['customizer'] );
     88 		}
     89 	}
     90 
     91 	/**
     92 	 * Check if any of the possible options are set.
     93 	 *
     94 	 * @param array $options
     95 	 *
     96 	 * @return bool
     97 	 */
     98 	private function any_import_options_set( $options ) {
     99 		$possible_options = array(
    100 			'content',
    101 			'widgets',
    102 			'customizer',
    103 			'predefined',
    104 		);
    105 
    106 		foreach ( $possible_options as $option ) {
    107 			if ( array_key_exists( $option, $options ) ) {
    108 				return true;
    109 			}
    110 		}
    111 
    112 		return false;
    113 	}
    114 
    115 	/**
    116 	 * Import the predefined demo content/widgets/customizer settings with OCDI.
    117 	 *
    118 	 * @param int $predefined_index Index of a OCDI predefined demo import.
    119 	 */
    120 	private function import_predefined( $predefined_index ) {
    121 		if ( ! is_numeric( $predefined_index ) ) {
    122 			WP_CLI::error( esc_html__( 'The "predefined" parameter should be a number (an index of the OCDI predefined demo import)!', 'pt-ocdi' ) );
    123 		}
    124 
    125 		$predefined_index = absint( $predefined_index );
    126 
    127 		if ( ! array_key_exists( $predefined_index, $this->ocdi->import_files ) ) {
    128 			WP_CLI::warning( esc_html__( 'The supplied predefined index does not exist! Please take a look at the available predefined demo imports:', 'pt-ocdi' ) );
    129 
    130 			$this->list_predefined();
    131 
    132 			return false;
    133 		}
    134 
    135 		WP_CLI::log( esc_html__( 'Predefined demo import started! All other parameters will be ignored!', 'pt-ocdi' ) );
    136 
    137 		$selected_files = $this->ocdi->import_files[ $predefined_index ];
    138 
    139 		if ( ! empty( $selected_files['import_file_name'] ) ) {
    140 			WP_CLI::log( sprintf( esc_html__( 'Selected predefined demo import: %s', 'pt-ocdi' ), $selected_files['import_file_name'] ) );
    141 		}
    142 
    143 		WP_CLI::log( esc_html__( 'Preparing the demo import files...', 'pt-ocdi' ) );
    144 
    145 		$import_files =	Helpers::download_import_files( $selected_files );
    146 
    147 		if ( empty( $import_files ) ) {
    148 			WP_CLI::error( esc_html__( 'Demo import files could not be retrieved!', 'pt-ocdi' ) );
    149 		}
    150 
    151 		WP_CLI::log( esc_html__( 'Demo import files retrieved successfully!', 'pt-ocdi' ) );
    152 
    153 		WP_CLI::log( esc_html__( 'Importing...', 'pt-ocdi' ) );
    154 
    155 		if ( ! empty( $import_files['content'] ) ) {
    156 			$this->do_action( 'pt-ocdi/before_content_import_execution', $import_files, $this->ocdi->import_files, $predefined_index );
    157 
    158 			$this->import_content( $import_files['content'] );
    159 		}
    160 
    161 		if ( ! empty( $import_files['widgets'] ) ) {
    162 			$this->do_action( 'pt-ocdi/before_widgets_import', $import_files );
    163 
    164 			$this->import_widgets( $import_files['widgets'] );
    165 		}
    166 
    167 		if ( ! empty( $import_files['customizer'] ) ) {
    168 			$this->import_customizer( $import_files['customizer'] );
    169 		}
    170 
    171 		$this->do_action( 'pt-ocdi/after_all_import_execution', $import_files, $this->ocdi->import_files, $predefined_index );
    172 
    173 		WP_CLI::log( esc_html__( 'Predefined import finished!', 'pt-ocdi' ) );
    174 	}
    175 
    176 	/**
    177 	 * Import the content with OCDI.
    178 	 *
    179 	 * @param string $relative_file_path Relative file path to the content import file.
    180 	 */
    181 	private function import_content( $relative_file_path ) {
    182 		$content_import_file_path = realpath( $relative_file_path );
    183 
    184 		if ( ! file_exists( $content_import_file_path ) ) {
    185 			WP_CLI::warning( esc_html__( 'Content import file provided does not exist! Skipping this import!', 'pt-ocdi' ) );
    186 			return false;
    187 		}
    188 
    189 		// Change the single AJAX call duration so the whole content import will be done in one go.
    190 		add_filter( 'pt-ocdi/time_for_one_ajax_call', function() {
    191 			return 3600;
    192 		} );
    193 
    194 		WP_CLI::log( esc_html__( 'Importing content (this might take a while)...', 'pt-ocdi' ) );
    195 
    196 		Helpers::append_to_file( '', $this->ocdi->log_file_path, esc_html__( 'Importing content' , 'pt-ocdi' ) );
    197 
    198 		$this->ocdi->append_to_frontend_error_messages( $this->ocdi->importer->import_content( $content_import_file_path ) );
    199 
    200 		if( empty( $this->ocdi->frontend_error_messages ) ) {
    201 			WP_CLI::success( esc_html__( 'Content import finished!', 'pt-ocdi' ) );
    202 		}
    203 		else {
    204 			WP_CLI::warning( esc_html__( 'There were some issues while importing the content!', 'pt-ocdi' ) );
    205 
    206 			foreach ( $this->ocdi->frontend_error_messages as $line ) {
    207 				WP_CLI::log( $line );
    208 			}
    209 
    210 			$this->ocdi->frontend_error_messages = array();
    211 		}
    212 	}
    213 
    214 	/**
    215 	 * Import the widgets with OCDI.
    216 	 *
    217 	 * @param string $relative_file_path Relative file path to the widgets import file.
    218 	 */
    219 	private function import_widgets( $relative_file_path ) {
    220 		$widgets_import_file_path = realpath( $relative_file_path );
    221 
    222 		if ( ! file_exists( $widgets_import_file_path ) ) {
    223 			WP_CLI::warning( esc_html__( 'Widgets import file provided does not exist! Skipping this import!', 'pt-ocdi' ) );
    224 			return false;
    225 		}
    226 
    227 		WP_CLI::log( esc_html__( 'Importing widgets...', 'pt-ocdi' ) );
    228 
    229 		WidgetImporter::import( $widgets_import_file_path );
    230 
    231 		if( empty( $this->ocdi->frontend_error_messages ) ) {
    232 			WP_CLI::success( esc_html__( 'Widgets imported successfully!', 'pt-ocdi' ) );
    233 		}
    234 		else {
    235 			WP_CLI::warning( esc_html__( 'There were some issues while importing widgets!', 'pt-ocdi' ) );
    236 
    237 			foreach ( $this->ocdi->frontend_error_messages as $line ) {
    238 				WP_CLI::log( $line );
    239 			}
    240 
    241 			$this->ocdi->frontend_error_messages = array();
    242 		}
    243 	}
    244 
    245 	/**
    246 	 * Import the customizer settings with OCDI.
    247 	 *
    248 	 * @param string $relative_file_path Relative file path to the customizer import file.
    249 	 */
    250 	private function import_customizer( $relative_file_path ) {
    251 		$customizer_import_file_path = realpath( $relative_file_path );
    252 
    253 		if ( ! file_exists( $customizer_import_file_path ) ) {
    254 			WP_CLI::warning( esc_html__( 'Customizer import file provided does not exist! Skipping this import!', 'pt-ocdi' ) );
    255 			return false;
    256 		}
    257 
    258 		WP_CLI::log( esc_html__( 'Importing customizer settings...', 'pt-ocdi' ) );
    259 
    260 		CustomizerImporter::import( $customizer_import_file_path );
    261 
    262 		if( empty( $this->ocdi->frontend_error_messages ) ) {
    263 			WP_CLI::success( esc_html__( 'Customizer settings imported successfully!', 'pt-ocdi' ) );
    264 		}
    265 		else {
    266 			WP_CLI::warning( esc_html__( 'There were some issues while importing customizer settings!', 'pt-ocdi' ) );
    267 
    268 			foreach ( $this->ocdi->frontend_error_messages as $line ) {
    269 				WP_CLI::log( $line );
    270 			}
    271 
    272 			$this->ocdi->frontend_error_messages = array();
    273 		}
    274 	}
    275 
    276 	/**
    277 	 * Run the registered actions.
    278 	 *
    279 	 * @param string $action            Name of the action.
    280 	 * @param array  $selected_files    Selected import files.
    281 	 * @param array  $all_import_files  All predefined demos.
    282 	 * @param null   $selected_index    Selected predefined index.
    283 	 */
    284 	private function do_action( $action, $import_files = array(), $all_import_files = array(), $selected_index = null ) {
    285 		if ( false !== has_action( $action ) ) {
    286 			WP_CLI::log( sprintf( esc_html__( 'Executing action: %s ...', 'pt-ocdi' ), $action ) );
    287 
    288 			ob_start();
    289 				do_action( $action, $import_files, $all_import_files, $selected_index );
    290 			$message = ob_get_clean();
    291 
    292 			Helpers::append_to_file( $message, $this->ocdi->log_file_path, $action );
    293 		}
    294 	}
    295 }