PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / admin / class-personalised-coupons.php

class-personalised-coupons.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster trunk, at includes/admin/class-personalised-coupons.php

154 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || exit;
3
4 if ( ! class_exists( 'SellKit_Personalised_Coupons' ) ) {
5 /**
6 * Settings.
7 *
8 * @since 1.1.0
9 */
10 class SellKit_Personalised_Coupons {
11
12 /**
13 * Constructor.
14 *
15 * @since 1.1.0
16 */
17 public function __construct() {
18 add_action( 'wp_ajax_sellkit_create_personalised_coupons', [ $this, 'create_new_personalised_coupons' ] );
19 }
20
21 /**
22 * Creating new Personalised Coupons.
23 *
24 * @since 1.1.0
25 */
26 public function create_new_personalised_coupons() {
27 check_ajax_referer( 'sellkit_control_panel', 'nonce' );
28
29 if ( isset( $_POST['type'] ) && 'save' === $_POST['type'] ) { // phpcs:ignore WordPress.Security
30 $fields = wp_unslash( $_POST['fields'] ); // phpcs:ignore WordPress.Security
31 $sanitized_fields = $this->sanitize_fields( $fields );
32
33 $this->validate_fields( $sanitized_fields );
34
35 $result = $this->save_new_coupon( $sanitized_fields );
36 }
37
38 if ( empty( $result ) ) {
39 wp_send_json_error( [ 'message' => __( 'something went wrong', 'sellkit' ) ] );
40 die();
41 }
42
43 wp_send_json_success(
44 [
45 'message' => __( 'The new coupon was added', 'sellkit' ),
46 'redirect_url' => admin_url( 'admin.php?page=personalised-coupons' ),
47 ]
48 );
49 die();
50 }
51
52 /**
53 * Saving new Personalised Coupons.
54 *
55 * @param array $coupon_data Coupon data.
56 *
57 * @return bool
58 *
59 * @since 1.1.0
60 */
61 private function save_new_coupon( $coupon_data ) {
62 $title = sanitize_text_field( $coupon_data['sellkit_personalised_coupons_name'] );
63 $active_coupon = filter_var( $coupon_data['sellkit_personalised_coupons_active_immediately'], FILTER_VALIDATE_BOOLEAN );
64 $post_status = $active_coupon ? 'publish' : 'draft';
65
66 $post_args = [
67 'post_title' => $title,
68 'post_type' => 'sk-personalised-coupons',
69 'post_status' => $post_status,
70 ];
71
72 $new_coupon_id = wp_insert_post( $post_args );
73
74 if ( ! empty( $new_coupon_id ) && ! is_wp_error( $new_coupon_id ) ) {
75 update_post_meta( $new_coupon_id, 'sellkit_personalised_coupon_data', $coupon_data );
76 return true;
77 }
78
79 return false;
80 }
81
82 /**
83 * Sanitize fields.
84 *
85 * @param array $fields Fields.
86 *
87 * @return array
88 *
89 * @since 1.1.0
90 */
91 private function sanitize_fields( $fields ) {
92 $new_fields = [];
93
94 foreach ( $fields as $key => $field ) {
95 if ( is_array( $fields[ $key ] ) ) {
96 $new_fields[ $key ] = $this->sanitize_fields( $fields[ $key ] );
97 }
98
99 $field_value = sanitize_text_field( $field );
100
101 if ( ! is_array( $fields[ $key ] ) && ! empty( $field_value ) ) {
102 $new_fields[ $key ] = $field_value;
103 }
104 }
105
106 return $new_fields;
107 }
108
109 /**
110 * Validate fields.
111 *
112 * @param array $fields Fields.
113 *
114 * @return void
115 *
116 * @since 1.1.0
117 */
118 public function validate_fields( $fields ) {
119 if ( ! condition_row_validation( $fields['condition_row'] ) ) {
120 wp_send_json_error( [ 'message' => 'Your condition was not completed.' ] );
121 }
122 }
123
124 /**
125 * Check Personalised Coupon is empty Or not.
126 *
127 * @since 1.1.0
128 */
129 public static function personalised_coupon_is_empty() {
130 $args = array(
131 'post_type' => 'sk-personalised-coupons',
132 'posts_per_page' => 1,
133 'post_status' => [ 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ],
134 );
135
136 $coupon_query = new WP_Query( $args );
137
138 if ( $coupon_query->have_posts() ) {
139 while ( $coupon_query->have_posts() ) {
140 $coupon_query->the_post();
141
142 return false;
143 }
144
145 wp_reset_postdata();
146 }
147
148 return true;
149 }
150 }
151
152 new SellKit_Personalised_Coupons();
153 }
154