'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 ); self::$dir = trailingslashit( dirname( __FILE__ ) ); self::$url = plugin_dir_url( __FILE__ ); self::$labels = array( 'year' => __( 'year', 'jetpack' ), 'years' => __( 'years', 'jetpack' ), 'month' => __( 'month', 'jetpack' ), 'months' => __( 'months', 'jetpack' ), 'day' => __( 'day', 'jetpack' ), 'days' => __( 'days', 'jetpack' ), 'hour' => __( 'hour', 'jetpack' ), 'hours' => __( 'hours', 'jetpack' ), 'minute' => __( 'minute', 'jetpack' ), 'minutes' => __( 'minutes', 'jetpack' ), 'second' => __( 'second', 'jetpack' ), 'seconds' => __( 'seconds', 'jetpack' ), ); 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' ) ); } } public static function enqueue_admin( $hook_suffix ) { if ( 'widgets.php' == $hook_suffix ) { wp_enqueue_style( 'milestone-admin', self::$url . 'style-admin.css', array(), '20161215' ); } } public static function enqueue_template() { wp_enqueue_script( 'milestone', self::$url . 'milestone.js', array( 'jquery' ), '20160520', true ); } public static function styles_template() { global $themecolors; $colors = wp_parse_args( $themecolors, array( 'bg' => 'ffffff', 'border' => 'cccccc', 'text' => '333333', ) ); ?> sanitize_instance( $instance ); $milestone = mktime( $instance['hour'], $instance['min'], 0, $instance['month'], $instance['day'], $instance['year'] ); $now = (int) current_time( 'timestamp' ); $diff = (int) floor( $milestone - $now ); $number = 0; $label = ''; if ( 63113852 < $diff ) { // more than 2 years - show in years, one decimal point $number = round( $diff / 60 / 60 / 24 / 365, 1 ); $label = self::$labels['years']; } else if ( 7775999 < $diff ) { // fewer than 2 years - show in months $number = floor( $diff / 60 / 60 / 24 / 30 ); $label = ( 1 == $number ) ? self::$labels['month'] : self::$labels['months']; } else if ( 86399 < $diff ) { // fewer than 3 months - show in days $number = floor( $diff / 60 / 60 / 24 ) + 1; $label = ( 1 == $number ) ? self::$labels['day'] : self::$labels['days']; } else if ( 3599 < $diff ) { // less than 1 day - show in hours $number = floor( $diff / 60 / 60 ); $label = ( 1 == $number ) ? self::$labels['hour'] : self::$labels['hours']; } else if ( 59 < $diff ) { // less than 1 hour - show in minutes $number = floor( $diff / 60 ) + 1; $label = ( 1 == $number ) ? self::$labels['minute'] : self::$labels['minutes']; } else { // less than 1 minute - show in seconds $number = $diff; $label = ( 1 == $number ) ? self::$labels['second'] : self::$labels['seconds'] ; } echo $args['before_widget']; $title = apply_filters( 'widget_title', $instance['title'] ); if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; } echo '
'; echo '
'; echo '' . esc_html( $instance['event'] ) . ''; echo '' . esc_html( date_i18n( __( 'F jS, Y', 'jetpack' ), $milestone ) ) . ''; echo '
'; if ( 1 > $diff ) { /* Milestone has past. */ echo '
' . $instance['message'] . '
'; } else { /* Countdown to the milestone. */ echo '
' . sprintf( __( '%1$s %2$s to go.', 'jetpack' ), '' . esc_html( $number ) . '', '' . esc_html( $label ) . '' ) . '
'; self::$config_js['instances'][] = array( 'id' => $args['widget_id'], 'diff' => $diff, 'message' => $instance['message'], ); } echo '
'; echo $args['after_widget']; /** This action is documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', 'milestone' ); } /** * Update */ function update( $new_instance, $old_instance ) { 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. */ 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 http://php.net/manual/en/function.mktime.php#refsect1-function.mktime-parameters * * @uses Milestone_Widget::sanitize_range(). */ function sanitize_instance( $dirty ) { $now = (int) current_time( 'timestamp' ); $dirty = wp_parse_args( $dirty, array( 'title' => '', 'event' => __( 'The Big Day', 'jetpack' ), 'message' => __( 'The big day is here.', 'jetpack' ), 'day' => date( 'd', $now ), 'month' => date( 'm', $now ), 'year' => date( 'Y', $now ), 'hour' => 0, 'min' => 0, ) ); $allowed_tags = array( 'a' => array( 'title' => array(), 'href' => array(), 'target' => array() ), 'em' => array( 'title' => array() ), 'strong' => array( 'title' => array() ), ); $clean = array( 'title' => trim( strip_tags( stripslashes( $dirty['title'] ) ) ), 'event' => trim( strip_tags( stripslashes( $dirty['event'] ) ) ), '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, date( 't', mktime( 0, 0, 0, $clean['month'], 1, $clean['year'] ) ) ); return $clean; } /** * Form */ function form( $instance ) { $instance = $this->sanitize_instance( $instance ); ?>

,
: