| 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) "; |
| 132 |
|
| 133 |
foreach ( $post_meta_infos as $meta_info ) { |
| 134 |
|
| 135 |
$meta_key = $meta_info->meta_key; |
| 136 |
|
| 137 |
if ( '_wp_old_slug' === $meta_key ) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
$meta_value = addslashes( $meta_info->meta_value ); |
| 142 |
|
| 143 |
$sql_query_sel[] = "SELECT $new_flow_id, '$meta_key', '$meta_value'"; |
| 144 |
} |
| 145 |
|
| 146 |
$sql_query .= implode( ' UNION ALL ', $sql_query_sel ); |
| 147 |
|
| 148 |
// @codingStandardsIgnoreStart |
| 149 |
$wpdb->query( $sql_query ); |
| 150 |
// @codingStandardsIgnoreEnd |
| 151 |
} |
| 152 |
|
| 153 |
/* Steps Cloning */ |
| 154 |
$flow_steps = get_post_meta( $post_id, 'wcf-steps', true ); |
| 155 |
$new_flow_steps = array(); |
| 156 |
|
| 157 |
if ( is_array( $flow_steps ) && ! empty( $flow_steps ) ) { |
| 158 |
|
| 159 |
foreach ( $flow_steps as $index => $step_data ) { |
| 160 |
|
| 161 |
$step_id = $step_data['id']; |
| 162 |
$step_type = get_post_meta( $step_id, 'wcf-step-type', true ); |
| 163 |
|
| 164 |
$step_object = get_post( $step_id ); |
| 165 |
|
| 166 |
/** |
| 167 |
* New step post data array |
| 168 |
*/ |
| 169 |
$step_args = array( |
| 170 |
'comment_status' => $step_object->comment_status, |
| 171 |
'ping_status' => $step_object->ping_status, |
| 172 |
'post_author' => $new_post_author, |
| 173 |
'post_content' => $step_object->post_content, |
| 174 |
'post_excerpt' => $step_object->post_excerpt, |
| 175 |
'post_name' => $step_object->post_name, |
| 176 |
'post_parent' => $step_object->post_parent, |
| 177 |
'post_password' => $step_object->post_password, |
| 178 |
'post_status' => $step_object->post_status, |
| 179 |
'post_title' => $step_object->post_title, |
| 180 |
'post_type' => $step_object->post_type, |
| 181 |
'to_ping' => $step_object->to_ping, |
| 182 |
'menu_order' => $step_object->menu_order, |
| 183 |
); |
| 184 |
|
| 185 |
/** |
| 186 |
* Insert the post |
| 187 |
*/ |
| 188 |
$new_step_id = wp_insert_post( $step_args ); |
| 189 |
|
| 190 |
/** |
| 191 |
* Duplicate all step meta |
| 192 |
*/ |
| 193 |
// @codingStandardsIgnoreStart |
| 194 |
$post_meta_infos = $wpdb->get_results( |
| 195 |
"SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$step_id" |
| 196 |
); |
| 197 |
// @codingStandardsIgnoreEnd |
| 198 |
|
| 199 |
if ( ! empty( $post_meta_infos ) ) { |
| 200 |
|
| 201 |
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) "; |
| 202 |
|
| 203 |
foreach ( $post_meta_infos as $meta_info ) { |
| 204 |
|
| 205 |
$meta_key = $meta_info->meta_key; |
| 206 |
|
| 207 |
if ( '_wp_old_slug' === $meta_key ) { |
| 208 |
continue; |
| 209 |
} |
| 210 |
|
| 211 |
$meta_value = addslashes( $meta_info->meta_value ); |
| 212 |
|
| 213 |
$sql_query_sel[] = "SELECT $new_step_id, '$meta_key', '$meta_value'"; |
| 214 |
} |
| 215 |
|
| 216 |
$sql_query .= implode( ' UNION ALL ', $sql_query_sel ); |
| 217 |
|
| 218 |
// @codingStandardsIgnoreStart |
| 219 |
$wpdb->query( $sql_query ); |
| 220 |
// @codingStandardsIgnoreEnd |
| 221 |
} |
| 222 |
|
| 223 |
// insert post meta. |
| 224 |
update_post_meta( $new_step_id, 'wcf-flow-id', $new_flow_id ); |
| 225 |
update_post_meta( $new_step_id, 'wcf-step-type', $step_type ); |
| 226 |
|
| 227 |
wp_set_object_terms( $new_step_id, $step_type, CARTFLOWS_TAXONOMY_STEP_TYPE ); |
| 228 |
wp_set_object_terms( $new_step_id, 'flow-' . $new_flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW ); |
| 229 |
|
| 230 |
/* Add New Flow Steps */ |
| 231 |
$new_flow_steps[] = array( |
| 232 |
'id' => $new_step_id, |
| 233 |
'title' => $step_object->post_title, |
| 234 |
'type' => $step_type, |
| 235 |
); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/* Update New Flow Step Post Meta */ |
| 240 |
update_post_meta( $new_flow_id, 'wcf-steps', $new_flow_steps ); |
| 241 |
|
| 242 |
/* Clear Page Builder Cache */ |
| 243 |
$this->clear_cache(); |
| 244 |
|
| 245 |
/** |
| 246 |
* Redirect to the new flow edit screen |
| 247 |
*/ |
| 248 |
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_flow_id ) ); |
| 249 |
exit; |
| 250 |
} else { |
| 251 |
wp_die( 'Post creation failed, could not find original post: ' . $post_id ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Clone step with its meta. |
| 257 |
*/ |
| 258 |
function clone_step() { |
| 259 |
|
| 260 |
global $wpdb; |
| 261 |
|
| 262 |
if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] ) || ( isset( $_REQUEST['action'] ) && 'cartflows_clone_step' === $_REQUEST['action'] ) ) ) { |
| 263 |
wp_die( 'No post to duplicate has been supplied!' ); |
| 264 |
} |
| 265 |
|
| 266 |
/* |
| 267 |
* Nonce verification |
| 268 |
*/ |
| 269 |
if ( ! isset( $_GET['step_clone_nonce'] ) || ! wp_verify_nonce( $_GET['step_clone_nonce'], 'step_clone' ) ) { |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Get the original post id |
| 275 |
*/ |
| 276 |
$post_id = ( isset( $_GET['post'] ) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) ); |
| 277 |
|
| 278 |
/** |
| 279 |
* And all the original post data then |
| 280 |
*/ |
| 281 |
$post = get_post( $post_id ); |
| 282 |
|
| 283 |
/** |
| 284 |
* Assign current user to be the new post author |
| 285 |
*/ |
| 286 |
$current_user = wp_get_current_user(); |
| 287 |
$new_post_author = $current_user->ID; |
| 288 |
|
| 289 |
/** |
| 290 |
* If post data exists, create the post duplicate |
| 291 |
*/ |
| 292 |
if ( isset( $post ) && null !== $post ) { |
| 293 |
|
| 294 |
/** |
| 295 |
* New post data array |
| 296 |
*/ |
| 297 |
$args = array( |
| 298 |
'comment_status' => $post->comment_status, |
| 299 |
'ping_status' => $post->ping_status, |
| 300 |
'post_author' => $new_post_author, |
| 301 |
'post_content' => $post->post_content, |
| 302 |
'post_excerpt' => $post->post_excerpt, |
| 303 |
'post_name' => $post->post_name, |
| 304 |
'post_parent' => $post->post_parent, |
| 305 |
'post_password' => $post->post_password, |
| 306 |
'post_status' => $post->post_status, |
| 307 |
'post_title' => $post->post_title . ' Clone', |
| 308 |
'post_type' => $post->post_type, |
| 309 |
'to_ping' => $post->to_ping, |
| 310 |
'menu_order' => $post->menu_order, |
| 311 |
); |
| 312 |
|
| 313 |
/** |
| 314 |
* Insert the post |
| 315 |
*/ |
| 316 |
$new_step_id = wp_insert_post( $args ); |
| 317 |
|
| 318 |
/** |
| 319 |
* Get all current post terms ad set them to the new post |
| 320 |
*/ |
| 321 |
// returns array of taxonomy names for post type, ex array("category", "post_tag");. |
| 322 |
$taxonomies = get_object_taxonomies( $post->post_type ); |
| 323 |
|
| 324 |
foreach ( $taxonomies as $taxonomy ) { |
| 325 |
|
| 326 |
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) ); |
| 327 |
|
| 328 |
wp_set_object_terms( $new_step_id, $post_terms, $taxonomy, false ); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Duplicate all post meta just in two SQL queries |
| 333 |
*/ |
| 334 |
// @codingStandardsIgnoreStart |
| 335 |
$post_meta_infos = $wpdb->get_results( |
| 336 |
"SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id" |
| 337 |
); |
| 338 |
// @codingStandardsIgnoreEnd |
| 339 |
|
| 340 |
if ( ! empty( $post_meta_infos ) ) { |
| 341 |
|
| 342 |
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) "; |
| 343 |
|
| 344 |
foreach ( $post_meta_infos as $meta_info ) { |
| 345 |
|
| 346 |
$meta_key = $meta_info->meta_key; |
| 347 |
|
| 348 |
if ( '_wp_old_slug' === $meta_key ) { |
| 349 |
continue; |
| 350 |
} |
| 351 |
|
| 352 |
$meta_value = addslashes( $meta_info->meta_value ); |
| 353 |
|
| 354 |
$sql_query_sel[] = "SELECT $new_step_id, '$meta_key', '$meta_value'"; |
| 355 |
} |
| 356 |
|
| 357 |
$sql_query .= implode( ' UNION ALL ', $sql_query_sel ); |
| 358 |
|
| 359 |
// @codingStandardsIgnoreStart |
| 360 |
$wpdb->query( $sql_query ); |
| 361 |
// @codingStandardsIgnoreEnd |
| 362 |
} |
| 363 |
|
| 364 |
$flow_id = get_post_meta( $post_id, 'wcf-flow-id', true ); |
| 365 |
$flow_steps = get_post_meta( $flow_id, 'wcf-steps', true ); |
| 366 |
$step_type = get_post_meta( $post_id, 'wcf-step-type', true ); |
| 367 |
|
| 368 |
if ( ! is_array( $flow_steps ) ) { |
| 369 |
$flow_steps = array(); |
| 370 |
} |
| 371 |
|
| 372 |
$flow_steps[] = array( |
| 373 |
'id' => $new_step_id, |
| 374 |
'title' => $post->post_title, |
| 375 |
'type' => $step_type, |
| 376 |
); |
| 377 |
|
| 378 |
update_post_meta( $flow_id, 'wcf-steps', $flow_steps ); |
| 379 |
|
| 380 |
/* Clear Page Builder Cache */ |
| 381 |
$this->clear_cache(); |
| 382 |
|
| 383 |
/** |
| 384 |
* Redirect to the new flow edit screen |
| 385 |
*/ |
| 386 |
$redirect_url = add_query_arg( 'highlight-step-id', $new_step_id, get_edit_post_link( $flow_id, 'default' ) ); |
| 387 |
|
| 388 |
wp_redirect( $redirect_url ); |
| 389 |
exit; |
| 390 |
} else { |
| 391 |
wp_die( 'Post creation failed, could not find original post: ' . $post_id ); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Add the clone link to action list for flows row actions |
| 397 |
* |
| 398 |
* @param array $actions Actions array. |
| 399 |
* @param object $post Post object. |
| 400 |
* |
| 401 |
* @return array |
| 402 |
*/ |
| 403 |
function clone_link( $actions, $post ) { |
| 404 |
|
| 405 |
if ( current_user_can( 'edit_posts' ) && isset( $post ) && CARTFLOWS_FLOW_POST_TYPE === $post->post_type ) { |
| 406 |
|
| 407 |
if ( isset( $actions['duplicate'] ) ) { // Duplicate page plugin remove. |
| 408 |
unset( $actions['duplicate'] ); |
| 409 |
} |
| 410 |
if ( isset( $actions['edit_as_new_draft'] ) ) { // Duplicate post plugin remove. |
| 411 |
unset( $actions['edit_as_new_draft'] ); |
| 412 |
} |
| 413 |
|
| 414 |
$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>'; |
| 415 |
} |
| 416 |
|
| 417 |
return $actions; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Clear Page Builder Cache |
| 422 |
*/ |
| 423 |
function clear_cache() { |
| 424 |
|
| 425 |
// Clear 'Elementor' file cache. |
| 426 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 427 |
Elementor\Plugin::$instance->files_manager->clear_cache(); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Kicking this off by calling 'get_instance()' method |
| 435 |
*/ |
| 436 |
Cartflows_Cloning::get_instance(); |
| 437 |
|