Skip to content

Commit

Permalink
allow formating date tag
Browse files Browse the repository at this point in the history
  • Loading branch information
picocodes committed Apr 13, 2023
1 parent de420d0 commit 2a38a04
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
Expand Up @@ -191,8 +191,8 @@ public function get_known_smart_tags() {

'date' => array(
'description' => __( 'The current date', 'newsletter-optin-box' ),
'replacement' => current_time( 'Y-m-d' ),
'example' => 'date',
'callback' => 'Noptin_Dynamic_Content_Tags::get_date',
'example' => 'date format="j, F Y" localized=1',
'conditional_logic' => 'date',
'placeholder' => current_time( 'Y-m-d' ),
),
Expand Down Expand Up @@ -233,10 +233,9 @@ public function get_known_smart_tags() {
),

'time' => array(
// translators: %s is the current time.
'description' => __( 'The current time', 'newsletter-optin-box' ),
'replacement' => gmdate( 'H:i:s' ),
'example' => 'time',
'callback' => 'Noptin_Dynamic_Content_Tags::get_time',
'example' => 'time format="g:i a" localized=1',
),

);
Expand Down
42 changes: 41 additions & 1 deletion includes/class-noptin-dynamic-content-tags.php
Expand Up @@ -75,7 +75,8 @@ protected function register() {
$this->tags['date'] = array(
// translators: %s is the current date.
'description' => sprintf( __( 'The current date. Example: %s.', 'newsletter-optin-box' ), '<strong>' . date_i18n( get_option( 'date_format' ) ) . '</strong>' ),
'replacement' => date_i18n( get_option( 'date_format' ) ),
'callback' => 'Noptin_Dynamic_Content_Tags::get_date',
'example' => 'date format="j, F Y" localized=1',
);

$this->tags['time'] = array(
Expand Down Expand Up @@ -297,6 +298,45 @@ public static function get_cookie( $args = array() ) {
return esc_html( $default );
}

/**
* Gets a formatted date.
*
* @param array $args
*
* @return string
*/
public static function get_date( $args = array() ) {
$time = ! empty( $args['relative'] ) ? strtotime( $args['relative'] ) : time();
$time = $time + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
$format = ! empty( $args['format'] ) ? $args['format'] : 'Y-m-d';
$localized = ! empty( $args['localized'] );

if ( $localized ) {
return date_i18n( $format, $time );
}

return gmdate( $format, $time );
}

/**
* Gets a formatted time.
*
* @param array $args
* @return string
*/
public static function get_time( $args = array() ) {
$time = ! empty( $args['relative'] ) ? strtotime( $args['relative'] ) : time();
$time = $time + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
$format = ! empty( $args['format'] ) ? $args['format'] : 'H:i:s';
$localized = ! empty( $args['localized'] );

if ( $localized ) {
return date_i18n( $format, $time );
}

return gmdate( $format, $time );
}

/*
* Custom field value of the current subscriber (if known).
*
Expand Down

0 comments on commit 2a38a04

Please sign in to comment.