| 1 |
<?php |
| 2 |
/* Copyright 2014-2026 Baptiste Placé |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License, version 2, as |
| 6 |
published by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program; if not, write to the Free Software |
| 15 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 |
*/ |
| 17 |
/** |
| 18 |
* Plugin Name: iCalendrier |
| 19 |
* Plugin URI: https://icalendrier.fr/widget/wordpress-plugin |
| 20 |
* Description: Un simple calendrier qui affiche des infos du jour, comme le numéro de semaine, la date, la fête du jour et la phase de lune. |
| 21 |
* Version: 2.0 |
| 22 |
* Author: Baptiste Placé |
| 23 |
* Author URI: https://icalendrier.fr/ |
| 24 |
* License: GNU General Public License, version 2 |
| 25 |
* Requires PHP: 8.2 |
| 26 |
* Text Domain: icalendrier |
| 27 |
* Domain Path: /languages |
| 28 |
*/ |
| 29 |
|
| 30 |
defined('ABSPATH') or die("No script kiddies please!"); |
| 31 |
|
| 32 |
/** Keep in sync with the "Version" header above and "Stable tag" in readme.txt (make release checks it). */ |
| 33 |
const ICALENDRIER_VERSION = '2.0'; |
| 34 |
|
| 35 |
// Include lib |
| 36 |
require_once(dirname(__FILE__) . '/lib/Icalendrier.php'); |
| 37 |
|
| 38 |
// Admin form labels (MOD_ICAL_*). Registered on init, as WordPress 6.7+ requires. |
| 39 |
add_action('init', function () { |
| 40 |
load_plugin_textdomain('icalendrier', false, basename(dirname(__FILE__)) . '/languages'); |
| 41 |
}); |
| 42 |
|
| 43 |
// Enqueue CSS |
| 44 |
function icalendrier_wp_head() |
| 45 |
{ |
| 46 |
wp_register_style('icalendrier-default', plugins_url('/css/icalendrier.css', __FILE__), array(), ICALENDRIER_VERSION, 'all'); |
| 47 |
wp_register_style('icalendrier-alt-1', plugins_url('/css/themes/icalendrier-alt-1.css', __FILE__), array(), ICALENDRIER_VERSION, 'all'); |
| 48 |
wp_enqueue_style('icalendrier-default'); |
| 49 |
wp_enqueue_style('icalendrier-alt-1'); |
| 50 |
// The modern layout's stylesheet is only enqueued when that layout is rendered (see icalendrier_render()). |
| 51 |
wp_register_style('icalendrier-modern', plugins_url('/css/icalendrier-modern.css', __FILE__), array(), ICALENDRIER_VERSION, 'all'); |
| 52 |
} |
| 53 |
|
| 54 |
function icalendrier_wp_admin_head() |
| 55 |
{ |
| 56 |
wp_enqueue_style('icalendrier', plugins_url('/css/icalendrier-admin.css', __FILE__), array(), ICALENDRIER_VERSION); |
| 57 |
} |
| 58 |
|
| 59 |
add_action('wp_enqueue_scripts', 'icalendrier_wp_head'); |
| 60 |
|
| 61 |
add_action('admin_enqueue_scripts', 'icalendrier_wp_admin_head'); |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Parse shortcode or widget attributes and apply defaults. |
| 66 |
* |
| 67 |
* Single source of truth for both entry points. Keys are matched case-insensitively because |
| 68 |
* WordPress lowercases shortcode attribute names while the widget stores them in camelCase. |
| 69 |
* |
| 70 |
* @param array|string $atts Shortcode attributes, or a widget instance. WordPress passes '' for a bare shortcode. |
| 71 |
* |
| 72 |
* @return array{calType: string, language: string, timezone: string|int, showLink: int, showMoon: int, showNameDay: int, bgColor: string|false, style: string, widgetTitle: string} |
| 73 |
*/ |
| 74 |
function icalendrier_get_attributes($atts) |
| 75 |
{ |
| 76 |
$atts = is_array($atts) ? array_change_key_case($atts, CASE_LOWER) : []; |
| 77 |
|
| 78 |
$type = $atts['type'] ?? 'comp175'; |
| 79 |
if ( ! in_array($type, ['comp175', 'wide300', 'modern'], true)) { |
| 80 |
$type = 'comp175'; |
| 81 |
} |
| 82 |
|
| 83 |
return [ |
| 84 |
'calType' => $type, |
| 85 |
'language' => $atts['language'] ?? 'en', |
| 86 |
'timezone' => $atts['timezone'] ?? 0, |
| 87 |
'showLink' => array_key_exists('showlink', $atts) ? (int)icalendrier_is_on($atts['showlink']) : 1, |
| 88 |
'showMoon' => array_key_exists('moonphases', $atts) ? (int)icalendrier_is_on($atts['moonphases']) : 1, |
| 89 |
'showNameDay' => array_key_exists('namedays', $atts) ? (int)icalendrier_is_on($atts['namedays']) : 1, |
| 90 |
'bgColor' => $atts['bgcolor'] ?? false, |
| 91 |
'style' => $atts['style'] ?? 'default', |
| 92 |
'widgetTitle' => $atts['widgettitle'] ?? '', |
| 93 |
]; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Legacy name (with its typo) kept for third parties that may call it. |
| 98 |
* |
| 99 |
* @deprecated 1.9 Use icalendrier_get_attributes(). |
| 100 |
*/ |
| 101 |
function getAttibutes($atts) |
| 102 |
{ |
| 103 |
return icalendrier_get_attributes($atts); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Reads a checkbox or shortcode flag: "on", "1", "true", "yes" and true mean on; everything else off. |
| 108 |
* |
| 109 |
* @param mixed $value |
| 110 |
*/ |
| 111 |
function icalendrier_is_on($value): bool |
| 112 |
{ |
| 113 |
if (is_bool($value)) { |
| 114 |
return $value; |
| 115 |
} |
| 116 |
|
| 117 |
return in_array(strtolower(trim((string)$value)), ['on', '1', 'true', 'yes'], true); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Render the calendar for parsed attributes (see icalendrier_get_attributes()). |
| 122 |
*/ |
| 123 |
function icalendrier_render(array $attributes): string |
| 124 |
{ |
| 125 |
$iCalendrier = new Icalendrier($attributes['language'], $attributes['timezone']); |
| 126 |
|
| 127 |
if ('modern' === $attributes['calType']) { |
| 128 |
wp_enqueue_style('icalendrier-modern', plugins_url('/css/icalendrier-modern.css', __FILE__), array(), ICALENDRIER_VERSION, 'all'); |
| 129 |
|
| 130 |
return $iCalendrier->iCalendrierModern($attributes); |
| 131 |
} |
| 132 |
|
| 133 |
if ('wide300' === $attributes['calType']) { |
| 134 |
return $iCalendrier->iCalendrierWide($attributes); |
| 135 |
} |
| 136 |
|
| 137 |
return $iCalendrier->iCalendrierComp($attributes); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
// Shortcode |
| 142 |
add_shortcode('icalendrier', 'icalendrier_shortcode'); |
| 143 |
|
| 144 |
function icalendrier_shortcode($atts) |
| 145 |
{ |
| 146 |
return icalendrier_render(icalendrier_get_attributes($atts)); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Class ICalendrier |
| 152 |
*/ |
| 153 |
class ICalendrierWidget extends WP_Widget |
| 154 |
{ |
| 155 |
|
| 156 |
/** |
| 157 |
* ICalendrierWidget constructor. |
| 158 |
*/ |
| 159 |
public function __construct() |
| 160 |
{ |
| 161 |
parent::__construct('icalendrier_widget', 'iCalendrier Widget', [ |
| 162 |
'classname' => 'calendar', |
| 163 |
'description' => 'Simple daily calendar' |
| 164 |
]); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* @param $args |
| 169 |
* @param $instance |
| 170 |
*/ |
| 171 |
public function widget($args, $instance) |
| 172 |
{ |
| 173 |
$attributes = icalendrier_get_attributes($instance); |
| 174 |
$title = apply_filters('widget_title', $attributes['widgetTitle'], $instance, $this->id_base); |
| 175 |
|
| 176 |
echo $args['before_widget'] ?? ''; |
| 177 |
|
| 178 |
if ('' !== $title) { |
| 179 |
echo ($args['before_title'] ?? '') . $title . ($args['after_title'] ?? ''); |
| 180 |
} |
| 181 |
|
| 182 |
echo icalendrier_render($attributes); |
| 183 |
|
| 184 |
echo $args['after_widget'] ?? ''; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @param $new_instance |
| 189 |
* @param $old_instance |
| 190 |
* |
| 191 |
* @return mixed |
| 192 |
*/ |
| 193 |
public function update($new_instance, $old_instance) |
| 194 |
{ |
| 195 |
$instance = $old_instance; |
| 196 |
foreach (['type', 'language', 'timezone', 'widgetTitle', 'bgColor', 'style'] as $key) { |
| 197 |
$instance[$key] = wp_strip_all_tags((string)($new_instance[$key] ?? '')); |
| 198 |
} |
| 199 |
// An unchecked checkbox is absent from the submitted form: that means off. |
| 200 |
$instance['showLink'] = (int)icalendrier_is_on($new_instance['showLink'] ?? ''); |
| 201 |
$instance['moonphases'] = (int)icalendrier_is_on($new_instance['moonphases'] ?? ''); |
| 202 |
$instance['namedays'] = (int)icalendrier_is_on($new_instance['namedays'] ?? ''); |
| 203 |
|
| 204 |
return $instance; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* @param $instance |
| 209 |
*/ |
| 210 |
public function form($instance) |
| 211 |
{ |
| 212 |
$type = isset($instance['type']) ? esc_attr($instance['type']) : "modern"; // new widgets get the modern layout |
| 213 |
$language = isset($instance['language']) ? esc_attr($instance['language']) : "en"; |
| 214 |
$timezone = isset($instance['timezone']) ? esc_attr((string)$instance['timezone']) : '0'; |
| 215 |
$widgetTitle = isset($instance['widgetTitle']) ? esc_attr($instance['widgetTitle']) : ""; |
| 216 |
$showLink = isset($instance['showLink']) ? icalendrier_is_on($instance['showLink']) : true; |
| 217 |
$showMoon = isset($instance['moonphases']) ? icalendrier_is_on($instance['moonphases']) : true; |
| 218 |
$showNameDay = isset($instance['namedays']) ? icalendrier_is_on($instance['namedays']) : true; |
| 219 |
$bgColor = isset($instance['bgColor']) ? esc_attr($instance['bgColor']) : ""; |
| 220 |
$style = isset($instance['style']) ? esc_attr($instance['style']) : "default"; |
| 221 |
?> |
| 222 |
|
| 223 |
<p> |
| 224 |
<label for="<?php echo esc_attr($this->get_field_id('type')); ?>"><?php esc_html_e('MOD_ICAL_CALTYPE_LABEL', 'icalendrier'); ?></label> |
| 225 |
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('type')); ?>" name="<?php echo esc_attr($this->get_field_name('type')); ?>"> |
| 226 |
<option value="modern"<?php if ($type == 'modern') { |
| 227 |
echo " selected=\"selected\""; |
| 228 |
} ?>><?php esc_html_e('MOD_ICAL_CALTYPE_MODERN', 'icalendrier'); ?></option> |
| 229 |
<option value="comp175"<?php if ($type == 'comp175') { |
| 230 |
echo " selected=\"selected\""; |
| 231 |
} ?>><?php esc_html_e('MOD_ICAL_CALTYPE_COMP175', 'icalendrier'); ?></option> |
| 232 |
<option value="wide300"<?php if ($type == 'wide300') { |
| 233 |
echo " selected=\"selected\""; |
| 234 |
} ?>><?php esc_html_e('MOD_ICAL_CALTYPE_COMP300', 'icalendrier'); ?></option> |
| 235 |
</select> |
| 236 |
</p> |
| 237 |
|
| 238 |
<p> |
| 239 |
<label for="<?php echo esc_attr($this->get_field_id('style')); ?>"><?php esc_html_e('MOD_ICAL_STYLE_LABEL', 'icalendrier'); ?></label> |
| 240 |
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('style')); ?>" name="<?php echo esc_attr($this->get_field_name('style')); ?>"> |
| 241 |
<option value="default"<?php if ($style == 'default') { |
| 242 |
echo " selected=\"selected\""; |
| 243 |
} ?>><?php esc_html_e('MOD_ICAL_STYLE_DEFAULT', 'icalendrier'); ?></option> |
| 244 |
<option value="alt-1"<?php if ($style == 'alt-1') { |
| 245 |
echo " selected=\"selected\""; |
| 246 |
} ?>><?php esc_html_e('MOD_ICAL_STYLE_ALT_1', 'icalendrier'); ?></option> |
| 247 |
</select> |
| 248 |
</p> |
| 249 |
|
| 250 |
<p> |
| 251 |
<label for="<?php echo esc_attr($this->get_field_id('language')); ?>"><?php esc_html_e('MOD_ICAL_LANGUAGE_LABEL', 'icalendrier'); ?></label> |
| 252 |
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('language')); ?>" name="<?php echo esc_attr($this->get_field_name('language')); ?>"> |
| 253 |
<option value="da"<?php if ($language == 'da') { |
| 254 |
echo " selected=\"selected\""; |
| 255 |
} ?>>Dansk |
| 256 |
</option> |
| 257 |
<option value="de"<?php if ($language == 'de') { |
| 258 |
echo " selected=\"selected\""; |
| 259 |
} ?>>Deutsch |
| 260 |
</option> |
| 261 |
<option value="en"<?php if ($language == 'en') { |
| 262 |
echo " selected=\"selected\""; |
| 263 |
} ?>>English (US) |
| 264 |
</option> |
| 265 |
<option value="en-GB"<?php if ($language == 'en-GB') { |
| 266 |
echo " selected=\"selected\""; |
| 267 |
} ?>>English (UK) |
| 268 |
</option> |
| 269 |
<option value="es"<?php if ($language == 'es') { |
| 270 |
echo " selected=\"selected\""; |
| 271 |
} ?>>Español |
| 272 |
</option> |
| 273 |
<option value="fr"<?php if ($language == 'fr') { |
| 274 |
echo " selected=\"selected\""; |
| 275 |
} ?>>Français |
| 276 |
</option> |
| 277 |
<option value="it"<?php if ($language == 'it') { |
| 278 |
echo " selected=\"selected\""; |
| 279 |
} ?>>Italiano |
| 280 |
</option> |
| 281 |
<option value="ro"<?php if ($language == 'ro') { |
| 282 |
echo " selected=\"selected\""; |
| 283 |
} ?>>Română |
| 284 |
</option> |
| 285 |
<option value="pt"<?php if ($language == 'pt') { |
| 286 |
echo " selected=\"selected\""; |
| 287 |
} ?>>Português |
| 288 |
</option> |
| 289 |
<option value="pt-BR"<?php if ($language == 'pt-BR') { |
| 290 |
echo " selected=\"selected\""; |
| 291 |
} ?>>Português (Brasil) |
| 292 |
</option> |
| 293 |
<option value="sv"<?php if ($language == 'sv') { |
| 294 |
echo " selected=\"selected\""; |
| 295 |
} ?>>Svenska |
| 296 |
</option> |
| 297 |
</select> |
| 298 |
</p> |
| 299 |
|
| 300 |
<p> |
| 301 |
<label for="<?php echo esc_attr($this->get_field_id('timezone')); ?>"><?php esc_html_e('MOD_ICAL_TIMEZONE_LABEL', 'icalendrier'); ?></label> |
| 302 |
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('timezone')); ?>" name="<?php echo esc_attr($this->get_field_name('timezone')); ?>"> |
| 303 |
<option value="0"<?php if ('0' === $timezone) { |
| 304 |
echo " selected=\"selected\""; |
| 305 |
} ?>><?php esc_html_e('MOD_ICAL_TIMEZONE_AUTO', 'icalendrier'); ?></option> |
| 306 |
|
| 307 |
<?php |
| 308 |
// Group identifiers by region; UTC has no region and is listed separately below. |
| 309 |
$regions = array(); |
| 310 |
foreach (DateTimeZone::listIdentifiers() as $timezone_identifier) { |
| 311 |
if ('UTC' === $timezone_identifier) { |
| 312 |
continue; |
| 313 |
} |
| 314 |
$region = strstr($timezone_identifier, '/', true) ?: $timezone_identifier; |
| 315 |
$regions[$region][] = $timezone_identifier; |
| 316 |
} |
| 317 |
foreach ($regions as $region => $identifiers) { |
| 318 |
echo '<optgroup label="' . esc_attr($region) . '">' . PHP_EOL; |
| 319 |
foreach ($identifiers as $timezone_identifier) { |
| 320 |
echo '<option value="' . esc_attr($timezone_identifier) . '"' . ($timezone === $timezone_identifier ? ' selected="selected"' : '') . '>' . esc_html($timezone_identifier) . '</option>' . PHP_EOL; |
| 321 |
} |
| 322 |
echo '</optgroup>' . PHP_EOL; |
| 323 |
} |
| 324 |
?> |
| 325 |
<option value="UTC"<?php if ($timezone == 'UTC') { |
| 326 |
echo " selected=\"selected\""; |
| 327 |
} ?>>UTC |
| 328 |
</option> |
| 329 |
</select> |
| 330 |
</p> |
| 331 |
|
| 332 |
<p> |
| 333 |
<label for="<?php echo esc_attr($this->get_field_id('showLink')); ?>"><?php esc_html_e('MOD_ICAL_SHOWLINK_LABEL', 'icalendrier'); ?></label> |
| 334 |
<input id="<?php echo esc_attr($this->get_field_id('showLink')); ?>" name="<?php echo esc_attr($this->get_field_name('showLink')); ?>" |
| 335 |
type="checkbox" <?php if ($showLink) { |
| 336 |
echo ' checked="checked"'; |
| 337 |
} ?> /> |
| 338 |
</p> |
| 339 |
|
| 340 |
<p> |
| 341 |
<label for="<?php echo esc_attr($this->get_field_id('moonphases')); ?>"><?php esc_html_e('MOD_ICAL_MOONPHASES_LABEL', 'icalendrier'); ?></label> |
| 342 |
<input id="<?php echo esc_attr($this->get_field_id('moonphases')); ?>" name="<?php echo esc_attr($this->get_field_name('moonphases')); ?>" |
| 343 |
type="checkbox" <?php if ($showMoon) { |
| 344 |
echo ' checked="checked"'; |
| 345 |
} ?> /> |
| 346 |
</p> |
| 347 |
|
| 348 |
<p> |
| 349 |
<label for="<?php echo esc_attr($this->get_field_id('namedays')); ?>"><?php esc_html_e('MOD_ICAL_NAMEDAYS_LABEL', 'icalendrier'); ?></label> |
| 350 |
<input id="<?php echo esc_attr($this->get_field_id('namedays')); ?>" name="<?php echo esc_attr($this->get_field_name('namedays')); ?>" |
| 351 |
type="checkbox" <?php if ($showNameDay) { |
| 352 |
echo ' checked="checked"'; |
| 353 |
} ?> /> |
| 354 |
</p> |
| 355 |
|
| 356 |
<p> |
| 357 |
<label for="<?php echo esc_attr($this->get_field_id('widgetTitle')); ?>"><?php esc_html_e('MOD_ICAL_WIDGET_TITLE_LABEL', 'icalendrier'); ?></label> |
| 358 |
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('widgetTitle')); ?>" name="<?php echo esc_attr($this->get_field_name('widgetTitle')); ?>" |
| 359 |
type="text" |
| 360 |
value="<?php echo $widgetTitle; ?>"/> |
| 361 |
</p> |
| 362 |
|
| 363 |
<p> |
| 364 |
<label for="<?php echo esc_attr($this->get_field_id('bgColor')); ?>"><?php esc_html_e('MOD_ICAL_BGCOLOR_LABEL', 'icalendrier'); ?></label><br/> |
| 365 |
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('bgColor')); ?>" name="<?php echo esc_attr($this->get_field_name('bgColor')); ?>" |
| 366 |
type="text" |
| 367 |
value="<?php echo $bgColor; ?>"/> |
| 368 |
</p> |
| 369 |
|
| 370 |
<?php |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
add_action('widgets_init', function () { |
| 375 |
register_widget('ICalendrierWidget'); |
| 376 |
}); |