put_contents($file, $contents, $mode); } /** * Get contents from a file * * @param string $file The file path * @return string|bool File contents on success, false on failure */ public static function get_contents($file) { if (!self::init()) { return false; } return self::$filesystem->get_contents($file); } /** * Check if a file exists * * @param string $file The file path * @return bool True if file exists, false otherwise */ public static function exists($file) { if (!self::init()) { return false; } return self::$filesystem->exists($file); } /** * Delete a file * * @param string $file The file path * @return bool True on success, false on failure */ public static function delete($file) { if (!self::init()) { return false; } return self::$filesystem->delete($file); } /** * Create a directory * * @param string $path The directory path * @param int $mode Directory permissions (optional) * @return bool True on success, false on failure */ public static function mkdir($path, $mode = null) { if (!self::init()) { return false; } if ($mode === null) { $mode = defined('FS_CHMOD_DIR') ? FS_CHMOD_DIR : 0755; } return self::$filesystem->mkdir($path, $mode); } /** * Generate CSV content from array data * * @param array $headers Column headers * @param array $data Array of data rows * @return string CSV formatted content */ public static function generate_csv($headers, $data) { $csv = ''; // Add headers $csv .= implode(',', $headers) . "\n"; // Add data rows foreach ($data as $row) { // Ensure all headers have a value $processed_row = array(); foreach ($headers as $index => $header) { $key = is_numeric($index) ? $index : $header; $processed_row[] = isset($row[$key]) ? $row[$key] : ''; } // Escape commas in each field and wrap in quotes if needed foreach ($processed_row as &$field) { if (strpos($field, ',') !== false || strpos($field, '"') !== false) { $field = '"' . str_replace('"', '""', $field) . '"'; } } $csv .= implode(',', $processed_row) . "\n"; } return $csv; } /** * Generate CSV content from array data with proper escaping * * @param array $headers Column headers * @param array $data Array of data rows * @return string CSV formatted content (already escaped for output) */ public static function generate_csv_safe($headers, $data) { // First generate the raw CSV $csv = self::generate_csv($headers, $data); // Escape it for safe output - not needed for CSV since we're setting the Content-Type header // But we'll sanitize it nevertheless to ensure no malicious content $csv = wp_check_invalid_utf8($csv); return $csv; } /** * Safely output JSON data * * @param mixed $data Data to encode as JSON * @return string JSON encoded data (already escaped for output) */ public static function generate_json_safe($data) { return wp_json_encode($data, JSON_PRETTY_PRINT); } /** * Safely handle an uploaded file and move it to a destination * * @param array $file_data The $_FILES array element for the uploaded file * @param string $destination The destination path where the file should be saved * @param array $validation Optional validation callbacks for the file content * @return array|WP_Error Array with success status and message, or WP_Error on failure */ public static function handle_uploaded_file($file_data, $destination, $validation = array()) { if (empty($file_data) || !isset($file_data['tmp_name']) || empty($file_data['tmp_name'])) { return new WP_Error('no_file', __('No file was uploaded.', 'activity-log-pro')); } // Make sure filesystem is initialized if (!self::init()) { return new WP_Error('filesystem_error', __('Could not initialize the WordPress filesystem.', 'activity-log-pro')); } // Create a temporary file $temp_file = wp_tempnam(); // Copy the uploaded file to the temporary file if (!@copy($file_data['tmp_name'], $temp_file)) { return new WP_Error('copy_failed', __('Failed to process the uploaded file.', 'activity-log-pro')); } // Read the temporary file $file_content = self::$filesystem->get_contents($temp_file); // Delete the temporary file self::$filesystem->delete($temp_file); // If we have validation callbacks, run them if (!empty($validation) && is_array($validation)) { foreach ($validation as $callback) { if (is_callable($callback)) { $result = call_user_func($callback, $file_content); if (is_wp_error($result)) { return $result; } } } } // Write the file to the destination $result = self::$filesystem->put_contents($destination, $file_content, FS_CHMOD_FILE); if (!$result) { return new WP_Error('write_failed', __('Failed to save the file. Please check directory permissions.', 'activity-log-pro')); } return array( 'success' => true, 'message' => __('File uploaded successfully.', 'activity-log-pro') ); } }