PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / trunk
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce vtrunk
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / includes / User_Session_Detection.php

User_Session_Detection.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce trunk, at includes/User_Session_Detection.php

109 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hurrytimer;
4
5 class User_Session_Detection
6 {
7
8 const TS_META_KEY = '_hurryt_end_ts';
9 const RESET_META_KEY = '_hurryt_reset_token';
10 protected $campaignEndTSKey;
11 protected $campaignResetTokenKey;
12 protected $campaignId;
13
14 public function __construct( $campaignId )
15 {
16 $this->campaignId = $campaignId;
17 $this->campaignEndTSKey = self::TS_META_KEY . '_' . $this->campaignId;
18 $this->campaignResetTokenKey = self::RESET_META_KEY . '_' . $this->campaignId;
19 }
20
21 /**
22 * Get end date time timestamp of the campaign and current user.
23 */
24 public function getEndDateTime()
25 {
26 if ( is_user_logged_in() ) {
27 return get_user_meta( get_current_user_id(), $this->campaignEndTSKey, true );
28 }
29 }
30
31 public function forgetCurrentCampaignOfCurrentUser()
32 {
33 if ( is_user_logged_in() ) {
34 delete_user_meta( get_current_user_id(), $this->campaignEndTSKey );
35 }
36
37 }
38
39 public static function forgetAllCampaignsOfCurrentUser()
40 {
41
42 if ( is_user_logged_in() ) {
43 global $wpdb;
44
45 $wpdb->delete( $wpdb->usermeta, [
46 'meta_key' => self::TS_META_KEY . '%',
47 'user_id' => get_current_user_id()
48 ], [ '%s', '%d' ] );
49
50 $wpdb->delete( $wpdb->usermeta, [
51 'meta_key' => self::RESET_META_KEY . '%',
52 'user_id' => get_current_user_id()
53 ], [ '%s', '%d' ] );
54
55 }
56 }
57
58 public function forgetCurrentCampaignOfAllUsers()
59 {
60 global $wpdb;
61
62 $wpdb->delete( $wpdb->usermeta, [ 'meta_key' => $this->campaignEndTSKey ] );
63
64 }
65
66
67 public static function forgetAllCampaignsOfAllUsers()
68 {
69 global $wpdb;
70 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE %s", self::TS_META_KEY . '%' ) );
71 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE %s", self::RESET_META_KEY . '%' ) );
72
73 }
74
75 public function updateOrCreateCurrentUserEndDateTime( $endDateTimeTS )
76 {
77
78
79 if ( is_user_logged_in() ) {
80 if ( empty( $endDateTimeTS ) ) {
81 delete_user_meta( get_current_user_id(), $this->campaignEndTSKey );
82 } else {
83 update_user_meta( get_current_user_id(), $this->campaignEndTSKey, $endDateTimeTS );
84 }
85 }
86
87 }
88
89 public function getCurrentUserResetToken()
90 {
91 if ( is_user_logged_in() ) {
92 $ts = get_user_meta( get_current_user_id(), $this->campaignResetTokenKey, true );
93 return $ts ?: false;
94 }
95
96 return false;
97 }
98
99 public function updateOrCreateCurrentUserResetToken( $resetToken )
100 {
101 if ( is_user_logged_in() ) {
102 update_user_meta( get_current_user_id(), $this->campaignResetTokenKey, $resetToken );
103 }
104 }
105
106 // TODO:
107 // - clean up when campaign is deleted.
108
109 }