1 | <?php |
||
16 | class Milestone_Widget extends WP_Widget { |
||
17 | private static $dir = null; |
||
18 | private static $url = null; |
||
19 | private static $labels = null; |
||
20 | private static $defaults = null; |
||
|
|||
21 | private static $config_js = null; |
||
22 | |||
23 | function __construct() { |
||
24 | $widget = array( |
||
25 | 'classname' => 'milestone-widget', |
||
26 | 'description' => __( 'Display a countdown to a certain date.', 'jetpack' ), |
||
27 | ); |
||
28 | |||
29 | parent::__construct( |
||
30 | 'Milestone_Widget', |
||
31 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
32 | apply_filters( 'jetpack_widget_name', __( 'Milestone', 'jetpack' ) ), |
||
33 | $widget |
||
34 | ); |
||
35 | |||
36 | self::$dir = trailingslashit( dirname( __FILE__ ) ); |
||
37 | self::$url = plugin_dir_url( __FILE__ ); |
||
38 | self::$labels = array( |
||
39 | 'year' => __( 'year', 'jetpack' ), |
||
40 | 'years' => __( 'years', 'jetpack' ), |
||
41 | 'month' => __( 'month', 'jetpack' ), |
||
42 | 'months' => __( 'months', 'jetpack' ), |
||
43 | 'day' => __( 'day', 'jetpack' ), |
||
44 | 'days' => __( 'days', 'jetpack' ), |
||
45 | 'hour' => __( 'hour', 'jetpack' ), |
||
46 | 'hours' => __( 'hours', 'jetpack' ), |
||
47 | 'minute' => __( 'minute', 'jetpack' ), |
||
48 | 'minutes' => __( 'minutes', 'jetpack' ), |
||
49 | 'second' => __( 'second', 'jetpack' ), |
||
50 | 'seconds' => __( 'seconds', 'jetpack' ), |
||
51 | ); |
||
52 | |||
53 | add_action( 'wp_enqueue_scripts', array( __class__, 'enqueue_template' ) ); |
||
54 | add_action( 'admin_enqueue_scripts', array( __class__, 'enqueue_admin' ) ); |
||
55 | add_action( 'wp_footer', array( $this, 'localize_script' ) ); |
||
56 | |||
57 | if ( is_active_widget( false, false, $this->id_base, true ) || is_active_widget( false, false, 'monster', true ) || is_customize_preview() ) { |
||
58 | add_action( 'wp_head', array( __class__, 'styles_template' ) ); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | public static function enqueue_admin( $hook_suffix ) { |
||
63 | if ( 'widgets.php' == $hook_suffix ) { |
||
64 | wp_enqueue_style( 'milestone-admin', self::$url . 'style-admin.css', array(), '20161215' ); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | public static function enqueue_template() { |
||
69 | wp_enqueue_script( 'milestone', self::$url . 'milestone.js', array( 'jquery' ), '20160520', true ); |
||
70 | } |
||
71 | |||
72 | public static function styles_template() { |
||
73 | global $themecolors; |
||
74 | $colors = wp_parse_args( $themecolors, array( |
||
75 | 'bg' => 'ffffff', |
||
76 | 'border' => 'cccccc', |
||
77 | 'text' => '333333', |
||
78 | ) ); |
||
79 | ?> |
||
80 | <style> |
||
81 | .milestone-widget { |
||
82 | margin-bottom: 1em; |
||
83 | } |
||
84 | .milestone-content { |
||
85 | line-height: 2; |
||
86 | margin-top: 5px; |
||
87 | max-width: 100%; |
||
88 | padding: 0; |
||
89 | text-align: center; |
||
90 | } |
||
91 | .milestone-header { |
||
92 | background-color: <?php echo self::sanitize_color_hex( $colors['text'] ); ?>; |
||
93 | color: <?php echo self::sanitize_color_hex( $colors['bg'] ); ?>; |
||
94 | line-height: 1.3; |
||
95 | margin: 0; |
||
96 | padding: .8em; |
||
97 | } |
||
98 | .milestone-header .event, |
||
99 | .milestone-header .date { |
||
100 | display: block; |
||
101 | } |
||
102 | .milestone-header .event { |
||
103 | font-size: 120%; |
||
104 | } |
||
105 | .milestone-countdown .difference { |
||
106 | display: block; |
||
107 | font-size: 500%; |
||
108 | font-weight: bold; |
||
109 | line-height: 1.2; |
||
110 | } |
||
111 | .milestone-countdown, |
||
112 | .milestone-message { |
||
113 | background-color: <?php echo self::sanitize_color_hex( $colors['bg'] ); ?>; |
||
114 | border: 1px solid <?php echo self::sanitize_color_hex( $colors['border'] ); ?>; |
||
115 | border-top: 0; |
||
116 | color: <?php echo self::sanitize_color_hex( $colors['text'] ); ?>; |
||
117 | padding-bottom: 1em; |
||
118 | } |
||
119 | .milestone-message { |
||
120 | padding-top: 1em |
||
121 | } |
||
122 | </style> |
||
123 | <?php |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Ensure that a string representing a color in hexadecimal |
||
128 | * notation is safe for use in css and database saves. |
||
129 | * |
||
130 | * @param string Color in hexadecimal notation. "#" may or may not be prepended to the string. |
||
131 | * @return string Color in hexadecimal notation on success - the string "transparent" otherwise. |
||
132 | */ |
||
133 | public static function sanitize_color_hex( $hex, $prefix = '#' ) { |
||
134 | $hex = trim( $hex ); |
||
135 | |||
136 | /* Strip recognized prefixes. */ |
||
137 | if ( 0 === strpos( $hex, '#' ) ) { |
||
138 | $hex = substr( $hex, 1 ); |
||
139 | } elseif ( 0 === strpos( $hex, '%23' ) ) { |
||
140 | $hex = substr( $hex, 3 ); |
||
141 | } |
||
142 | |||
143 | if ( 0 !== preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) { |
||
144 | return $prefix . $hex; |
||
145 | } |
||
146 | |||
147 | return 'transparent'; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Localize Front-end Script. |
||
152 | * |
||
153 | * Print the javascript configuration array only if the |
||
154 | * current template has an instance of the widget that |
||
155 | * is still counting down. In all other cases, this |
||
156 | * function will dequeue milestone.js. |
||
157 | * |
||
158 | * Hooks into the "wp_footer" action. |
||
159 | */ |
||
160 | function localize_script() { |
||
161 | if ( empty( self::$config_js['instances'] ) ) { |
||
162 | wp_dequeue_script( 'milestone' ); |
||
163 | return; |
||
164 | } |
||
165 | self::$config_js['labels'] = self::$labels; |
||
166 | wp_localize_script( 'milestone', 'MilestoneConfig', self::$config_js ); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Widget |
||
171 | */ |
||
172 | function widget( $args, $instance ) { |
||
173 | $instance = $this->sanitize_instance( $instance ); |
||
174 | |||
175 | $milestone = mktime( $instance['hour'], $instance['min'], 0, $instance['month'], $instance['day'], $instance['year'] ); |
||
176 | $now = (int) current_time( 'timestamp' ); |
||
177 | $diff = (int) floor( $milestone - $now ); |
||
178 | |||
179 | $number = 0; |
||
180 | $label = ''; |
||
181 | |||
182 | if ( 63113852 < $diff ) { // more than 2 years - show in years, one decimal point |
||
183 | $number = round( $diff / 60 / 60 / 24 / 365, 1 ); |
||
184 | $label = self::$labels['years']; |
||
185 | } else if ( 7775999 < $diff ) { // fewer than 2 years - show in months |
||
186 | $number = floor( $diff / 60 / 60 / 24 / 30 ); |
||
187 | $label = ( 1 == $number ) ? self::$labels['month'] : self::$labels['months']; |
||
188 | } else if ( 86399 < $diff ) { // fewer than 3 months - show in days |
||
189 | $number = floor( $diff / 60 / 60 / 24 ) + 1; |
||
190 | $label = ( 1 == $number ) ? self::$labels['day'] : self::$labels['days']; |
||
191 | } else if ( 3599 < $diff ) { // less than 1 day - show in hours |
||
192 | $number = floor( $diff / 60 / 60 ); |
||
193 | $label = ( 1 == $number ) ? self::$labels['hour'] : self::$labels['hours']; |
||
194 | } else if ( 59 < $diff ) { // less than 1 hour - show in minutes |
||
195 | $number = floor( $diff / 60 ) + 1; |
||
196 | $label = ( 1 == $number ) ? self::$labels['minute'] : self::$labels['minutes']; |
||
197 | } else { // less than 1 minute - show in seconds |
||
198 | $number = $diff; |
||
199 | $label = ( 1 == $number ) ? self::$labels['second'] : self::$labels['seconds'] ; |
||
200 | } |
||
201 | |||
202 | echo $args['before_widget']; |
||
203 | |||
204 | $title = apply_filters( 'widget_title', $instance['title'] ); |
||
205 | if ( ! empty( $title ) ) { |
||
206 | echo $args['before_title'] . $title . $args['after_title']; |
||
207 | } |
||
208 | |||
209 | echo '<div class="milestone-content">'; |
||
210 | |||
211 | echo '<div class="milestone-header">'; |
||
212 | echo '<strong class="event">' . esc_html( $instance['event'] ) . '</strong>'; |
||
213 | echo '<span class="date">' . esc_html( date_i18n( __( 'F jS, Y', 'jetpack' ), $milestone ) ) . '</span>'; |
||
214 | echo '</div>'; |
||
215 | |||
216 | if ( 1 > $diff ) { |
||
217 | /* Milestone has past. */ |
||
218 | echo '<div class="milestone-message">' . $instance['message'] . '</div>'; |
||
219 | } else { |
||
220 | /* Countdown to the milestone. */ |
||
221 | echo '<div class="milestone-countdown">' . sprintf( __( '%1$s %2$s to go.', 'jetpack' ), |
||
222 | '<span class="difference">' . esc_html( $number ) . '</span>', |
||
223 | '<span class="label">' . esc_html( $label ) . '</span>' |
||
224 | ) . '</div>'; |
||
225 | |||
226 | self::$config_js['instances'][] = array( |
||
227 | 'id' => $args['widget_id'], |
||
228 | 'diff' => $diff, |
||
229 | 'message' => $instance['message'], |
||
230 | ); |
||
231 | } |
||
232 | |||
233 | echo '</div><!--milestone-content-->'; |
||
234 | |||
235 | echo $args['after_widget']; |
||
236 | |||
237 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
238 | do_action( 'jetpack_stats_extra', 'widget_view', 'milestone' ); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Update |
||
243 | */ |
||
244 | function update( $new_instance, $old_instance ) { |
||
247 | |||
248 | /* |
||
249 | * Make sure that a number is within a certain range. |
||
250 | * If the number is too small it will become the possible lowest value. |
||
251 | * If the number is too large it will become the possible highest value. |
||
252 | * |
||
253 | * @param int $n The number to check. |
||
254 | * @param int $floor The lowest possible value. |
||
255 | * @param int $ceil The highest possible value. |
||
256 | */ |
||
257 | function sanitize_range( $n, $floor, $ceil ) { |
||
266 | |||
267 | /* |
||
268 | * Sanitize an instance of this widget. |
||
269 | * |
||
270 | * Date ranges match the documentation for mktime in the php manual. |
||
271 | * @see http://php.net/manual/en/function.mktime.php#refsect1-function.mktime-parameters |
||
272 | * |
||
273 | * @uses Milestone_Widget::sanitize_range(). |
||
274 | */ |
||
275 | function sanitize_instance( $dirty ) { |
||
309 | |||
310 | /** |
||
311 | * Form |
||
312 | */ |
||
313 | function form( $instance ) { |
||
368 | } |
||
369 |
This check marks private properties in classes that are never used. Those properties can be removed.