'milestone-widget',
'description' => __( 'Display a countdown to a certain date.', 'jetpack' ),
);
parent::__construct(
'Milestone_Widget',
/** This filter is documented in modules/widgets/facebook-likebox.php */
apply_filters( 'jetpack_widget_name', __( 'Milestone', 'jetpack' ) ),
$widget
);
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_template' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin' ) );
add_action( 'wp_footer', array( $this, 'localize_script' ) );
if ( is_active_widget( false, false, $this->id_base, true ) || is_active_widget( false, false, 'monster', true ) || is_customize_preview() ) {
add_action( 'wp_head', array( __CLASS__, 'styles_template' ) );
}
}
/**
* Enqueue admin assets.
*
* @param string $hook_suffix Hook suffix provided by WordPress.
*/
public static function enqueue_admin( $hook_suffix ) {
if ( 'widgets.php' === $hook_suffix ) {
wp_enqueue_style( 'milestone-admin', plugin_dir_url( __FILE__ ) . 'style-admin.css', array(), '20201113' );
wp_enqueue_script(
'milestone-admin-js',
Assets::get_file_url_for_environment(
'_inc/build/widgets/milestone/admin.min.js',
'modules/widgets/milestone/admin.js'
),
array( 'jquery' ),
'20201113',
true
);
}
}
/**
* Enqueue the frontend JS.
*/
public static function enqueue_template() {
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
return;
}
wp_enqueue_script(
'milestone',
Assets::get_file_url_for_environment(
'_inc/build/widgets/milestone/milestone.min.js',
'modules/widgets/milestone/milestone.js'
),
array(),
'20201113',
true
);
}
/**
* Output the frontend styling.
*/
public static function styles_template() {
global $themecolors;
$colors = wp_parse_args(
$themecolors,
array(
'bg' => 'ffffff',
'border' => 'cccccc',
'text' => '333333',
)
);
?>
getTimestamp();
return array(
'title' => '',
'event' => __( 'The Big Day', 'jetpack' ),
'unit' => 'automatic',
'type' => 'until',
'message' => __( 'The big day is here.', 'jetpack' ),
'day' => gmdate( 'd', $now_timestamp ),
'month' => gmdate( 'm', $now_timestamp ),
'year' => gmdate( 'Y', $now_timestamp ),
'hour' => 0,
'min' => 0,
);
}
/**
* Widget
*
* @param array $args Widget args.
* @param array $instance Widget instance.
*/
public function widget( $args, $instance ) {
$instance = wp_parse_args( $instance, $this->defaults() );
$this->enqueue_scripts();
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $instance['title'] );
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$widget_id = ! empty( $args['widget_id'] ) ? $args['widget_id'] : 'milestone_widget';
$data = $this->get_widget_data( $instance );
$config = array(
'id' => $widget_id,
'message' => $data['message'],
'refresh' => $data['refresh'],
);
/*
* Sidebars may be configured to not expose the `widget_id`. Example: `twentytwenty` footer areas.
*
* We need our own unique identifier.
*/
$config['content_id'] = $widget_id . '-content';
self::$config_js['instances'][] = $config;
printf( '
', esc_html( $config['content_id'] ) );
echo '';
echo wp_kses_post( $data['message'] );
echo '
';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
/** This action is documented in modules/widgets/gravatar-profile.php */
do_action( 'jetpack_stats_extra', 'widget_view', 'milestone' );
}
/**
* Enqueue widget styles
*/
public function enqueue_scripts() {
wp_enqueue_style(
'milestone-widget',
plugins_url( 'milestone-widget.css', __FILE__ ),
array(),
JETPACK__VERSION
);
}
/**
* Getter for the widget data.
*
* @param array $instance Widget instance.
*
* @return array
*/
public function get_widget_data( $instance ) {
$data = array();
$instance = $this->sanitize_instance( $instance );
$milestone = mktime( $instance['hour'], $instance['min'], 0, $instance['month'], $instance['day'], $instance['year'] );
$now = (int) current_time( 'timestamp' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
$type = $instance['type'];
if ( 'since' === $type ) {
$diff = (int) floor( $now - $milestone );
} else {
$diff = (int) floor( $milestone - $now );
}
$data['diff'] = $diff;
$data['unit'] = $this->get_unit( $diff, $instance['unit'] );
// Setting the refresh counter to equal the number of seconds it takes to flip a unit.
$refresh_intervals = array(
0, // should be YEAR_IN_SECONDS, but doing setTimeout for a year doesn't seem to be logical.
0, // same goes for MONTH_IN_SECONDS.
DAY_IN_SECONDS,
HOUR_IN_SECONDS,
MINUTE_IN_SECONDS,
1,
);
$data['refresh'] = $refresh_intervals[ array_search( $data['unit'], $this->available_units, true ) ];
$data['milestone'] = $milestone;
if ( ( 1 > $diff ) && ( 'until' === $type ) ) {
$data['message'] = '' . $instance['message'] . '
';
$data['refresh'] = 0; // No need to refresh, the milestone has been reached.
} else {
$interval_text = $this->get_interval_in_units( $diff, $data['unit'] );
$interval = (int) $interval_text;
if ( 'since' === $type ) {
switch ( $data['unit'] ) {
case 'years':
$data['message'] = sprintf(
/* translators: %s is the number of year(s). */
_n(
'%s year ago.',
'%s years ago.',
$interval,
'jetpack'
),
$interval_text
);
break;
case 'months':
$data['message'] = sprintf(
/* translators: %s is the number of month(s). */
_n(
'%s month ago.',
'%s months ago.',
$interval,
'jetpack'
),
$interval_text
);
break;
case 'days':
$data['message'] = sprintf(
/* translators: %s is the number of days(s). */
_n(
'%s day ago.',
'%s days ago.',
$interval,
'jetpack'
),
$interval_text
);
break;
case 'hours':
$data['message'] = sprintf(
/* translators: %s is the number of hours(s). */
_n(
'%s hour ago.',
'%s hours ago.',
$interval,
'jetpack'
),
$interval_text
);
break;
case 'minutes':
$data['message'] = sprintf(
/* translators: %s is the number of minutes(s). */
_n(
'%s minute ago.',
'%s minutes ago.',
$interval,
'jetpack'
),
$interval_text
);
break;
case 'seconds':
$data['message'] = sprintf(
/* translators: %s is the number of second(s). */
_n(
'%s second ago.',
'%s seconds ago.',
$interval,
'jetpack'
),
$interval_text
);
break;
}
} else {
switch ( $this->get_unit( $diff, $instance['unit'] ) ) {
case 'years':
$data['message'] = sprintf(
/* translators: %s is the number of year(s). */
_n(
'%s year to go.',
'%s years to go.',
$interval,
'jetpack'
),
$interval_text
);
break;
case 'months':
$data['message'] = sprintf(
/* translators: %s is the number of month(s). */
_n(
'%s month to go.',
'%s months to go.',
$interval,
'jetpack'
),
$interval_text
);
break;
case 'days':
$data['message'] = sprintf(
/* translators: %s is the number of days(s). */
_n(
'%s day to go.',
'%s days to go.',
$interval,
'jetpack'
),
$interval_text
);
break;
case 'hours':
$data['message'] = sprintf(
/* translators: %s is the number of hour(s). */
_n(
'%s hour to go.',
'%s hours to go.',
$interval,
'jetpack'
),
$interval_text
);
break;
case 'minutes':
$data['message'] = sprintf(
/* translators: %s is the number of minute(s). */
_n(
'%s minute to go.',
'%s minutes to go.',
$interval,
'jetpack'
),
$interval_text
);
break;
case 'seconds':
$data['message'] = sprintf(
/* translators: %s is the number of second(s). */
_n(
'%s second to go.',
'%s seconds to go.',
$interval,
'jetpack'
),
$interval_text
);
break;
}
}
$data['message'] = '' . $data['message'] . '
';
}
return $data;
}
/**
* Return the largest possible time unit that the difference will be displayed in.
*
* @param Integer $seconds the interval in seconds.
* @param String $maximum_unit the maximum unit that will be used. Optional.
* @return String $calculated_unit
*/
protected function get_unit( $seconds, $maximum_unit = 'automatic' ) {
$unit = '';
if ( $seconds >= YEAR_IN_SECONDS * 2 ) {
// more than 2 years - show in years, one decimal point.
$unit = 'years';
} elseif ( $seconds >= YEAR_IN_SECONDS ) {
if ( 'years' === $maximum_unit ) {
$unit = 'years';
} else {
// automatic mode - showing months even if it's between one and two years.
$unit = 'months';
}
} elseif ( $seconds >= MONTH_IN_SECONDS * 3 ) {
// fewer than 2 years - show in months.
$unit = 'months';
} elseif ( $seconds >= MONTH_IN_SECONDS ) {
if ( 'months' === $maximum_unit ) {
$unit = 'months';
} else {
// automatic mode - showing days even if it's between one and three months.
$unit = 'days';
}
} elseif ( $seconds >= DAY_IN_SECONDS - 1 ) {
// fewer than a month - show in days.
$unit = 'days';
} elseif ( $seconds >= HOUR_IN_SECONDS - 1 ) {
// less than 1 day - show in hours.
$unit = 'hours';
} elseif ( $seconds >= MINUTE_IN_SECONDS - 1 ) {
// less than 1 hour - show in minutes.
$unit = 'minutes';
} else {
// less than 1 minute - show in seconds.
$unit = 'seconds';
}
$maximum_unit_index = array_search( $maximum_unit, $this->available_units, true );
$unit_index = array_search( $unit, $this->available_units, true );
if (
false === $maximum_unit_index // the maximum unit parameter is automatic.
|| $unit_index > $maximum_unit_index // there is not enough seconds for even one maximum time unit.
) {
return $unit;
}
return $maximum_unit;
}
/**
* Returns a time difference value in specified units.
*
* @param int $seconds Number of seconds.
* @param string $units Unit.
* @return int $time_in_units.
*/
protected function get_interval_in_units( $seconds, $units ) {
switch ( $units ) {
case 'years':
$years = $seconds / YEAR_IN_SECONDS;
$decimals = abs( round( $years, 1 ) - round( $years ) ) > 0 ? 1 : 0;
return number_format_i18n( $years, $decimals );
case 'months':
return (int) ( $seconds / 60 / 60 / 24 / 30 );
case 'days':
return (int) ( $seconds / 60 / 60 / 24 + 1 );
case 'hours':
return (int) ( $seconds / 60 / 60 );
case 'minutes':
return (int) ( $seconds / 60 + 1 );
default:
return $seconds;
}
}
/**
* Update widget.
*
* @param array $new_instance New instance of the widget being saved.
* @param array $old_instance Previous instance being saved over.
*
* @return array
*/
public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
return $this->sanitize_instance( $new_instance );
}
/**
* Make sure that a number is within a certain range.
* If the number is too small it will become the possible lowest value.
* If the number is too large it will become the possible highest value.
*
* @param int $n The number to check.
* @param int $floor The lowest possible value.
* @param int $ceil The highest possible value.
*/
public function sanitize_range( $n, $floor, $ceil ) {
$n = (int) $n;
if ( $n < $floor ) {
$n = $floor;
} elseif ( $n > $ceil ) {
$n = $ceil;
}
return $n;
}
/**
* Sanitize an instance of this widget.
*
* Date ranges match the documentation for mktime in the php manual.
*
* @see https://php.net/manual/en/function.mktime.php#refsect1-function.mktime-parameters
*
* @uses Milestone_Widget::sanitize_range().
*
* @param array $dirty Unsantized data for the widget.
*
* @return array Santized data.
*/
public function sanitize_instance( $dirty ) {
$dirty = wp_parse_args(
$dirty,
$this->defaults()
);
$allowed_tags = array(
'a' => array(
'title' => array(),
'href' => array(),
'target' => array(),
),
'em' => array( 'title' => array() ),
'strong' => array( 'title' => array() ),
);
$clean = array(
'title' => trim( wp_strip_all_tags( stripslashes( $dirty['title'] ) ) ),
'event' => trim( wp_strip_all_tags( stripslashes( $dirty['event'] ) ) ),
'unit' => $dirty['unit'],
'type' => $dirty['type'],
'message' => wp_kses( $dirty['message'], $allowed_tags ),
'year' => $this->sanitize_range( $dirty['year'], 1901, 2037 ),
'month' => $this->sanitize_range( $dirty['month'], 1, 12 ),
'hour' => $this->sanitize_range( $dirty['hour'], 0, 23 ),
'min' => zeroise( $this->sanitize_range( $dirty['min'], 0, 59 ), 2 ),
);
$clean['day'] = $this->sanitize_range( $dirty['day'], 1, gmdate( 't', mktime( 0, 0, 0, $clean['month'], 1, $clean['year'] ) ) );
return $clean;
}
/**
* Form
*
* @param array $instance Widget instance.
*/
public function form( $instance ) {
$instance = $this->sanitize_instance( $instance );
$units = array(
'automatic' => _x( 'Automatic', 'Milestone widget: mode in which the date unit is determined automatically', 'jetpack' ),
'years' => _x( 'Years', 'Milestone widget: mode in which the date unit is set to years', 'jetpack' ),
'months' => _x( 'Months', 'Milestone widget: mode in which the date unit is set to months', 'jetpack' ),
'days' => _x( 'Days', 'Milestone widget: mode in which the date unit is set to days', 'jetpack' ),
'hours' => _x( 'Hours', 'Milestone widget: mode in which the date unit is set to hours', 'jetpack' ),
);
?>