Source Code Documentation


s2member\includes\classes\utils-time.inc.php


  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. $eot_grace_time = (integer)$GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["eot_grace_time"];
  130. $eot_grace_time = (integer)apply_filters("ws_plugin__s2member_eot_grace_time", $eot_grace_time, get_defined_vars());
  131.  
  132. $registration_time = strtotime ($user->user_registered);
  133. $last_payment_time = get_user_option ("s2member_last_payment_time", $user_id);
  134. $last_payment_time = ((int)$lpt) ? (int)$lpt : (int)$last_payment_time;
  135.  
  136. if (!($p1_time = 0) && ($period1 = trim (strtoupper ($period1))))
  137. {
  138. list ($num, $span) = preg_split ("/ /", $period1, 2);
  139.  
  140. $days = 0; // Days start at 0.
  141.  
  142. if (is_numeric ($num) && !is_numeric ($span))
  143. {
  144. $days = ($span === "D") ? 1 : $days;
  145. $days = ($span === "W") ? 7 : $days;
  146. $days = ($span === "M") ? 30 : $days;
  147. $days = ($span === "Y") ? 365 : $days;
  148. }
  149.  
  150. $p1_days = (int)$num * (int)$days;
  151. $p1_time = $p1_days * 86400;
  152. }
  153. if (!($p3_time = 0) && ($period3 = trim (strtoupper ($period3))))
  154. {
  155. list ($num, $span) = preg_split ("/ /", $period3, 2);
  156.  
  157. $days = 0; // Days start at 0.
  158.  
  159. if (is_numeric ($num) && !is_numeric ($span))
  160. {
  161. $days = ($span === "D") ? 1 : $days;
  162. $days = ($span === "W") ? 7 : $days;
  163. $days = ($span === "M") ? 30 : $days;
  164. $days = ($span === "Y") ? 365 : $days;
  165. }
  166.  
  167. $p3_days = (int)$num * (int)$days;
  168. $p3_time = $p3_days * 86400;
  169. }
  170. if (!$last_payment_time) /* If there's been no payment yet.
  171. After p1, if there was a p1. Otherwise, reg. time + 1 day grace. */
  172. $auto_eot_time = $registration_time + $p1_time + $eot_grace_time;
  173.  
  174. // Else if p1, and last payment within p1, last + p1 + 1 day grace.
  175. else if ($p1_time && $last_payment_time <= $registration_time + $p1_time)
  176. $auto_eot_time = $last_payment_time + $p1_time + $eot_grace_time;
  177.  
  178. else // Otherwise, after last payment + p3 + 1 day grace.
  179. $auto_eot_time = $last_payment_time + $p3_time + $eot_grace_time;
  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. $auto_eot_time = strtotime ("now") + $eot_time + $eot_grace_time;
  201. }
  202. settype ($auto_eot_time, "integer"); // Force to integer type here.
  203.  
  204. if ($ext && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["eot_time_ext_behavior"] === "extend")
  205. if ((int)$ext > strtotime ("now")) // Existing EOT Time must be in the future.
  206. $auto_eot_time = $auto_eot_time + ((int)$ext - strtotime ("now"));
  207.  
  208. return ($auto_eot_time <= 0) ? strtotime ("now") : $auto_eot_time;
  209. }
  210. /**
  211. * Converts a Term `[D,W,M,Y,L,Day,Week,Month,Year,Lifetime]` into `Daily`, `Weekly`, `Monthly`, `Yearly`, `Lifetime`.
  212. *
  213. * Can also handle "Period Term" combinations. Where the Period will be stripped automatically before conversion.
  214. *
  215. * For example, `1 D`, would become, just `Daily`. Another example, `3 Y` would become `Yearly`; and `1 L`, would become `Lifetime`.
  216. * Recurring examples: `2 W`, becomes `Bi-Weekly`, `3 M` becomes `Quarterly`, and `2 M` becomes `Bi-Monthly`.
  217. *
  218. * @package s2Member\Utilities
  219. * @since 3.5
  220. *
  221. * @param str $term_or_period_term A Term, or a "Period Term" combination.
  222. * @param str $directive Optional. One of `recurring|singular|plural`. Defaults to `recurring`.
  223. * @return str|bool A Term Cycle *( i.e. `Daily`, `Weekly`, `Monthly`, `Yearly`, `Lifetime`, etc. )*, else false on failure.
  224. *
  225. * @todo Add support here for fixed recurring payments configured through `rrt=""`.
  226. */
  227. public static function term_cycle ($term_or_period_term = FALSE, $directive = "recurring")
  228. {
  229. $term_cycle_key = trim (strtoupper (preg_replace ("/^(.+?) /", "", $term_or_period_term)));
  230.  
  231. if ($term_cycle_key && $directive === "recurring") // recurring = Daily, Weekly, Bi-Weekly, Monthly, Bi-Monthly, Quarterly, Yearly, Lifetime.
  232. {
  233. $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"));
  234.  
  235. $term_cycle = isset ($paypal_term_cycles[$term_cycle_key]) ? $paypal_term_cycles[$term_cycle_key] : false;
  236.  
  237. $term_cycle = (strtoupper ($term_or_period_term) === "2 W") ? _x ("Bi-Weekly", "s2member-front", "s2member") : $term_cycle;
  238. $term_cycle = (strtoupper ($term_or_period_term) === "2 M") ? _x ("Bi-Monthly", "s2member-front", "s2member") : $term_cycle;
  239. $term_cycle = (strtoupper ($term_or_period_term) === "3 M") ? _x ("Quarterly", "s2member-front", "s2member") : $term_cycle;
  240. $term_cycle = (strtoupper ($term_or_period_term) === "6 M") ? _x ("Semi-Yearly", "s2member-front", "s2member") : $term_cycle;
  241. }
  242. else if ($term_cycle_key && $directive === "singular") // singular = Day, Week, Month, Year, Lifetime.
  243. {
  244. $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"));
  245.  
  246. $term_cycle = isset ($paypal_term_cycles[$term_cycle_key]) ? $paypal_term_cycles[$term_cycle_key] : false;
  247. }
  248. else if ($term_cycle_key && $directive === "plural") // plural = Days, Weeks, Months, Years, Lifetimes.
  249. {
  250. $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"));
  251.  
  252. $term_cycle = isset ($paypal_term_cycles[$term_cycle_key]) ? $paypal_term_cycles[$term_cycle_key] : false;
  253. }
  254. return (!empty ($term_cycle)) ? $term_cycle : false;
  255. }
  256. /**
  257. * Converts a "Period Term", and Recurring flag.
  258. *
  259. * Returns a full Term explanation *(lowercase)*.
  260. * Example: `2 months`.
  261. *
  262. * @package s2Member\Utilities
  263. * @since 3.5
  264. *
  265. * @param str $period_term A "Period Term" combination.
  266. * @param bool|int|str $recurring Defaults to false. If true, the ``$period_term`` is recurring. Can also be the string `0|1|BN`.
  267. * @return str Verbose *(lowercase)* Period Term description *( i.e. `weekly`, `every 3 weeks`, `lifetime`, `3 months`, `1 month`, etc. )*.
  268. *
  269. * @todo Add support here for fixed recurring payments configured through `rrt=""`.
  270. */
  271. public static function period_term ($period_term = FALSE, $recurring = FALSE)
  272. {
  273. list ($period, $term) = preg_split ("/ /", ($period_term = strtoupper ($period_term)), 2);
  274. $recurring = (is_string ($recurring) && strtoupper ($recurring) === "BN") ? (int)0 : (int)$recurring;
  275.  
  276. $cycle_recurring = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "recurring");
  277. $cycle_singular = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "singular");
  278. $cycle_plural = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "plural");
  279.  
  280. if ($recurring && in_array ($period_term, array ("1 D", "1 W", "2 W", "1 M", "2 M", "3 M", "6 M", "1 Y")))
  281. $period_term = strtolower ($cycle_recurring); // Results in an "ly" ending.
  282.  
  283. else if ($recurring) // Otherwise, it's recurring; but NOT an "ly" ending.
  284. /* translators: Each cycle ( i.e. `each day/week/month` or `every 2 days/weeks/months`, etc. ). Cycles are translated elsewhere. */
  285. $period_term = strtolower (sprintf (_nx ('each %2$s', 'every %1$s %3$s', $period, "s2member-front", "s2member"), $period, $cycle_singular, $cycle_plural));
  286.  
  287. else if (strtoupper ($term) === "L") // One-payment for lifetime access.
  288. $period_term = strtolower (_x ("lifetime", "s2member-front", "s2member")); // Life.
  289.  
  290. else // Otherwise, this is NOT recurring. Results in X days/weeks/months/years/lifetime.
  291. /* translators: Membership cycle ( i.e. `1 day/week/month` or `2 days/weeks/months`, etc. ). Most of this is translated elsewhere. */
  292. $period_term = strtolower (sprintf (_nx ('%1$s %2$s', '%1$s %3$s', $period, "s2member-front", "s2member"), $period, $cycle_singular, $cycle_plural));
  293.  
  294. return $period_term; // Return converted value.
  295. }
  296. /**
  297. * Converts a Billing Amount, Period Term, and Recurring flag.
  298. *
  299. * Returns a full Billing Term explanation.
  300. * Example: `1.00 for 2 months`.
  301. *
  302. * @package s2Member\Utilities
  303. * @since 3.5
  304. *
  305. * @param int|str $amount A numeric amount, usually in US dollars.
  306. * @param str $period_term A "Period Term" combo, with space separation.
  307. * @param bool|int|str $recurring Defaults to false. If true, the ``$period_term`` is recurring. Can also be the string `0|1|BN`.
  308. * @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. )*.
  309. *
  310. * @todo Add support here for fixed recurring payments configured through `rrt=""`.
  311. */
  312. public static function amount_period_term ($amount = FALSE, $period_term = FALSE, $recurring = FALSE)
  313. {
  314. list ($period, $term) = preg_split ("/ /", ($period_term = strtoupper ($period_term)), 2);
  315. $recurring = (is_string ($recurring) && strtoupper ($recurring) === "BN") ? (int)0 : (int)$recurring;
  316.  
  317. $cycle_recurring = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "recurring");
  318. $cycle_singular = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "singular");
  319. $cycle_plural = c_ws_plugin__s2member_utils_time::term_cycle ($period_term, "plural");
  320.  
  321. if ($recurring && in_array ($period_term, array ("1 D", "1 W", "2 W", "1 M", "2 M", "3 M", "6 M", "1 Y")))
  322. $amount_period_term = number_format ($amount, 2, ".", "") . " / " . strtolower ($cycle_recurring);
  323.  
  324. else if ($recurring) // Otherwise, it's recurring; but NOT an "ly" ending.
  325. /* translators: Each cycle ( i.e. `each day/week/month` or `every 2 days/weeks/months`, etc. ). Cycles are translated elsewhere. */
  326. $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));
  327.  
  328. else if (strtoupper ($term) === "L") // One-payment for lifetime access.
  329. $amount_period_term = number_format ($amount, 2, ".", ""); // Price.
  330.  
  331. else // Otherwise, this is NOT recurring. Results in 0.00 for X days/weeks/months/years/lifetime.
  332. /* translators: Cycle ( i.e. `for 1 day/week/month` or `for 2 days/weeks/months`, etc. ). Most of this is translated elsewhere. */
  333. $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));
  334.  
  335. return $amount_period_term; // Return converted value.
  336. }
  337. }
  338. }
  339. ?>

s2member\includes\classes\utils-time.inc.php

Source Code Documentation