PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.3.2
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.3.2
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 / contact-segmentation / contact-data.php

contact-data.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.3.2, at includes/contact-segmentation/contact-data.php

209 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Contact_Segmentation;
4
5 use Sellkit\Database;
6
7 defined( 'ABSPATH' ) || die();
8
9 /**
10 * Class Contact Data
11 *
12 * @package Sellkit\Contact_Segmentation
13 * @since 1.1.0
14 */
15 class Contact_Data {
16
17 /**
18 * Class instance.
19 *
20 * @since 1.1.0
21 * @var Contact_Data
22 */
23 private static $instance = null;
24
25 /**
26 * Contact data.
27 *
28 * @since 1.1.0
29 * @var array Contact data.
30 */
31 public $data = [];
32
33 /**
34 * Historical data.
35 *
36 * @since 1.1.0
37 * @var array Contact historical data.
38 */
39 public static $historical_data = [];
40
41 /**
42 * Contact_Data constructor.
43 *
44 * @since 1.1.0
45 */
46 public function __construct() {
47 $this->force_update_hooks();
48
49 if ( ! function_exists( 'wc' ) || ( is_admin() && ! wp_doing_ajax() ) ) {
50 return;
51 }
52
53 $this->get_contact_data();
54
55 $this->update_user_info();
56
57 $this->data = array_merge( $this->data, self::$historical_data );
58
59 Contact_Data_Updater::get_instance();
60 }
61
62 /**
63 * Update user info.
64 *
65 * @since 1.1.0
66 */
67 private function update_user_info() {
68 $current_user = wp_get_current_user();
69
70 $this->data['signup_date'] = strtotime( $current_user->user_registered );
71 $this->data['user_role'] = $current_user->roles;
72 }
73
74 /**
75 * Get the class instance.
76 *
77 * @since 1.1.0
78 */
79 public static function get_instance() {
80 if ( null === self::$instance ) {
81 self::$instance = new self();
82 }
83
84 return self::$instance;
85 }
86
87 /**
88 * Gets data.
89 *
90 * @since 1.1.0
91 * @return array
92 */
93 public function get_data() {
94 return array_replace( $this->data, Contact_Data_Updater::$new_data );
95 }
96
97 /**
98 * Gets cart terms.
99 *
100 * @since 1.1.0
101 * @param string $taxonomy Taxonomy.
102 */
103 public static function get_cart_terms( $taxonomy = 'product_cat' ) {
104 $product_ids = [];
105
106 foreach ( WC()->cart->get_cart() as $cart_item ) {
107 $product_ids[] = $cart_item['product_id'];
108 }
109
110 $terms = get_terms( [
111 'taxonomy' => $taxonomy,
112 'object_ids' => $product_ids,
113 ] );
114
115 $term_ids = [];
116
117 foreach ( $terms as $category ) {
118 $term_ids[] = $category->term_id;
119 }
120
121 return $term_ids;
122 }
123
124 /**
125 * Gets cart items.
126 *
127 * @since 1.1.0
128 * @return array
129 */
130 public static function get_cart_items() {
131 $product_ids = [];
132
133 foreach ( WC()->cart->get_cart() as $cart_item ) {
134 $product_ids[] = $cart_item['product_id'];
135 }
136
137 return $product_ids;
138 }
139
140 /**
141 * Gets contact data
142 *
143 * @since 1.1.0
144 */
145 public function get_contact_data() {
146 if ( ! is_user_logged_in() && ! empty( $_COOKIE['sellkit_contact_segmentation'] ) ) {
147 self::$historical_data = (array) json_decode( wp_unslash( $_COOKIE['sellkit_contact_segmentation'] ) ); // phpcs:ignore
148
149 return;
150 }
151
152 global $wpdb;
153
154 $sellkit_prefix = Database::DATABASE_PREFIX;
155
156 $results = $wpdb->get_results( // phpcs:ignore
157 $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}{$sellkit_prefix}contact_segmentation WHERE email=%s", self::get_user_email() ),// phpcs:ignore
158 ARRAY_A ); // phpcs:ignore
159
160 if ( is_wp_error( $results ) ) {
161 new \WP_Error( __( 'Somethings went wrong', 'sellkit' ) );
162 }
163
164 if ( empty( $results[0] ) || ! is_array( $results[0] ) ) {
165 return;
166 }
167
168 $output = [];
169
170 foreach ( $results[0] as $key => $result ) {
171 $output[ $key ] = maybe_unserialize( $result );
172 }
173
174 self::$historical_data = $output;
175 }
176
177 /**
178 * Gets use email
179 *
180 * @since 1.1.0
181 */
182 public static function get_user_email() {
183 global $current_user;
184
185 wp_get_current_user();
186
187 $email = $current_user->user_email;
188
189 return $email;
190 }
191
192 /**
193 * Force update hooks.
194 *
195 * @since 1.1.0
196 */
197 public function force_update_hooks() {
198 add_action( 'woocommerce_order_status_changed', function ( $order_id, $old_status, $new_status ) {
199 if ( 'completed' === $old_status ) {
200 Contact_Data_Updater::force_update_data( $order_id );
201 }
202
203 if ( 'completed' === $new_status ) {
204 Contact_Data_Updater::force_update_data( $order_id );
205 }
206 }, 10, 3 );
207 }
208 }
209