| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
add_shortcode( 'wppb-format-date', 'wppb_toolbox_format_date_handler' ); |
| 6 |
function wppb_toolbox_format_date_handler( $atts ){ |
| 7 |
$a = shortcode_atts( array( |
| 8 |
'date' => null, |
| 9 |
'format' => null, |
| 10 |
),$atts ); |
| 11 |
|
| 12 |
if ($a['date'] === null) |
| 13 |
return; |
| 14 |
|
| 15 |
if ($a['format'] === null) |
| 16 |
return esc_html( $a['date'] ); |
| 17 |
|
| 18 |
$format = wppb_toolbox_sanitize_date_format( $a['format'] ); |
| 19 |
|
| 20 |
if ( $format === '' ) |
| 21 |
return esc_html( $a['date'] ); |
| 22 |
|
| 23 |
$date = strtotime($a['date']); |
| 24 |
|
| 25 |
return esc_html( date( $format, $date ) ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Keep letters, digits, and date separators; drop markup and other symbols. |
| 30 |
* |
| 31 |
* @param string $format User-supplied date format. |
| 32 |
* @return string |
| 33 |
*/ |
| 34 |
function wppb_toolbox_sanitize_date_format( $format ) { |
| 35 |
if ( ! is_string( $format ) ) |
| 36 |
return ''; |
| 37 |
|
| 38 |
return preg_replace( '/[^\p{L}0-9 \-\/.,:\\\\()]/u', '', $format ); |
| 39 |
} |
| 40 |
|