ReduxImporter.php (2805B)
1 <?php 2 /** 3 * Class for the Redux importer used in the One Click Demo Import plugin. 4 * 5 * @see https://wordpress.org/plugins/redux-framework/ 6 * @package ocdi 7 */ 8 9 namespace OCDI; 10 11 class ReduxImporter { 12 /** 13 * Import Redux data from a JSON file, generated by the Redux plugin. 14 * 15 * @param array $import_data Array of arrays. Child array contains 'option_name' and 'file_path'. 16 */ 17 public static function import( $import_data ) { 18 $ocdi = OneClickDemoImport::get_instance(); 19 $log_file_path = $ocdi->get_log_file_path(); 20 21 // Redux plugin is not active! 22 if ( ! class_exists( 'ReduxFramework' ) ) { 23 $error_message = esc_html__( 'The Redux plugin is not activated, so the Redux import was skipped!', 'pt-ocdi' ); 24 25 // Add any error messages to the frontend_error_messages variable in OCDI main class. 26 $ocdi->append_to_frontend_error_messages( $error_message ); 27 28 // Write error to log file. 29 Helpers::append_to_file( 30 $error_message, 31 $log_file_path, 32 esc_html__( 'Importing Redux settings' , 'pt-ocdi' ) 33 ); 34 35 return; 36 } 37 38 foreach ( $import_data as $redux_item ) { 39 $redux_options_raw_data = Helpers::data_from_file( $redux_item['file_path'] ); 40 preg_match_all("!https?:[^?#]+\.(?:jpe?g|png|gif)!Ui",$redux_options_raw_data , $remote_url_new); 41 foreach($remote_url_new[0] as $remote_url){ 42 if(strpos($remote_url,"wp-content")!==false){ 43 $urlpath=substr_replace($remote_url, "", 0, strpos($remote_url,"wp-content")+11); 44 $site_url=rtrim(site_url(), '/'); 45 $remote_url_rep= $site_url."/wp-content/".$urlpath; 46 $redux_options_raw_data=str_replace($remote_url,$remote_url_rep,$redux_options_raw_data); 47 } 48 } 49 50 51 $redux_options_data = json_decode( $redux_options_raw_data, true ); 52 53 $redux_framework = \ReduxFrameworkInstances::get_instance( $redux_item['option_name'] ); 54 55 if ( isset( $redux_framework->args['opt_name'] ) ) { 56 // Import Redux settings. 57 $redux_framework->set_options( $redux_options_data ); 58 59 // Add this message to log file. 60 $log_added = Helpers::append_to_file( 61 sprintf( esc_html__( 'Redux settings import for: %s finished successfully!', 'pt-ocdi' ), $redux_item['option_name'] ), 62 $log_file_path, 63 esc_html__( 'Importing Redux settings' , 'pt-ocdi' ) 64 ); 65 } 66 else { 67 $error_message = sprintf( esc_html__( 'The Redux option name: %s, was not found in this WP site, so it was not imported!', 'pt-ocdi' ), $redux_item['option_name'] ); 68 69 // Add any error messages to the frontend_error_messages variable in OCDI main class. 70 $ocdi->append_to_frontend_error_messages( $error_message ); 71 72 // Write error to log file. 73 Helpers::append_to_file( 74 $error_message, 75 $log_file_path, 76 esc_html__( 'Importing Redux settings' , 'pt-ocdi' ) 77 ); 78 } 79 } 80 } 81 }