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 / contact-segmentation / contact-data.php

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

213 lines 3.9 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 if ( ! class_exists( 'Contact_Data_Updater' ) ) {
60 require_once __DIR__ . '/contact-data-updater.php';
61 }
62
63 Contact_Data_Updater::get_instance();
64 }
65
66 /**
67 * Update user info.
68 *
69 * @since 1.1.0
70 */
71 private function update_user_info() {
72 $current_user = wp_get_current_user();
73
74 $this->data['signup_date'] = strtotime( $current_user->user_registered );
75 $this->data['user_role'] = $current_user->roles;
76 }
77
78 /**
79 * Get the class instance.
80 *
81 * @since 1.1.0
82 */
83 public static function get_instance() {
84 if ( null === self::$instance ) {
85 self::$instance = new self();
86 }
87
88 return self::$instance;
89 }
90
91 /**
92 * Gets data.
93 *
94 * @since 1.1.0
95 * @return array
96 */
97 public function get_data() {
98 return array_replace( $this->data, Contact_Data_Updater::$new_data );
99 }
100
101 /**
102 * Gets cart terms.
103 *
104 * @since 1.1.0
105 * @param string $taxonomy Taxonomy.
106 */
107 public static function get_cart_terms( $taxonomy = 'product_cat' ) {
108 $product_ids = [];
109
110 foreach ( WC()->cart->get_cart() as $cart_item ) {
111 $product_ids[] = $cart_item['product_id'];
112 }
113
114 $terms = get_terms( [
115 'taxonomy' => $taxonomy,
116 'object_ids' => $product_ids,
117 ] );
118
119 $term_ids = [];
120
121 foreach ( $terms as $category ) {
122 $term_ids[] = $category->term_id;
123 }
124
125 return $term_ids;
126 }
127
128 /**
129 * Gets cart items.
130 *
131 * @since 1.1.0
132 * @return array
133 */
134 public static function get_cart_items() {
135 $product_ids = [];
136
137 foreach ( WC()->cart->get_cart() as $cart_item ) {
138 $product_ids[] = $cart_item['product_id'];
139 }
140
141 return $product_ids;
142 }
143
144 /**
145 * Gets contact data
146 *
147 * @since 1.1.0
148 */
149 public function get_contact_data() {
150 if ( ! is_user_logged_in() && ! empty( $_COOKIE['sellkit_contact_segmentation'] ) ) {
151 self::$historical_data = (array) json_decode( wp_unslash( $_COOKIE['sellkit_contact_segmentation'] ) ); // phpcs:ignore
152
153 return;
154 }
155
156 global $wpdb;
157
158 $sellkit_prefix = Database::DATABASE_PREFIX;
159
160 $results = $wpdb->get_results( // phpcs:ignore
161 $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}{$sellkit_prefix}contact_segmentation WHERE email=%s", self::get_user_email() ),// phpcs:ignore
162 ARRAY_A ); // phpcs:ignore
163
164 if ( is_wp_error( $results ) ) {
165 new \WP_Error( __( 'Somethings went wrong', 'sellkit' ) );
166 }
167
168 if ( empty( $results[0] ) || ! is_array( $results[0] ) ) {
169 return;
170 }
171
172 $output = [];
173
174 foreach ( $results[0] as $key => $result ) {
175 $output[ $key ] = maybe_unserialize( $result );
176 }
177
178 self::$historical_data = $output;
179 }
180
181 /**
182 * Gets use email
183 *
184 * @since 1.1.0
185 */
186 public static function get_user_email() {
187 global $current_user;
188
189 wp_get_current_user();
190
191 $email = $current_user->user_email;
192
193 return $email;
194 }
195
196 /**
197 * Force update hooks.
198 *
199 * @since 1.1.0
200 */
201 public function force_update_hooks() {
202 add_action( 'woocommerce_order_status_changed', function ( $order_id, $old_status, $new_status ) {
203 if ( 'completed' === $old_status ) {
204 Contact_Data_Updater::force_update_data( $order_id );
205 }
206
207 if ( 'completed' === $new_status ) {
208 Contact_Data_Updater::force_update_data( $order_id );
209 }
210 }, 10, 3 );
211 }
212 }
213