Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Plugin Name: Customize Date Time Control Test
* Author: Weston Ruter, XWP
*/
namespace Customize_Date_Time_Control_Test;
/**
* Register date established.
*
* @param \WP_Customize_Manager $wp_customize
*/
function register_date_established( \WP_Customize_Manager $wp_customize ) {
if ( ! class_exists( '\WP_Customize_Date_Time_Control' ) ) {
return;
}
$wp_customize->add_setting( 'date_established', array(
'type' => 'option',
'transport' => 'postMessage',
'validate_callback' => function( \WP_Error $validity, $value ) {
if ( ! preg_match( '/^(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d)$/', $value, $matches ) || ! checkdate( intval( $matches['month'] ), intval( $matches['day'] ), intval( $matches['year'] ) ) ) {
$validity->add( 'invalid_date', __( 'Invalid date' ) );
}
return $validity;
},
) );
$wp_customize->add_control( new \WP_Customize_Date_Time_Control(
$wp_customize,
'date_established',
array(
'label' => 'Date Established',
'section' => 'title_tagline',
'twelve_hour_format' => true,
'include_time' => false,
'settings' => array(
'default' => 'date_established',
),
)
) );
$wp_customize->selective_refresh->add_partial(
'date_established',
array(
'selector' => '#date-established',
'container_inclusive' => true,
'render_callback' => __NAMESPACE__ . '\render_date_established',
)
);
}
\add_action( 'customize_register', __NAMESPACE__ . '\register_date_established' );
/**
* Add some styles to force the date-established element to be more readily seen across themes.
*/
function print_styles() {
?>
<style>
#date-established {
position: fixed;
bottom: 0;
right: 0;
margin: 0.5em;
background-color: rgba( 255, 255, 255, 0.5 );
color: black;
font-weight: bold;
}
</style>
<?php
}
\add_action( 'wp_print_styles', __NAMESPACE__ . '\print_styles' );
/**
* Render date established.
*/
function render_date_established() {
if ( ! \get_option( 'date_established' ) && ! \is_customize_preview() ) {
return;
}
$date_established = \get_option( 'date_established' );
try {
if ( \get_option( 'timezone_string' ) ) {
$timezone = new \DateTimeZone( \get_option( 'timezone_string' ) );
} else {
$offset = \get_option( 'gmt_offset', 0 );
$abs_offset = abs( $offset );
$formatted_offset = sprintf(
'%s%02d:%02d',
( $offset < 0 ? '-' : '+' ),
floor( $abs_offset ),
( $abs_offset - floor( $abs_offset ) ) * 60
);
$timezone = \DateTime::createFromFormat( 'O', $formatted_offset )->getTimezone();
}
} catch ( \Exception $e ) {
$timezone = null;
}
try {
$datetime = new \DateTime( $date_established, $timezone );
} catch ( \Exception $e ) {
$datetime = null;
}
?>
<footer id="date-established">
<?php if ( $datetime ) : ?>
<?php
printf(
/* translators: placeholder is the date. */
__( 'This site was established %s.', 'customize-date-time-control-test' ),
$datetime->format( get_option( 'date_format' ) )
);
?>
<?php endif; ?>
</footer>
<?php
}
\add_action( 'wp_footer', __NAMESPACE__ . '\render_date_established' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.