PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.5.4
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.5.4
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 1.5.4, at includes/admin/funnel/importer/ajax-handler.php

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