ImportActions.php (6131B)
1 <?php 2 /** 3 * Class for the import actions used in the One Click Demo Import plugin. 4 * Register default WP actions for OCDI plugin. 5 * 6 * @package ocdi 7 */ 8 9 namespace OCDI; 10 11 class ImportActions { 12 /** 13 * Register all action hooks for this class. 14 */ 15 public function register_hooks() { 16 // Before content import. 17 add_action( 'pt-ocdi/before_content_import_execution', array( $this, 'before_content_import_action' ), 10, 3 ); 18 19 // After content import. 20 add_action( 'pt-ocdi/after_content_import_execution', array( $this, 'before_widget_import_action' ), 10, 3 ); 21 add_action( 'pt-ocdi/after_content_import_execution', array( $this, 'widgets_import' ), 20, 3 ); 22 add_action( 'pt-ocdi/after_content_import_execution', array( $this, 'redux_import' ), 30, 3 ); 23 24 // Customizer import. 25 add_action( 'pt-ocdi/customizer_import_execution', array( $this, 'customizer_import' ), 10, 1 ); 26 27 // After full import action. 28 add_action( 'pt-ocdi/after_all_import_execution', array( $this, 'after_import_action' ), 10, 3 ); 29 30 // Special widget import cases. 31 if ( apply_filters( 'pt_ocdi/enable_custom_menu_widget_ids_fix', true ) ) { 32 add_action( 'pt-ocdi/widget_settings_array', array( $this, 'fix_custom_menu_widget_ids' ) ); 33 } 34 } 35 36 37 /** 38 * Change the menu IDs in the custom menu widgets in the widget import data. 39 * This solves the issue with custom menu widgets not having the correct (new) menu ID, because they 40 * have the old menu ID from the export site. 41 * 42 * @param array $widget The widget settings array. 43 */ 44 public function fix_custom_menu_widget_ids( $widget ) { 45 // Skip (no changes needed), if this is not a custom menu widget. 46 if ( ! array_key_exists( 'nav_menu', $widget ) || empty( $widget['nav_menu'] ) || ! is_int( $widget['nav_menu'] ) ) { 47 return $widget; 48 } 49 50 // Get import data, with new menu IDs. 51 $ocdi = OneClickDemoImport::get_instance(); 52 $content_import_data = $ocdi->importer->get_importer_data(); 53 $term_ids = $content_import_data['mapping']['term_id']; 54 55 // Set the new menu ID for the widget. 56 $widget['nav_menu'] = $term_ids[ $widget['nav_menu'] ]; 57 58 return $widget; 59 } 60 61 62 /** 63 * Execute the widgets import. 64 * 65 * @param array $selected_import_files Actual selected import files (content, widgets, customizer, redux). 66 * @param array $import_files The filtered import files defined in `pt-ocdi/import_files` filter. 67 * @param int $selected_index Selected index of import. 68 */ 69 public function widgets_import( $selected_import_files, $import_files, $selected_index ) { 70 if ( ! empty( $selected_import_files['widgets'] ) ) { 71 WidgetImporter::import( $selected_import_files['widgets'] ); 72 } 73 } 74 75 76 /** 77 * Execute the Redux import. 78 * 79 * @param array $selected_import_files Actual selected import files (content, widgets, customizer, redux). 80 * @param array $import_files The filtered import files defined in `pt-ocdi/import_files` filter. 81 * @param int $selected_index Selected index of import. 82 */ 83 public function redux_import( $selected_import_files, $import_files, $selected_index ) { 84 if ( ! empty( $selected_import_files['redux'] ) ) { 85 ReduxImporter::import( $selected_import_files['redux'] ); 86 } 87 } 88 89 90 /** 91 * Execute the customizer import. 92 * 93 * @param array $selected_import_files Actual selected import files (content, widgets, customizer, redux). 94 * @param array $import_files The filtered import files defined in `pt-ocdi/import_files` filter. 95 * @param int $selected_index Selected index of import. 96 */ 97 public function customizer_import( $selected_import_files ) { 98 if ( ! empty( $selected_import_files['customizer'] ) ) { 99 CustomizerImporter::import( $selected_import_files['customizer'] ); 100 } 101 } 102 103 104 /** 105 * Execute the action: 'pt-ocdi/before_content_import'. 106 * 107 * @param array $selected_import_files Actual selected import files (content, widgets, customizer, redux). 108 * @param array $import_files The filtered import files defined in `pt-ocdi/import_files` filter. 109 * @param int $selected_index Selected index of import. 110 */ 111 public function before_content_import_action( $selected_import_files, $import_files, $selected_index ) { 112 $this->do_import_action( 'pt-ocdi/before_content_import', $import_files[ $selected_index ] ); 113 } 114 115 116 /** 117 * Execute the action: 'pt-ocdi/before_widgets_import'. 118 * 119 * @param array $selected_import_files Actual selected import files (content, widgets, customizer, redux). 120 * @param array $import_files The filtered import files defined in `pt-ocdi/import_files` filter. 121 * @param int $selected_index Selected index of import. 122 */ 123 public function before_widget_import_action( $selected_import_files, $import_files, $selected_index ) { 124 $this->do_import_action( 'pt-ocdi/before_widgets_import', $import_files[ $selected_index ] ); 125 } 126 127 128 /** 129 * Execute the action: 'pt-ocdi/after_import'. 130 * 131 * @param array $selected_import_files Actual selected import files (content, widgets, customizer, redux). 132 * @param array $import_files The filtered import files defined in `pt-ocdi/import_files` filter. 133 * @param int $selected_index Selected index of import. 134 */ 135 public function after_import_action( $selected_import_files, $import_files, $selected_index ) { 136 $this->do_import_action( 'pt-ocdi/after_import', $import_files[ $selected_index ] ); 137 } 138 139 140 /** 141 * Register the do_action hook, so users can hook to these during import. 142 * 143 * @param string $action The action name to be executed. 144 * @param array $selected_import The data of selected import from `pt-ocdi/import_files` filter. 145 */ 146 private function do_import_action( $action, $selected_import ) { 147 if ( false !== has_action( $action ) ) { 148 $ocdi = OneClickDemoImport::get_instance(); 149 $log_file_path = $ocdi->get_log_file_path(); 150 151 ob_start(); 152 do_action( $action, $selected_import ); 153 $message = ob_get_clean(); 154 155 // Add this message to log file. 156 $log_added = Helpers::append_to_file( 157 $message, 158 $log_file_path, 159 $action 160 ); 161 } 162 } 163 }