PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.0.1
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.0.1
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / admin-legacy-core / ajax / steps.php

steps.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.0.1, at admin-legacy-core/ajax/steps.php

460 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows Flows ajax actions.
4 *
5 * @package CartFlows
6 */
7
8 namespace CartflowsAdmin\AdminLegacyCore\Ajax;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 use CartflowsAdmin\AdminLegacyCore\Ajax\AjaxBase;
16 use CartflowsAdmin\AdminLegacyCore\Inc\AdminHelper;
17 use CartflowsAdmin\AdminLegacyCore\Inc\MetaOps;
18
19 /**
20 * Class Steps.
21 */
22 class Steps extends AjaxBase {
23
24 /**
25 * Instance
26 *
27 * @access private
28 * @var object Class object.
29 * @since 1.0.0
30 */
31 private static $instance;
32
33 /**
34 * Initiator
35 *
36 * @since 1.0.0
37 * @return object initialized object of class.
38 */
39 public static function get_instance() {
40 if ( ! isset( self::$instance ) ) {
41 self::$instance = new self();
42 }
43 return self::$instance;
44 }
45
46 /**
47 * Register ajax events.
48 *
49 * @since 1.0.0
50 * @return void
51 */
52 public function register_ajax_events() {
53
54 $ajax_events = array(
55 'clone_step',
56 'delete_step',
57 'save_meta_settings',
58 'update_step_title',
59 );
60
61 $this->init_ajax_events( $ajax_events );
62 }
63
64 /**
65 * Update step title.
66 *
67 * @since 1.0.0
68 * @return void
69 */
70 public function update_step_title() {
71
72 $response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) );
73
74 if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) {
75 wp_send_json_error( $response_data );
76 }
77
78 /**
79 * Nonce verification
80 */
81 if ( ! check_ajax_referer( 'cartflows_update_step_title', 'security', false ) ) {
82 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
83 wp_send_json_error( $response_data );
84 }
85
86 $step_id = isset( $_POST['step_id'] ) ? intval( $_POST['step_id'] ) : 0;
87 $new_step_title = isset( $_POST['new_step_title'] ) ? sanitize_text_field( $_POST['new_step_title'] ) : '';
88
89 $result = array(
90 'status' => false,
91 'text' => __( 'Can\'t update the step title', 'cartflows' ),
92 );
93
94 /* Check if CartFlows Post type */
95 if ( CARTFLOWS_STEP_POST_TYPE !== get_post_type( $step_id ) ) {
96 wp_send_json( $result );
97 }
98
99 if ( empty( $step_id ) || empty( $new_step_title ) ) {
100 wp_send_json( $result );
101 }
102
103 $new_step_data = array(
104 'ID' => $step_id,
105 'post_title' => $new_step_title,
106 );
107 wp_update_post( $new_step_data );
108
109 $result = array(
110 'status' => true,
111 /* translators: %s flow id */
112 'text' => sprintf( __( 'Step title updated - %s', 'cartflows' ), $step_id ),
113 );
114
115 wp_send_json( $result );
116 }
117
118 /**
119 * Clone step with its meta.
120 */
121 public function clone_step() {
122
123 global $wpdb;
124
125 $response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) );
126
127 if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) {
128 wp_send_json_error( $response_data );
129 }
130
131 /**
132 * Nonce verification
133 */
134 if ( ! check_ajax_referer( 'cartflows_clone_step', 'security', false ) ) {
135 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
136 wp_send_json_error( $response_data );
137 }
138
139 if ( isset( $_POST['post_id'] ) && isset( $_POST['step_id'] ) ) {
140 $flow_id = intval( $_POST['post_id'] );
141 $step_id = intval( $_POST['step_id'] );
142 }
143
144 $result = array(
145 'status' => false,
146 'reload' => true,
147 /* translators: %s flow id */
148 'text' => sprintf( __( 'Can\'t clone this step - %1$s. Flow - %2$s', 'cartflows' ), $step_id, $flow_id ),
149 );
150
151 if ( ! $flow_id || ! $step_id ) {
152 wp_send_json( $result );
153 }
154
155 /* Check if CartFlows Post type */
156 if ( CARTFLOWS_FLOW_POST_TYPE !== get_post_type( $flow_id ) || CARTFLOWS_STEP_POST_TYPE !== get_post_type( $step_id ) ) {
157 wp_send_json( $result );
158 }
159
160 /**
161 * And all the original post data then
162 */
163 $post = get_post( $step_id );
164
165 /**
166 * Assign current user to be the new post author
167 */
168 $current_user = wp_get_current_user();
169 $new_post_author = $current_user->ID;
170
171 /**
172 * If post data exists, create the post duplicate
173 */
174 if ( isset( $post ) && null !== $post ) {
175
176 /**
177 * New post data array
178 */
179 $args = array(
180 'comment_status' => $post->comment_status,
181 'ping_status' => $post->ping_status,
182 'post_author' => $new_post_author,
183 'post_content' => $post->post_content,
184 'post_excerpt' => $post->post_excerpt,
185 'post_name' => $post->post_name,
186 'post_parent' => $post->post_parent,
187 'post_password' => $post->post_password,
188 'post_status' => $post->post_status,
189 'post_title' => $post->post_title . ' Clone',
190 'post_type' => $post->post_type,
191 'to_ping' => $post->to_ping,
192 'menu_order' => $post->menu_order,
193 );
194
195 /**
196 * Insert the post
197 */
198 $new_step_id = wp_insert_post( $args );
199
200 /**
201 * Get all current post terms ad set them to the new post
202 */
203 // returns array of taxonomy names for post type, ex array("category", "post_tag");.
204 $taxonomies = get_object_taxonomies( $post->post_type );
205
206 foreach ( $taxonomies as $taxonomy ) {
207
208 $post_terms = wp_get_object_terms( $step_id, $taxonomy, array( 'fields' => 'slugs' ) );
209
210 wp_set_object_terms( $new_step_id, $post_terms, $taxonomy, false );
211 }
212
213 /**
214 * Duplicate all post meta just in two SQL queries
215 */
216 $post_meta_infos = $wpdb->get_results( //phpcs:ignore WordPress.DB.DirectDatabaseQuery
217 $wpdb->prepare( "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=%d", $step_id )
218 );
219
220 if ( ! empty( $post_meta_infos ) ) {
221
222 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES ";
223
224 $sql_query_sel = array();
225
226 foreach ( $post_meta_infos as $meta_info ) {
227
228 $meta_key = $meta_info->meta_key;
229
230 if ( '_wp_old_slug' === $meta_key ) {
231 continue;
232 }
233
234 $sql_query_sel[] = $wpdb->prepare( '( %d, %s, %s )', $new_step_id, $meta_key, $meta_info->meta_value );
235
236 }
237
238 $sql_query .= implode( ',', $sql_query_sel );
239
240 $wpdb->query( $sql_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery
241 }
242
243 $flow_steps = get_post_meta( $flow_id, 'wcf-steps', true );
244 $step_type = get_post_meta( $step_id, 'wcf-step-type', true );
245
246 if ( ! is_array( $flow_steps ) ) {
247 $flow_steps = array();
248 }
249
250 $flow_steps[] = array(
251 'id' => $new_step_id,
252 'title' => $post->post_title,
253 'type' => $step_type,
254 );
255
256 $flow_steps = \Cartflows_Helper::get_instance()->maybe_update_flow_steps( $flow_id, $flow_steps );
257
258 update_post_meta( $flow_id, 'wcf-steps', $flow_steps );
259
260 /* Clear Page Builder Cache */
261 $this->clear_cache();
262
263 $result = array(
264 'status' => true,
265 'reload' => true,
266 /* translators: %s flow id */
267 'text' => sprintf( __( 'Step - %1$s cloned. Flow - %2$s', 'cartflows' ), $step_id, $flow_id ),
268 );
269 }
270
271 wp_send_json( $result );
272 }
273
274 /**
275 * Clear Page Builder Cache
276 */
277 public function clear_cache() {
278
279 // Clear 'Elementor' file cache.
280 if ( class_exists( '\Elementor\Plugin' ) ) {
281 \Elementor\Plugin::$instance->files_manager->clear_cache();
282 }
283 }
284
285 /**
286 * Delete step for flow
287 *
288 * @since 1.0.0
289 *
290 * @return void
291 */
292 public function delete_step() {
293
294 $response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) );
295
296 if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) {
297 wp_send_json_error( $response_data );
298 }
299
300 /**
301 * Nonce verification
302 */
303 if ( ! check_ajax_referer( 'cartflows_delete_step', 'security', false ) ) {
304 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
305 wp_send_json_error( $response_data );
306 }
307
308 $flow_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
309 $step_id = isset( $_POST['step_id'] ) ? intval( $_POST['step_id'] ) : 0;
310
311 $result = array(
312 'status' => false,
313 'reload' => false,
314 /* translators: %s flow id */
315 'text' => sprintf( __( 'Step not deleted for flow - %s', 'cartflows' ), $flow_id ),
316 );
317
318 if ( ! $flow_id || ! $step_id ) {
319 wp_send_json( $result );
320 }
321
322 /* Check if CartFlows Post type */
323 if ( CARTFLOWS_FLOW_POST_TYPE !== get_post_type( $flow_id ) || CARTFLOWS_STEP_POST_TYPE !== get_post_type( $step_id ) ) {
324 wp_send_json( $result );
325 }
326
327 $flow_steps = get_post_meta( $flow_id, 'wcf-steps', true );
328
329 if ( ! is_array( $flow_steps ) ) {
330 wp_send_json( $result );
331 }
332
333 $is_ab_test = get_post_meta( $step_id, 'wcf-ab-test', true );
334
335 if ( ! $is_ab_test ) {
336
337 foreach ( $flow_steps as $index => $data ) {
338
339 if ( intval( $data['id'] ) === $step_id ) {
340 unset( $flow_steps[ $index ] );
341 break;
342 }
343 }
344
345 /* Set index order properly */
346 $flow_steps = array_merge( $flow_steps );
347
348 /* Update latest data */
349 update_post_meta( $flow_id, 'wcf-steps', $flow_steps );
350
351 /* Delete step */
352 wp_delete_post( $step_id, true );
353
354 $result = array(
355 'status' => true,
356 'reload' => false,
357 /* translators: %s flow id */
358 'text' => sprintf( __( 'Step deleted for flow - %s', 'cartflows' ), $flow_id ),
359 );
360
361 } else {
362
363 $result = array(
364 'status' => false,
365 'reload' => false,
366 /* translators: %s flow id */
367 'text' => sprintf( __( 'This step can not be deleted.', 'cartflows' ), $flow_id ),
368 );
369 /**
370 Action do_action( 'cartflows_step_delete_ab_test', $step_id, $flow_id, $flow_steps );
371 */
372 }
373
374 wp_send_json( $result );
375 }
376
377 /**
378 * Save meta settings for steps.
379 */
380 public function save_meta_settings() {
381
382 $response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) );
383
384 if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) {
385 wp_send_json_error( $response_data );
386 }
387
388 /**
389 * Nonce verification
390 */
391 if ( ! check_ajax_referer( 'cartflows_save_meta_settings', 'security', false ) ) {
392 $response_data = array( 'message' => $this->get_error_msg( 'nonce' ) );
393 wp_send_json_error( $response_data );
394 }
395
396 $result = array(
397 'status' => false,
398 'reload' => false,
399 /* translators: %s flow id */
400 'text' => sprintf( __( 'Invalid Step Id has been provided.', 'cartflows' ) ),
401 );
402
403 if ( ! isset( $_POST['post_id'] ) || ! isset( $_POST['step_id'] ) ) {
404 wp_send_json( $result );
405 }
406
407 $step_id = isset( $_POST['step_id'] ) ? intval( $_POST['step_id'] ) : 0;
408
409 if ( CARTFLOWS_STEP_POST_TYPE !== get_post_type( $step_id ) ) {
410 wp_send_json_error( $result );
411 }
412
413 $step_tile = isset( $_POST['post_title'] ) ? sanitize_text_field( wp_unslash( $_POST['post_title'] ) ) : get_the_title( $step_id );
414 if ( '' === $step_tile ) {
415 $step_tile = __( '(no title)', 'cartflows' );
416 }
417 $step_slug = isset( $_POST['step_post_name'] ) ? sanitize_text_field( wp_unslash( $_POST['step_post_name'] ) ) : get_post_field( 'post_name', $step_id );
418
419 $step_data = array(
420 'ID' => $step_id,
421 'post_title' => $step_tile,
422 'post_name' => $step_slug,
423 );
424
425 wp_update_post( $step_data );
426
427 $post_meta = '';
428
429 $step_type = wcf()->utils->get_step_type( $step_id );
430
431 $post_meta = AdminHelper::get_step_default_meta( $step_type, $step_id );
432
433 MetaOps::save_meta_fields( $step_id, $post_meta, 'cartflows_save_meta_settings' );
434
435 // We are storing the step dynamic css in the post meta and deleting when any setting changes. Once deleted it will be regenerated with first page load.
436 delete_post_meta( $step_id, 'wcf-dynamic-css' );
437
438 do_action( 'cartflows_admin_save_step_meta', $step_id );
439
440 $meta_options = AdminHelper::get_step_meta_options( $step_id );
441
442 // Delete the Google font URL and generate it while loading the page first time.
443 delete_post_meta( $step_id, 'wcf-field-google-font-url' );
444
445 $result = array(
446 'status' => true,
447 'reload' => false,
448 'options' => $meta_options,
449 'step_view_url' => get_the_permalink( $step_id ),
450 /* translators: %s flow id */
451 'text' => sprintf( __( 'Data saved successfully for step id %s', 'cartflows' ), $step_id ),
452 );
453
454 wp_send_json( $result );
455
456 }
457
458
459 }
460