This repository was archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwpds-clock.php
159 lines (147 loc) · 5.07 KB
/
wpds-clock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
Plugin Name: WPDS Clock Widget
Plugin URI: http://pixelydo.com/work/wordpress-digital-signage/
Description: The clock for WPDS. Many thanks to <a href="https://twitter.com/Bluxart" target="_blank">@Bluxart</a> for <a href="http://www.alessioatzeni.com/blog/css3-digital-clock-with-jquery" target="_blank">the original clock</a>.
Author: Nate Jones
Version: 1.0
Author URI: http://pixelydo.com/
Text Domain: wpds-clock
*/
// Load text domain
function wpds_clock_load_plugin_textdomain() {
load_plugin_textdomain( 'wpds-clock', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'wpds_clock_load_plugin_textdomain' );
class wpds_clock_widget extends WP_Widget {
/**
* Constructor
*/
function __construct() {
parent::__construct(
'wpds-clock-widget',
__('The Clock', 'wpds-clock'),
array(
'description' => __( 'A simple digital clock for the dock.', 'wpds-clock' ),
)
);
}
/**
* Widget form creation
*/
function form($instance) {
// Check values
if( $instance) {
$locale = esc_attr($instance['locale']);
$timezone = esc_attr($instance['timezone']);
$animate = esc_attr($instance['animate']) == "true";
} else {
$locale = '';
$timezone = '';
$animate = false;
}
?>
<p>
<label for="<?php echo $this->get_field_id('locale'); ?>"><?php _e('Locale', 'wpds-clock'); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('locale'); ?>" name="<?php echo $this->get_field_name('locale'); ?>">
<option value=""><?=__('(Use client default)')?></option>
</select>
<script>
jQuery(function(){
moment.locales().forEach(function(entry) {
jQuery('#<?php echo $this->get_field_id('locale'); ?>').append(jQuery('<option>', {
value: entry,
text: entry,
selected: '<?=$locale?>' == entry
}));
});
});
</script>
</p>
<p>
<label for="<?php echo $this->get_field_id('timezone'); ?>"><?php _e('Timezone', 'wpds-clock'); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('timezone'); ?>" name="<?php echo $this->get_field_name('timezone'); ?>">
<option value=""><?=__('(Use client default)')?></option>
</select>
<script>
jQuery(function(){
moment.tz.names().forEach(function(entry) {
jQuery('#<?php echo $this->get_field_id('timezone'); ?>').append(jQuery('<option>', {
value: entry,
text: entry,
selected: '<?=$timezone?>' == entry
}));
});
});
</script>
</p>
<p>
<input type="checkbox" id="<?php echo $this->get_field_id('animate'); ?>" name="<?php echo $this->get_field_name('animate'); ?>" value="true" <?php echo $animate ? ' checked="checked"' : '';?>>
<label for="<?php echo $this->get_field_id('animate'); ?>"><?php _e('Animate', 'wpds-clock'); ?></label>
</p>
<?php
}
/**
* Update widget
*/
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance['locale'] = strip_tags($new_instance['locale']);
$instance['timezone'] = strip_tags($new_instance['timezone']);
$instance['animate'] = strip_tags($new_instance['animate']);
return $instance;
}
/**
* Display widget
*/
function widget($args, $instance) {
extract( $args );
// these are the widget options
$locale = $instance['locale'];
$timezone = $instance['timezone'];
$animate = $instance['animate'] == "true";
echo $before_widget;
// Display the widget
echo '<div class="clock"';
if (!empty($locale)) {
echo ' data-locale="' . $locale . '"';
}
if (!empty($timezone)) {
echo ' data-timezone="' . $timezone . '"';
}
echo '><div class="clock-time">',
'<span class="clock-hours"> </span>',
'<span class="clock-point' . ( $animate ? ' clock-point-animated' : '' ) . '">:</span>',
'<span class="clock-minutes"> </span>',
'</div><div class="clock-date"></div></div>';
echo $after_widget;
}
}
/**
* Register and load the widget
*/
function wpds_clock_load_widget() {
register_widget( 'wpds_clock_widget' );
}
add_action( 'widgets_init', 'wpds_clock_load_widget' );
/**
* Load styles
*/
function wpds_clock_load_styles()
{
wp_register_style( 'wpds_clock-style', plugins_url( '/clock.css', __FILE__ ) );
wp_enqueue_style( 'wpds_clock-style' );
wp_register_script( 'moment-js', plugins_url( '/moment-with-locales.min.js', __FILE__ ), array(), false, true );
wp_register_script( 'moment-timezone-js', plugins_url( '/moment-timezone-with-data.min.js', __FILE__ ), array('moment-js'), false, true );
wp_register_script( 'wpds-clock-script', plugins_url( '/clock.js', __FILE__ ), array('jquery', 'moment-timezone-js'), false, true );
wp_enqueue_script( 'wpds-clock-script' );
}
add_action( 'wp_enqueue_scripts', 'wpds_clock_load_styles' );
function wpds_clock_selectively_enqueue_admin_script( $hook ) {
if ( 'widgets.php' == $hook ) {
wp_enqueue_script( 'moment-js', plugin_dir_url( __FILE__ ) . 'moment-with-locales.min.js' );
wp_enqueue_script( 'moment-timezone-js', plugin_dir_url( __FILE__ ) . 'moment-timezone-with-data.min.js' );
}
}
add_action( 'admin_enqueue_scripts', 'wpds_clock_selectively_enqueue_admin_script' );