| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- /**
- * Utility Helper Class - Common utility functions
- *
- * @package StudiouWCCustomReports
- */
- if (!defined('ABSPATH')) {
- exit;
- }
- class StudiouWC_Utils {
-
- /**
- * Recursively sanitize request data
- */
- public static function sanitize_request_data($data) {
- if (is_array($data)) {
- return array_map(array(self::class, 'sanitize_request_data'), $data);
- }
-
- return sanitize_text_field($data);
- }
-
- /**
- * Format price using WooCommerce formatting
- */
- public static function format_price($price) {
- return wc_price($price);
- }
-
- /**
- * Get WooCommerce order statuses
- */
- public static function get_order_statuses() {
- return wc_get_order_statuses();
- }
-
- /**
- * Validate date format
- */
- public static function is_valid_date_format($date, $format = 'Y-m-d') {
- $d = DateTime::createFromFormat($format, $date);
- return $d && $d->format($format) === $date;
- }
-
- /**
- * Generate pagination links
- */
- public static function get_pagination_links($current_page, $total_pages, $base_url) {
- $links = array();
-
- if ($current_page > 1) {
- $links['prev'] = add_query_arg('paged', $current_page - 1, $base_url);
- }
-
- if ($current_page < $total_pages) {
- $links['next'] = add_query_arg('paged', $current_page + 1, $base_url);
- }
-
- return $links;
- }
-
- /**
- * Get file extension from filename
- */
- public static function get_file_extension($filename) {
- return pathinfo($filename, PATHINFO_EXTENSION);
- }
-
- /**
- * Generate secure filename for exports
- */
- public static function generate_export_filename($prefix = 'custom-reports', $extension = 'csv') {
- $timestamp = date('Y-m-d-H-i-s');
- return sanitize_file_name($prefix . '-' . $timestamp . '.' . $extension);
- }
-
- /**
- * Convert array to CSV row
- */
- public static function array_to_csv_row($data, $delimiter = ',') {
- $output = fopen('php://temp', 'r+');
- fputcsv($output, $data, $delimiter);
- rewind($output);
- $csv_row = stream_get_contents($output);
- fclose($output);
-
- return rtrim($csv_row, "\n");
- }
-
- /**
- * Validate and sanitize URL parameters
- */
- public static function sanitize_url_params($params) {
- $sanitized = array();
-
- foreach ($params as $key => $value) {
- switch ($key) {
- case 'date_from':
- case 'date_to':
- if (self::is_valid_date_format($value)) {
- $sanitized[$key] = $value;
- }
- break;
-
- case 'per_page':
- case 'paged':
- $sanitized[$key] = max(1, intval($value));
- break;
-
- case 'orderby':
- $valid_orderby = array('category', 'orders_count', 'total_price');
- if (in_array($value, $valid_orderby)) {
- $sanitized[$key] = $value;
- }
- break;
-
- case 'order':
- if (in_array(strtoupper($value), array('ASC', 'DESC'))) {
- $sanitized[$key] = strtoupper($value);
- }
- break;
-
- case 'statuses':
- if (is_array($value)) {
- $sanitized[$key] = array_map('sanitize_text_field', $value);
- }
- break;
-
- default:
- $sanitized[$key] = sanitize_text_field($value);
- break;
- }
- }
-
- return $sanitized;
- }
-
- /**
- * Format bytes to human readable format
- */
- public static function format_bytes($bytes, $precision = 2) {
- $units = array('B', 'KB', 'MB', 'GB', 'TB');
-
- for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
- $bytes /= 1024;
- }
-
- return round($bytes, $precision) . ' ' . $units[$i];
- }
-
- /**
- * Log debug information
- */
- public static function debug_log($message, $data = null) {
- if (defined('WP_DEBUG') && WP_DEBUG) {
- $log_message = '[Studiou WC Custom Reports] ' . $message;
-
- if ($data !== null) {
- $log_message .= ' | Data: ' . print_r($data, true);
- }
-
- error_log($log_message);
- }
- }
-
- /**
- * Check if current user can manage reports
- */
- public static function current_user_can_manage_reports() {
- return current_user_can('manage_woocommerce');
- }
-
- /**
- * Get plugin information
- */
- public static function get_plugin_info() {
- return array(
- 'name' => 'QDR - Studiou WC Custom Reports',
- 'version' => StudiouWC_Main::get_version(),
- 'text_domain' => StudiouWC_Main::get_text_domain(),
- 'plugin_dir' => StudiouWC_Main::get_plugin_dir(),
- 'plugin_url' => StudiouWC_Main::get_plugin_url()
- );
- }
- }
|