functions.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Helper functions for the plugin.
  4. *
  5. * @package QDR_Temporary_Shared_Storage
  6. * @since 1.0.0
  7. */
  8. if (!defined('WPINC')) {
  9. die;
  10. }
  11. /**
  12. * Format bytes to human readable format.
  13. *
  14. * @since 1.0.0
  15. * @param int $bytes The size in bytes.
  16. * @param int $precision The number of decimal places.
  17. * @return string Human readable size.
  18. */
  19. function qdr_tss_format_bytes($bytes, $precision = 2) {
  20. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  21. $bytes = max($bytes, 0);
  22. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  23. $pow = min($pow, count($units) - 1);
  24. $bytes /= pow(1024, $pow);
  25. return round($bytes, $precision) . ' ' . $units[$pow];
  26. }
  27. /**
  28. * Format date to localized format.
  29. *
  30. * @since 1.0.0
  31. * @param string $date_string Date string in MySQL format.
  32. * @return string Formatted date.
  33. */
  34. function qdr_tss_format_date($date_string) {
  35. if (!$date_string || $date_string === '0000-00-00 00:00:00') {
  36. return '-';
  37. }
  38. $date = strtotime($date_string);
  39. return date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $date);
  40. }
  41. /**
  42. * Check if a date is expired.
  43. *
  44. * @since 1.0.0
  45. * @param string $date_string Date string in MySQL format.
  46. * @return bool True if date is expired, false otherwise.
  47. */
  48. function qdr_tss_is_expired($date_string) {
  49. if (!$date_string || $date_string === '0000-00-00 00:00:00') {
  50. return false;
  51. }
  52. $expiry_date = strtotime($date_string);
  53. $current_time = current_time('timestamp');
  54. return $current_time > $expiry_date;
  55. }
  56. /**
  57. * Check if a date is in the future.
  58. *
  59. * @since 1.0.0
  60. * @param string $date_string Date string in MySQL format.
  61. * @return bool True if date is in the future, false otherwise.
  62. */
  63. function qdr_tss_is_future($date_string) {
  64. if (!$date_string || $date_string === '0000-00-00 00:00:00') {
  65. return false;
  66. }
  67. $future_date = strtotime($date_string);
  68. $current_time = current_time('timestamp');
  69. return $current_time < $future_date;
  70. }
  71. /**
  72. * Get file status class based on active dates.
  73. *
  74. * @since 1.0.0
  75. * @param string $active_from Active from date in MySQL format.
  76. * @param string $active_to Active to date in MySQL format.
  77. * @return string CSS class for status.
  78. */
  79. function qdr_tss_get_status_class($active_from, $active_to) {
  80. $current_time = current_time('timestamp');
  81. $from_time = strtotime($active_from);
  82. $to_time = strtotime($active_to);
  83. if ($current_time < $from_time) {
  84. return 'qdr-tss-status-pending';
  85. } elseif ($current_time > $to_time) {
  86. return 'qdr-tss-status-expired';
  87. } else {
  88. return 'qdr-tss-status-active';
  89. }
  90. }
  91. /**
  92. * Get file status label based on active dates.
  93. *
  94. * @since 1.0.0
  95. * @param string $active_from Active from date in MySQL format.
  96. * @param string $active_to Active to date in MySQL format.
  97. * @return string Status label.
  98. */
  99. function qdr_tss_get_status_label($active_from, $active_to) {
  100. $current_time = current_time('timestamp');
  101. $from_time = strtotime($active_from);
  102. $to_time = strtotime($active_to);
  103. if ($current_time < $from_time) {
  104. return __('Pending', 'qdr-temporary-shared-storage');
  105. } elseif ($current_time > $to_time) {
  106. return __('Expired', 'qdr-temporary-shared-storage');
  107. } else {
  108. return __('Active', 'qdr-temporary-shared-storage');
  109. }
  110. }