MS_Helper_Period

Utilities class.

Defined (1)

The class is defined in the following location(s).

/app/helper/class-ms-helper-period.php  
  1. class MS_Helper_Period extends MS_Helper { 
  2.  
  3. /** 
  4. * Period types 
  5. */ 
  6. const PERIOD_TYPE_DAYS = 'days'; 
  7. const PERIOD_TYPE_WEEKS = 'weeks'; 
  8. const PERIOD_TYPE_MONTHS = 'months'; 
  9. const PERIOD_TYPE_YEARS = 'years'; 
  10.  
  11. /** 
  12. * Date formats 
  13. */ 
  14. const PERIOD_FORMAT = 'Y-m-d'; 
  15. const DATE_TIME_FORMAT = 'Y-m-d H:i'; 
  16. const DATE_FORMAT_SHORT = 'y-m-d'; 
  17.  
  18. /** 
  19. * Add a period interval to a date. 
  20. * @since 1.0.0 
  21. * @param int $period_unit The period unit to add. 
  22. * @param string $period_type The period type to add. 
  23. * @param string|int $start_date The start date to add to. 
  24. * @return string The added date. 
  25. */ 
  26. public static function add_interval( $period_unit, $period_type, $start_date = null ) { 
  27. if ( empty ( $start_date ) ) { 
  28. $start_date = self::current_date(); 
  29. if ( ! is_numeric( $start_date ) ) { 
  30. $start_date = strtotime( $start_date ); 
  31. $result = $start_date; 
  32.  
  33. if ( is_numeric( $period_unit ) && $period_unit > 0 ) { 
  34. $days = self::get_period_in_days( $period_unit, $period_type ); 
  35. $result = strtotime( '+' . $days . 'days', $start_date ); 
  36.  
  37. if ( false === $result ) { 
  38. $result = $start_date; 
  39.  
  40. return apply_filters( 
  41. 'ms_helper_period_add_interval',  
  42. gmdate( self::PERIOD_FORMAT, $result ) 
  43. ); 
  44.  
  45. /** 
  46. * Subtract a period interval to a date. 
  47. * @since 1.0.0 
  48. * @param int $period_unit The period unit to subtract. 
  49. * @param string $period_type The period type to subtract. 
  50. * @param string|int $start_date The start date to subtract to. 
  51. * @return string The subtracted date. 
  52. */ 
  53. public static function subtract_interval( $period_unit, $period_type, $start_date = null ) { 
  54. if ( empty ( $start_date ) ) { 
  55. $start_date = self::current_date(); 
  56. if ( ! is_numeric( $start_date ) ) { 
  57. $start_date = strtotime( $start_date ); 
  58. $result = $start_date; 
  59.  
  60. if ( is_numeric( $period_unit ) && $period_unit > 0 ) { 
  61. $days = self::get_period_in_days( $period_unit, $period_type ); 
  62. $result = strtotime( '-' . $days . 'days', $start_date ); 
  63.  
  64. if ( false === $result ) { 
  65. $result = $start_date; 
  66.  
  67. return apply_filters( 
  68. 'ms_helper_period_subtract_interval',  
  69. gmdate( self::PERIOD_FORMAT, $result ) 
  70. ); 
  71.  
  72. /** 
  73. * Subtract dates. 
  74. * Return (end_date - start_date) in period_type format 
  75. * @since 1.0.0 
  76. * @param Date $end_date The end date to subtract from in the format yyyy-mm-dd 
  77. * @param Date $start_date The start date to subtraction the format yyyy-mm-dd 
  78. * @param int $precission Time constant HOURS_IN_SECONDS will return the 
  79. * difference in hours. Default is DAY_IN_SECONDS (return = days). 
  80. * @param bool $real_diff If set to true then the result is negative if 
  81. * enddate is before startdate. Default is false, which will return 
  82. * the absolute difference which is always positive. 
  83. * @return int The resulting difference of the date subtraction. 
  84. */ 
  85. public static function subtract_dates( $end_date, $start_date, $precission = null, $real_diff = false ) { 
  86. if ( empty( $end_date ) ) { 
  87. // Empty end date is assumed to mean "never" 
  88. $end_date = '2999-12-31'; 
  89.  
  90. $end_date = new DateTime( $end_date ); 
  91. $start_date = new DateTime( $start_date ); 
  92.  
  93. if ( ! is_numeric( $precission ) || $precission <= 0 ) { 
  94. $precission = DAY_IN_SECONDS; 
  95.  
  96. $result = intval( 
  97. ( $end_date->format( 'U' ) - $start_date->format( 'U' ) ) / 
  98. $precission 
  99. ); 
  100.  
  101. if ( ! $real_diff ) { 
  102. $result = abs( $result ); 
  103.  
  104. return apply_filters( 
  105. 'ms_helper_period_subtract_dates',  
  106. $result 
  107. ); 
  108.  
  109. /** 
  110. * Checks two things: First if the_date is a valid date and second if 
  111. * the_date occurs AFTER any other date in the arguments list. 
  112. * This function can be used to compare multiple dates, like 
  113. * $valid = is_after( $today, $date1, $date2, $date3 ); 
  114. * @since 1.0.0 
  115. * @param string|Date $the_date Date value that is compared with other dates. 
  116. * @param string|Date $before_1 Comparison Date 1 
  117. * @param ... 
  118. * @return bool True means that the_date is valid and after all other dates. 
  119. */ 
  120. public static function is_after( $the_date, $before_1 ) { 
  121. $result = true; 
  122.  
  123. if ( ! is_numeric( $the_date ) ) { 
  124. $the_date = strtotime( $the_date ); 
  125.  
  126. if ( empty( $the_date ) ) { 
  127. // No valid date specified. Fail. 
  128. $result = false; 
  129. } else { 
  130. // Valid date specified, compare with other params. 
  131. $dates = func_get_args(); 
  132.  
  133. // Remove the_date from the param list 
  134. array_shift( $dates ); 
  135.  
  136. foreach ( $dates as $comp_date ) { 
  137. if ( ! is_numeric( $comp_date ) ) { 
  138. $comp_date = strtotime( $comp_date ); 
  139.  
  140. // The date param is invalid, skip comparison. 
  141. if ( empty( $comp_date ) ) { continue; } 
  142.  
  143. if ( $comp_date > $the_date ) { 
  144. // Comparison date is bigger (=after) the_date. Fail. 
  145. $result = false; 
  146. break; 
  147.  
  148. return $result; 
  149.  
  150. /** 
  151. * Return current date. 
  152. * @since 1.0.0 
  153. * @param string $format A valid php date format. Default value is 'Y-m-d'. 
  154. * @return string The current date. 
  155. */ 
  156. public static function current_date( $format = null, $ignore_filters = false ) { 
  157. static $Date = array(); 
  158. $key = (string) $format . (int) $ignore_filters; 
  159.  
  160. if ( ! isset( $Date[$key] ) ) { 
  161. if ( empty( $format ) ) { 
  162. $format = self::PERIOD_FORMAT; 
  163.  
  164. $format = apply_filters( 
  165. 'ms_helper_period_current_date_format',  
  166. $format 
  167. ); 
  168.  
  169. $date = gmdate( $format ); 
  170.  
  171. if ( ! $ignore_filters ) { 
  172. $date = apply_filters( 
  173. 'ms_helper_period_current_date',  
  174. $date 
  175. ); 
  176. $Date[$key] = $date; 
  177.  
  178. return $Date[$key]; 
  179.  
  180. /** 
  181. * Return current timestamp. 
  182. * @since 1.0.0 
  183. * @param string $format [ mysql | timestamp | date-format like 'Y-m-d' ]. 
  184. * @return string The current timestamp. 
  185. */ 
  186. public static function current_time( $format = 'mysql', $ignore_filters = false ) { 
  187. static $Time = array(); 
  188. $key = (string) $format . (int) $ignore_filters; 
  189.  
  190. if ( ! isset( $Time[$key] ) ) { 
  191. $time = current_time( $format, 1 ); 
  192.  
  193. if ( ! $ignore_filters ) { 
  194. $time = apply_filters( 
  195. 'ms_helper_period_current_time',  
  196. $time 
  197. ); 
  198.  
  199. $Time[$key] = $time; 
  200.  
  201. return $Time[$key]; 
  202.  
  203. /** 
  204. * Return the existing period types. 
  205. * @since 1.0.0 
  206. * @param string $type [all|singular|plural] 
  207. * @return array The period types and descriptions. 
  208. */ 
  209. public static function get_period_types( $type = 'all' ) { 
  210. $singular = array( 
  211. '1' . self::PERIOD_TYPE_DAYS => __( 'one day', 'membership2' ),  
  212. '1' . self::PERIOD_TYPE_WEEKS => __( 'one week', 'membership2' ),  
  213. '1' . self::PERIOD_TYPE_MONTHS => __( 'one month', 'membership2' ),  
  214. '1' . self::PERIOD_TYPE_YEARS => __( 'one year', 'membership2' ),  
  215. '1-' . self::PERIOD_TYPE_DAYS => __( 'day', 'membership2' ),  
  216. '1-' . self::PERIOD_TYPE_WEEKS => __( 'week', 'membership2' ),  
  217. '1-' . self::PERIOD_TYPE_MONTHS => __( 'month', 'membership2' ),  
  218. '1-' . self::PERIOD_TYPE_YEARS => __( 'year', 'membership2' ),  
  219. ); 
  220. $plural = array( 
  221. self::PERIOD_TYPE_DAYS => __( 'days', 'membership2' ),  
  222. self::PERIOD_TYPE_WEEKS => __( 'weeks', 'membership2' ),  
  223. self::PERIOD_TYPE_MONTHS => __( 'months', 'membership2' ),  
  224. self::PERIOD_TYPE_YEARS => __( 'years', 'membership2' ),  
  225. ); 
  226.  
  227. switch ( $type ) { 
  228. case 'singular': $res = $singular; break; 
  229. case 'plural': $res = $plural; break; 
  230. default: $res = $singular + $plural; break; 
  231.  
  232. return apply_filters( 
  233. 'ms_helper_period_get_periods',  
  234. $res 
  235. ); 
  236.  
  237. /** 
  238. * Get period in days. 
  239. * Convert period in week, month, years to days. 
  240. * @since 1.0.0 
  241. * @param $period The period to convert. 
  242. * @return int The calculated days. 
  243. */ 
  244. public static function get_period_in_days( $unit, $type ) { 
  245. $days = 0; 
  246.  
  247. switch ( $type ) { 
  248. case self::PERIOD_TYPE_DAYS: 
  249. $days = intval( $unit ); 
  250. break; 
  251.  
  252. case self::PERIOD_TYPE_WEEKS: 
  253. $days = intval( $unit ) * 7; 
  254. break; 
  255.  
  256. case self::PERIOD_TYPE_MONTHS: 
  257. $days = intval( $unit ) * 30; 
  258. break; 
  259.  
  260. case self::PERIOD_TYPE_YEARS: 
  261. $days = intval( $unit ) * 365; 
  262. break; 
  263.  
  264. return apply_filters( 
  265. 'ms_helper_period_get_period_in_days',  
  266. $days,  
  267. $type 
  268. ); 
  269.  
  270. public static function get_period_value( $period, $field ) { 
  271. $value = null; 
  272.  
  273. if ( isset( $period[ $field ] ) ) { 
  274. $value = $period[ $field ]; 
  275. } elseif ( 'period_unit' == $field ) { 
  276. $value = 1; 
  277. } elseif ( 'period_type' == $field ) { 
  278. $value = self::PERIOD_TYPE_DAYS; 
  279.  
  280. return apply_filters( 
  281. 'ms_helper_period_get_period_value',  
  282. $value 
  283. ); 
  284.  
  285. public static function get_period_desc( $period, $include_quanity_one = false ) { 
  286. $period_unit = MS_Helper_Period::get_period_value( 
  287. $period,  
  288. 'period_unit' 
  289. ); 
  290. $period_type = MS_Helper_Period::get_period_value( 
  291. $period,  
  292. 'period_type' 
  293. ); 
  294.  
  295. $types = self::get_period_types(); 
  296.  
  297. if ( 1 == $period_unit ) { 
  298. $desc = '%2$s'; 
  299.  
  300. if ( $include_quanity_one ) { 
  301. $period_type = $types['1' . $period_type]; 
  302. } else { 
  303. $period_type = $types['1-' . $period_type]; 
  304. } else { 
  305. $desc = '%1$s %2$s'; 
  306. $desc = sprintf( $desc, $period_unit, $period_type ); 
  307.  
  308. return apply_filters( 
  309. 'ms_helper_period_get_period_desc',  
  310. $desc 
  311. ); 
  312.  
  313. /** 
  314. * Returns a valid value for the specified range-unit. 
  315. * This validation is according to PayPal IPN requiremens: 
  316. * Day -> value will be between 1 - 90 
  317. * Week -> value will be between 1 - 52 
  318. * Month -> value will be between 1 - 24 
  319. * Year -> value will be between 1 - 5 
  320. * @since 1.0.0 
  321. * @param int $value The value to validate 
  322. * @param string $unit Period unit (D/W/M/Y or long days/weeks/...) 
  323. * @return int The validated value 
  324. */ 
  325. public static function validate_range( $value, $unit ) { 
  326. if ( $value <= 1 ) { 
  327. $value = 1; 
  328. } else { 
  329. $unit = strtoupper( $unit[0] ); 
  330. $max = 1; 
  331.  
  332. switch ( $unit ) { 
  333. case 'D': $max = 90; break; 
  334. case 'W': $max = 52; break; 
  335. case 'M': $max = 24; break; 
  336. case 'Y': $max = 5; break; 
  337.  
  338. $value = min( $value, $max ); 
  339.  
  340. return $value; 
  341.  
  342. /** 
  343. * Returns a formated date string 
  344. * @param string $date The date value. 
  345. * @param string $format Optional the format to apply. 
  346. */ 
  347. public static function format_date( $date, $format = null ) { 
  348. if ( empty( $format ) ) { 
  349. $format = get_option( 'date_format' ); 
  350.  
  351. $result = self::get_date_time_value( $format, strtotime( $date ), true, true ); 
  352.  
  353. return apply_filters( 
  354. 'ms_format_date',  
  355. $result,  
  356. $date,  
  357. $format 
  358. ); 
  359.  
  360. public static function get_date_time_value( $format = null, $timestamp = false, $date = true, $time = false, $gmt = true, $zone = false ) { 
  361. $res = ''; 
  362.  
  363. if ( empty( $format ) ) { 
  364. $format = get_option( 'date_format' ); 
  365.  
  366. if ( $timestamp == false ) { 
  367. $str = current_time( 'timestamp' ); 
  368.  
  369. if ( $date ) { 
  370. $res .= date_i18n( $format, $timestamp ); 
  371.  
  372. if ( $time ) { 
  373. $zone_setting = floatval( get_option( 'gmt_offset' ) ); 
  374.  
  375. if ( $gmt ) { 
  376. $gm_offset = $zone_setting * 3600; 
  377. } else { 
  378. $gm_offset = 0; 
  379. $res .= ' ' . date_i18n( get_option( 'time_format' ), $timestamp + $gm_offset ); 
  380.  
  381. if ( $zone ) { 
  382. if ( $zone_setting ) { 
  383. $res .= ' UTC'; 
  384. } else { 
  385. $res .= ' UTC ' . ( $zone_setting > 0 ? '+ ' : '- ' ) . $zone_setting; 
  386.  
  387. return $res;