class-utils.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Utility Helper Class - Common utility functions
  4. *
  5. * @package StudiouWCCustomReports
  6. */
  7. if (!defined('ABSPATH')) {
  8. exit;
  9. }
  10. class StudiouWC_Utils {
  11. /**
  12. * Recursively sanitize request data
  13. */
  14. public static function sanitize_request_data($data) {
  15. if (is_array($data)) {
  16. return array_map(array(self::class, 'sanitize_request_data'), $data);
  17. }
  18. return sanitize_text_field($data);
  19. }
  20. /**
  21. * Format price using WooCommerce formatting
  22. */
  23. public static function format_price($price) {
  24. return wc_price($price);
  25. }
  26. /**
  27. * Get WooCommerce order statuses
  28. */
  29. public static function get_order_statuses() {
  30. return wc_get_order_statuses();
  31. }
  32. /**
  33. * Validate date format
  34. */
  35. public static function is_valid_date_format($date, $format = 'Y-m-d') {
  36. $d = DateTime::createFromFormat($format, $date);
  37. return $d && $d->format($format) === $date;
  38. }
  39. /**
  40. * Generate pagination links
  41. */
  42. public static function get_pagination_links($current_page, $total_pages, $base_url) {
  43. $links = array();
  44. if ($current_page > 1) {
  45. $links['prev'] = add_query_arg('paged', $current_page - 1, $base_url);
  46. }
  47. if ($current_page < $total_pages) {
  48. $links['next'] = add_query_arg('paged', $current_page + 1, $base_url);
  49. }
  50. return $links;
  51. }
  52. /**
  53. * Get file extension from filename
  54. */
  55. public static function get_file_extension($filename) {
  56. return pathinfo($filename, PATHINFO_EXTENSION);
  57. }
  58. /**
  59. * Generate secure filename for exports
  60. */
  61. public static function generate_export_filename($prefix = 'custom-reports', $extension = 'csv') {
  62. $timestamp = date('Y-m-d-H-i-s');
  63. return sanitize_file_name($prefix . '-' . $timestamp . '.' . $extension);
  64. }
  65. /**
  66. * Convert array to CSV row
  67. */
  68. public static function array_to_csv_row($data, $delimiter = ',') {
  69. $output = fopen('php://temp', 'r+');
  70. fputcsv($output, $data, $delimiter);
  71. rewind($output);
  72. $csv_row = stream_get_contents($output);
  73. fclose($output);
  74. return rtrim($csv_row, "\n");
  75. }
  76. /**
  77. * Validate and sanitize URL parameters
  78. */
  79. public static function sanitize_url_params($params) {
  80. $sanitized = array();
  81. foreach ($params as $key => $value) {
  82. switch ($key) {
  83. case 'date_from':
  84. case 'date_to':
  85. if (self::is_valid_date_format($value)) {
  86. $sanitized[$key] = $value;
  87. }
  88. break;
  89. case 'per_page':
  90. case 'paged':
  91. $sanitized[$key] = max(1, intval($value));
  92. break;
  93. case 'orderby':
  94. $valid_orderby = array('category', 'orders_count', 'total_price');
  95. if (in_array($value, $valid_orderby)) {
  96. $sanitized[$key] = $value;
  97. }
  98. break;
  99. case 'order':
  100. if (in_array(strtoupper($value), array('ASC', 'DESC'))) {
  101. $sanitized[$key] = strtoupper($value);
  102. }
  103. break;
  104. case 'statuses':
  105. if (is_array($value)) {
  106. $sanitized[$key] = array_map('sanitize_text_field', $value);
  107. }
  108. break;
  109. default:
  110. $sanitized[$key] = sanitize_text_field($value);
  111. break;
  112. }
  113. }
  114. return $sanitized;
  115. }
  116. /**
  117. * Format bytes to human readable format
  118. */
  119. public static function format_bytes($bytes, $precision = 2) {
  120. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  121. for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
  122. $bytes /= 1024;
  123. }
  124. return round($bytes, $precision) . ' ' . $units[$i];
  125. }
  126. /**
  127. * Log debug information
  128. */
  129. public static function debug_log($message, $data = null) {
  130. if (defined('WP_DEBUG') && WP_DEBUG) {
  131. $log_message = '[Studiou WC Custom Reports] ' . $message;
  132. if ($data !== null) {
  133. $log_message .= ' | Data: ' . print_r($data, true);
  134. }
  135. error_log($log_message);
  136. }
  137. }
  138. /**
  139. * Check if current user can manage reports
  140. */
  141. public static function current_user_can_manage_reports() {
  142. return current_user_can('manage_woocommerce');
  143. }
  144. /**
  145. * Get plugin information
  146. */
  147. public static function get_plugin_info() {
  148. return array(
  149. 'name' => 'QDR - Studiou WC Custom Reports',
  150. 'version' => StudiouWC_Main::get_version(),
  151. 'text_domain' => StudiouWC_Main::get_text_domain(),
  152. 'plugin_dir' => StudiouWC_Main::get_plugin_dir(),
  153. 'plugin_url' => StudiouWC_Main::get_plugin_url()
  154. );
  155. }
  156. }