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

211 lines 5.2 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 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 = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_STRING );
46 $step_id = filter_input( INPUT_GET, 'step_id', FILTER_SANITIZE_STRING );
47 $funnel_id = filter_input( INPUT_GET, 'funnel_id', FILTER_SANITIZE_STRING );
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 = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_STRING );
69 $type = filter_input( INPUT_GET, 'type', FILTER_SANITIZE_STRING );
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 = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_STRING );
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 = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_STRING );
116 $funnel_id = filter_input( INPUT_GET, 'funnel_id', FILTER_SANITIZE_STRING );
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 = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_STRING );
136 $funnel_id = filter_input( INPUT_GET, 'funnel_id', FILTER_SANITIZE_STRING );
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 $new_step_data[] = (array) $step;
164 }
165
166 update_post_meta( $new_funnel_id, 'sellkit_steps', $new_step_data );
167
168 $this->step_importer->run();
169
170 wp_send_json_success( [ 'funnelId' => $new_funnel_id ] );
171 }
172
173 /**
174 * It creates new funnel.
175 *
176 * @param object $funnel_data Funnel data.
177 */
178 public function create_new_funnel( $funnel_data ) {
179 $new_funnel_id = wp_insert_post( [
180 'post_type' => $funnel_data->post_type,
181 'post_title' => $funnel_data->title,
182 'post_name' => sanitize_title( $funnel_data->title ),
183 'post_status' => 'draft',
184 ] );
185
186 return $new_funnel_id;
187 }
188
189 /**
190 * Imports funnel data.
191 *
192 * @since 1.1.0
193 * @param int $funnel_id Funnel Id.
194 */
195 public function import_funnel_data_from_api( $funnel_id ) {
196 $response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/funnel/{$funnel_id}", [
197 'timeout' => 10,
198 ] );
199
200 if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
201 $funnel_data = json_decode( $response['body'] );
202
203 return $funnel_data;
204 }
205
206 return false;
207 }
208 }
209
210 new Ajax_Handler();
211