PluginProbe
CoolClock / 2.0
CoolClock v2.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 2.0, at coolclock.php

372 lines 14.5 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: 2.0
9 Author: RavanH
10 Author URI: http://status301.net/
11 */
12
13 /**
14 * CoolClock Widget Class
15 */
16 class CoolClock_Widget extends WP_Widget {
17
18 /** constructor -- name this the same as the class above */
19 function CoolClock_Widget() {
20 parent::WP_Widget(
21 'coolclock-widget',
22 __('Analog Clock', 'coolclock'),
23 array(
24 'classname' => 'coolclock',
25 'description' => __('Add an analog clock to your sidebar.', 'coolclock')
26 ),
27 array(
28 'width' => 340,
29 /*'height' => 350, */
30 'id_base' => 'coolclock-widget'
31 )
32 );
33 }
34
35 /** @see WP_Widget::widget -- do not rename this */
36 function widget($args, $instance) {
37 extract( $args );
38 $title = apply_filters('widget_title', $instance['title']);
39 $skin = ( isset($instance['skin']) ) ? $instance['skin'] : 'swissRail';
40 $radius = ( isset($instance['radius']) ) ? $instance['radius'] : 85 ;
41 $clock_height = ( $radius ) ? 2*$radius.'px' : '170px';
42 $clock_width = ( $radius ) ? 2*$radius.'px' : '170px';
43
44 $background_height = ( isset($instance['background_height']) && $instance['background_height'] != '' ) ? $instance['background_height'].'px' : $clock_height;
45 $background_width = ( isset($instance['background_width']) && $instance['background_width'] != '' ) ? $instance['background_width'].'px' : '100%';
46 //$background_color = ( isset($instance['background_color']) ) ? $instance['background_color'] : 'transparent';
47 $vertical_position_dist = ( isset($instance['vertical_position_dist']) ) ? $instance['vertical_position_dist'].'px' : '0';
48 $horizontal_position_dist = ( isset($instance['horizontal_position_dist']) ) ? $instance['horizontal_position_dist'].'px' : '0';
49
50 // add custom skin parameters to the plugin skins array
51 if ( 'custom_'.$this->number == $skin )
52 CoolClock::$advanced_skins_config[$skin] = $instance['custom_skin'];
53
54 // set footer script flags
55 CoolClock::$add_script = true;
56 if ( in_array( $skin, CoolClock::$more_skins ) )
57 CoolClock::$add_moreskins = true;
58 if ( isset( CoolClock::$advanced_skins_config[$skin] ) )
59 CoolClock::$add_customskins = true;
60
61 ?>
62 <?php echo $before_widget; ?>
63 <?php if ( $title )
64 echo $before_title . $title . $after_title; ?>
65 <div style="<?php if ( isset($instance['background_image']) && $instance['background_image'] != '' ) { echo 'background-image:url(\''.$instance['background_image'].'\');'; ?><?php if ( isset($instance['background_position']) ) echo 'background-position:'.$instance['background_position'].';'; ?><?php if ( isset($instance['background_repeat']) && $instance['background_repeat'] == false ) echo 'background-repeat:no-repeat;'; } ?>height:<?php echo $background_height; ?>;width:<?php echo $background_width; ?>;position:relative<?php //if ( isset($instance['background_color']) ) echo ';background-color:'.$instance['background_color']; ?>">
66 <div style="position:absolute;<?php if ( isset($instance['vertical_position_from']) ) echo $instance['vertical_position_from'] . ':' . $vertical_position_dist . ';'; ?><?php if ( isset($instance['horizontal_position_from']) ) echo $instance['horizontal_position_from'] . ':' . $horizontal_position_dist . ';'; ?>height:<?php echo $clock_height; ?>;width:<?php echo $clock_width; ?>">
67 <?php
68
69 echo CoolClock::canvas( array( 'skin' => $skin,
70 'radius' => $radius,
71 'noseconds' => $instance['noseconds'],
72 'gmtoffset' => $instance['gmtoffset'],
73 'showdigital' => $instance['showdigital'],
74 'scale' => $instance['scale']
75 ) );
76
77 ?>
78 </div>
79 </div>
80
81 <?php echo $after_widget; ?>
82 <?php
83 }
84
85 /** @see WP_Widget::update -- do not rename this */
86 function update($new_instance, $old_instance) {
87
88 $instance = $old_instance;
89
90 return CoolClock::update($instance, $new_instance);
91 }
92
93 /** @see WP_Widget::form -- do not rename this */
94 function form($instance) {
95
96 CoolClock::form($this, $instance, $defaults);
97 }
98
99 } // end class example_widget
100
101 /**
102 * CoolClock Class
103 */
104 class CoolClock {
105 static $add_script;
106
107 static $add_moreskins;
108
109 static $add_customskins;
110
111 static $defaults = array (
112 'skin' => 'swissRail',
113 'radius' => 100,
114 'noseconds' => false, // Hide seconds
115 'gmtoffset' => '', // GMT offset
116 'showdigital' => false, // Show digital time
117 'scale' => 'linear' // Define type of clock linear/logarithmic/log reversed
118 );
119
120 static $advanced;
121
122 static $advanced_defaults = array ();
123
124 static $default_skins = array (
125 'swissRail',
126 'chunkySwiss',
127 'chunkySwissOnBlack'
128 );
129
130 static $more_skins = array (
131 'fancy',
132 'machine',
133 'simonbaird_com',
134 'classic',
135 'modern',
136 'simple',
137 'securephp',
138 'Tes2',
139 'Lev',
140 'Sand',
141 'Sun',
142 'Tor',
143 'Cold',
144 'Babosa',
145 'Tumb',
146 'Stone',
147 'Disc',
148 'watermelon'
149 );
150
151 static $advanced_skins = array ();
152
153 static $advanced_skins_config = array ();
154
155 static $clock_types = array (
156 'linear',
157 'logClock',
158 'logClockRev'
159 );
160
161 // FUNCTIONS //
162
163 static function update($instance, $new_instance) {
164
165 $instance['title'] = strip_tags($new_instance['title']);
166 $instance['skin'] = strip_tags($new_instance['skin']);
167 $instance['custom_skin'] = strip_tags($new_instance['custom_skin']);
168 $instance['radius'] = ( (int) $new_instance['radius'] < 5 ) ? 5 : (int) $new_instance['radius'];
169 $instance['noseconds'] = (bool) $new_instance['noseconds'];
170 $instance['gmtoffset'] = ( !$new_instance['gmtoffset'] ) ? '' : (float) $new_instance['gmtoffset'];
171 $instance['showdigital'] = (bool) $new_instance['showdigital'];
172 $instance['scale'] = strip_tags($new_instance['scale']);
173
174 if ( class_exists('CoolClockAdvanced') )
175 $instance = CoolClockAdvanced::update($instance, $new_instance);
176
177 return $instance;
178 }
179
180 static function form($obj, $instance, $defaults) {
181
182 $defaults = array (
183 'title' => '',
184 'custom_skin' => '',
185 );
186
187 $defaults = array_merge($defaults, self::$defaults, self::$advanced_defaults);
188
189 $instance = wp_parse_args( (array) $instance, $defaults );
190
191 $title = esc_attr( $instance['title'] );
192 $custom_skin = esc_attr( $instance['custom_skin'] );
193
194 // Translatable skin names go here
195 $skin_names = array (
196 'swissRail' => __('Swiss Rail','coolclock'),
197 'chunkySwiss' => __('Chunky Swiss','coolclock'),
198 'chunkySwissOnBlack' => __('Chunky Swiss Black','coolclock'),
199 'fancy' => __('Fancy','coolclock'),
200 'machine' => __('Machine','coolclock'),
201 'simonbaird_com' => __('SimonBaird.com','coolclock'),
202 'classic' => __('Classic by Bonstio','coolclock'),
203 'modern' => __('Modern by Bonstio','coolclock'),
204 'simple' => __('Simple by Bonstio','coolclock'),
205 'securephp' => __('SecurePHP','coolclock'),
206 'Tes2' => __('Tes2','coolclock'),
207 'Lev' => __('Lev','coolclock'),
208 'Sand' => __('Sand','coolclock'),
209 'Sun' => __('Sun','coolclock'),
210 'Tor' => __('Tor','coolclock'),
211 'Cold' => __('Cold','coolclock'),
212 'Babosa' => __('Babosa','coolclock'),
213 'Tumb' => __('Tumb','coolclock'),
214 'Stone' => __('Stone','coolclock'),
215 'Disc' => __('Disc','coolclock'),
216 'watermelon' => __('Watermelon by Yoo Nhe','coolclock'),
217 'minimal' => __('Minimal','coolclock')
218 );
219
220 $skins = array_merge(self::$default_skins,self::$more_skins,self::$advanced_skins);
221
222 // Translatable type names go here
223 $type_names = array (
224 'linear' => __('Linear','coolclock'),
225 'logClock' => __('Logarithmic','coolclock'),
226 'logClockRev' => __('Logarithmic reversed','coolclock')
227 );
228
229
230 ?>
231 <p>
232 <label for="<?php echo $obj->get_field_id('title'); ?>"><?php _e('Title:'); ?> </label>
233 <input class="widefat" id="<?php echo $obj->get_field_id('title'); ?>" name="<?php echo $obj->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
234 </p>
235
236 <p><strong><?php _e('Clock', 'coolclock'); ?></strong></p>
237
238 <p><label for="<?php echo $obj->get_field_id('skin'); ?>"><?php _e('Skin:', 'coolclock'); ?> </label>
239 <select class="select" id="<?php echo $obj->get_field_id('skin'); ?>" name="<?php echo $obj->get_field_name('skin'); ?>"><?php foreach ($skins as $value) { echo "<option value=\"$value\""; if ($value == $instance['skin']) echo ' selected="selected"'; echo '>'; if ( isset($skin_names[$value]) ) echo $skin_names[$value]; else echo $value; echo'</option>'; } unset($value); ?><option value="custom_<?php echo $obj->number ?>"<?php if ( 'custom_'.$obj->number == $instance['skin']) echo ' selected="selected"'; ?>><?php _e('Custom (define below)', 'coolclock') ?></option></select></p>
240
241 <p><label for="<?php echo $obj->get_field_id('custom_skin'); ?>"><?php _e('Custom skin parameters:', 'coolclock'); ?> </label>
242 <textarea class="widefat" id="<?php echo $obj->get_field_id('custom_skin'); ?>" name="<?php echo $obj->get_field_name('custom_skin'); ?>"><?php echo $custom_skin; ?></textarea></p>
243
244 <p><label for="<?php echo $obj->get_field_id('radius'); ?>"><?php _e('Radius:', 'coolclock'); ?>
245 <input class="small-text" id="<?php echo $obj->get_field_id('radius'); ?>" name="<?php echo $obj->get_field_name('radius'); ?>" type="number" min="10" value="<?php echo $instance['radius']; ?>" /> <?php _e('pixels', 'coolclock'); ?></label></p>
246
247 <p><input id="<?php echo $obj->get_field_id('noseconds'); ?>" name="<?php echo $obj->get_field_name('noseconds'); ?>" type="checkbox" value=<?php echo ( $instance['noseconds'] ) ? '"true" checked="checked"' : '"false"'; ?> />
248 <label for="<?php echo $obj->get_field_id('noseconds'); ?>"><?php _e('Hide second hand', 'coolclock'); ?></label></p>
249
250 <p><input id="<?php echo $obj->get_field_id('showdigital'); ?>" name="<?php echo $obj->get_field_name('showdigital'); ?>" type="checkbox" value=<?php echo ( $instance['showdigital'] ) ? '"true" checked="checked"' : '"false"'; ?> />
251 <label for="<?php echo $obj->get_field_id('showdigital'); ?>"><?php _e('Show digital time', 'coolclock'); ?></label></p>
252
253 <p><label for="<?php echo $obj->get_field_id('gmtoffset'); ?>"><?php _e('GMT offset:', 'coolclock'); ?></label>
254 <input class="small-text" id="<?php echo $obj->get_field_id('gmtoffset'); ?>" name="<?php echo $obj->get_field_name('gmtoffset'); ?>" type="number" step="0.5" value="<?php echo $instance['gmtoffset']; ?>" /> <?php _e('(leave blank for local time)', 'coolclock'); ?></p>
255
256 <p><label for="<?php echo $obj->get_field_id('scale'); ?>"><?php _e('Scale:', 'coolclock'); ?> </label>
257 <select class="select" id="<?php echo $obj->get_field_id('scale'); ?>" name="<?php echo $obj->get_field_name('scale'); ?>"><?php foreach (self::$clock_types as $value) { echo "<option value=\"$value\""; if ($value == $instance['scale']) echo ' selected="selected"'; echo '>'; if ( isset($type_names[$value]) ) echo $type_names[$value]; else echo $value; echo'</option>'; } unset($value); ?></select></p>
258
259 <?php
260
261 if ( class_exists('CoolClockAdvanced') ) {
262 CoolClockAdvanced::form($obj, $instance, $defaults);
263 } else {
264 ?>
265 <p><strong><?php _e('Background'); ?></strong></p>
266
267 <p><a href="http://status301.net/wordpress-plugins/coolclock-pro/"><?php _e('Available in the Pro extension &raquo;', 'coolclock'); ?></a></p>
268 <?php
269 }
270 }
271
272 static function init() {
273 load_plugin_textdomain('coolclock', false, dirname(plugin_basename( __FILE__ )).'/languages');
274 add_action('widgets_init', create_function('', 'return register_widget("CoolClock_Widget");'));
275
276 add_shortcode('coolclock', array(__CLASS__, 'handle_shortcode'));
277
278 add_action('init', array(__CLASS__, 'register_scripts'));
279 add_action('wp_footer', array(__CLASS__, 'print_scripts'));
280 }
281
282 static function handle_shortcode($atts) {
283
284 if ( isset($atts['align']) )
285 $align = $atts['align'];
286
287 $atts = shortcode_atts( self::$defaults, $atts );
288
289 // set footer script flags
290 self::$add_script = true;
291 if ( in_array( $atts['skin'], self::$more_skins ) )
292 self::$add_moreskins = true;
293 if ( isset( self::$advanced_skins[$atts['skin']] ) )
294 self::$add_customskins = true;
295
296 // return the clock unless it's a feed
297 if ( !is_feed() ) {
298 return self::canvas( $atts, $align );
299 } else {
300 return '';
301 }
302
303 }
304
305 static function canvas($atts, $align = false) {
306 $atts = shortcode_atts( self::$defaults, $atts );
307
308 extract( $atts );
309
310 $output = '<canvas class="CoolClock:'.$skin.':'.$radius.':';
311 // parameters
312 $output .= ( $noseconds == 'true' || $noseconds == '1' ) ? 'noSeconds:' : ':';
313 $output .= $gmtoffset.':';
314 $output .= ( $showdigital == 'true' || $showdigital == '1' ) ? 'showDigital' : '';
315
316 // set type
317 switch ($scale) {
318 case 'linear':
319 default:
320 break;
321 case 'logClock':
322 $output .= ':logClock';
323 break;
324 case 'logClockRev':
325 $output .= ':logClockRev';
326 }
327
328 // align class
329 $output .= ( $align ) ? ' align'.$align : '';
330 $output .= '"></canvas>';
331
332 return $output;
333 }
334
335 static function register_scripts() {
336 wp_register_script('coolclock', plugins_url('/js/coolclock.min.js', __FILE__), array('jquery'), '2.1.4', true);
337 wp_register_script('coolclock-moreskins', plugins_url('/js/moreskins.min.js', __FILE__), array('coolclock'), '2.1.4', true);
338
339 // could use http://cdnjs.cloudflare.com/ajax/libs/flot/0.7/excanvas.min.js here...
340 wp_register_script('excanvas', plugins_url('/js/excanvas.compiled.js', __FILE__), array(), '3', true);
341 }
342
343 static function print_scripts() {
344 if ( ! self::$add_script )
345 return;
346
347 wp_print_scripts('coolclock');
348
349 if ( self::$add_moreskins )
350 wp_print_scripts('coolclock-moreskins');
351
352 if ( self::$add_customskins ) {
353 echo '<script type="text/javascript">jQuery.extend(CoolClock.config.skins, {
354 ';
355 // loop through plugin custom skins
356 foreach (self::$advanced_skins_config as $key => $value)
357 echo $key.':{'.$value.'},
358 ';
359 echo '});</script>
360 ';
361 }
362
363 echo '<!--[if lt IE 9]>'; //<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/flot/0.7/excanvas.min.js"></script>
364 wp_print_scripts('excanvas');
365 echo '<![endif]-->
366 ';
367 }
368 }
369
370 CoolClock::init();
371
372