# sellkit/1.1.4/includes/admin/funnel/importer/step-importer.php

SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster, version 1.1.4. 161 lines.

- Page: https://pluginprobe.com/plugins/sellkit/1.1.4/code/includes/admin/funnel/importer/step-importer.php
- Raw: https://pluginprobe.com/plugins/sellkit/1.1.4/raw/includes/admin/funnel/importer/step-importer.php
- Modified: 2022-04-14T05:16:22+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sellkit/1.1.4/code/includes/admin/funnel/importer/step-importer.php#L10-L20`.

```php
<?php

namespace Sellkit\Admin\Funnel\Importer;

use Sellkit\Admin\Funnel\Importer\Page_Builder\Importer_Base;

defined( 'ABSPATH' ) || die();

/**
 * Class Step Importer.
 *
 * @since 1.1.0
 */
class Step_Importer {

	/**
	 * Importer object.
	 *
	 * @var object
	 */
	public static $importer;

	/**
	 * Step data.
	 *
	 * @var array
	 */
	public $step_data;

	/**
	 * Step_Importer constructor.
	 *
	 * @since 1.1.0
	 */
	public function __construct() {
		self::$importer = new Importer_Base();
	}

	/**
	 * Importing template.
	 *
	 * @since 1.1.0
	 * @param object $data Funnel id.
	 * @param string $new_step_id New step id.
	 */
	public function import_template( $data, $new_step_id ) {
		self::$importer->push_to_queue( [
			'data' => $data,
			'new_step_id' => $new_step_id,
		] );
	}

	/**
	 * Runs all importing.
	 *
	 * @since 1.1.0
	 */
	public function run() {
		self::$importer->save()->dispatch();
	}

	/**
	 * Imports the step
	 *
	 * @since 1.1.0
	 * @param string $step_id Step id.
	 */
	public function import_from_api( $step_id ) {
		if ( empty( $step_id ) ) {
			return new \WP_Error( 'sellkit_step_importer_has_no_step_id', esc_html__( 'Does not have any step id for importing', 'sellkit' ) );
		}

		$response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/step/{$step_id}" );

		if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
			$step_data = json_decode( $response['body'] );

			return $step_data;
		}

		new \WP_Error( 'sellkit_funnel_step_importing_data_error', esc_html__( 'Something went wrong in importing data from API.', 'sellkit' ) );
	}

	/**
	 * Imports data to funnel.
	 *
	 * @since 1.1.0
	 * @param object $data Step data.
	 * @param int    $funnel_id Funnel id.
	 */
	public function import_to_funnel( $data, $funnel_id ) {
		$meta_data = (array) $data->meta;
		$step_data = unserialize( $meta_data['step_data'] ); //phpcs:ignore

		unset( $step_data['funnel_id'] );
		unset( $step_data['number'] );
		unset( $step_data['data'] );

		$funnel_step_data     = get_post_meta( $funnel_id, 'sellkit_steps', true );
		$funnel_step_data     = ! empty( $funnel_step_data ) ? $funnel_step_data : [];
		$new_step_data        = $this->create_new_step_page( $step_data );
		$new_funnel_step_data = array_merge( $funnel_step_data, [ $new_step_data ] );

		update_post_meta( $funnel_id, 'sellkit_steps', $new_funnel_step_data );

		$args = array(
			'ID'           => $new_step_data['page_id'],
			'post_content' => $data->post_content,
		);

		wp_update_post( $args );

		foreach ( $data->meta as $meta_key => $meta_value ) {
			if ( '_elementor_data' === $meta_key ) {
				if ( ! is_array( $meta_value ) ) {
					$elementor_data = add_magic_quotes( $meta_value );
					$elementor_data = json_decode( $elementor_data, true );
				}
				update_post_meta( $new_step_data['page_id'], $meta_key, $elementor_data );
				continue;
			}

			if ( '_elementor_css' === $meta_key ) {
				continue;
			}

			if ( is_serialized( $meta_value, true ) ) {
				$meta_value = maybe_unserialize( stripslashes( $meta_value ) );
			}

			if ( 'step_data' === $meta_key ) {
				$meta_value['funnel_id'] = intval( $funnel_id );
			}

			update_post_meta( $new_step_data['page_id'], $meta_key, $meta_value );
		}

		return $new_step_data['page_id'];
	}

	/**
	 * Filter step data before saving.
	 *
	 * @since 1.1.0
	 * @param array $last_step Step data.
	 */
	public function create_new_step_page( $last_step ) {
		$new_step_id = wp_insert_post( [
			'post_type' => \Sellkit_Admin_Steps::SELLKIT_STEP_POST_TYPE,
			'post_title' => $last_step['title'],
			'post_name' => sanitize_title( $last_step['title'] ),
			'post_status' => 'publish',
		] );

		$last_step['page_id'] = $new_step_id;
		$last_step['slug']    = get_post_field( 'post_name', $new_step_id );

		return $last_step;
	}
}

```
