Last active
September 19, 2023 10:46
-
-
Save liranop/23b0525475f66ed1fbfedc3a661ce0ab to your computer and use it in GitHub Desktop.
numeric duration callback to literal (dynamic field widget - elementor + jetengine)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'jet-engine/listings/allowed-callbacks', 'jet_custom_callbacks' ); | |
/** | |
* Add callback to list. | |
*/ | |
function jet_custom_callbacks( $callbacks ) { | |
$callbacks['jet_number_to_duration'] = __( 'Number to duration', 'txt-domain' ); | |
return $callbacks; | |
} | |
/** | |
* Callback function for name. | |
*/ | |
function jet_number_to_duration( $duration ) { | |
// Check if $duration is numeric or a valid numeric string | |
if ( is_numeric( $duration ) || preg_match('/^\d+(\.\d+)?$/', $duration) ) { | |
// Convert $duration to a float | |
$duration = (float)$duration; | |
// Calculate hours and minutes | |
$hours = floor( $duration ); | |
$minutes = round(($duration - $hours) * 60); | |
// Build the duration string | |
$duration_string = ''; | |
if ($hours > 0) { | |
$duration_string .= $hours . ' ' . _n( 'hour', 'hours', $hours, 'txt-domain' ); | |
} | |
if ($minutes > 0) { | |
if ($duration_string !== '') { | |
$duration_string .= ' '; | |
} | |
$duration_string .= $minutes . ' ' . _n( 'minute', 'minutes', $minutes, 'txt-domain' ); | |
} | |
return $duration_string; | |
} | |
// If $duration is not numeric or empty, return it as is | |
if (!empty($duration)) { | |
return $duration; | |
} | |
// If $duration is empty, return an empty string | |
return ''; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment