Bug: PHP Warning "Undefined array key" in `gregorian_date` due to conflict with BuddyPress relative time strings
Bug Description
When wp-parsidate is active alongside the BuddyPress plugin, PHP warnings of type Warning: Undefined array key are generated on BuddyPress-specific pages (e.g., member directories, profiles).
The issue stems from the gregorian_date function in includes/parsidate.php which expects a full date string from which it can extract at least a year, month, and day. When it receives a string like "active now", the preg_match_all call finds no numbers, leading to an empty $matches array. The subsequent attempt to access $matches[0], $matches[1], and $matches[2] fails, triggering the warnings.
Error Messages
Warning: Undefined array key 0 in ...\wp-content\plugins\wp-parsidate\includes\parsidate.php on line 416
Warning: Undefined array key 1 in ...\wp-content\plugins\wp-parsidate\includes\parsidate.php on line 416
Warning: Undefined array key 2 in ...\wp-content\plugins\wp-parsidate\includes\parsidate.php on line 416
Proposed Fix
A simple "guard clause" can be added to the gregorian_date function to validate that enough numeric parts were found before attempting to process them. If the input string is not a valid date format, it should be returned as-is, which is the correct behavior for relative time strings.
public function gregorian_date( $format, $persiandate ) {
preg_match_all( '!\d+!', $persiandate, $matches );
$matches = $matches[0];
// ---- START OF PROPOSED FIX ----
if ( count( $matches ) < 3 ) {
return $persiandate;
}
// ---- END OF PROPOSED FIX ----
list( $year, $mon, $day ) = self::persian_to_gregorian( $matches[0], $matches[1], $matches[2] );
return date( $format, mktime( ( isset( $matches[3] ) ? $matches[3] : 0 ), ( isset( $matches[4] ) ? $matches[4] : 0 ), ( isset( $matches[5] ) ? $matches[5] : 0 ), $mon, $day, $year ) );
}
Thank you for maintaining this great plugin!
Environment
- WordPress Version: 6.8.1
- PHP Version: 8.3.20
- BuddyPress Version: 14.3.4
- wp-parsidate Version: 5.1.6
Hi, This issue has been resolved in the git version Please check and let us know. Thanks @BigSolo