PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.3.1
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.3.1
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 1.3.1, at includes/admin/class-personalised-coupons.php

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