| @@ -6,8 +6,9 @@ | ||
| 6 | 6 | * Forms class. |
| 7 | 7 | * |
| 8 | 8 | * @since 1.1.0 |
| 9 | 9 | * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) |
| 10 | + * @SuppressWarnings(PHPMD.NPathComplexity) | |
| 10 | 11 | */ |
| 11 | 12 | class Sellkit_Admin_Posts { |
| 12 | 13 | |
| 13 | 14 | /** |
| @@ -26,8 +27,16 @@ | ||
| 26 | 27 | */ |
| 27 | 28 | public $sub_action = ''; |
| 28 | 29 | |
| 29 | 30 | /** |
| 31 | + * Page. | |
| 32 | + * | |
| 33 | + * @since 1.7.4 | |
| 34 | + * @var string | |
| 35 | + */ | |
| 36 | + public $page; | |
| 37 | + | |
| 38 | + /** | |
| 30 | 39 | * Get a class instance. |
| 31 | 40 | * |
| 32 | 41 | * @since 1.1.0 |
| 33 | 42 | * |
| @@ -79,8 +88,9 @@ | ||
| 79 | 88 | public function get_post() { |
| 80 | 89 | check_ajax_referer( 'sellkit', 'nonce' ); |
| 81 | 90 | |
| 82 | 91 | $post_id = sellkit_htmlspecialchars( INPUT_GET, 'post_id' ); |
| 92 | + $page = sellkit_htmlspecialchars( INPUT_GET, 'page' ); | |
| 83 | 93 | |
| 84 | 94 | $post_data = get_post_meta( $post_id ); |
| 85 | 95 | |
| 86 | 96 | // Get post meta (without key) returns array by default, Now converts them to singles. |
| @@ -103,9 +113,11 @@ | ||
| 103 | 113 | if ( ! empty( $post_data['nodes'] ) ) { |
| 104 | 114 | delete_post_meta( $post_id, 'sellkit_steps' ); |
| 105 | 115 | unset( $post_data['sellkit_steps'] ); |
| 106 | 116 | |
| 107 | - $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; | |
| 108 | 120 | } |
| 109 | 121 | |
| 110 | 122 | $post_data['data_is_converted'] = false; |
| 111 | 123 | |
| @@ -123,8 +135,21 @@ | ||
| 123 | 135 | $post_data['sellkit_post_title'] = html_entity_decode( get_the_title( $post_id ) ); |
| 124 | 136 | $post_data['sellkit_post_slug'] = get_post_field( 'post_name', $post_id ); |
| 125 | 137 | $post_data['sellkit_post_status'] = get_post_status( $post_id ); |
| 126 | 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 | + | |
| 127 | 152 | wp_send_json_success( [ 'post_data' => $post_data ] ); |
| 128 | 153 | } |
| 129 | 154 | |
| 130 | 155 | /** |
| @@ -134,10 +159,16 @@ | ||
| 134 | 159 | * @param array $steps Steps. |
| 135 | 160 | */ |
| 136 | 161 | public static function convert_steps_to_nodes( $steps ) { |
| 137 | 162 | $nodes = []; |
| 138 | - $final_steps = self::add_decision_node_before_upsells( $steps ); | |
| 163 | + $final_steps = $steps; | |
| 139 | 164 | |
| 165 | + $first_key = array_key_first( $steps ); | |
| 166 | + | |
| 167 | + if ( empty( $steps[ $first_key ]['targets'] ) ) { | |
| 168 | + $final_steps = self::add_decision_node_before_upsells( $steps ); | |
| 169 | + } | |
| 170 | + | |
| 140 | 171 | foreach ( $final_steps as $key => $step ) { |
| 141 | 172 | $nodes[ $key ] = $step; |
| 142 | 173 | |
| 143 | 174 | if ( ! empty( $step['type']->key ) && 'decision' !== $step['type']->key ) { |
| @@ -155,8 +186,16 @@ | ||
| 155 | 186 | ]; |
| 156 | 187 | } |
| 157 | 188 | |
| 158 | 189 | if ( ! empty( $step['type']->key ) && 'decision' === $step['type']->key ) { |
| 190 | + if ( is_object( $step['targets'][0] ) ) { | |
| 191 | + $step['targets'][0] = (array) $step['targets'][0]; | |
| 192 | + } | |
| 193 | + | |
| 194 | + if ( is_object( $step['targets'][1] ) ) { | |
| 195 | + $step['targets'][1] = (array) $step['targets'][1]; | |
| 196 | + } | |
| 197 | + | |
| 159 | 198 | if ( empty( $final_steps[ $step['targets'][0]['nodeId'] ] ) ) { |
| 160 | 199 | $nodes[ $key ]['targets'][0] = ''; |
| 161 | 200 | } |
| 162 | 201 | |
| @@ -182,9 +221,9 @@ | ||
| 182 | 221 | $upsell_num = 0; |
| 183 | 222 | |
| 184 | 223 | foreach ( $steps as $key => $step ) { |
| 185 | 224 | $step['type'] = (object) $step['type']; |
| 186 | - $step['data'] = (object) $step['data']; | |
| 225 | + $step['data'] = ! empty( $step['data'] ) ? (object) $step['data'] : null; | |
| 187 | 226 | |
| 188 | 227 | if ( ! empty( $step['type']->key ) && 'upsell' === $step['type']->key ) { |
| 189 | 228 | $new_node_id = wp_insert_post( [ |
| 190 | 229 | 'post_type' => Sellkit_Admin_Steps::SELLKIT_STEP_POST_TYPE, |
| @@ -241,8 +280,9 @@ | ||
| 241 | 280 | $fields = sellkit_post( 'fields' ); |
| 242 | 281 | $post_id = sellkit_htmlspecialchars( INPUT_POST, 'post_id' ); |
| 243 | 282 | $post_type = sellkit_htmlspecialchars( INPUT_POST, 'post_type' ); |
| 244 | 283 | $this->sub_action = sellkit_htmlspecialchars( INPUT_POST, 'sub_action' ); |
| 284 | + $this->page = sellkit_htmlspecialchars( INPUT_POST, 'page' ); | |
| 245 | 285 | |
| 246 | 286 | $sanitized_fields = $this->sanitize_fields( $fields ); |
| 247 | 287 | |
| 248 | 288 | $this->validate_fields( $sanitized_fields ); |
| @@ -281,8 +321,12 @@ | ||
| 281 | 321 | $title = sanitize_text_field( $data['sellkit_post_title'] ); |
| 282 | 322 | $slug = ! empty( $data['sellkit_post_slug'] ) ? sanitize_text_field( $data['sellkit_post_slug'] ) : ''; |
| 283 | 323 | $post_status = 'publish' === $data['sellkit_post_status'] ? 'publish' : 'draft'; |
| 284 | 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 | + | |
| 285 | 329 | $post_args = [ |
| 286 | 330 | 'post_title' => $title, |
| 287 | 331 | 'post_name' => $slug ?: sanitize_title( $title ), |
| 288 | 332 | 'post_type' => $post_type, |
| @@ -329,8 +373,12 @@ | ||
| 329 | 373 | if ( empty( $data['nodes'] ) ) { |
| 330 | 374 | delete_post_meta( $new_post_id, 'nodes' ); |
| 331 | 375 | } |
| 332 | 376 | |
| 377 | + if ( 'checkout' === $this->page ) { | |
| 378 | + update_option( 'sellkit_global_checkout_id', $new_post_id ); | |
| 379 | + } | |
| 380 | + | |
| 333 | 381 | return $new_post_id; |
| 334 | 382 | } |
| 335 | 383 | |
| 336 | 384 | /** |
| @@ -385,8 +433,59 @@ | ||
| 385 | 433 | return; |
| 386 | 434 | } |
| 387 | 435 | |
| 388 | 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; | |
| 389 | 488 | } |
| 390 | 489 | } |
| 391 | 490 | |
| 392 | 491 | Sellkit_Admin_Posts::get_instance(); |