PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.1.4
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.1.4
2.7.0 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 All 43 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.1.4, at includes/contact-segmentation/contact-data.php

208 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 return;
149 }
150
151 global $wpdb;
152
153 $sellkit_prefix = Database::DATABASE_PREFIX;
154
155 $results = $wpdb->get_results( // phpcs:ignore
156 $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}{$sellkit_prefix}contact_segmentation WHERE email=%s", self::get_user_email() ),// phpcs:ignore
157 ARRAY_A ); // phpcs:ignore
158
159 if ( is_wp_error( $results ) ) {
160 new \WP_Error( __( 'Somethings went wrong', 'sellkit' ) );
161 }
162
163 if ( empty( $results[0] ) || ! is_array( $results[0] ) ) {
164 return;
165 }
166
167 $output = [];
168
169 foreach ( $results[0] as $key => $result ) {
170 $output[ $key ] = maybe_unserialize( $result );
171 }
172
173 self::$historical_data = $output;
174 }
175
176 /**
177 * Gets use email
178 *
179 * @since 1.1.0
180 */
181 public static function get_user_email() {
182 global $current_user;
183
184 wp_get_current_user();
185
186 $email = $current_user->user_email;
187
188 return $email;
189 }
190
191 /**
192 * Force update hooks.
193 *
194 * @since 1.1.0
195 */
196 public function force_update_hooks() {
197 add_action( 'woocommerce_order_status_changed', function ( $order_id, $old_status, $new_status ) {
198 if ( 'completed' === $old_status ) {
199 Contact_Data_Updater::force_update_data( $order_id );
200 }
201
202 if ( 'completed' === $new_status ) {
203 Contact_Data_Updater::force_update_data( $order_id );
204 }
205 }, 10, 3 );
206 }
207 }
208