Skip to content

Commit ba56ad7

Browse files
committed
refactor: Salesforce date time conversion
1 parent d52fac2 commit ba56ad7

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

includes/Actions/GiveWp/RecordApiHelper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ public function createGiveWpDonar($finalData)
3636
'_give_donor_last_name' => 'last_name',
3737
];
3838

39-
$donor = new Give_Donor();
39+
if (!class_exists('Give_Donor') || !\function_exists('Give')) {
40+
return;
41+
}
4042

43+
$donor = new Give_Donor();
4144
$donorId = $donor->create($finalData);
4245

4346
if (is_numeric($donorId)) {

includes/Actions/Salesforce/RecordApiHelper.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,22 @@ public static function convertToSalesforceFormat($input)
292292

293293
$input = trim($input);
294294

295-
// ------------------------------------------------------------
296-
// 1) Handle UNIX timestamps (10 or 13 digits)
297-
// ------------------------------------------------------------
298-
if (preg_match('/^\d{10}$/', $input)) {
299-
return gmdate('Y-m-d\TH:i:s\Z', (int) $input);
295+
if (empty($input)) {
296+
return $input;
300297
}
301-
if (preg_match('/^\d{13}$/', $input)) {
302-
return gmdate('Y-m-d\TH:i:s\Z', (int) ($input / 1000));
298+
299+
if (is_numeric($input)) {
300+
return $input;
301+
302+
// ------------------------------------------------------------
303+
// 1) Handle UNIX timestamps (10 or 13 digits)
304+
// ------------------------------------------------------------
305+
if (\strlen($input) === 10 && preg_match('/^\d{10}$/', $input)) {
306+
return gmdate('Y-m-d\TH:i:s\Z', (int) $input);
307+
}
308+
if (\strlen($input) === 13 && preg_match('/^\d{13}$/', $input)) {
309+
return gmdate('Y-m-d\TH:i:s\Z', (int) ($input / 1000));
310+
}
303311
}
304312

305313
// ------------------------------------------------------------
@@ -356,7 +364,7 @@ public static function convertToSalesforceFormat($input)
356364
// ------------------------------------------------------------
357365
// 7) Compact numeric formats (01022025, 20250201, 250201, 010225)
358366
// ------------------------------------------------------------
359-
if (preg_match('/^\d{8}$/', $clean)) {
367+
if (\strlen($clean) === 8 && preg_match('/^\d{8}$/', $clean)) {
360368
// YYYYMMDD or DDMMYYYY or MMDDYYYY → try multiple interpretations
361369
$candidates = [
362370
substr($clean, 0, 4) . '-' . substr($clean, 4, 2) . '-' . substr($clean, 6, 2), // YMD
@@ -369,7 +377,7 @@ public static function convertToSalesforceFormat($input)
369377
}
370378
}
371379

372-
if (preg_match('/^\d{6}$/', $clean)) {
380+
if (\strlen($clean) === 6 && preg_match('/^\d{6}$/', $clean)) {
373381
// DDMMYY / YYMMDD / MMDDYY
374382
$yy = \intval(substr($clean, -2));
375383

@@ -399,15 +407,7 @@ public static function convertToSalesforceFormat($input)
399407
// ------------------------------------------------------------
400408
// 8) Try direct DateTime parsing for most formats
401409
// ------------------------------------------------------------
402-
$tryParse = function ($value) {
403-
try {
404-
return new DateTimeImmutable($value);
405-
} catch (Throwable $e) {
406-
return;
407-
}
408-
};
409-
410-
$dt = $tryParse($clean);
410+
$dt = self::tryParseDateTimeImmutable($clean);
411411

412412
if ($dt instanceof DateTimeImmutable) {
413413
// Detect if datetime or pure date
@@ -440,4 +440,13 @@ public static function convertToSalesforceFormat($input)
440440
return $input;
441441
}
442442
}
443+
444+
private static function tryParseDateTimeImmutable($value)
445+
{
446+
try {
447+
return new DateTimeImmutable($value);
448+
} catch (Throwable $e) {
449+
return;
450+
}
451+
}
443452
}

0 commit comments

Comments
 (0)