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() ); } }