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 / EvergreenCampaign.php

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

187 lines 5.7 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 use Hurrytimer\Utils\Helpers;
6
7 class EvergreenCampaign extends Campaign
8 {
9 /**
10 * @var Cookie_Detection
11 */
12 protected $cookieDetection;
13
14 /**
15 * @var IP_Detection
16 */
17 protected $ipDetection;
18
19 /**
20 * @var User_Session_Detection $userSessionDetection
21 */
22 protected $userSessionDetection;
23
24 const RESET_FLAG = '_hurrytimer_reset_compaign_flag';
25
26 public function __construct( $id = null )
27 {
28 parent::__construct( $id );
29
30 $this->loadSettings();
31
32 $this->ipDetection = new IP_Detection( $id );
33 $this->cookieDetection = new Cookie_Detection( $id );
34 $this->userSessionDetection = new User_Session_Detection( $id );
35 }
36
37 public function isCookieMethodEnabled()
38 {
39 return in_array( C::DETECTION_METHOD_COOKIE, $this->detectionMethods, true );
40 }
41
42 public function isIPMethodEnabled()
43 {
44 return in_array( C::DETECTION_METHOD_IP, $this->detectionMethods, true );
45 }
46
47 public function isUserSessionMethodEnabled()
48 {
49 return apply_filters( 'hurryt_enable_user_session_detection',
50 in_array( C::DETECTION_METHOD_USER_SESSION, $this->detectionMethods, true ) );
51 }
52
53 /**
54 * Reset timer.
55 *
56 * @param string $scope
57 */
58
59 public function reset( $scope = 'admin' )
60 {
61
62 // Reset the given campaign for the current admin.
63 if ( $scope === 'admin' ) {
64 $this->ipDetection->forget( $this->get_id(), true );
65 $this->userSessionDetection->forgetCurrentCampaignOfCurrentUser();
66
67 } else {
68 // Reset the given campaign for all users.
69 $this->ipDetection->forget( $this->get_id() );
70 $this->userSessionDetection->forgetCurrentCampaignOfAllUsers();
71 $this->cookieDetection->deleteCampaignCookie();
72 $this->cookieDetection->deleteCampaignResetTokenCookie();
73 }
74 $this->enqueueResetRequest();
75
76
77 }
78
79 public function enqueueResetRequest( $allCampaigns = false )
80 {
81 if ( $allCampaigns ) {
82 $campaignIds = Helpers::getCampaigns( [ 'fields' => 'ids' ] );
83 } else {
84 $campaignIds[] = $this->get_id();
85 }
86
87 foreach ( $campaignIds as $campaignId ) {
88 update_post_meta( $campaignId, self::RESET_FLAG, time() );
89 }
90 }
91
92
93 function resetAll( $scope = 'admin' )
94 {
95 if ( $scope === 'admin' ) {
96 $this->ipDetection->forgetAll( true );
97 User_Session_Detection::forgetAllCampaignsOfCurrentUser();
98 } else {
99 $this->cookieDetection->deleteAllCookies();
100 $this->ipDetection->forgetAll();
101 User_Session_Detection::forgetAllCampaignsOfAllUsers();
102
103 }
104 $this->enqueueResetRequest( true );
105
106 }
107
108
109 public function getInitiatedResetToken()
110 {
111 return get_post_meta( $this->get_id(), self::RESET_FLAG, true );
112 }
113
114 public function shouldResetTimer()
115 {
116 // Check if there a reset request.
117 $initialResetToken = $this->getInitiatedResetToken();
118
119 // If No reset request found, then resume timer.
120 if ( empty( $initialResetToken ) ) {
121 return false;
122 }
123
124 $cookieResetToken = $this->isCookieMethodEnabled() ? $this->cookieDetection->getCurrentUserResetToken() : null;
125
126 list( $ipId, $ipResetToken ) = $this->isIPMethodEnabled() ? $this->ipDetection->getCurrentUserResetToken() : null;
127
128 $sessionResetToken = $this->isUserSessionMethodEnabled() ?
129 $this->userSessionDetection->getCurrentUserResetToken() :
130 null;
131
132 $resetToken = max( $ipResetToken, $cookieResetToken, $sessionResetToken );
133 $this->ipDetection->updateCurrentUserResetToken( $ipId, $resetToken );
134 $this->userSessionDetection->updateOrCreateCurrentUserResetToken( $resetToken );
135
136 // Not reset yet for this user.
137 if ( empty( $resetToken ) ) {
138 return true;
139 }
140 // A new request is made.
141 if ( $initialResetToken > $resetToken ) {
142 return true;
143 }
144 }
145
146
147 /**
148 * Returns client expiration time.
149 *
150 * @return int
151 */
152 public function getEndDate()
153 {
154
155 $cookieEndDateTimeTS = $this->isCookieMethodEnabled() ? $this->cookieDetection->getCurrentUserEndDate() : null;
156 list( $ipId, $ipEndDateTimeTS ) = $this->isIPMethodEnabled() ? $this->ipDetection->getCurrentUserEndDate() : null;
157
158 $userSessionEndDateTimeTS = $this->isUserSessionMethodEnabled() ? $this->userSessionDetection->getEndDateTime() : null;
159
160 $endDateTimeTS = max( $cookieEndDateTimeTS, $ipEndDateTimeTS, $userSessionEndDateTimeTS );
161
162 // First visit, load a fresh timer.
163 if ( empty( $endDateTimeTS ) ) {
164 return null;
165 }
166
167 // Make the timestamp is up-to-date across all methods.
168 $this->ipDetection->updateOrCreate( $ipId, $endDateTimeTS );
169
170 $initialResetToken = $this->getInitiatedResetToken();
171 $this->ipDetection->updateCurrentUserResetToken( $ipId, $initialResetToken );
172 $this->userSessionDetection->updateOrCreateCurrentUserEndDateTime( $endDateTimeTS );
173 $this->userSessionDetection->updateOrCreateCurrentUserResetToken( $initialResetToken );
174 return $endDateTimeTS;
175 }
176
177 function setEndDate( $timestamp )
178 {
179 $this->cookieDetection->updateOrCreate( $timestamp );
180 list( $ipId ) = $this->ipDetection->getCurrentUserEndDate();
181 $this->ipDetection->updateOrCreate( $ipId, $timestamp );
182 $this->userSessionDetection->updateOrCreateCurrentUserEndDateTime( $timestamp );
183 return $this->getEndDate();
184 }
185
186 }
187