PluginProbe
CoolClock / 3.0
CoolClock v3.0
4.3.8 trunk 0.1 2.0 2.9.8 3.0 3.1 3.2 3.3 4.0 4.1 4.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7
coolclock / coolclock.php

coolclock.php in CoolClock 3.0, at coolclock.php

493 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: CoolClock
4 Plugin URI: http://status301.net/wordpress-plugins/coolclock/
5 Description: Add an analog clock to your sidebar.
6 Text Domain: coolclock
7 Domain Path: languages
8 Version: 3.0
9 Author: RavanH
10 Author URI: http://status301.net/
11 */
12
13 /**
14 * CoolClock Class
15 */
16 class CoolClock {
17
18 static $plugin_version = '3.0';
19
20 static $script_version = '3.0.0';
21
22 static $add_script;
23
24 static $add_moreskins;
25
26 static $done_excanvas = false;
27
28 static $defaults = array (
29 'skin' => 'swissRail',
30 'radius' => 100,
31 'noseconds' => false, // Hide seconds
32 'gmtoffset' => '', // GMT offset
33 'showdigital' => '', // Show digital time or date
34 'scale' => 'linear' // Define type of clock linear/logarithmic/log reversed
35 );
36
37 static $showdigital_options = array (
38 '' => '',
39 'digital12' => 'showDigital'
40 );
41
42 static $advanced;
43
44 static $advanced_defaults = array (
45 'subtext' => '',
46 'align' => 'center'
47 );
48
49 static $default_skins = array (
50 'swissRail',
51 'chunkySwiss',
52 'chunkySwissOnBlack'
53 );
54
55 static $more_skins = array (
56 'fancy',
57 'machine',
58 'simonbaird_com',
59 'classic',
60 'modern',
61 'simple',
62 'securephp',
63 'Tes2',
64 'Lev',
65 'Sand',
66 'Sun',
67 'Tor',
68 'Cold',
69 'Babosa',
70 'Tumb',
71 'Stone',
72 'Disc',
73 'watermelon',
74 'mister'
75 );
76
77 static $advanced_skins = array ();
78
79 static $advanced_skins_config = array ();
80
81 static $clock_types = array (
82 'linear' => '',
83 'logClock' => 'logClock',
84 'logClockRev' => 'logClockRev'
85 );
86
87 /**
88 * MAIN
89 */
90
91 static function canvas( $atts )
92 {
93 extract( $atts );
94
95 $output = '<div';
96
97 // align class ans style
98 $output .= ( $align ) ? ' class="align' . $align . '"' : '';
99 $output .= ' style="width:' . 2 * $radius . 'px;max-width:100%;height:auto">';
100 // canvas parameters
101 $output .= '<canvas class="CoolClock:' . $skin . ':' . $radius . ':';
102 $output .= ( $noseconds == 'true' || $noseconds == '1' ) ? 'noSeconds:' : ':';
103 $output .= $gmtoffset;
104
105 // show digital
106 if ( $showdigital == 'true' || $showdigital == '1' )
107 $showdigital = 'digital12'; // backward compat
108
109 if ( isset(self::$showdigital_options[$showdigital]) )
110 $output .= ':'.self::$showdigital_options[$showdigital];
111 else
112 $output .= ':';
113
114 // set type
115 if ( isset(self::$clock_types[$scale]) )
116 $output .= ':'.self::$clock_types[$scale];
117 else
118 $output .= ':';
119
120 $output .= '"></canvas>';
121 $output .= ( $subtext ) ? '<div style="width:100%;text-align:center;padding-bottom:10px">' . $subtext . '</div></div>' : '</div>';
122
123 // before returning, try including excanvas which needs to be there before the first canvas...
124 self::print_excanvas();
125
126 return $output;
127 }
128
129 static function print_excanvas()
130 {
131 if ( self::$done_excanvas )
132 return;
133
134 echo '<!--[if lte IE 8]>';
135 wp_print_scripts( 'excanvas' );
136 echo '<![endif]-->
137 ';
138 self::$done_excanvas = true;
139 }
140
141 static function print_scripts()
142 {
143 if ( ! self::$add_script )
144 return;
145
146 wp_print_scripts( 'coolclock' );
147
148 if ( self::$add_moreskins )
149 wp_print_scripts('coolclock-moreskins');
150
151 if ( is_array( self::$advanced_skins_config ) && !empty( self::$advanced_skins_config ) ) {
152 echo '<script type="text/javascript">jQuery.extend(CoolClock.config.skins, {
153 ';
154 // loop through plugin custom skins
155 foreach (self::$advanced_skins_config as $key => $value)
156 echo $key.':{'.$value.'},
157 ';
158 echo '});</script>
159 ';
160 }
161 }
162
163 /**
164 * SHORTCODE
165 */
166
167 static function handle_shortcode( $atts, $content = null )
168 {
169 if ( is_feed() )
170 return '';
171
172 $atts = shortcode_atts( array_merge( self::$defaults, self::$advanced_defaults ), $atts );
173
174 // set footer script flags
175 self::$add_script = true;
176
177 $skin = ( isset( $atts['skin'] ) )
178 ? $atts['skin'] : 'swissRail';
179
180 if ( in_array( $atts['skin'], self::$more_skins ) )
181 self::$add_moreskins = true;
182
183 $output = self::canvas( $atts );
184 return apply_filters( 'coolclock_shortcode_advanced', $output, $atts, $content );
185 }
186
187 static function no_wptexturize($shortcodes)
188 {
189 $shortcodes[] = 'coolclock';
190 return $shortcodes;
191 }
192
193 /**
194 * WIDGET
195 */
196
197 static function widget( $args, $instance, $number )
198 {
199 $skin = ( isset( $instance['skin'] ) )
200 ? $instance['skin'] : 'swissRail';
201
202 // add custom skin parameters to the plugin skins array
203 if ( 'custom_'.$number == $skin )
204 self::$advanced_skins_config[$skin] = wp_strip_all_tags( $instance['custom_skin'], true );
205
206 // set footer script flags
207 self::$add_script = true;
208
209 if ( in_array( $skin, self::$more_skins ) )
210 self::$add_moreskins = true;
211
212 $output = self::canvas( array(
213 'skin' => $skin,
214 'radius' => $instance['radius'],
215 'noseconds' => $instance['noseconds'],
216 'gmtoffset' => $instance['gmtoffset'],
217 'showdigital' => $instance['showdigital'],
218 'scale' => $instance['scale'],
219 'align' => $instance['align'],
220 'subtext' => apply_filters('widget_text', $instance['subtext'], $instance)
221 ) );
222
223 return apply_filters( 'coolclock_widget_advanced', $output, $args, $instance );
224 }
225
226 static function update( $new_instance, $instance )
227 {
228 $instance['title'] = strip_tags( $new_instance['title'] );
229 $instance['skin'] = strip_tags( $new_instance['skin'] );
230 $instance['custom_skin'] = strip_tags( $new_instance['custom_skin'] );
231 $instance['radius'] = ( (int) $new_instance['radius'] < 5 ) ? 5 : (int) $new_instance['radius'];
232 $instance['noseconds'] = (bool) $new_instance['noseconds'];
233 $instance['gmtoffset'] = ( $new_instance['gmtoffset'] == '' ) ? '' : (float) $new_instance['gmtoffset'];
234 $instance['showdigital'] = strip_tags( $new_instance['showdigital'] );
235 $instance['scale'] = strip_tags( $new_instance['scale'] );
236 $instance['align'] = strip_tags( $new_instance['align'] );
237
238 if ( current_user_can('unfiltered_html') )
239 $instance['subtext'] = $new_instance['subtext'];
240 else
241 // wp_filter_post_kses() expects slashed
242 $instance['subtext'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['subtext']) ) );
243
244
245 return apply_filters( 'coolclock_widget_update_advanced', $instance, $new_instance );
246 }
247
248 static function form( $obj, $instance, $defaults = array('title'=>'','custom_skin'=>'') )
249 {
250 $defaults = array_merge( $defaults, self::$defaults, self::$advanced_defaults );
251
252 $instance = wp_parse_args( (array) $instance, $defaults );
253
254 $title = esc_attr( $instance['title'] );
255 $subtext = esc_attr( $instance['subtext'] );
256 $custom_skin = esc_attr( $instance['custom_skin'] );
257
258 // Translatable skin names go here
259 $skin_names = array (
260 'swissRail' => __('Swiss Rail','coolclock'),
261 'chunkySwiss' => __('Chunky Swiss','coolclock'),
262 'chunkySwissOnBlack' => __('Chunky Swiss Black','coolclock'),
263 'fancy' => __('Fancy','coolclock'),
264 'machine' => __('Machine','coolclock'),
265 'simonbaird_com' => __('SimonBaird.com','coolclock'),
266 'classic' => __('Classic by Bonstio','coolclock'),
267 'modern' => __('Modern by Bonstio','coolclock'),
268 'simple' => __('Simple by Bonstio','coolclock'),
269 'securephp' => __('SecurePHP','coolclock'),
270 'Tes2' => __('Tes2','coolclock'),
271 'Lev' => __('Lev','coolclock'),
272 'Sand' => __('Sand','coolclock'),
273 'Sun' => __('Sun','coolclock'),
274 'Tor' => __('Tor','coolclock'),
275 'Cold' => __('Cold','coolclock'),
276 'Babosa' => __('Babosa','coolclock'),
277 'Tumb' => __('Tumb','coolclock'),
278 'Stone' => __('Stone','coolclock'),
279 'Disc' => __('Disc','coolclock'),
280 'watermelon' => __('Watermelon by Yoo Nhe','coolclock'),
281 'mister' => __('Mister by Carl Lister','coolclock'),
282 'minimal' => __('Minimal','coolclock')
283 );
284
285 $skins = array_merge( self::$default_skins, self::$more_skins, self::$advanced_skins );
286
287 // Translatable type names go here
288 $type_names = array (
289 'linear' => __('Linear','coolclock'),
290 'logClock' => __('Logarithmic','coolclock'),
291 'logClockRev' => __('Logarithmic reversed','coolclock')
292 );
293
294 // Translatable show options go here
295 $showdigital_names = array (
296 '' => __('none','coolclock'),
297 'digital12' => __('time (am/pm)','coolclock'),
298 'digital24' => __('time (24h)','coolclock'),
299 'date' => __('date','coolclock')
300 );
301
302 // Misc translations
303 $stray = array(
304 'extra_settings' => __('Extra settings for the CoolClock widget.', 'coolclock')
305 );
306
307 // Title
308 $output = '<p><label for="' . $obj->get_field_id('title') . '">' . __('Title:') . '</label> ';
309 $output .= '<input class="widefat" id="' . $obj->get_field_id('title') . '" name="' . $obj->get_field_name('title') . '" type="text" value="' . $title . '" /></p>';
310
311 // Clock settings
312 $output .= '<p><strong>' . __('Clock', 'coolclock') . '</strong></p>';
313 $output .= '<p><label for="' . $obj->get_field_id('skin') . '">' . __('Skin:', 'coolclock') . '</label> ';
314 $output .= '<select class="select" id="' . $obj->get_field_id('skin') . '" name="' . $obj->get_field_name('skin') . '">';
315 foreach ($skins as $value) {
316 $output .= '<option value="' . $value . '"';
317 $output .= ( $value == $instance['skin'] ) ? ' selected="selected">' : '>';
318 $output .= ( isset($skin_names[$value]) ) ? $skin_names[$value] : $value;
319 $output .= '</option>';
320 } unset($value);
321 $output .= '<option value="custom_' . $obj->number . '"';
322 $output .= ( 'custom_'.$obj->number == $instance['skin'] ) ? ' selected="selected">' : '>';
323 $output .= __('Custom (define below)', 'coolclock') . '</option></select></p>';
324
325 // Custom skin field
326 $output .= '<p><label for="' . $obj->get_field_id('custom_skin') . '">' . __('Custom skin parameters:', 'coolclock') . '</label> ';
327 $output .= '<textarea class="widefat" id="' . $obj->get_field_id('custom_skin') . '" name="' . $obj->get_field_name('custom_skin') . '">' . $custom_skin . '</textarea></p>';
328
329 // Radius
330 $output .= '<p><label for="' . $obj->get_field_id('radius') . '">' . __('Radius:', 'coolclock') . '</label> ';
331 $output .= '<input class="small-text" id="' . $obj->get_field_id('radius') . '" name="' . $obj->get_field_name('radius') . '" type="number" min="10" value="' . $instance['radius'] . '" /></p>';
332
333 // Second hand
334 $output .= '<p><input id="' . $obj->get_field_id('noseconds') . '" name="' . $obj->get_field_name('noseconds') . '" type="checkbox" value=';
335 $output .= ( $instance['noseconds'] ) ? '"true" checked="checked" />' : '"false" />';
336 $output .= ' <label for="' . $obj->get_field_id('noseconds') . '">' . __('Hide second hand', 'coolclock') . '</label></p>';
337
338 // Show digital
339 if ( $instance['showdigital'] == 'true' || $instance['showdigital'] == '1' )
340 $instance['showdigital'] = 'digital12'; // backward compat
341
342 $output .= '<p><label for="' . $obj->get_field_id('showdigital') . '">' . __('Show digital:', 'coolclock') . '</label> ';
343 $output .= '<select class="select" id="' . $obj->get_field_id('showdigital') . '" name="' . $obj->get_field_name('showdigital') . '">';
344 foreach (self::$showdigital_options as $key => $value) {
345 $output .= '<option value="' . $key . '"';
346 $output .= ( $key == $instance['showdigital'] ) ? ' selected="selected">' : '>';
347 $output .= ( isset($showdigital_names[$key]) ) ? $showdigital_names[$key] : $value;
348 $output .= '</option>';
349 } unset($value);
350 $output .= '</select></p>';
351
352 // USe GMT offset
353 $output .= '<p><label for="' . $obj->get_field_id('gmtoffset') . '">' . __('GMT offset:', 'coolclock') . '</label> ';
354 $output .= '<input class="small-text" id="' . $obj->get_field_id('gmtoffset') . '" name="' . $obj->get_field_name('gmtoffset') . '" type="number" step="0.5" value="' . $instance['gmtoffset'] . '" /> ' . __('(leave blank for local time)', 'coolclock') . '</p>';
355
356 // Scale
357 $output .= '<p><label for="' . $obj->get_field_id('scale') . '">' . __('Scale:', 'coolclock') . '</label> ';
358 $output .= '<select class="select" id="' . $obj->get_field_id('scale') . '" name="' . $obj->get_field_name('scale') . '">';
359 foreach (self::$clock_types as $key => $value) {
360 $output .= '<option value="' . $key . '"';
361 $output .= ( $key == $instance['scale'] ) ? ' selected="selected">' : '>';
362 $output .= ( isset($type_names[$key]) ) ? $type_names[$key] : $value;
363 $output .= '</option>';
364 } unset($value);
365 $output .= '</select></p>';
366
367 // Align
368 $output .= '<p><label for="' . $obj->get_field_id('align') . '">' . __('Align:', 'coolclock') . '</label> ';
369 $output .= '<select class="select" id="' . $obj->get_field_id('align') . '" name="' . $obj->get_field_name('align') . '">';
370 $output .= '<option value=""';
371 $output .= ( $instance['align'] == '' ) ? ' selected="selected">' : '>';
372 $output .= __('none', 'coolclock') . '</option>';
373 $output .= '<option value="left"';
374 $output .= ( $instance['align'] == 'left' ) ? ' selected="selected">' : '>';
375 $output .= __('left', 'coolclock') . '</option>';
376 $output .= '<option value="right"';
377 $output .= ( $instance['align'] == 'right' ) ? ' selected="selected">' : '>';
378 $output .= __('right', 'coolclock') . '</option>';
379 $output .= '<option value="center"';
380 $output .= ( $instance['align'] == 'center' ) ? ' selected="selected">' : '>';
381 $output .= __('center', 'coolclock') . '</option>';
382 $output .= '</select></p>';
383
384 // Subtext
385 $output .= '<p><label for="' . $obj->get_field_id('subtext') . '">' . __('Subtext:', 'coolclock') . '</label> ';
386 $output .= '<input class="widefat" id="' . $obj->get_field_id('subtext') . '" name="' . $obj->get_field_name('subtext') . '" type="text" value="' . $subtext . '" /> ' . __('(basic HTML allowed)', 'coolclock') . '</p>';
387
388 // Advanced filter
389 if ( class_exists( 'CoolClockAdvanced' ) ) // add an upgrade notice
390 $advanced_form = '<p><strong>' . __('Background') . '</strong></p><p><strong>' . __('Please upgrade the CoolClock - Advanced extension.', 'coolclock') . '</strong> ' . __('You can download the new version using the remaining download credits and the link that you have received in the confirmation email after your first purchase.', 'coolclock') . ' <a href="http://status301.net/contact-en/">' . __('If you do not have that email anymore, please contact us.', 'coolclock') . '</a></p>' . '<p><strong>' . __('Do NOT resave widget settings before upgrading the Advanced extension or your advanced settings will be lost!', 'coolclock') . '</strong>';
391 else
392 $advanced_form = '<p><strong>' . __('Background') . '</strong></p><p><a href="http://status301.net/wordpress-plugins/coolclock-advanced/">' . __('Available in the Advanced extension &raquo;', 'coolclock') . '</a></p>';
393
394 $output .= apply_filters( 'coolclock_widget_form_advanced', $advanced_form, $obj, $instance, $defaults );
395
396 return $output;
397 }
398
399 /**
400 * INIT
401 */
402
403 static function go()
404 {
405 add_action('plugins_loaded', create_function( '', "return load_plugin_textdomain( 'coolclock', false, dirname(plugin_basename( __FILE__ )).'/languages' );" ) );
406 add_action( 'init', array(__CLASS__, 'init' ) );
407 add_action( 'widgets_init', create_function( '', 'return register_widget("CoolClock_Widget");' ) );
408 }
409
410 static function init()
411 {
412 add_shortcode( 'coolclock', array( __CLASS__, 'handle_shortcode' ) );
413
414 // allow shortcode in text widgets
415 add_filter('widget_text', 'do_shortcode', 11);
416
417 // prevent texturizing shortcode content
418 add_filter( 'no_texturize_shortcodes', array( __CLASS__, 'no_wptexturize') );
419
420 if ( defined('WP_DEBUG') && false != WP_DEBUG ) {
421 wp_register_script( 'coolclock', plugins_url('/js/coolclock.js', __FILE__), array('jquery'), self::$script_version, true );
422 wp_register_script( 'coolclock-moreskins', plugins_url('/js/moreskins.js', __FILE__), array('coolclock'), self::$script_version, true );
423 wp_register_script( 'excanvas', plugins_url( '/js/excanvas.js', __FILE__ ), array(), '73', true );
424 } else {
425 wp_register_script( 'coolclock', plugins_url('/js/coolclock.min.js', __FILE__), array('jquery'), self::$script_version, true );
426 wp_register_script( 'coolclock-moreskins', plugins_url('/js/moreskins.min.js', __FILE__), array('coolclock'), self::$script_version, true );
427 wp_register_script( 'excanvas', plugins_url( '/js/excanvas.min.js', __FILE__ ), array(), '73', true );
428 }
429
430 add_action( 'wp_footer', array( __CLASS__, 'print_scripts' ) );
431 }
432
433 }
434
435 CoolClock::go();
436
437 /**
438 * CoolClock Widget Class
439 */
440 class CoolClock_Widget extends WP_Widget {
441
442 /** constructor -- name this the same as the class above */
443 function CoolClock_Widget() {
444 parent::WP_Widget(
445 'coolclock-widget',
446 __('Analog Clock', 'coolclock'),
447 array(
448 'classname' => 'coolclock',
449 'description' => __('Add an analog clock to your sidebar.', 'coolclock')
450 ),
451 array(
452 'width' => 300,
453 'id_base' => 'coolclock-widget'
454 )
455 );
456 }
457
458 /** @see WP_Widget::widget -- do not rename this */
459 function widget( $args, $instance ) {
460
461 extract( $args );
462 $title = apply_filters( 'widget_title', $instance['title'] );
463 $number = $this->number;
464
465 // Print output
466 echo $before_widget;
467
468 if ( $title )
469 echo $before_title . $title . $after_title;
470
471 echo CoolClock::widget( $args, $instance, $number );
472
473 echo $after_widget;
474
475 }
476
477 /** @see WP_Widget::update -- do not rename this */
478 function update( $new_instance, $old_instance ) {
479
480 return CoolClock::update( $new_instance, $old_instance );
481
482 }
483
484 /** @see WP_Widget::form -- do not rename this */
485 function form( $instance ) {
486
487 // Print output
488 echo CoolClock::form( $this, $instance );
489
490 }
491
492 }
493