| @@ -27,8 +27,16 @@ | ||
| 27 | 27 | */ |
| 28 | 28 | public $sub_action = ''; |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | + * Page. | |
| 32 | + * | |
| 33 | + * @since 1.7.4 | |
| 34 | + * @var string | |
| 35 | + */ | |
| 36 | + public $page; | |
| 37 | + | |
| 38 | + /** | |
| 31 | 39 | * Get a class instance. |
| 32 | 40 | * |
| 33 | 41 | * @since 1.1.0 |
| 34 | 42 | * |
| @@ -80,8 +88,9 @@ | ||
| 80 | 88 | public function get_post() { |
| 81 | 89 | check_ajax_referer( 'sellkit', 'nonce' ); |
| 82 | 90 | |
| 83 | 91 | $post_id = sellkit_htmlspecialchars( INPUT_GET, 'post_id' ); |
| 92 | + $page = sellkit_htmlspecialchars( INPUT_GET, 'page' ); | |
| 84 | 93 | |
| 85 | 94 | $post_data = get_post_meta( $post_id ); |
| 86 | 95 | |
| 87 | 96 | // Get post meta (without key) returns array by default, Now converts them to singles. |
| @@ -104,9 +113,11 @@ | ||
| 104 | 113 | if ( ! empty( $post_data['nodes'] ) ) { |
| 105 | 114 | delete_post_meta( $post_id, 'sellkit_steps' ); |
| 106 | 115 | unset( $post_data['sellkit_steps'] ); |
| 107 | 116 | |
| 108 | - $post_data['nodes'] = (object) unserialize( $post_data['nodes'] ); // phpcs:ignore WordPress.PHP | |
| 117 | + $new_node = $this->check_product_status( $post_data['nodes'] ); | |
| 118 | + | |
| 119 | + $post_data['nodes'] = $new_node; | |
| 109 | 120 | } |
| 110 | 121 | |
| 111 | 122 | $post_data['data_is_converted'] = false; |
| 112 | 123 | |
| @@ -124,8 +135,21 @@ | ||
| 124 | 135 | $post_data['sellkit_post_title'] = html_entity_decode( get_the_title( $post_id ) ); |
| 125 | 136 | $post_data['sellkit_post_slug'] = get_post_field( 'post_name', $post_id ); |
| 126 | 137 | $post_data['sellkit_post_status'] = get_post_status( $post_id ); |
| 127 | 138 | |
| 139 | + // Unset sales-page when user exploring global checkout page. | |
| 140 | + if ( 'checkout' === $page && ! empty( $post_data['nodes'] ) ) { | |
| 141 | + foreach ( $post_data['nodes'] as $key => $step ) { | |
| 142 | + if ( is_array( $step['type'] ) && 'sales-page' === $step['type']['key'] ) { | |
| 143 | + unset( $post_data['nodes']->$key ); | |
| 144 | + } | |
| 145 | + | |
| 146 | + if ( is_object( $step['type'] ) && 'sales-page' === $step['type']->key ) { | |
| 147 | + unset( $post_data['nodes']->$key ); | |
| 148 | + } | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 128 | 152 | wp_send_json_success( [ 'post_data' => $post_data ] ); |
| 129 | 153 | } |
| 130 | 154 | |
| 131 | 155 | /** |
| @@ -137,9 +161,11 @@ | ||
| 137 | 161 | public static function convert_steps_to_nodes( $steps ) { |
| 138 | 162 | $nodes = []; |
| 139 | 163 | $final_steps = $steps; |
| 140 | 164 | |
| 141 | - if ( empty( $steps[0]['targets'] ) ) { | |
| 165 | + $first_key = array_key_first( $steps ); | |
| 166 | + | |
| 167 | + if ( empty( $steps[ $first_key ]['targets'] ) ) { | |
| 142 | 168 | $final_steps = self::add_decision_node_before_upsells( $steps ); |
| 143 | 169 | } |
| 144 | 170 | |
| 145 | 171 | foreach ( $final_steps as $key => $step ) { |
| @@ -195,9 +221,9 @@ | ||
| 195 | 221 | $upsell_num = 0; |
| 196 | 222 | |
| 197 | 223 | foreach ( $steps as $key => $step ) { |
| 198 | 224 | $step['type'] = (object) $step['type']; |
| 199 | - $step['data'] = (object) $step['data']; | |
| 225 | + $step['data'] = ! empty( $step['data'] ) ? (object) $step['data'] : null; | |
| 200 | 226 | |
| 201 | 227 | if ( ! empty( $step['type']->key ) && 'upsell' === $step['type']->key ) { |
| 202 | 228 | $new_node_id = wp_insert_post( [ |
| 203 | 229 | 'post_type' => Sellkit_Admin_Steps::SELLKIT_STEP_POST_TYPE, |
| @@ -254,8 +280,9 @@ | ||
| 254 | 280 | $fields = sellkit_post( 'fields' ); |
| 255 | 281 | $post_id = sellkit_htmlspecialchars( INPUT_POST, 'post_id' ); |
| 256 | 282 | $post_type = sellkit_htmlspecialchars( INPUT_POST, 'post_type' ); |
| 257 | 283 | $this->sub_action = sellkit_htmlspecialchars( INPUT_POST, 'sub_action' ); |
| 284 | + $this->page = sellkit_htmlspecialchars( INPUT_POST, 'page' ); | |
| 258 | 285 | |
| 259 | 286 | $sanitized_fields = $this->sanitize_fields( $fields ); |
| 260 | 287 | |
| 261 | 288 | $this->validate_fields( $sanitized_fields ); |
| @@ -294,8 +321,12 @@ | ||
| 294 | 321 | $title = sanitize_text_field( $data['sellkit_post_title'] ); |
| 295 | 322 | $slug = ! empty( $data['sellkit_post_slug'] ) ? sanitize_text_field( $data['sellkit_post_slug'] ) : ''; |
| 296 | 323 | $post_status = 'publish' === $data['sellkit_post_status'] ? 'publish' : 'draft'; |
| 297 | 324 | |
| 325 | + if ( isset( $data['sellkit_global_checkout_header_footer_templates'] ) && 'checkout' === $this->page ) { | |
| 326 | + unset( $data['sellkit_global_checkout_header_footer_templates'] ); | |
| 327 | + } | |
| 328 | + | |
| 298 | 329 | $post_args = [ |
| 299 | 330 | 'post_title' => $title, |
| 300 | 331 | 'post_name' => $slug ?: sanitize_title( $title ), |
| 301 | 332 | 'post_type' => $post_type, |
| @@ -342,8 +373,12 @@ | ||
| 342 | 373 | if ( empty( $data['nodes'] ) ) { |
| 343 | 374 | delete_post_meta( $new_post_id, 'nodes' ); |
| 344 | 375 | } |
| 345 | 376 | |
| 377 | + if ( 'checkout' === $this->page ) { | |
| 378 | + update_option( 'sellkit_global_checkout_id', $new_post_id ); | |
| 379 | + } | |
| 380 | + | |
| 346 | 381 | return $new_post_id; |
| 347 | 382 | } |
| 348 | 383 | |
| 349 | 384 | /** |
| @@ -398,8 +433,59 @@ | ||
| 398 | 433 | return; |
| 399 | 434 | } |
| 400 | 435 | |
| 401 | 436 | wp_send_json_error( $errors ); |
| 437 | + } | |
| 438 | + | |
| 439 | + /** | |
| 440 | + * Check product status | |
| 441 | + * | |
| 442 | + * @param mixed $nodes List of node data. | |
| 443 | + * @since 1.7.9 | |
| 444 | + * @return array | |
| 445 | + */ | |
| 446 | + public function check_product_status( $nodes ) { | |
| 447 | + $nodes = (object) unserialize( $nodes ); // phpcs:ignore WordPress.PHP | |
| 448 | + $new_node = []; | |
| 449 | + | |
| 450 | + if ( ! class_exists( 'WooCommerce' ) ) { | |
| 451 | + return $new_node; | |
| 452 | + } | |
| 453 | + | |
| 454 | + foreach ( $nodes as $index => $node ) { | |
| 455 | + if ( ! empty( $node['page_id'] ) ) { | |
| 456 | + $node['link'] = get_the_permalink( $node['page_id'] ); | |
| 457 | + } | |
| 458 | + | |
| 459 | + if ( 'checkout' !== $node['type']['key'] ) { | |
| 460 | + $new_node[ $index ] = $node; | |
| 461 | + continue; | |
| 462 | + } | |
| 463 | + | |
| 464 | + if ( ! empty( $node['data']['products'] ) && ! empty( $node['data']['products']['list'] ) ) { | |
| 465 | + foreach ( $node['data']['products']['list'] as $key => $product ) { | |
| 466 | + $product_obj = wc_get_product( $key ); | |
| 467 | + | |
| 468 | + if ( empty( $product_obj ) ) { | |
| 469 | + unset( $node['data']['products']['list'][ $key ] ); | |
| 470 | + } | |
| 471 | + | |
| 472 | + $data = ! empty( $product_obj ) ? $product_obj->get_data() : []; | |
| 473 | + | |
| 474 | + if ( ! empty( $data['status'] ) && 'trash' === $data['status'] ) { | |
| 475 | + unset( $node['data']['products']['list'][ $key ] ); | |
| 476 | + } | |
| 477 | + } | |
| 478 | + } | |
| 479 | + | |
| 480 | + $new_node[ $index ] = $node; | |
| 481 | + } | |
| 482 | + | |
| 483 | + if ( empty( $new_node ) ) { | |
| 484 | + return $node; | |
| 485 | + } | |
| 486 | + | |
| 487 | + return $new_node; | |
| 402 | 488 | } |
| 403 | 489 | } |
| 404 | 490 | |
| 405 | 491 | Sellkit_Admin_Posts::get_instance(); |