| 1 |
<?php |
| 2 |
/** |
| 3 |
* CoolClock Widget Class |
| 4 |
*/ |
| 5 |
|
| 6 |
class CoolClock_Widget extends WP_Widget { |
| 7 |
|
| 8 |
/** PHP5+ constructor */ |
| 9 |
public function __construct() { |
| 10 |
parent::__construct( |
| 11 |
'coolclock-widget', |
| 12 |
__('Analog Clock', 'coolclock'), |
| 13 |
array( |
| 14 |
'classname' => 'coolclock', |
| 15 |
'description' => __('Add an analog clock to your sidebar.', 'coolclock') |
| 16 |
), |
| 17 |
array( |
| 18 |
'width' => 300, |
| 19 |
'id_base' => 'coolclock-widget' |
| 20 |
) |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
/** @see WP_Widget::widget -- do not rename this */ |
| 25 |
public function widget( $args, $instance ) { |
| 26 |
extract( $args ); |
| 27 |
|
| 28 |
$defaults = array_merge( array('title'=>'','custom_skin'=>''), CoolClock::$defaults, CoolClock::$advanced_defaults ); |
| 29 |
|
| 30 |
// backward compat |
| 31 |
if ( isset($instance['digitalcolor']) && !isset($instance['fontcolor']) ) $instance['fontcolor'] = $instance['digitalcolor']; |
| 32 |
|
| 33 |
$title = !empty($instance['title']) ? apply_filters( 'widget_title', $instance['title'] ) : ''; |
| 34 |
//$number = $this->number; |
| 35 |
|
| 36 |
// set footer script flags |
| 37 |
CoolClock::$add_script = true; |
| 38 |
|
| 39 |
// Print output |
| 40 |
echo wp_kses_post( $before_widget ); |
| 41 |
|
| 42 |
if ( $title ) { |
| 43 |
echo wp_kses_post( $before_title ) . esc_html( $title ) . wp_kses_post( $after_title ); |
| 44 |
} |
| 45 |
|
| 46 |
// set skin |
| 47 |
$instance['skin'] = CoolClock::parse_skin( |
| 48 |
! empty( $instance['skin'] ) ? $instance['skin'] : $defaults['skin'], |
| 49 |
! empty( $instance['custom_skin'] ) ? $instance['custom_skin'] : '' |
| 50 |
); |
| 51 |
|
| 52 |
// radius, used in wrapper style and coolclock fields |
| 53 |
$instance['radius'] = !empty( $instance['radius'] ) && is_numeric( $instance['radius'] ) ? (int) $instance['radius'] : $defaults['radius']; |
| 54 |
if ( 10 > $instance['radius'] ) $instance['radius'] = 10; // absolute minimum size 20x20 |
| 55 |
|
| 56 |
$align = !empty( $instance['align'] ) ? $instance['align'] : $defaults['align']; |
| 57 |
$styles = array( |
| 58 |
'width' => 2 * $instance['radius'] . 'px', |
| 59 |
'height' => 'auto', |
| 60 |
); |
| 61 |
if ( in_array( $align, array('left','center') ) ) { |
| 62 |
$styles['margin-right'] = 'auto'; |
| 63 |
if ( 'left' == $align ) $styles['margin-left'] = '0'; |
| 64 |
} |
| 65 |
if ( in_array( $align, array('right','center') ) ) { |
| 66 |
$styles['margin-left'] = 'auto'; |
| 67 |
if ( 'right' == $align ) $styles['margin-right'] = '0'; |
| 68 |
} |
| 69 |
$styles = apply_filters( 'coolclock_container_styles', $styles, $instance, $defaults ); |
| 70 |
|
| 71 |
// Build output |
| 72 |
// begin wrapper |
| 73 |
$output = '<div class="coolclock-container"' . CoolClock::inline_style( $styles ) . '>'; |
| 74 |
// add canvas |
| 75 |
$output .= CoolClock::canvas( $instance ); |
| 76 |
// end wrapper |
| 77 |
$output .= '</div>'; |
| 78 |
|
| 79 |
// Print filtered output |
| 80 |
echo wp_kses_post( apply_filters( 'coolclock_widget', $output, $args, $instance ) ); |
| 81 |
|
| 82 |
echo wp_kses_post( $after_widget ); |
| 83 |
} |
| 84 |
|
| 85 |
/** @see WP_Widget::update -- do not rename this */ |
| 86 |
public function update( $new_instance, $old_instance ) { |
| 87 |
// parse custom skin code |
| 88 |
$skin_array = !empty($new_instance['custom_skin']) ? CoolClock::skin_array( wp_strip_all_tags( $new_instance['custom_skin'] ) ) : array(); |
| 89 |
|
| 90 |
$instance['title'] = !empty($new_instance['title']) ? wp_strip_all_tags( $new_instance['title'] ) : ''; |
| 91 |
$instance['skin'] = !empty($new_instance['skin']) ? wp_strip_all_tags( $new_instance['skin'] ) : ''; |
| 92 |
$instance['custom_skin'] = !empty($skin_array) ? wp_json_encode( $skin_array ) : ''; |
| 93 |
$instance['radius'] = ( empty($new_instance['radius']) || (int) $new_instance['radius'] < 5 ) ? 5 : (int) $new_instance['radius']; |
| 94 |
$instance['noseconds'] = !empty($new_instance['noseconds']) ? '1' : ''; |
| 95 |
$instance['gmtoffset'] = isset($new_instance['gmtoffset']) && $new_instance['gmtoffset'] !== '' ? (float) $new_instance['gmtoffset'] : ''; |
| 96 |
$instance['showdigital'] = !empty($new_instance['showdigital']) ? wp_strip_all_tags( $new_instance['showdigital'] ) : ''; |
| 97 |
$instance['fontcolor'] = !empty($new_instance['fontcolor']) ? CoolClock::colorval( $new_instance['fontcolor'] ) : ''; |
| 98 |
$instance['scale'] = !empty($new_instance['scale']) ? wp_strip_all_tags( $new_instance['scale'] ) : ''; |
| 99 |
$instance['align'] = !empty($new_instance['align']) ? wp_strip_all_tags( $new_instance['align'] ) : ''; |
| 100 |
$instance['subtext'] = wp_kses( $new_instance['subtext'], CoolClock::$allowed_tags ); |
| 101 |
|
| 102 |
return apply_filters( 'coolclock_widget_update_advanced', $instance, $new_instance ); |
| 103 |
} |
| 104 |
|
| 105 |
/** @see WP_Widget::form -- do not rename this */ |
| 106 |
public function form( $instance ) { |
| 107 |
$output = ''; |
| 108 |
$advanced = ''; |
| 109 |
|
| 110 |
// backward compat |
| 111 |
if ( isset($instance['digitalcolor']) && !isset($instance['fontcolor']) ) $instance['fontcolor'] = $instance['digitalcolor']; |
| 112 |
|
| 113 |
$defaults = array_merge( array('title'=>'','custom_skin'=>''), CoolClock::$defaults, CoolClock::$advanced_defaults ); |
| 114 |
|
| 115 |
$instance = wp_parse_args( (array) $instance, $defaults ); |
| 116 |
|
| 117 |
$title = esc_attr( $instance['title'] ); |
| 118 |
$subtext = esc_attr( $instance['subtext'] ); |
| 119 |
$custom_skin = esc_attr( $instance['custom_skin'] ); |
| 120 |
|
| 121 |
// Translatable skin names go here |
| 122 |
$skin_names = array ( |
| 123 |
'swissrail' => __('Swiss Rail','coolclock'), |
| 124 |
'chunkyswiss' => __('Chunky Swiss','coolclock'), |
| 125 |
'chunkyswissonblack' => __('Chunky Swiss White','coolclock'), |
| 126 |
'fancy' => __('Fancy','coolclock'), |
| 127 |
'machine' => __('Machine','coolclock'), |
| 128 |
'simonbaird_com' => __('SimonBaird.com','coolclock'), |
| 129 |
'classic' => __('Classic by Bonstio','coolclock'), |
| 130 |
'modern' => __('Modern by Bonstio','coolclock'), |
| 131 |
'simple' => __('Simple by Bonstio','coolclock'), |
| 132 |
'securephp' => __('SecurePHP','coolclock'), |
| 133 |
'tes2' => __('Tes2','coolclock'), |
| 134 |
'lev' => __('Lev','coolclock'), |
| 135 |
'sand' => __('Sand','coolclock'), |
| 136 |
'sun' => __('Sun','coolclock'), |
| 137 |
'tor' => __('Tor','coolclock'), |
| 138 |
'cold' => __('Cold','coolclock'), |
| 139 |
'babosa' => __('Babosa','coolclock'), |
| 140 |
'tumb' => __('Tumb','coolclock'), |
| 141 |
'stone' => __('Stone','coolclock'), |
| 142 |
'disc' => __('Disc','coolclock'), |
| 143 |
'watermelon' => __('Watermelon by Yoo Nhe','coolclock'), |
| 144 |
'mister' => __('Mister by Carl Lister','coolclock'), |
| 145 |
'minimal' => __('Minimal','coolclock') |
| 146 |
); |
| 147 |
|
| 148 |
// Translatable type names go here |
| 149 |
$type_names = array ( |
| 150 |
'linear' => __('Linear','coolclock'), |
| 151 |
'logclock' => __('Logarithmic','coolclock'), |
| 152 |
'logclockrev' => __('Logarithmic reversed','coolclock') |
| 153 |
); |
| 154 |
|
| 155 |
// Translatable show digital options go here |
| 156 |
$showdigital_names = array ( |
| 157 |
'' => __( 'No', 'coolclock' ), |
| 158 |
'digital12' => __('time (am/pm)','coolclock'), |
| 159 |
'digital24' => __('time (24h)','coolclock'), |
| 160 |
'date' => __('date','coolclock'), |
| 161 |
'digital12+' => __('time + seconds (am/pm)','coolclock'), |
| 162 |
'digital24+' => __('time + seconds (24h)','coolclock'), |
| 163 |
'text' => __('custom text','coolclock') |
| 164 |
); |
| 165 |
|
| 166 |
// Misc translations |
| 167 |
$stray = array( |
| 168 |
'extra_settings' => __('Extra settings for the CoolClock widget.', 'coolclock') |
| 169 |
); |
| 170 |
|
| 171 |
// Title |
| 172 |
$output .= '<style type="text/css">#available-widgets [class*=clock] .widget-title:before{content:"\f469"}</style> |
| 173 |
<p><label for="' . $this->get_field_id('title') . '">' . __( 'Title:', 'coolclock' ) . '</label> '; |
| 174 |
$output .= '<input class="widefat" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . $title . '" /></p>'; |
| 175 |
|
| 176 |
// Clock settings |
| 177 |
$output .= '<p><strong>' . __('Clock', 'coolclock') . '</strong></p>'; |
| 178 |
$output .= '<p><a href="https://premium.status301.com/coolclock-widget-settings/" target="_blank">' . __('CoolClock widget instructions »', 'coolclock') . '</a></p>'; |
| 179 |
|
| 180 |
$output .= '<p><label for="' . $this->get_field_id('skin') . '">' . __('Skin:', 'coolclock') . '</label> '; |
| 181 |
$output .= '<select class="select" id="' . $this->get_field_id('skin') . '" name="' . $this->get_field_name('skin') . '">'; |
| 182 |
$output .= '<option value="' . $defaults['skin'] . '" ' . selected( $defaults['skin'], $instance['skin'], false ) . '>'; |
| 183 |
$output .= ( isset($skin_names[$defaults['skin']]) ) ? $skin_names[$defaults['skin']] : $defaults['skin']; |
| 184 |
$output .= '</option>'; |
| 185 |
foreach ( CoolClock::get_all_skins() as $skin => $parms ) { |
| 186 |
$output .= '<option value="' . $skin . '" ' . selected( $skin, $instance['skin'], false ) . '>'; |
| 187 |
$output .= ( isset($skin_names[$skin]) ) ? $skin_names[$skin] : $skin; |
| 188 |
$output .= '</option>'; |
| 189 |
} |
| 190 |
$output .= '<option value="custom_' . $this->number . '" ' . selected( 'custom_'.$this->number, $instance['skin'], false ) . '>'; |
| 191 |
$output .= __('Custom (define below)', 'coolclock') . '</option></select></p>'; |
| 192 |
|
| 193 |
// Custom skin field |
| 194 |
$output .= '<p><label for="' . $this->get_field_id('custom_skin') . '">' . __('Custom skin parameters:', 'coolclock') . '</label> '; |
| 195 |
$output .= '<textarea class="widefat" id="' . $this->get_field_id('custom_skin') . '" name="' . $this->get_field_name('custom_skin') . '">' . $custom_skin . '</textarea> '; |
| 196 |
/* translators: %s: link to the JSON parameters documentation */ |
| 197 |
$output .= '<em>' . sprintf( __('(set Skin to Custom above, then add %s here)', 'coolclock'), '<a href="https://premium.status301.com/coolclock-custom-skin/" target="_blank">' . __('parameters in JSON format', 'coolclock') . '</a>' ) . '</em></p>'; |
| 198 |
|
| 199 |
// Radius |
| 200 |
$output .= '<p><label for="' . $this->get_field_id('radius') . '">' . __('Radius:', 'coolclock') . '</label> '; |
| 201 |
$output .= '<input class="small-text" id="' . $this->get_field_id('radius') . '" name="' . $this->get_field_name('radius') . '" type="number" min="10" value="' . $instance['radius'] . '" /></p>'; |
| 202 |
|
| 203 |
// Second hand |
| 204 |
$output .= '<p><input id="' . $this->get_field_id('noseconds') . '" name="' . $this->get_field_name('noseconds') . '" type="checkbox" value='; |
| 205 |
$output .= ( $instance['noseconds'] ) ? '"true" checked="checked" />' : '"false" />'; |
| 206 |
$output .= ' <label for="' . $this->get_field_id('noseconds') . '">' . __('Hide second hand', 'coolclock') . '</label></p>'; |
| 207 |
|
| 208 |
// Align |
| 209 |
$output .= '<p><label for="' . $this->get_field_id('align') . '">' . __('Align:', 'coolclock') . '</label> '; |
| 210 |
$output .= '<select class="select" id="' . $this->get_field_id('align') . '" name="' . $this->get_field_name('align') . '">'; |
| 211 |
$output .= '<option value="">'; |
| 212 |
$output .= __( 'none', 'coolclock' ) . '</option>'; |
| 213 |
$output .= '<option value="left" ' . selected( $instance['align'], 'left', false ) . '>'; |
| 214 |
$output .= __('left', 'coolclock') . '</option>'; |
| 215 |
$output .= '<option value="right" ' . selected( $instance['align'], 'right', false ) . '>'; |
| 216 |
$output .= __('right', 'coolclock') . '</option>'; |
| 217 |
$output .= '<option value="center" ' . selected( $instance['align'], 'center', false ) . '>'; |
| 218 |
$output .= __('center', 'coolclock') . '</option>'; |
| 219 |
$output .= '</select></p>'; |
| 220 |
|
| 221 |
// Subtext |
| 222 |
$output .= '<p><label for="' . $this->get_field_id('subtext') . '">' . __('Subtext:', 'coolclock') . '</label> '; |
| 223 |
$output .= '<input class="widefat" id="' . $this->get_field_id('subtext') . '" name="' . $this->get_field_name('subtext') . '" type="text" value="' . $subtext . '" /> <em>' . __('(basic HTML allowed)', 'coolclock') . '</em></p>'; |
| 224 |
|
| 225 |
$output .= '<div class="coolclock-advanced" style="background-color:rgba(0,0,0,.03);padding:1px 7px;border-radius:5px;margin-bottom:10px">'; |
| 226 |
|
| 227 |
$output .= '<p><strong>' . __( 'Advanced', 'coolclock' ) . '</strong></p>'; |
| 228 |
|
| 229 |
// Use GMT offset |
| 230 |
$output .= '<p><label for="' . $this->get_field_id('gmtoffset') . '">' . __('GMT offset:', 'coolclock') . '</label> '; |
| 231 |
$output .= '<input class="small-text" id="' . $this->get_field_id('gmtoffset') . '" name="' . $this->get_field_name('gmtoffset') . '" type="number" step="0.5" value="' . $instance['gmtoffset'] . '" /> <em>' . __('(leave blank for visitor local time)', 'coolclock') . '</em></p>'; |
| 232 |
|
| 233 |
// Scale |
| 234 |
$output .= '<p><label for="' . $this->get_field_id('scale') . '">' . __('Scale:', 'coolclock') . '</label> '; |
| 235 |
$output .= '<select class="select" id="' . $this->get_field_id('scale') . '" name="' . $this->get_field_name('scale') . '">'; |
| 236 |
foreach ( CoolClock::$clock_types as $key => $value ) { |
| 237 |
$output .= '<option value="' . $key . '" ' . selected( $key, strtolower($instance['scale']), false ) . '>'; |
| 238 |
$output .= ( isset($type_names[$key]) ) ? $type_names[$key] : $value; |
| 239 |
$output .= '</option>'; |
| 240 |
} |
| 241 |
$output .= '</select></p>'; |
| 242 |
|
| 243 |
// Show digital |
| 244 |
if ( $instance['showdigital'] == 'true' || $instance['showdigital'] == '1' ) |
| 245 |
$instance['showdigital'] = 'digital12'; // backward compat |
| 246 |
|
| 247 |
$output .= '<p><label for="' . $this->get_field_id('showdigital') . '">' . __('Show digital:', 'coolclock') . '</label> '; |
| 248 |
$output .= '<select class="select" id="' . $this->get_field_id('showdigital') . '" name="' . $this->get_field_name('showdigital') . '">'; |
| 249 |
foreach ( CoolClock::$showdigital_options as $key => $value ) { |
| 250 |
$output .= '<option value="' . $key . '" ' . selected( $key, $instance['showdigital'], false ) . '>'; |
| 251 |
$output .= ( isset($showdigital_names[$key]) ) ? $showdigital_names[$key] : $value; |
| 252 |
$output .= '</option>'; |
| 253 |
} |
| 254 |
$output .= '</select></p>'; |
| 255 |
|
| 256 |
$output .= '<p><label for="' . $this->get_field_id('fontcolor') . '">' . __('Digital font color:', 'coolclock') . '</label> '; |
| 257 |
$output .= '<input id="' . $this->get_field_id('fontcolor') . '" name="' . $this->get_field_name('fontcolor') . '" type="text" value="' . $instance['fontcolor'] . '" /> <em>' . __('(use a valid HTML color code or name)', 'coolclock') . '</em></p>'; |
| 258 |
|
| 259 |
$advanced .= '<p><a href="https://premium.status301.com/downloads/coolclock-advanced/">' . __('More digital font options »', 'coolclock') . '</a></p> |
| 260 |
<p><strong>' . __( 'Background', 'coolclock' ) . '</strong></p><p><a href="https://premium.status301.com/downloads/coolclock-advanced/">' . __('Available in the Advanced extension »', 'coolclock') . '</a></p>'; |
| 261 |
|
| 262 |
// Advanced filter |
| 263 |
$output .= apply_filters( 'coolclock_widget_form_advanced', $advanced, $this, $instance, $defaults ); |
| 264 |
|
| 265 |
$output .= '</div>'; |
| 266 |
|
| 267 |
if ( class_exists( 'CoolClockAdvanced' ) && isset(CoolClockAdvanced::$plugin_version) && version_compare( CoolClockAdvanced::$plugin_version, '7.1', '<' ) ) { // add an upgrade notice |
| 268 |
$output .= '<div class="update-nag"><strong>' . __('Please upgrade the CoolClock - Advanced extension.', 'coolclock') . '</strong> '. ' <a href="https://premium.status301.com/account/" target="_blank">' . __('Please log in with your account credentials here.', 'coolclock') . '</a>' . __('You can download the new version using the link in the downloads list.', 'coolclock') . '</div>'; |
| 269 |
} |
| 270 |
|
| 271 |
echo wp_kses_post( $output ); |
| 272 |
} |
| 273 |
} |
| 274 |
|