@@ -39,7 +39,7 @@ public function generateReqDataFromFieldMap($data, $fieldMap)
3939 $ dataFinal [$ actionValue ] = self ::convertToSalesforceFormat (
4040 $ triggerValue === 'custom ' && isset ($ value ->customValue )
4141 ? Common::replaceFieldWithValue ($ value ->customValue , $ data )
42- : $ data [$ triggerValue ]
42+ : ( $ data [$ triggerValue ] ?? null )
4343 );
4444 }
4545
@@ -158,8 +158,8 @@ public function execute($integrationDetails, $fieldValues, $fieldMap, $actions)
158158 $ finalData = $ this ->generateReqDataFromFieldMap ($ fieldValues , $ fieldMap );
159159 $ response = $ this ->insertContact ($ finalData , $ update );
160160
161- $ responseType = empty ($ response ) || isset ($ response ->id ) ? 'success ' : 'error ' ;
162- $ typeName = !$ update || isset ($ response ->id ) ? 'Contact-create ' : 'Contact-update ' ;
161+ $ responseType = empty ($ response ) || ( \is_object ( $ response ) && isset ($ response ->id ) ) ? 'success ' : 'error ' ;
162+ $ typeName = !$ update || ( \is_object ( $ response ) && isset ($ response ->id ) ) ? 'Contact-create ' : 'Contact-update ' ;
163163 $ message = $ responseType === 'success ' ? wp_json_encode (wp_sprintf (__ ('Created contact id is : %s ' , 'bit-integrations ' ), $ response ->id )) : wp_json_encode ($ response );
164164
165165 if ($ responseType === 'success ' && $ update ) {
@@ -285,144 +285,159 @@ public function execute($integrationDetails, $fieldValues, $fieldMap, $actions)
285285
286286 public static function convertToSalesforceFormat ($ input )
287287 {
288- if (!$ input || !\is_string ($ input )) {
289- return $ input ;
290- }
288+ try {
289+ if (!$ input || !\is_string ($ input )) {
290+ return $ input ;
291+ }
291292
292- $ input = trim ($ input );
293+ $ input = trim ($ input );
293294
294- // ------------------------------------------------------------
295- // 1) Handle UNIX timestamps (10 or 13 digits)
296- // ------------------------------------------------------------
297- if (preg_match ('/^\d{10}$/ ' , $ input )) {
298- return gmdate ('Y-m-d\TH:i:s\Z ' , (int ) $ input );
299- }
300- if (preg_match ('/^\d{13}$/ ' , $ input )) {
301- return gmdate ('Y-m-d\TH:i:s\Z ' , (int ) ($ input / 1000 ));
302- }
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 );
300+ }
301+ if (preg_match ('/^\d{13}$/ ' , $ input )) {
302+ return gmdate ('Y-m-d\TH:i:s\Z ' , (int ) ($ input / 1000 ));
303+ }
303304
304- // ------------------------------------------------------------
305- // 2) Natural-language dates ("today", "tomorrow", "next Monday", etc.)
306- // ------------------------------------------------------------
307- if (preg_match ('/^[a-zA-Z ]+$/ ' , $ input ) || str_contains ($ input , 'ago ' )) {
308- $ ts = strtotime ($ input );
309- if ($ ts ) {
310- return gmdate ('Y-m-d ' , $ ts );
305+ // ------------------------------------------------------------
306+ // 2) Natural-language dates ("today", "tomorrow", "next Monday", etc.)
307+ // ------------------------------------------------------------
308+ if (preg_match ('/^[a-zA-Z ]+$/ ' , $ input ) || str_contains ($ input , 'ago ' )) {
309+ $ ts = strtotime ($ input );
310+ if ($ ts ) {
311+ return gmdate ('Y-m-d ' , $ ts );
312+ }
311313 }
312- }
313314
314- // ------------------------------------------------------------
315- // 3) Clean ordinals: 1st, 2nd, 3rd, 21st, 31st...
316- // ------------------------------------------------------------
317- $ clean = preg_replace ('/\b(\d+)(st|nd|rd|th)\b/i ' , '$1 ' , $ input );
318-
319- // ------------------------------------------------------------
320- // 4) Japanese/Chinese/Korean locale replacements
321- // ------------------------------------------------------------
322- $ clean = str_replace (
323- ['年 ' , '月 ' , '日 ' , '년 ' , '월 ' , '일 ' ],
324- ['- ' , '- ' , '' , '- ' , '- ' , '' ],
325- $ clean
326- );
327-
328- // ------------------------------------------------------------
329- // 5) Week-based formats (2025-W05 or 2025-W05-6)
330- // ------------------------------------------------------------
331- if (preg_match ('/^(\d{4})-?W(\d{2})(?:-?(\d))?$/i ' , $ clean , $ m )) {
332- $ year = $ m [1 ];
333- $ week = $ m [2 ];
334- $ day = $ m [3 ] ?? 1 ;
335-
336- try {
337- $ dt = new DateTime ("{$ year }-W {$ week }- {$ day }" , new DateTimeZone ('UTC ' ));
315+ // ------------------------------------------------------------
316+ // 3) Clean ordinals: 1st, 2nd, 3rd, 21st, 31st...
317+ // ------------------------------------------------------------
318+ $ clean = preg_replace ('/\b(\d+)(st|nd|rd|th)\b/i ' , '$1 ' , $ input );
319+
320+ // ------------------------------------------------------------
321+ // 4) Japanese/Chinese/Korean locale replacements
322+ // ------------------------------------------------------------
323+ $ clean = str_replace (
324+ ['年 ' , '月 ' , '日 ' , '년 ' , '월 ' , '일 ' ],
325+ ['- ' , '- ' , '' , '- ' , '- ' , '' ],
326+ $ clean
327+ );
338328
339- return $ dt ->format ('Y-m-d ' );
340- } catch (Throwable $ e ) {
329+ // ------------------------------------------------------------
330+ // 5) Week-based formats (2025-W05 or 2025-W05-6)
331+ // ------------------------------------------------------------
332+ if (preg_match ('/^(\d{4})-?W(\d{2})(?:-?(\d))?$/i ' , $ clean , $ m )) {
333+ $ year = $ m [1 ];
334+ $ week = $ m [2 ];
335+ $ day = $ m [3 ] ?? 1 ;
336+
337+ try {
338+ $ dt = new DateTime ("{$ year }-W {$ week }- {$ day }" , new DateTimeZone ('UTC ' ));
339+
340+ return $ dt ->format ('Y-m-d ' );
341+ } catch (Throwable $ e ) {
342+ }
341343 }
342- }
343344
344- // ------------------------------------------------------------
345- // 6) Quarter formats (Q1 2025, 2025 Q1, 1st Quarter 2025)
346- // ------------------------------------------------------------
347- if (preg_match ('/(Q[1-4]|[1-4]st Quarter)\s*[, ]*\s*(\d{4})/i ' , $ clean , $ m )) {
348- $ q = preg_replace ('/\D/ ' , '' , $ m [1 ]); // Extract 1–4
349- $ year = $ m [2 ];
350- $ month = (($ q - 1 ) * 3 ) + 1 ;
345+ // ------------------------------------------------------------
346+ // 6) Quarter formats (Q1 2025, 2025 Q1, 1st Quarter 2025)
347+ // ------------------------------------------------------------
348+ if (preg_match ('/(Q[1-4]|[1-4]st Quarter)\s*[, ]*\s*(\d{4})/i ' , $ clean , $ m )) {
349+ $ q = preg_replace ('/\D/ ' , '' , $ m [1 ]); // Extract 1–4
350+ $ year = $ m [2 ];
351+ $ month = (($ q - 1 ) * 3 ) + 1 ;
351352
352- return "{$ year }- " . str_pad ($ month , 2 , '0 ' , STR_PAD_LEFT ) . '-01 ' ;
353- }
353+ return "{$ year }- " . str_pad ($ month , 2 , '0 ' , STR_PAD_LEFT ) . '-01 ' ;
354+ }
354355
355- // ------------------------------------------------------------
356- // 7) Compact numeric formats (01022025, 20250201, 250201, 010225)
357- // ------------------------------------------------------------
358- if (preg_match ('/^\d{8}$/ ' , $ clean )) {
359- // YYYYMMDD or DDMMYYYY or MMDDYYYY → try multiple interpretations
360- $ candidates = [
361- substr ($ clean , 0 , 4 ) . '- ' . substr ($ clean , 4 , 2 ) . '- ' . substr ($ clean , 6 , 2 ), // YMD
362- substr ($ clean , 4 , 4 ) . '- ' . substr ($ clean , 2 , 2 ) . '- ' . substr ($ clean , 0 , 2 ), // DMY
363- ];
364- foreach ($ candidates as $ c ) {
365- if (strtotime ($ c )) {
366- return $ c ;
356+ // ------------------------------------------------------------
357+ // 7) Compact numeric formats (01022025, 20250201, 250201, 010225)
358+ // ------------------------------------------------------------
359+ if (preg_match ('/^\d{8}$/ ' , $ clean )) {
360+ // YYYYMMDD or DDMMYYYY or MMDDYYYY → try multiple interpretations
361+ $ candidates = [
362+ substr ($ clean , 0 , 4 ) . '- ' . substr ($ clean , 4 , 2 ) . '- ' . substr ($ clean , 6 , 2 ), // YMD
363+ substr ($ clean , 4 , 4 ) . '- ' . substr ($ clean , 2 , 2 ) . '- ' . substr ($ clean , 0 , 2 ), // DMY
364+ ];
365+ foreach ($ candidates as $ c ) {
366+ if (strtotime ($ c )) {
367+ return $ c ;
368+ }
367369 }
368370 }
369- }
370371
371- if (preg_match ('/^\d{6}$/ ' , $ clean )) {
372- // DDMMYY / YYMMDD / MMDDYY
373- $ yy = substr ($ clean , -2 );
374- $ year = $ yy > 70 ? "19 {$ yy }" : "20 {$ yy }" ;
372+ if (preg_match ('/^\d{6}$/ ' , $ clean )) {
373+ // DDMMYY / YYMMDD / MMDDYY
374+ $ yy = \intval (substr ($ clean , -2 ));
375375
376- $ dm = substr ($ clean , 0 , 2 ) . '- ' . substr ($ clean , 2 , 2 ) . '- ' . $ year ;
377- $ md = substr ($ clean , 2 , 2 ) . '- ' . substr ($ clean , 0 , 2 ) . '- ' . $ year ;
376+ // Sliding window: interpret two-digit year as closest to current year within 50 years
377+ $ currentYear = \intval (date ('Y ' ));
378+ $ century = \intval ($ currentYear / 100 ) * 100 ;
379+ $ fullYear = $ century + $ yy ;
380+ $ window = 50 ;
378381
379- foreach ([ $ dm , $ md ] as $ c ) {
380- $ ts = strtotime ( $ c ) ;
381- if ( $ ts ) {
382- return gmdate ( ' Y-m-d ' , $ ts ) ;
382+ if ( $ fullYear < $ currentYear - $ window ) {
383+ $ fullYear += 100 ;
384+ } elseif ( $ fullYear > $ currentYear + $ window ) {
385+ $ fullYear -= 100 ;
383386 }
384- }
385- }
386387
387- // ------------------------------------------------------------
388- // 8) Try direct DateTime parsing for most formats
389- // ------------------------------------------------------------
390- $ tryParse = function ($ value ) {
391- try {
392- return new DateTimeImmutable ($ value );
393- } catch (Throwable $ e ) {
394- return ;
388+ $ dm = substr ($ clean , 0 , 2 ) . '- ' . substr ($ clean , 2 , 2 ) . '- ' . $ fullYear ;
389+ $ md = substr ($ clean , 2 , 2 ) . '- ' . substr ($ clean , 0 , 2 ) . '- ' . $ fullYear ;
390+
391+ foreach ([$ dm , $ md ] as $ c ) {
392+ $ ts = strtotime ($ c );
393+ if ($ ts ) {
394+ return gmdate ('Y-m-d ' , $ ts );
395+ }
396+ }
395397 }
396- };
397398
398- $ dt = $ tryParse ($ clean );
399+ // ------------------------------------------------------------
400+ // 8) Try direct DateTime parsing for most formats
401+ // ------------------------------------------------------------
402+ $ tryParse = function ($ value ) {
403+ try {
404+ return new DateTimeImmutable ($ value );
405+ } catch (Throwable $ e ) {
406+ return ;
407+ }
408+ };
409+
410+ $ dt = $ tryParse ($ clean );
399411
400- if ($ dt instanceof DateTimeImmutable) {
401- // Detect if datetime or pure date
402- if (preg_match ('/\d{1,2}:\d/ ' , $ clean )) {
403- return $ dt ->setTimezone (new DateTimeZone ('UTC ' ))
404- ->format ('Y-m-d\TH:i:s\Z ' );
412+ if ($ dt instanceof DateTimeImmutable) {
413+ // Detect if datetime or pure date
414+ if (preg_match ('/\d{1,2}:\d/ ' , $ clean )) {
415+ return $ dt ->setTimezone (new DateTimeZone ('UTC ' ))
416+ ->format ('Y-m-d\TH:i:s\Z ' );
417+ }
418+
419+ return $ dt ->format ('Y-m-d ' );
405420 }
406421
407- return $ dt ->format ('Y-m-d ' );
408- }
422+ // ------------------------------------------------------------
423+ // 9) Last fallback using strtotime()
424+ // ------------------------------------------------------------
425+ $ ts = strtotime ($ clean );
426+ if ($ ts ) {
427+ // Detect datetime or date-only
428+ if (preg_match ('/\d{1,2}:\d/ ' , $ clean )) {
429+ return gmdate ('Y-m-d\TH:i:s\Z ' , $ ts );
430+ }
409431
410- // ------------------------------------------------------------
411- // 9) Last fallback using strtotime()
412- // ------------------------------------------------------------
413- $ ts = strtotime ($ clean );
414- if ($ ts ) {
415- // Detect datetime or date-only
416- if (preg_match ('/\d{1,2}:\d/ ' , $ clean )) {
417- return gmdate ('Y-m-d\TH:i:s\Z ' , $ ts );
432+ return gmdate ('Y-m-d ' , $ ts );
418433 }
419434
420- return gmdate ('Y-m-d ' , $ ts );
435+ // ------------------------------------------------------------
436+ // 10) No match → return original
437+ // ------------------------------------------------------------
438+ return $ input ;
439+ } catch (Throwable $ th ) {
440+ return $ input ;
421441 }
422-
423- // ------------------------------------------------------------
424- // 10) No match → return original
425- // ------------------------------------------------------------
426- return $ input ;
427442 }
428443}
0 commit comments