PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / admin / funnel / importer / ajax-handler.php

ajax-handler.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster trunk, at includes/admin/funnel/importer/ajax-handler.php

285 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Admin\Funnel\Importer;
4
5 defined( 'ABSPATH' ) || die();
6
7 use Sellkit\Global_Checkout\Checkout;
8
9 /**
10 * Class Ajax handler.
11 *
12 * @SuppressWarnings(PHPMD.NPathComplexity)
13 * @since 1.1.0
14 */
15 class Ajax_Handler {
16
17 /**
18 * Step importer object.
19 *
20 * @since 1.1.0
21 * @var Step_Importer
22 */
23 public $step_importer;
24
25 /**
26 * Ajax_Handler constructor.
27 *
28 * @since 1.1.0
29 */
30 public function __construct() {
31 $this->step_importer = new Step_Importer();
32
33 add_action( 'wp_ajax_sellkit_import_step_data', [ $this, 'import_steps_data' ] );
34 add_action( 'wp_ajax_sellkit_funnel_import_funnel', [ $this, 'import_funnel' ] );
35 add_action( 'wp_ajax_sellkit_funnel_get_funnel_data', [ $this, 'get_funnel_data' ] );
36 add_action( 'wp_ajax_sellkit_funnel_get_steps', [ $this, 'get_steps' ] );
37 add_action( 'wp_ajax_sellkit_funnel_get_funnels', [ $this, 'get_funnels' ] );
38 }
39
40 /**
41 * Imports step data.
42 *
43 * @since 1.1.0
44 */
45 public function import_steps_data() {
46 check_ajax_referer( 'sellkit', 'nonce' );
47
48 $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' );
49 $step_id = sellkit_htmlspecialchars( INPUT_GET, 'step_id' );
50 $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' );
51 $origin_node = filter_input( INPUT_GET, 'origin_node', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
52 $target_node = filter_input( INPUT_GET, 'target_node', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
53 $target_index = filter_input( INPUT_GET, 'target_index', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
54 $origin_node = isset( $origin_node ) ? $origin_node : 'none';
55 $target_node = ! empty( $target_node ) ? $target_node : 'none';
56
57 if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) {
58 wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) );
59 }
60
61 $data = $this->step_importer->import_from_api( $step_id );
62
63 if ( empty( $data ) || is_wp_error( $data ) ) {
64 wp_send_json_error( __( 'Something went wrong.', 'sellkit' ) );
65 }
66
67 $new_step_id = $this->step_importer->import_to_funnel( $data, $funnel_id, $origin_node, $target_node, $target_index );
68
69 $this->step_importer->import_template( $data, $new_step_id );
70 $this->step_importer->run();
71
72 sleep( 3 );
73
74 wp_send_json_success( esc_html__( 'The data has been imported successfully.', 'sellkit' ) );
75 }
76
77 /**
78 * Gets steps.
79 *
80 * @since 1.1.0
81 */
82 public function get_steps() {
83 check_ajax_referer( 'sellkit', 'nonce' );
84
85 $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' );
86 $type = sellkit_htmlspecialchars( INPUT_GET, 'type' );
87
88 if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) {
89 wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) );
90 }
91
92 $response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/steps/type={$type}", [
93 'timeout' => 10,
94 ] );
95
96 if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
97 $step_data = json_decode( $response['body'] );
98
99 wp_send_json_success( $step_data );
100 }
101
102 wp_send_json_error( esc_html__( 'Something went wrong', 'sellkit' ) );
103 }
104
105 /**
106 * Gets Funnels.
107 *
108 * @since 1.1.0
109 */
110 public function get_funnels() {
111 check_ajax_referer( 'sellkit', 'nonce' );
112
113 $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' );
114
115 if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) {
116 wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) );
117 }
118
119 $response = wp_remote_get( 'https://templates.getsellkit.com/wp-json/sellkit/v1/funnels', [
120 'timeout' => 10,
121 ] );
122
123 if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
124 $funnel_data = json_decode( $response['body'] );
125
126 wp_send_json_success( $funnel_data );
127 }
128
129 wp_send_json_error( esc_html__( 'Something went wrong', 'sellkit' ) );
130 }
131
132 /**
133 * Gets funnel data.
134 *
135 * @since 1.1.0
136 */
137 public function get_funnel_data() {
138 check_ajax_referer( 'sellkit', 'nonce' );
139
140 $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' );
141 $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' );
142
143 if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) {
144 wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) );
145 }
146
147 $funnel_data = $this->import_funnel_data_from_api( $funnel_id );
148
149 if ( ! empty( $funnel_data ) ) {
150 wp_send_json_success( $funnel_data );
151 }
152
153 wp_send_json_error( esc_html__( 'Something went wrong.', 'sellkit' ) );
154 }
155
156 /**
157 * Imports step data.
158 *
159 * @since 1.1.0
160 */
161 public function import_funnel() {
162 check_ajax_referer( 'sellkit', 'nonce' );
163
164 $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' );
165 $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' );
166 $page = sellkit_htmlspecialchars( INPUT_GET, 'page' );
167
168 if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) {
169 wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) );
170 }
171
172 if ( 'checkout' === $page && get_post_status( get_option( Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, 0 ) ) ) {
173 wp_send_json_error( __( 'In order to iport a new global checkout,Please remove previous one.', 'sellkit' ) );
174 }
175
176 $funnel_data = $this->import_funnel_data_from_api( $funnel_id );
177
178 if ( empty( $funnel_data ) ) {
179 wp_send_json_error( __( 'something went wrong', 'sellkit' ) );
180 }
181
182 $new_funnel_id = $this->import_funnel_by_data( $funnel_data );
183
184 if ( 'checkout' === $page ) {
185 update_option( Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, $new_funnel_id );
186 }
187
188 wp_send_json_success( [ 'funnelId' => $new_funnel_id ] );
189 }
190
191 /**
192 * Import funnel.
193 *
194 * @since 1.5.0
195 * @param object $funnel_data funnel data.
196 * @param boolean|object $steps_data steps data.
197 */
198 public function import_funnel_by_data( $funnel_data, $steps_data = false ) {
199 $new_funnel_id = $this->create_new_funnel( $funnel_data );
200 $steps = $funnel_data->sellkit_steps;
201 $new_step_data = [];
202 $data = '';
203
204 foreach ( $steps as $key => $step ) {
205 if ( false !== $steps_data && property_exists( $step, 'page_id' ) ) {
206 $step_id = $step->page_id;
207 $data = $steps_data->$step_id;
208 }
209
210 if ( false === $steps_data && property_exists( $step, 'page_id' ) ) {
211 $data = $this->step_importer->import_from_api( $step->page_id );
212 }
213
214 if ( is_wp_error( $data ) ) {
215 wp_send_json_error( __( 'Something went wrong.', 'sellkit' ) );
216 }
217
218 $new_step_id = $this->step_importer->import_to_funnel( $data, $new_funnel_id );
219
220 $this->step_importer->import_template( $data, $new_step_id );
221
222 $step->page_id = $new_step_id;
223 $step->funnel_id = $new_funnel_id;
224
225 if ( ! empty( $step->data->products->list ) ) {
226 unset( $step->data->products->list );
227 }
228
229 if ( ! empty( $step->bump[0]->data->products->list ) ) {
230 unset( $step->bump[0]->data->products->list );
231 }
232
233 $new_step_data[ $key ] = json_decode( wp_json_encode( $step ), true );
234 }
235
236 if ( false === $steps_data ) {
237 update_post_meta( $new_funnel_id, 'sellkit_steps', $new_step_data );
238 } else {
239 update_post_meta( $new_funnel_id, 'nodes', $new_step_data );
240 }
241
242 $this->step_importer->run();
243
244 return $new_funnel_id;
245 }
246
247 /**
248 * It creates new funnel.
249 *
250 * @param object $funnel_data Funnel data.
251 */
252 public function create_new_funnel( $funnel_data ) {
253 $new_funnel_id = wp_insert_post( [
254 'post_type' => $funnel_data->post_type,
255 'post_title' => $funnel_data->title,
256 'post_name' => sanitize_title( $funnel_data->title ),
257 'post_status' => 'draft',
258 ] );
259
260 return $new_funnel_id;
261 }
262
263 /**
264 * Imports funnel data.
265 *
266 * @since 1.1.0
267 * @param int $funnel_id Funnel Id.
268 */
269 public function import_funnel_data_from_api( $funnel_id ) {
270 $response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/funnel/{$funnel_id}", [
271 'timeout' => 10,
272 ] );
273
274 if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
275 $funnel_data = json_decode( $response['body'] );
276
277 return $funnel_data;
278 }
279
280 return false;
281 }
282 }
283
284 new Ajax_Handler();
285