| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Admin\Funnel\Importer; |
| 4 |
|
| 5 |
use Sellkit\Admin\Funnel\Importer\Page_Builder\Importer_Base; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || die(); |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Step Importer. |
| 11 |
* |
| 12 |
* @since 1.1.0 |
| 13 |
*/ |
| 14 |
class Step_Importer { |
| 15 |
|
| 16 |
/** |
| 17 |
* Importer object. |
| 18 |
* |
| 19 |
* @var object |
| 20 |
*/ |
| 21 |
public static $importer; |
| 22 |
|
| 23 |
/** |
| 24 |
* Step data. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
public $step_data; |
| 29 |
|
| 30 |
/** |
| 31 |
* Step_Importer constructor. |
| 32 |
* |
| 33 |
* @since 1.1.0 |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
self::$importer = new Importer_Base(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Importing template. |
| 41 |
* |
| 42 |
* @since 1.1.0 |
| 43 |
* @param object $data Funnel id. |
| 44 |
* @param string $new_step_id New step id. |
| 45 |
*/ |
| 46 |
public function import_template( $data, $new_step_id ) { |
| 47 |
self::$importer->push_to_queue( [ |
| 48 |
'data' => $data, |
| 49 |
'new_step_id' => $new_step_id, |
| 50 |
] ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Runs all importing. |
| 55 |
* |
| 56 |
* @since 1.1.0 |
| 57 |
*/ |
| 58 |
public function run() { |
| 59 |
self::$importer->save()->dispatch(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Imports the step |
| 64 |
* |
| 65 |
* @since 1.1.0 |
| 66 |
* @param string $step_id Step id. |
| 67 |
*/ |
| 68 |
public function import_from_api( $step_id ) { |
| 69 |
if ( empty( $step_id ) ) { |
| 70 |
return new \WP_Error( 'sellkit_step_importer_has_no_step_id', esc_html__( 'Does not have any step id for importing', 'sellkit' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
$response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/step/{$step_id}" ); |
| 74 |
|
| 75 |
if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) { |
| 76 |
$step_data = json_decode( $response['body'] ); |
| 77 |
|
| 78 |
return $step_data; |
| 79 |
} |
| 80 |
|
| 81 |
new \WP_Error( 'sellkit_funnel_step_importing_data_error', esc_html__( 'Something went wrong in importing data from API.', 'sellkit' ) ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Imports data to funnel. |
| 86 |
* |
| 87 |
* @since 1.1.0 |
| 88 |
* @param object $data Step data. |
| 89 |
* @param int $funnel_id Funnel id. |
| 90 |
*/ |
| 91 |
public function import_to_funnel( $data, $funnel_id ) { |
| 92 |
$meta_data = (array) $data->meta; |
| 93 |
$step_data = unserialize( $meta_data['step_data'] ); //phpcs:ignore |
| 94 |
|
| 95 |
unset( $step_data['funnel_id'] ); |
| 96 |
unset( $step_data['number'] ); |
| 97 |
unset( $step_data['data'] ); |
| 98 |
|
| 99 |
$funnel_step_data = get_post_meta( $funnel_id, 'sellkit_steps', true ); |
| 100 |
$funnel_step_data = ! empty( $funnel_step_data ) ? $funnel_step_data : []; |
| 101 |
$new_step_data = $this->create_new_step_page( $step_data ); |
| 102 |
$new_funnel_step_data = array_merge( $funnel_step_data, [ $new_step_data ] ); |
| 103 |
|
| 104 |
update_post_meta( $funnel_id, 'sellkit_steps', $new_funnel_step_data ); |
| 105 |
|
| 106 |
$args = array( |
| 107 |
'ID' => $new_step_data['page_id'], |
| 108 |
'post_content' => $data->post_content, |
| 109 |
); |
| 110 |
|
| 111 |
wp_update_post( $args ); |
| 112 |
|
| 113 |
foreach ( $data->meta as $meta_key => $meta_value ) { |
| 114 |
if ( '_elementor_data' === $meta_key ) { |
| 115 |
if ( ! is_array( $meta_value ) ) { |
| 116 |
$elementor_data = add_magic_quotes( $meta_value ); |
| 117 |
$elementor_data = json_decode( $elementor_data, true ); |
| 118 |
} |
| 119 |
update_post_meta( $new_step_data['page_id'], $meta_key, $elementor_data ); |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
if ( '_elementor_css' === $meta_key ) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
if ( is_serialized( $meta_value, true ) ) { |
| 128 |
$meta_value = maybe_unserialize( stripslashes( $meta_value ) ); |
| 129 |
} |
| 130 |
|
| 131 |
if ( 'step_data' === $meta_key ) { |
| 132 |
$meta_value['funnel_id'] = intval( $funnel_id ); |
| 133 |
} |
| 134 |
|
| 135 |
update_post_meta( $new_step_data['page_id'], $meta_key, $meta_value ); |
| 136 |
} |
| 137 |
|
| 138 |
return $new_step_data['page_id']; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Filter step data before saving. |
| 143 |
* |
| 144 |
* @since 1.1.0 |
| 145 |
* @param array $last_step Step data. |
| 146 |
*/ |
| 147 |
public function create_new_step_page( $last_step ) { |
| 148 |
$new_step_id = wp_insert_post( [ |
| 149 |
'post_type' => \Sellkit_Admin_Steps::SELLKIT_STEP_POST_TYPE, |
| 150 |
'post_title' => $last_step['title'], |
| 151 |
'post_name' => sanitize_title( $last_step['title'] ), |
| 152 |
'post_status' => 'publish', |
| 153 |
] ); |
| 154 |
|
| 155 |
$last_step['page_id'] = $new_step_id; |
| 156 |
$last_step['slug'] = get_post_field( 'post_name', $new_step_id ); |
| 157 |
|
| 158 |
return $last_step; |
| 159 |
} |
| 160 |
} |
| 161 |
|