Skip to content

Commit

Permalink
Merge branch 'master' of github.com:awesomemotive/one-click-demo-import
Browse files Browse the repository at this point in the history
  • Loading branch information
donmhico committed Oct 10, 2024
2 parents 414032d + 79d7b6f commit 10f5e09
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
23 changes: 23 additions & 0 deletions inc/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ private static function is_import_file_info_format_correct( $import_file_info )
/**
* Download import files. Content .xml and widgets .wie|.json files.
*
* @since {VERSION} Add WPForms support.
*
* @param array $import_file_info array with import file details.
*
* @return array|WP_Error array of paths to the downloaded files or WP_Error object with error message.
*/
public static function download_import_files( $import_file_info ) {
Expand All @@ -64,6 +67,7 @@ public static function download_import_files( $import_file_info ) {
'widgets' => '',
'customizer' => '',
'redux' => '',
'wpforms' => '',
);
$downloader = new Downloader();

Expand Down Expand Up @@ -170,6 +174,25 @@ public static function download_import_files( $import_file_info ) {
$downloaded_files['redux'] = $redux_items;
}

// ----- Set WPForms file paths -----
// Get WPForms import file as well. If defined!
if ( ! empty( $import_file_info['import_wpforms_file_url'] ) ) {
// Setup filename path to save the WPForms content.
$wpforms_filename = self::apply_filters( 'ocdi/downloaded_wpforms_file_prefix', 'demo-wpforms-import-file_' ) . self::$demo_import_start_time . self::apply_filters( 'ocdi/downloaded_wpforms_file_suffix_and_file_extension', '.json' );

// Download the customizer import file.
$downloaded_files['wpforms'] = $downloader->download_file( $import_file_info['import_wpforms_file_url'], $wpforms_filename );

// Return from this function if there was an error.
if ( is_wp_error( $downloaded_files['wpforms'] ) ) {
return $downloaded_files['wpforms'];
}
} else if ( ! empty( $import_file_info['local_import_wpforms_file'] ) ) {
if ( file_exists( $import_file_info['local_import_wpforms_file'] ) ) {
$downloaded_files['wpforms'] = $import_file_info['local_import_wpforms_file'];
}
}

return $downloaded_files;
}

Expand Down
13 changes: 13 additions & 0 deletions inc/ImportActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function register_hooks() {
add_action( 'ocdi/after_content_import_execution', array( $this, 'before_widget_import_action' ), 10, 3 );
add_action( 'ocdi/after_content_import_execution', array( $this, 'widgets_import' ), 20, 3 );
add_action( 'ocdi/after_content_import_execution', array( $this, 'redux_import' ), 30, 3 );
add_action( 'ocdi/after_content_import_execution', array( $this, 'wpforms_import' ), 40, 3 );

// Customizer import.
add_action( 'ocdi/customizer_import_execution', array( $this, 'customizer_import' ), 10, 1 );
Expand Down Expand Up @@ -86,6 +87,18 @@ public function redux_import( $selected_import_files, $import_files, $selected_i
}
}

/**
* Execute the WPForms import.
*
* @param array $selected_import_files Actual selected import files (content, widgets, customizer, redux).
* @param array $import_files The filtered import files defined in `ocdi/import_files` filter.
* @param int $selected_index Selected index of import.
*/
public function wpforms_import( $selected_import_files, $import_files, $selected_index ) {
if ( ! empty( $selected_import_files['wpforms'] ) ) {
( new WPFormsImporter( $selected_import_files['wpforms'] ) )->import();
}
}

/**
* Execute the customizer import.
Expand Down
2 changes: 1 addition & 1 deletion inc/OneClickDemoImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ public function import_demo_data_ajax_callback() {
* 1 - Before widgets import setup (with priority 10).
* 2 - Import widgets (with priority 20).
* 3 - Import Redux data (with priority 30).
* 4 - Import WPForms data (with priority 40).
*/
Helpers::do_action( 'ocdi/after_content_import_execution', $this->selected_import_files, $this->import_files, $this->selected_index );

Expand All @@ -408,7 +409,6 @@ public function import_demo_data_ajax_callback() {
$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
Expand Down
97 changes: 97 additions & 0 deletions inc/WPFormsImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Class for the Redux importer used in the One Click Demo Import plugin.
*
* @see https://wordpress.org/plugins/wpforms-lite/
* @package ocdi
*/

namespace OCDI;

class WPFormsImporter {

/**
* The path to the import file.
*
* @since {VERSION}
*
* @var string
*/
private $import_file_path = false;

/**
* The OneClickDemoImport instance.
*
* @since {VERSION}
*
* @var OneClickDemoImport
*/
private $ocdi;

/**
* Constructor.
*
* @since {VERSION}
*
* @param string $import_file_path The path to the import file.
*/
public function __construct( $import_file_path ) {

$this->import_file_path = $import_file_path;
$this->ocdi = OneClickDemoImport::get_instance();
}

/**
* Import WPForms data.
*
* @since {VERSION}
*/
public function import() {

// WPForms plugin is not active!
if ( ! class_exists( 'WPForms' ) || ! function_exists( 'wpforms' ) ) {
$this->log_error( esc_html__( 'The WPForms plugin is not activated, so the WPForms import was skipped!', 'one-click-demo-import' ) );
return;
}

$wpforms_api = method_exists( wpforms(), 'obj' ) ? wpforms()->obj( 'api' ) : wpforms()->get("api");

if ( ! is_a( $wpforms_api, "WPForms\API" ) ) {
$this->log_error( esc_html__( 'The WPForms plugin\'s version is not >= v1.8.6, so the WPForms import was skipped!', 'one-click-demo-import' ) );
return;
}

$import = $wpforms_api->import_forms( $this->import_file_path );

if ( is_wp_error( $import ) ) {
$this->log_error( sprintf( 'WPForms import failed: %1$s', $import->get_error_message() ) );
return;
}

Helpers::append_to_file(
esc_html__( 'WPForms import finished successfully!', 'one-click-demo-import' ),
$this->ocdi->get_log_file_path(),
esc_html__( 'Importing WPForms' , 'one-click-demo-import' )
);
}

/**
* Log error message.
*
* @since {VERSION}
*
* @param string $error_message The error message.
*/
private function log_error( $error_message ) {

// Add any error messages to the frontend_error_messages variable in OCDI main class.
$this->ocdi->append_to_frontend_error_messages( $error_message );

// Write error to log file.
Helpers::append_to_file(
$error_message,
$this->ocdi->get_log_file_path(),
esc_html__( 'Importing WPForms' , 'one-click-demo-import' )
);
}
}

0 comments on commit 10f5e09

Please sign in to comment.