PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / API / REST / V3 / Support / Item.php

Item.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/API/REST/V3/Support/Item.php

48 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\API\REST\V3\Support;
4
5 use DateTime;
6
7 /**
8 * Item utilities for WordPress REST API V3
9 *
10 * Formats DateTime objects for API responses using WordPress-compatible format.
11 * Date formatting uses WordPress mysql_to_rfc3339() function for full compatibility.
12 * Only DateTime objects are supported for date formatting.
13 *
14 * @since 4.9.0
15 */
16 class Item
17 {
18 /**
19 * @since 4.9.0
20 */
21 public static function formatDatesForResponse(array $item, array $dateFields): array
22 {
23 foreach ($dateFields as $field) {
24 if (isset($item[$field]) && self::isDateTimeObject($item[$field])) {
25 // Convert DateTime to WordPress-compatible ISO 8601 format.
26 // PHP's DateTime::format('c') includes timezone (e.g., "2025-09-02T20:27:02+00:00"),
27 // but WordPress removes the timezone for consistency across all endpoints.
28 // WordPress format: "2025-09-02T20:27:02" (without timezone)
29 // Using mysql_to_rfc3339() ensures compatibility with WordPress date formatting standards.
30
31 // phpcs:disable -- WordPress core function mysql_to_rfc3339 is intentionally used for compatibility
32 $item[$field] = mysql_to_rfc3339($item[$field]->format('c'));
33 // phpcs:enable
34 }
35 }
36
37 return $item;
38 }
39
40 /**
41 * @since 4.9.0
42 */
43 private static function isDateTimeObject($value): bool
44 {
45 return $value instanceof DateTime;
46 }
47 }
48