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

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