PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.19
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.19
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 / classes / class-cartflows-cloning.php

class-cartflows-cloning.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 1.1.19, at classes/class-cartflows-cloning.php

446 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 * Cloning.
4 *
5 * @package cartflows-pro
6 */
7
8 /**
9 * Initialization
10 *
11 * @since 1.0.0
12 */
13 class Cartflows_Cloning {
14
15
16 /**
17 * Member Variable
18 *
19 * @var instance
20 */
21 private static $instance;
22
23 /**
24 * Initiator
25 */
26 public static function get_instance() {
27 if ( ! isset( self::$instance ) ) {
28 self::$instance = new self;
29 }
30 return self::$instance;
31 }
32
33 /**
34 * Constructor
35 */
36 public function __construct() {
37
38 add_filter( 'post_row_actions', array( $this, 'clone_link' ), 99, 2 );
39 add_action( 'admin_action_cartflows_clone_flow', array( $this, 'clone_flow' ) );
40 add_action( 'admin_action_cartflows_clone_step', array( $this, 'clone_step' ) );
41 }
42
43 /**
44 * Clone flow with steps and its meta.
45 */
46 function clone_flow() {
47
48 global $wpdb;
49
50 if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] ) || ( isset( $_REQUEST['action'] ) && 'cartflows_clone_flow' == $_REQUEST['action'] ) ) ) {
51 wp_die( 'No post to duplicate has been supplied!' );
52 }
53
54 /*
55 * Nonce verification
56 */
57 if ( ! isset( $_GET['flow_clone_nonce'] ) || ! wp_verify_nonce( $_GET['flow_clone_nonce'], basename( __FILE__ ) ) ) {
58 return;
59 }
60
61 /**
62 * Get the original post id
63 */
64 $post_id = ( isset( $_GET['post'] ) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
65
66 /**
67 * And all the original post data then
68 */
69 $post = get_post( $post_id );
70
71 /**
72 * Assign current user to be the new post author
73 */
74 $current_user = wp_get_current_user();
75 $new_post_author = $current_user->ID;
76
77 /**
78 * If post data exists, create the post duplicate
79 */
80 if ( isset( $post ) && null !== $post ) {
81
82 /**
83 * New post data array
84 */
85
86 $args = array(
87 'comment_status' => $post->comment_status,
88 'ping_status' => $post->ping_status,
89 'post_author' => $new_post_author,
90 'post_content' => $post->post_content,
91 'post_excerpt' => $post->post_excerpt,
92 'post_name' => $post->post_name,
93 'post_parent' => $post->post_parent,
94 'post_password' => $post->post_password,
95 'post_status' => $post->post_status,
96 'post_title' => $post->post_title . ' Clone',
97 'post_type' => $post->post_type,
98 'to_ping' => $post->to_ping,
99 'menu_order' => $post->menu_order,
100 );
101
102 /**
103 * Insert the post
104 */
105 $new_flow_id = wp_insert_post( $args );
106
107 /**
108 * Get all current post terms ad set them to the new post
109 */
110 // returns array of taxonomy names for post type, ex array("category", "post_tag");.
111 $taxonomies = get_object_taxonomies( $post->post_type );
112
113 foreach ( $taxonomies as $taxonomy ) {
114
115 $post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
116
117 wp_set_object_terms( $new_flow_id, $post_terms, $taxonomy, false );
118 }
119
120 /**
121 * Duplicate all post meta just in two SQL queries
122 */
123 // @codingStandardsIgnoreStart
124 $post_meta_infos = $wpdb->get_results(
125 "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id"
126 );
127 // @codingStandardsIgnoreEnd
128
129 if ( ! empty( $post_meta_infos ) ) {
130
131 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES ";
132
133 $sql_query_sel = array();
134
135 foreach ( $post_meta_infos as $meta_info ) {
136
137 $meta_key = $meta_info->meta_key;
138
139 if ( '_wp_old_slug' === $meta_key ) {
140 continue;
141 }
142
143 $meta_value = addslashes( $meta_info->meta_value );
144
145 $sql_query_sel[] = "($new_flow_id, '$meta_key', '$meta_value')";
146 }
147
148 $sql_query .= implode( ',', $sql_query_sel );
149
150 // @codingStandardsIgnoreStart
151 $wpdb->query( $sql_query );
152 // @codingStandardsIgnoreEnd
153 }
154
155 /* Steps Cloning */
156 $flow_steps = get_post_meta( $post_id, 'wcf-steps', true );
157 $new_flow_steps = array();
158
159 /* Set Steps Empty */
160 update_post_meta( $new_flow_id, 'wcf-steps', $new_flow_steps );
161
162 if ( is_array( $flow_steps ) && ! empty( $flow_steps ) ) {
163
164 foreach ( $flow_steps as $index => $step_data ) {
165
166 $step_id = $step_data['id'];
167 $step_type = get_post_meta( $step_id, 'wcf-step-type', true );
168
169 $step_object = get_post( $step_id );
170
171 /**
172 * New step post data array
173 */
174 $step_args = array(
175 'comment_status' => $step_object->comment_status,
176 'ping_status' => $step_object->ping_status,
177 'post_author' => $new_post_author,
178 'post_content' => $step_object->post_content,
179 'post_excerpt' => $step_object->post_excerpt,
180 'post_name' => $step_object->post_name,
181 'post_parent' => $step_object->post_parent,
182 'post_password' => $step_object->post_password,
183 'post_status' => $step_object->post_status,
184 'post_title' => $step_object->post_title,
185 'post_type' => $step_object->post_type,
186 'to_ping' => $step_object->to_ping,
187 'menu_order' => $step_object->menu_order,
188 );
189
190 /**
191 * Insert the post
192 */
193 $new_step_id = wp_insert_post( $step_args );
194
195 /**
196 * Duplicate all step meta
197 */
198 // @codingStandardsIgnoreStart
199 $post_meta_infos = $wpdb->get_results(
200 "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$step_id"
201 );
202 // @codingStandardsIgnoreEnd
203
204 if ( ! empty( $post_meta_infos ) ) {
205
206 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES ";
207
208 $sql_query_sel = array();
209
210 foreach ( $post_meta_infos as $meta_info ) {
211
212 $meta_key = $meta_info->meta_key;
213
214 if ( '_wp_old_slug' === $meta_key ) {
215 continue;
216 }
217
218 $meta_value = addslashes( $meta_info->meta_value );
219
220 $sql_query_sel[] = "($new_step_id, '$meta_key', '$meta_value')";
221 }
222
223 $sql_query .= implode( ',', $sql_query_sel );
224
225 // @codingStandardsIgnoreStart
226 $wpdb->query( $sql_query );
227 // @codingStandardsIgnoreEnd
228 }
229
230 // insert post meta.
231 update_post_meta( $new_step_id, 'wcf-flow-id', $new_flow_id );
232 update_post_meta( $new_step_id, 'wcf-step-type', $step_type );
233
234 wp_set_object_terms( $new_step_id, $step_type, CARTFLOWS_TAXONOMY_STEP_TYPE );
235 wp_set_object_terms( $new_step_id, 'flow-' . $new_flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW );
236
237 /* Add New Flow Steps */
238 $new_flow_steps[] = array(
239 'id' => $new_step_id,
240 'title' => $step_object->post_title,
241 'type' => $step_type,
242 );
243 }
244 }
245
246 /* Update New Flow Step Post Meta */
247 update_post_meta( $new_flow_id, 'wcf-steps', $new_flow_steps );
248
249 /* Clear Page Builder Cache */
250 $this->clear_cache();
251
252 /**
253 * Redirect to the new flow edit screen
254 */
255 wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_flow_id ) );
256 exit;
257 } else {
258 wp_die( 'Post creation failed, could not find original post: ' . $post_id );
259 }
260 }
261
262 /**
263 * Clone step with its meta.
264 */
265 function clone_step() {
266
267 global $wpdb;
268
269 if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] ) || ( isset( $_REQUEST['action'] ) && 'cartflows_clone_step' === $_REQUEST['action'] ) ) ) {
270 wp_die( 'No post to duplicate has been supplied!' );
271 }
272
273 /*
274 * Nonce verification
275 */
276 if ( ! isset( $_GET['step_clone_nonce'] ) || ! wp_verify_nonce( $_GET['step_clone_nonce'], 'step_clone' ) ) {
277 return;
278 }
279
280 /**
281 * Get the original post id
282 */
283 $post_id = ( isset( $_GET['post'] ) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
284
285 /**
286 * And all the original post data then
287 */
288 $post = get_post( $post_id );
289
290 /**
291 * Assign current user to be the new post author
292 */
293 $current_user = wp_get_current_user();
294 $new_post_author = $current_user->ID;
295
296 /**
297 * If post data exists, create the post duplicate
298 */
299 if ( isset( $post ) && null !== $post ) {
300
301 /**
302 * New post data array
303 */
304 $args = array(
305 'comment_status' => $post->comment_status,
306 'ping_status' => $post->ping_status,
307 'post_author' => $new_post_author,
308 'post_content' => $post->post_content,
309 'post_excerpt' => $post->post_excerpt,
310 'post_name' => $post->post_name,
311 'post_parent' => $post->post_parent,
312 'post_password' => $post->post_password,
313 'post_status' => $post->post_status,
314 'post_title' => $post->post_title . ' Clone',
315 'post_type' => $post->post_type,
316 'to_ping' => $post->to_ping,
317 'menu_order' => $post->menu_order,
318 );
319
320 /**
321 * Insert the post
322 */
323 $new_step_id = wp_insert_post( $args );
324
325 /**
326 * Get all current post terms ad set them to the new post
327 */
328 // returns array of taxonomy names for post type, ex array("category", "post_tag");.
329 $taxonomies = get_object_taxonomies( $post->post_type );
330
331 foreach ( $taxonomies as $taxonomy ) {
332
333 $post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
334
335 wp_set_object_terms( $new_step_id, $post_terms, $taxonomy, false );
336 }
337
338 /**
339 * Duplicate all post meta just in two SQL queries
340 */
341 // @codingStandardsIgnoreStart
342 $post_meta_infos = $wpdb->get_results(
343 "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id"
344 );
345 // @codingStandardsIgnoreEnd
346
347 if ( ! empty( $post_meta_infos ) ) {
348
349 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES ";
350
351 $sql_query_sel = array();
352
353 foreach ( $post_meta_infos as $meta_info ) {
354
355 $meta_key = $meta_info->meta_key;
356
357 if ( '_wp_old_slug' === $meta_key ) {
358 continue;
359 }
360
361 $meta_value = addslashes( $meta_info->meta_value );
362
363 $sql_query_sel[] = "($new_step_id, '$meta_key', '$meta_value')";
364 }
365
366 $sql_query .= implode( ',', $sql_query_sel );
367
368 // @codingStandardsIgnoreStart
369 $wpdb->query( $sql_query );
370 // @codingStandardsIgnoreEnd
371 }
372
373 $flow_id = get_post_meta( $post_id, 'wcf-flow-id', true );
374 $flow_steps = get_post_meta( $flow_id, 'wcf-steps', true );
375 $step_type = get_post_meta( $post_id, 'wcf-step-type', true );
376
377 if ( ! is_array( $flow_steps ) ) {
378 $flow_steps = array();
379 }
380
381 $flow_steps[] = array(
382 'id' => $new_step_id,
383 'title' => $post->post_title,
384 'type' => $step_type,
385 );
386
387 update_post_meta( $flow_id, 'wcf-steps', $flow_steps );
388
389 /* Clear Page Builder Cache */
390 $this->clear_cache();
391
392 /**
393 * Redirect to the new flow edit screen
394 */
395 $redirect_url = add_query_arg( 'highlight-step-id', $new_step_id, get_edit_post_link( $flow_id, 'default' ) );
396
397 wp_redirect( $redirect_url );
398 exit;
399 } else {
400 wp_die( 'Post creation failed, could not find original post: ' . $post_id );
401 }
402 }
403
404 /**
405 * Add the clone link to action list for flows row actions
406 *
407 * @param array $actions Actions array.
408 * @param object $post Post object.
409 *
410 * @return array
411 */
412 function clone_link( $actions, $post ) {
413
414 if ( current_user_can( 'edit_posts' ) && isset( $post ) && CARTFLOWS_FLOW_POST_TYPE === $post->post_type ) {
415
416 if ( isset( $actions['duplicate'] ) ) { // Duplicate page plugin remove.
417 unset( $actions['duplicate'] );
418 }
419 if ( isset( $actions['edit_as_new_draft'] ) ) { // Duplicate post plugin remove.
420 unset( $actions['edit_as_new_draft'] );
421 }
422
423 $actions['clone'] = '<a href="' . wp_nonce_url( 'admin.php?action=cartflows_clone_flow&post=' . $post->ID, basename( __FILE__ ), 'flow_clone_nonce' ) . '" title="' . __( 'Clone this flow', 'cartflows' ) . '" rel="permalink">' . __( 'Clone', 'cartflows' ) . '</a>';
424 }
425
426 return $actions;
427 }
428
429 /**
430 * Clear Page Builder Cache
431 */
432 function clear_cache() {
433
434 // Clear 'Elementor' file cache.
435 if ( class_exists( '\Elementor\Plugin' ) ) {
436 Elementor\Plugin::$instance->files_manager->clear_cache();
437 }
438 }
439
440 }
441
442 /**
443 * Kicking this off by calling 'get_instance()' method
444 */
445 Cartflows_Cloning::get_instance();
446