| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Admin\Funnel\Importer; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || die(); |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Ajax handler. |
| 11 |
* |
| 12 |
* @since 1.1.0 |
| 13 |
*/ |
| 14 |
class Ajax_Handler { |
| 15 |
|
| 16 |
/** |
| 17 |
* Step importer object. |
| 18 |
* |
| 19 |
* @since 1.1.0 |
| 20 |
* @var Step_Importer |
| 21 |
*/ |
| 22 |
public $step_importer; |
| 23 |
|
| 24 |
/** |
| 25 |
* Ajax_Handler constructor. |
| 26 |
* |
| 27 |
* @since 1.1.0 |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$this->step_importer = new Step_Importer(); |
| 31 |
|
| 32 |
add_action( 'wp_ajax_sellkit_import_step_data', [ $this, 'import_steps_data' ] ); |
| 33 |
add_action( 'wp_ajax_sellkit_funnel_import_funnel', [ $this, 'import_funnel' ] ); |
| 34 |
add_action( 'wp_ajax_sellkit_funnel_get_funnel_data', [ $this, 'get_funnel_data' ] ); |
| 35 |
add_action( 'wp_ajax_sellkit_funnel_get_steps', [ $this, 'get_steps' ] ); |
| 36 |
add_action( 'wp_ajax_sellkit_funnel_get_funnels', [ $this, 'get_funnels' ] ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Imports step data. |
| 41 |
* |
| 42 |
* @since 1.1.0 |
| 43 |
*/ |
| 44 |
public function import_steps_data() { |
| 45 |
$nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); |
| 46 |
$step_id = sellkit_htmlspecialchars( INPUT_GET, 'step_id' ); |
| 47 |
$funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' ); |
| 48 |
|
| 49 |
wp_verify_nonce( $nonce, 'sellkit' ); |
| 50 |
|
| 51 |
$data = $this->step_importer->import_from_api( $step_id ); |
| 52 |
$new_step_id = $this->step_importer->import_to_funnel( $data, $funnel_id ); |
| 53 |
|
| 54 |
$this->step_importer->import_template( $data, $new_step_id ); |
| 55 |
$this->step_importer->run(); |
| 56 |
|
| 57 |
sleep( 3 ); |
| 58 |
|
| 59 |
wp_send_json_success( esc_html__( 'The data has been imported successfully.' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Gets steps. |
| 64 |
* |
| 65 |
* @since 1.1.0 |
| 66 |
*/ |
| 67 |
public function get_steps() { |
| 68 |
$nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); |
| 69 |
$type = sellkit_htmlspecialchars( INPUT_GET, 'type' ); |
| 70 |
|
| 71 |
wp_verify_nonce( $nonce, 'sellkit' ); |
| 72 |
|
| 73 |
$response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/steps/type={$type}", [ |
| 74 |
'timeout' => 10, |
| 75 |
] ); |
| 76 |
|
| 77 |
if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) { |
| 78 |
$step_data = json_decode( $response['body'] ); |
| 79 |
|
| 80 |
wp_send_json_success( $step_data ); |
| 81 |
} |
| 82 |
|
| 83 |
wp_send_json_error( esc_html__( 'Something went wrong', 'sellkit' ) ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Gets Funnels. |
| 88 |
* |
| 89 |
* @since 1.1.0 |
| 90 |
*/ |
| 91 |
public function get_funnels() { |
| 92 |
$nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); |
| 93 |
|
| 94 |
wp_verify_nonce( $nonce, 'sellkit' ); |
| 95 |
|
| 96 |
$response = wp_remote_get( 'https://templates.getsellkit.com/wp-json/sellkit/v1/funnels', [ |
| 97 |
'timeout' => 10, |
| 98 |
] ); |
| 99 |
|
| 100 |
if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) { |
| 101 |
$funnel_data = json_decode( $response['body'] ); |
| 102 |
|
| 103 |
wp_send_json_success( $funnel_data ); |
| 104 |
} |
| 105 |
|
| 106 |
wp_send_json_error( esc_html__( 'Something went wrong', 'sellkit' ) ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Gets funnel data. |
| 111 |
* |
| 112 |
* @since 1.1.0 |
| 113 |
*/ |
| 114 |
public function get_funnel_data() { |
| 115 |
$nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); |
| 116 |
$funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' ); |
| 117 |
|
| 118 |
wp_verify_nonce( $nonce, 'sellkit' ); |
| 119 |
|
| 120 |
$funnel_data = $this->import_funnel_data_from_api( $funnel_id ); |
| 121 |
|
| 122 |
if ( ! empty( $funnel_data ) ) { |
| 123 |
wp_send_json_success( $funnel_data ); |
| 124 |
} |
| 125 |
|
| 126 |
wp_send_json_error( esc_html__( 'Something went wrong', 'sellkit' ) ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Imports step data. |
| 131 |
* |
| 132 |
* @since 1.1.0 |
| 133 |
*/ |
| 134 |
public function import_funnel() { |
| 135 |
$nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); |
| 136 |
$funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' ); |
| 137 |
|
| 138 |
wp_verify_nonce( $nonce, 'sellkit' ); |
| 139 |
|
| 140 |
$funnel_data = $this->import_funnel_data_from_api( $funnel_id ); |
| 141 |
|
| 142 |
if ( empty( $funnel_data ) ) { |
| 143 |
wp_send_json_error( __( 'something went wrong', 'sellkit' ) ); |
| 144 |
} |
| 145 |
|
| 146 |
$steps = $funnel_data->sellkit_steps; |
| 147 |
$new_funnel_id = $this->create_new_funnel( $funnel_data ); |
| 148 |
$new_step_data = []; |
| 149 |
|
| 150 |
foreach ( $steps as $step ) { |
| 151 |
$data = $this->step_importer->import_from_api( $step->page_id ); |
| 152 |
$new_step_id = $this->step_importer->import_to_funnel( $data, $new_funnel_id ); |
| 153 |
|
| 154 |
$this->step_importer->import_template( $data, $new_step_id ); |
| 155 |
|
| 156 |
$step->page_id = $new_step_id; |
| 157 |
$step->funnel_id = $new_funnel_id; |
| 158 |
|
| 159 |
if ( ! empty( $step->data->products->list ) ) { |
| 160 |
unset( $step->data->products->list ); |
| 161 |
} |
| 162 |
|
| 163 |
if ( ! empty( $step->bump[0]->data->products->list ) ) { |
| 164 |
unset( $step->bump[0]->data->products->list ); |
| 165 |
} |
| 166 |
|
| 167 |
$new_step_data[] = (array) $step; |
| 168 |
} |
| 169 |
|
| 170 |
update_post_meta( $new_funnel_id, 'sellkit_steps', $new_step_data ); |
| 171 |
|
| 172 |
$this->step_importer->run(); |
| 173 |
|
| 174 |
wp_send_json_success( [ 'funnelId' => $new_funnel_id ] ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* It creates new funnel. |
| 179 |
* |
| 180 |
* @param object $funnel_data Funnel data. |
| 181 |
*/ |
| 182 |
public function create_new_funnel( $funnel_data ) { |
| 183 |
$new_funnel_id = wp_insert_post( [ |
| 184 |
'post_type' => $funnel_data->post_type, |
| 185 |
'post_title' => $funnel_data->title, |
| 186 |
'post_name' => sanitize_title( $funnel_data->title ), |
| 187 |
'post_status' => 'draft', |
| 188 |
] ); |
| 189 |
|
| 190 |
return $new_funnel_id; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Imports funnel data. |
| 195 |
* |
| 196 |
* @since 1.1.0 |
| 197 |
* @param int $funnel_id Funnel Id. |
| 198 |
*/ |
| 199 |
public function import_funnel_data_from_api( $funnel_id ) { |
| 200 |
$response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/funnel/{$funnel_id}", [ |
| 201 |
'timeout' => 10, |
| 202 |
] ); |
| 203 |
|
| 204 |
if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) { |
| 205 |
$funnel_data = json_decode( $response['body'] ); |
| 206 |
|
| 207 |
return $funnel_data; |
| 208 |
} |
| 209 |
|
| 210 |
return false; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
new Ajax_Handler(); |
| 215 |
|