PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 111216
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v111216
260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 120219 All 188 releases
s2member / includes / classes / utils-time.inc.php

utils-time.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 111216, at includes/classes/utils-time.inc.php

341 lines 18.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Time utilities.
4 *
5 * Copyright: © 2009-2011
6 * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
7 * ( coded in the USA )
8 *
9 * Released under the terms of the GNU General Public License.
10 * You should have received a copy of the GNU General Public License,
11 * along with this software. In the main directory, see: /licensing/
12 * If not, see: {@link http://www.gnu.org/licenses/}.
13 *
14 * @package s2Member\Utilities
15 * @since 3.5
16 */
17 if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"]))
18 exit("Do not access this file directly.");
19 /**/
20 if (!class_exists ("c_ws_plugin__s2member_utils_time"))
21 {
22 /**
23 * Time utilities.
24 *
25 * @package s2Member\Utilities
26 * @since 3.5
27 */
28 class c_ws_plugin__s2member_utils_time
29 {
30 /**
31 * Determines the difference between two timestamps.
32 *
33 * Returns the difference in a human readable format.
34 * Supports: minutes, hours, days, weeks, months, and years. This is an improvement on WordPress® ``human_time_diff()``.
35 * This returns an "approximate" time difference. Rounded to the nearest minute, hour, day, week, month, year.
36 *
37 * @package s2Member\Utilities
38 * @since 3.5
39 *
40 * @param int $from Beginning timestamp to start from.
41 * @param int $to Ending timestamp to stop at.
42 * @return str Human readable difference between ``$from`` and ``$to``.
43 */
44 public static function approx_time_difference ($from = FALSE, $to = FALSE)
45 {
46 $from = (!$from) ? strtotime ("now") : (int)$from;
47 $to = (!$to) ? strtotime ("now") : (int)$to;
48 /**/
49 if (($difference = abs ($to - $from)) < 3600)
50 {
51 $m = (int)round ($difference / 60);
52 /**/
53 $since = ($m < 1) ? _x ("less than a minute", "s2member-front", "s2member") : $since;
54 $since = ($m === 1) ? _x ("1 minute", "s2member-front", "s2member") : $since;
55 $since = ($m > 1) ? sprintf (_nx ("%s minute", "%s minutes", $m, "s2member-front", "s2member"), $m) : $since;
56 $since = ($m >= 60) ? _x ("about 1 hour", "s2member-front", "s2member") : $since;
57 }
58 else if ($difference >= 3600 && $difference < 86400)
59 {
60 $h = (int)round ($difference / 3600);
61 /**/
62 $since = ($h === 1) ? _x ("1 hour", "s2member-front", "s2member") : $since;
63 $since = ($h > 1) ? sprintf (_nx ("%s hour", "%s hours", $h, "s2member-front", "s2member"), $h) : $since;
64 $since = ($h >= 24) ? _x ("about 1 day", "s2member-front", "s2member") : $since;
65 }
66 else if ($difference >= 86400 && $difference < 604800)
67 {
68 $d = (int)round ($difference / 86400);
69 /**/
70 $since = ($d === 1) ? _x ("1 day", "s2member-front", "s2member") : $since;
71 $since = ($d > 1) ? sprintf (_nx ("%s day", "%s days", $d, "s2member-front", "s2member"), $d) : $since;
72 $since = ($d >= 7) ? _x ("about 1 week", "s2member-front", "s2member") : $since;
73 }
74 else if ($difference >= 604800 && $difference < 2592000)
75 {
76 $w = (int)round ($difference / 604800);
77 /**/
78 $since = ($w === 1) ? _x ("1 week", "s2member-front", "s2member") : $since;
79 $since = ($w > 1) ? sprintf (_nx ("%s week", "%s weeks", $w, "s2member-front", "s2member"), $w) : $since;
80 $since = ($w >= 4) ? _x ("about 1 month", "s2member-front", "s2member") : $since;
81 }
82 else if ($difference >= 2592000 && $difference < 31556926)
83 {
84 $m = (int)round ($difference / 2592000);
85 /**/
86 $since = ($m === 1) ? _x ("1 month", "s2member-front", "s2member") : $since;
87 $since = ($m > 1) ? sprintf (_nx ("%s month", "%s months", $m, "s2member-front", "s2member"), $m) : $since;
88 $since = ($m >= 12) ? _x ("about 1 year", "s2member-front", "s2member") : $since;
89 }
90 else if ($difference >= 31556926) /* Years. */
91 {
92 $y = (int)round ($difference / 31556926);
93 /**/
94 $since = ($y === 1) ? _x ("1 year", "s2member-front", "s2member") : $since;
95 $since = ($y > 1) ? sprintf (_nx ("%s year", "%s years", $y, "s2member-front", "s2member"), $y) : $since;
96 }
97 /**/
98 return $since;
99 }
100 /**
101 * Calculate Auto-EOT Time, based on `user_id`, `period1`, `period3`, `last_payment_time`, or an optional `eotper`.
102 *
103 * Used by s2Member's built-in Auto-EOT System, and also by its IPN routines.
104 * `last_payment_time` can be forced w/ ``$lpt`` *( i.e. for delayed eots )*.
105 *
106 * @package s2Member\Utilities
107 * @since 3.5
108 *
109 * @param int|str $user_id Optional. A WordPress® User ID.
110 * @param str $period1 Optional. First Intial "Period Term" *( i.e. `0 D` )*.
111 * Only used when ``$user_id`` is passed in.
112 * @param str $period3 Optional. Regular "Period Term" *( i.e. `1 M` )*.
113 * Only used when ``$user_id`` is passed in.
114 * @param str $eotper Optional. A Fixed "Period Term" *( i.e. `1 M` )*.
115 * This replaces ``$period1`` / ``$period3``.
116 * Not used when ``$user_id`` is passed in.
117 * Only when ``$user_id`` is not passed in.
118 * @param int $lpt Optional. Force feed the Last Payment Time.
119 * Only used when ``$user_id`` is passed in.
120 * @param int $ext Optional. Existing EOT Time for the User.
121 * Always considered; even when ``$user_id`` is not passed in.
122 * But only when ``$GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["eot_time_ext_behavior"] === "extend"``.
123 * @return int Unix timestamp indicating the EOT Time calculated by this routine.
124 */
125 public static function auto_eot_time ($user_id = FALSE, $period1 = FALSE, $period3 = FALSE, $eotper = FALSE, $lpt = FALSE, $ext = FALSE)
126 {
127 if ($user_id && ($user = new WP_User ($user_id)) && $user->ID) /* Valid user_id? */
128 {
129 $registration_time = strtotime ($user->user_registered);
130 $last_payment_time = get_user_option ("s2member_last_payment_time", $user_id);
131 $last_payment_time = ((int)$lpt) ? (int)$lpt : (int)$last_payment_time;
132 /**/
133 if (!($p1_time = 0) && ($period1 = trim (strtoupper ($period1))))
134 {
135 list ($num, $span) = preg_split ("/ /", $period1, 2);
136 /**/
137 $days = 0; /* Days start at 0. */
138 /**/
139 if (is_numeric ($num) && !is_numeric ($span))
140 {
141 $days = ($span === "D") ? 1 : $days;
142 $days = ($span === "W") ? 7 : $days;
143 $days = ($span === "M") ? 30 : $days;
144 $days = ($span === "Y") ? 365 : $days;
145 }
146 /**/
147 $p1_days = (int)$num * (int)$days;
148 $p1_time = $p1_days * 86400;
149 }
150 /**/
151 if (!($p3_time = 0) && ($period3 = trim (strtoupper ($period3))))
152 {
153 list ($num, $span) = preg_split ("/ /", $period3, 2);
154 /**/
155 $days = 0; /* Days start at 0. */
156 /**/
157 if (is_numeric ($num) && !is_numeric ($span))
158 {
159 $days = ($span === "D") ? 1 : $days;
160 $days = ($span === "W") ? 7 : $days;
161 $days = ($span === "M") ? 30 : $days;
162 $days = ($span === "Y") ? 365 : $days;
163 }
164 /**/
165 $p3_days = (int)$num * (int)$days;
166 $p3_time = $p3_days * 86400;
167 }
168 /**/
169 if (!$last_payment_time) /* If there's been no payment yet.
170 After p1, if there was a p1. Otherwise, reg. time + 1 day grace. */
171 $auto_eot_time = $registration_time + $p1_time + 86400;
172 /**/
173 /* Else if p1, and last payment within p1, last + p1 + 1 day grace. */
174 else if ($p1_time && $last_payment_time <= $registration_time + $p1_time)
175 $auto_eot_time = $last_payment_time + $p1_time + 86400;
176 /**/
177 else /* Otherwise, after last payment + p3 + 1 day grace. */
178 $auto_eot_time = $last_payment_time + $p3_time + 86400;
179 }
180 /**/
181 else if ($eotper) /* Otherwise, if we have a specific EOT period; calculate from today. */
182 {
183 if (!($eot_time = 0) && ($eotper = trim (strtoupper ($eotper))))
184 {
185 list ($num, $span) = preg_split ("/ /", $eotper, 2);
186 /**/
187 $days = 0; /* Days start at 0. */
188 /**/
189 if (is_numeric ($num) && !is_numeric ($span))
190 {
191 $days = ($span === "D") ? 1 : $days;
192 $days = ($span === "W") ? 7 : $days;
193 $days = ($span === "M") ? 30 : $days;
194 $days = ($span === "Y") ? 365 : $days;
195 }
196 /**/
197 $eot_days = (int)$num * (int)$days;
198 $eot_time = $eot_days * 86400;
199 }
200 /**/
201 $auto_eot_time = strtotime ("now") + $eot_time + 86400;
202 }
203 /**/
204 settype ($auto_eot_time, "integer"); /* Force to integer type here. */
205 /**/
206 if ($ext && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["eot_time_ext_behavior"] === "extend")
207 if ((int)$ext > strtotime ("now")) /* Existing EOT Time must be in the future. */
208 $auto_eot_time = $auto_eot_time + ((int)$ext - strtotime ("now"));
209 /**/
210 return ($auto_eot_time <= 0) ? strtotime ("now") : $auto_eot_time;
211 }
212 /**
213 * Converts a Term `[D,W,M,Y,L,Day,Week,Month,Year,Lifetime]` into `Daily`, `Weekly`, `Monthly`, `Yearly`, `Lifetime`.
214 *
215 * Can also handle "Period Term" combinations. Where the Period will be stripped automatically before conversion.
216 *
217 * For example, `1 D`, would become, just `Daily`. Another example, `3 Y` would become `Yearly`; and `1 L`, would become `Lifetime`.
218 * Recurring examples: `2 W`, becomes `Bi-Weekly`, `3 M` becomes `Quarterly`, and `2 M` becomes `Bi-Monthly`.
219 *
220 * @package s2Member\Utilities
221 * @since 3.5
222 *
223 * @param str $term_or_period_term A Term, or a "Period Term" combination.
224 * @param str $directive Optional. One of `recurring|singular|plural`. Defaults to `recurring`.
225 * @return str|bool A Term Cycle *( i.e. `Daily`, `Weekly`, `Monthly`, `Yearly`, `Lifetime`, etc. )*, else false on failure.
226 *
227 * @todo Add support here for fixed recurring payments configured through `rrt=""`.
228 */
229 public static function term_cycle ($term_or_period_term = FALSE, $directive = "recurring")
230 {
231 $term_cycle_key = trim (strtoupper (preg_replace ("/^(.+?) /", "", $term_or_period_term)));
232 /**/
233 if ($term_cycle_key && $directive === "recurring") /* recurring = Daily, Weekly, Bi-Weekly, Monthly, Bi-Monthly, Quarterly, Yearly, Lifetime. */
234 {
235 $paypal_term_cycles = array ("D" => _x ("Daily", "s2member-front", "s2member"), "W" => _x ("Weekly", "s2member-front", "s2member"), "M" => _x ("Monthly", "s2member-front", "s2member"), "Y" => _x ("Yearly", "s2member-front", "s2member"), "L" => _x ("Lifetime", "s2member-front", "s2member"), "DAY" => _x ("Daily", "s2member-front", "s2member"), "WEEK" => _x ("Weekly", "s2member-front", "s2member"), "MONTH" => _x ("Monthly", "s2member-front", "s2member"), "YEAR" => _x ("Yearly", "s2member-front", "s2member"), "Lifetime" => _x ("Lifetime", "s2member-front", "s2member"));
236 /**/
237 $term_cycle = isset ($paypal_term_cycles[$term_cycle_key]) ? $paypal_term_cycles[$term_cycle_key] : false;
238 /**/
239 $term_cycle = (strtoupper ($term_or_period_term) === "2 W") ? _x ("Bi-Weekly", "s2member-front", "s2member") : $term_cycle;
240 $term_cycle = (strtoupper ($term_or_period_term) === "2 M") ? _x ("Bi-Monthly", "s2member-front", "s2member") : $term_cycle;
241 $term_cycle = (strtoupper ($term_or_period_term) === "3 M") ? _x ("Quarterly", "s2member-front", "s2member") : $term_cycle;
242 }
243 else if ($term_cycle_key && $directive === "singular") /* singular = Day, Week, Month, Year, Lifetime. */
244 {
245 $paypal_term_cycles = array ("D" => _x ("Day", "s2member-front", "s2member"), "W" => _x ("Week", "s2member-front", "s2member"), "M" => _x ("Month", "s2member-front", "s2member"), "Y" => _x ("Year", "s2member-front", "s2member"), "L" => _x ("Lifetime", "s2member-front", "s2member"), "DAY" => _x ("Day", "s2member-front", "s2member"), "WEEK" => _x ("Week", "s2member-front", "s2member"), "MONTH" => _x ("Month", "s2member-front", "s2member"), "YEAR" => _x ("Year", "s2member-front", "s2member"), "Lifetime" => _x ("Lifetime", "s2member-front", "s2member"));
246 /**/
247 $term_cycle = isset ($paypal_term_cycles[$term_cycle_key]) ? $paypal_term_cycles[$term_cycle_key] : false;
248 }
249 else if ($term_cycle_key && $directive === "plural") /* plural = Days, Weeks, Months, Years, Lifetimes. */
250 {
251 $paypal_term_cycles = array ("D" => _x ("Days", "s2member-front", "s2member"), "W" => _x ("Weeks", "s2member-front", "s2member"), "M" => _x ("Months", "s2member-front", "s2member"), "Y" => _x ("Years", "s2member-front", "s2member"), "L" => _x ("Lifetimes", "s2member-front", "s2member"), "DAY" => _x ("Days", "s2member-front", "s2member"), "WEEK" => _x ("Weeks", "s2member-front", "s2member"), "MONTH" => _x ("Months", "s2member-front", "s2member"), "YEAR" => _x ("Years", "s2member-front", "s2member"), "Lifetime" => _x ("Lifetimes", "s2member-front", "s2member"));
252 /**/
253 $term_cycle = isset ($paypal_term_cycles[$term_cycle_key]) ? $paypal_term_cycles[$term_cycle_key] : false;
254 }
255 /**/
256 return (!empty ($term_cycle)) ? $term_cycle : false;
257 }
258 /**
259 * Converts a "Period Term", and Recurring flag.
260 *
261 * Returns a full Term explanation *( lowercase )*.
262 * Example: `2 months`.
263 *
264 * @package s2Member\Utilities
265 * @since 3.5
266 *
267 * @param str $period_term A "Period Term" combination.
268 * @param bool|int|str $recurring Defaults to false. If true, the ``$period_term`` is recurring. Can also be the string `0|1|BN`.
269 * @return str Verbose *( lowercase )* Period Term description *( i.e. `weekly`, `every 3 weeks`, `lifetime`, `3 months`, `1 month`, etc. )*.
270 *
271 * @todo Add support here for fixed recurring payments configured through `rrt=""`.
272 */
273 public static function period_term ($period_term = FALSE, $recurring = FALSE)
274 {
275 list ($period, $term) = preg_split ("/ /", ($period_term = strtoupper ($period_term)), 2);
276 $recurring = (is_string ($recurring) && strtoupper ($recurring) === "BN") ? (int)0 : (int)$recurring;
277 /**/
278 $cycle_recurring = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "recurring");
279 $cycle_singular = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "singular");
280 $cycle_plural = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "plural");
281 /**/
282 if ($recurring && in_array ($period_term, array ("1 D", "1 W", "2 W", "1 M", "2 M", "3 M", "1 Y")))
283 $period_term = strtolower ($cycle_recurring); /* Results in an "ly" ending. */
284 /**/
285 else if ($recurring) /* Otherwise, it's recurring; but NOT an "ly" ending. */
286 /* translators: Each cycle ( i.e. `each day/week/month` or `every 2 days/weeks/months`, etc. ). Cycles are translated elsewhere. */
287 $period_term = strtolower (sprintf (_nx ('each %2$s', 'every %1$s %3$s', $period, "s2member-front", "s2member"), $period, $cycle_singular, $cycle_plural));
288 /**/
289 else if (strtoupper ($term) === "L") /* One-payment for lifetime access. */
290 $period_term = strtolower (_x ("lifetime", "s2member-front", "s2member")); /* Life. */
291 /**/
292 else /* Otherwise, this is NOT recurring. Results in X days/weeks/months/years/lifetime. */
293 /* translators: Membership cycle ( i.e. `1 day/week/month` or `2 days/weeks/months`, etc. ). Most of this is translated elsewhere. */
294 $period_term = strtolower (sprintf (_nx ('%1$s %2$s', '%1$s %3$s', $period, "s2member-front", "s2member"), $period, $cycle_singular, $cycle_plural));
295 /**/
296 return $period_term; /* Return converted value. */
297 }
298 /**
299 * Converts a Billing Amount, Period Term, and Recurring flag.
300 *
301 * Returns a full Billing Term explanation.
302 * Example: `1.00 for 2 months`.
303 *
304 * @package s2Member\Utilities
305 * @since 3.5
306 *
307 * @param int|str $amount A numeric amount, usually in US dollars.
308 * @param str $period_term A "Period Term" combo, with space separation.
309 * @param bool|int|str $recurring Defaults to false. If true, the ``$period_term`` is recurring. Can also be the string `0|1|BN`.
310 * @return str Verbose *( lowercase )* Amount Period Term description *( i.e. `1.00`, `1.00 / monthly`, `1.00 every 3 months`, `1.00 for 1 month`, `1.00 for 3 months`, etc. )*.
311 *
312 * @todo Add support here for fixed recurring payments configured through `rrt=""`.
313 */
314 public static function amount_period_term ($amount = FALSE, $period_term = FALSE, $recurring = FALSE)
315 {
316 list ($period, $term) = preg_split ("/ /", ($period_term = strtoupper ($period_term)), 2);
317 $recurring = (is_string ($recurring) && strtoupper ($recurring) === "BN") ? (int)0 : (int)$recurring;
318 /**/
319 $cycle_recurring = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "recurring");
320 $cycle_singular = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "singular");
321 $cycle_plural = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "plural");
322 /**/
323 if ($recurring && in_array ($period_term, array ("1 D", "1 W", "2 W", "1 M", "2 M", "3 M", "1 Y")))
324 $amount_period_term = number_format ($amount, 2, ".", "") . " / " . strtolower ($cycle_recurring);
325 /**/
326 else if ($recurring) /* Otherwise, it's recurring; but NOT an "ly" ending. */
327 /* translators: Each cycle ( i.e. `each day/week/month` or `every 2 days/weeks/months`, etc. ). Cycles are translated elsewhere. */
328 $amount_period_term = number_format ($amount, 2, ".", "") . " " . strtolower (sprintf (_nx ('each %2$s', 'every %1$s %3$s', $period, "s2member-front", "s2member"), $period, $cycle_singular, $cycle_plural));
329 /**/
330 else if (strtoupper ($term) === "L") /* One-payment for lifetime access. */
331 $amount_period_term = number_format ($amount, 2, ".", ""); /* Price. */
332 /**/
333 else /* Otherwise, this is NOT recurring. Results in 0.00 for X days/weeks/months/years/lifetime. */
334 /* translators: Cycle ( i.e. `for 1 day/week/month` or `for 2 days/weeks/months`, etc. ). Most of this is translated elsewhere. */
335 $amount_period_term = number_format ($amount, 2, ".", "") . " " . strtolower (sprintf (_nx ('for %1$s %2$s', 'for %1$s %3$s', $period, "s2member-front", "s2member"), $period, $cycle_singular, $cycle_plural));
336 /**/
337 return $amount_period_term; /* Return converted value. */
338 }
339 }
340 }
341 ?>