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

265 lines 7.0 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 $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' );
47 $step_id = sellkit_htmlspecialchars( INPUT_GET, 'step_id' );
48 $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' );
49 $origin_node = filter_input( INPUT_GET, 'origin_node', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
50 $target_node = filter_input( INPUT_GET, 'target_node', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
51 $target_index = filter_input( INPUT_GET, 'target_index', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
52 $origin_node = isset( $origin_node ) ? $origin_node : 'none';
53 $target_node = ! empty( $target_node ) ? $target_node : 'none';
54
55 wp_verify_nonce( $nonce, 'sellkit' );
56
57 $data = $this->step_importer->import_from_api( $step_id );
58
59 if ( empty( $data ) || is_wp_error( $data ) ) {
60 wp_send_json_error( __( 'Something went wrong.', 'sellkit' ) );
61 }
62
63 $new_step_id = $this->step_importer->import_to_funnel( $data, $funnel_id, $origin_node, $target_node, $target_index );
64
65 $this->step_importer->import_template( $data, $new_step_id );
66 $this->step_importer->run();
67
68 sleep( 3 );
69
70 wp_send_json_success( esc_html__( 'The data has been imported successfully.' ) );
71 }
72
73 /**
74 * Gets steps.
75 *
76 * @since 1.1.0
77 */
78 public function get_steps() {
79 $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' );
80 $type = sellkit_htmlspecialchars( INPUT_GET, 'type' );
81
82 wp_verify_nonce( $nonce, 'sellkit' );
83
84 $response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/steps/type={$type}", [
85 'timeout' => 10,
86 ] );
87
88 if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
89 $step_data = json_decode( $response['body'] );
90
91 wp_send_json_success( $step_data );
92 }
93
94 wp_send_json_error( esc_html__( 'Something went wrong', 'sellkit' ) );
95 }
96
97 /**
98 * Gets Funnels.
99 *
100 * @since 1.1.0
101 */
102 public function get_funnels() {
103 $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' );
104
105 wp_verify_nonce( $nonce, 'sellkit' );
106
107 $response = wp_remote_get( 'https://templates.getsellkit.com/wp-json/sellkit/v1/funnels', [
108 'timeout' => 10,
109 ] );
110
111 if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
112 $funnel_data = json_decode( $response['body'] );
113
114 wp_send_json_success( $funnel_data );
115 }
116
117 wp_send_json_error( esc_html__( 'Something went wrong', 'sellkit' ) );
118 }
119
120 /**
121 * Gets funnel data.
122 *
123 * @since 1.1.0
124 */
125 public function get_funnel_data() {
126 $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' );
127 $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' );
128
129 wp_verify_nonce( $nonce, 'sellkit' );
130
131 $funnel_data = $this->import_funnel_data_from_api( $funnel_id );
132
133 if ( ! empty( $funnel_data ) ) {
134 wp_send_json_success( $funnel_data );
135 }
136
137 wp_send_json_error( esc_html__( 'Something went wrong.', 'sellkit' ) );
138 }
139
140 /**
141 * Imports step data.
142 *
143 * @since 1.1.0
144 */
145 public function import_funnel() {
146 $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' );
147 $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' );
148 $page = sellkit_htmlspecialchars( INPUT_GET, 'page' );
149
150 wp_verify_nonce( $nonce, 'sellkit' );
151
152 if ( 'checkout' === $page && get_post_status( get_option( Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, 0 ) ) ) {
153 wp_send_json_error( __( 'In order to iport a new global checkout,Please remove previous one.', 'sellkit' ) );
154 }
155
156 $funnel_data = $this->import_funnel_data_from_api( $funnel_id );
157
158 if ( empty( $funnel_data ) ) {
159 wp_send_json_error( __( 'something went wrong', 'sellkit' ) );
160 }
161
162 $new_funnel_id = $this->import_funnel_by_data( $funnel_data );
163
164 if ( 'checkout' === $page ) {
165 update_option( Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, $new_funnel_id );
166 }
167
168 wp_send_json_success( [ 'funnelId' => $new_funnel_id ] );
169 }
170
171 /**
172 * Import funnel.
173 *
174 * @since 1.5.0
175 * @param object $funnel_data funnel data.
176 * @param boolean|object $steps_data steps data.
177 */
178 public function import_funnel_by_data( $funnel_data, $steps_data = false ) {
179 $new_funnel_id = $this->create_new_funnel( $funnel_data );
180 $steps = $funnel_data->sellkit_steps;
181 $new_step_data = [];
182 $data = '';
183
184 foreach ( $steps as $key => $step ) {
185 if ( false !== $steps_data && property_exists( $step, 'page_id' ) ) {
186 $step_id = $step->page_id;
187 $data = $steps_data->$step_id;
188 }
189
190 if ( false === $steps_data ) {
191 $data = $this->step_importer->import_from_api( $step->page_id );
192 }
193
194 if ( is_wp_error( $data ) ) {
195 wp_send_json_error( __( 'Something went wrong.', 'sellkit' ) );
196 }
197
198 $new_step_id = $this->step_importer->import_to_funnel( $data, $new_funnel_id );
199
200 $this->step_importer->import_template( $data, $new_step_id );
201
202 $step->page_id = $new_step_id;
203 $step->funnel_id = $new_funnel_id;
204
205 if ( ! empty( $step->data->products->list ) ) {
206 unset( $step->data->products->list );
207 }
208
209 if ( ! empty( $step->bump[0]->data->products->list ) ) {
210 unset( $step->bump[0]->data->products->list );
211 }
212
213 $new_step_data[ $key ] = (array) $step;
214 }
215
216 if ( false === $steps_data ) {
217 update_post_meta( $new_funnel_id, 'sellkit_steps', $new_step_data );
218 } else {
219 update_post_meta( $new_funnel_id, 'nodes', $new_step_data );
220 }
221
222 $this->step_importer->run();
223
224 return $new_funnel_id;
225 }
226
227 /**
228 * It creates new funnel.
229 *
230 * @param object $funnel_data Funnel data.
231 */
232 public function create_new_funnel( $funnel_data ) {
233 $new_funnel_id = wp_insert_post( [
234 'post_type' => $funnel_data->post_type,
235 'post_title' => $funnel_data->title,
236 'post_name' => sanitize_title( $funnel_data->title ),
237 'post_status' => 'draft',
238 ] );
239
240 return $new_funnel_id;
241 }
242
243 /**
244 * Imports funnel data.
245 *
246 * @since 1.1.0
247 * @param int $funnel_id Funnel Id.
248 */
249 public function import_funnel_data_from_api( $funnel_id ) {
250 $response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/funnel/{$funnel_id}", [
251 'timeout' => 10,
252 ] );
253
254 if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
255 $funnel_data = json_decode( $response['body'] );
256
257 return $funnel_data;
258 }
259
260 return false;
261 }
262 }
263
264 new Ajax_Handler();
265