# icalendrier/2.0/icalendrier-widget.php

iCalendrier, version 2.0. 376 lines.

- Page: https://pluginprobe.com/plugins/icalendrier/2.0/code/icalendrier-widget.php
- Raw: https://pluginprobe.com/plugins/icalendrier/2.0/raw/icalendrier-widget.php
- Modified: 2026-09-03T13:46:06+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/icalendrier/2.0/code/icalendrier-widget.php#L10-L20`.

```php
<?php
/*  Copyright 2014-2026 Baptiste Placé

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
/**
 * Plugin Name: iCalendrier
 * Plugin URI: https://icalendrier.fr/widget/wordpress-plugin
 * 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.
 * Version: 2.0
 * Author: Baptiste Placé
 * Author URI: https://icalendrier.fr/
 * License: GNU General Public License, version 2
 * Requires PHP: 8.2
 * Text Domain: icalendrier
 * Domain Path: /languages
 */

defined('ABSPATH') or die("No script kiddies please!");

/** Keep in sync with the "Version" header above and "Stable tag" in readme.txt (make release checks it). */
const ICALENDRIER_VERSION = '2.0';

// Include lib
require_once(dirname(__FILE__) . '/lib/Icalendrier.php');

// Admin form labels (MOD_ICAL_*). Registered on init, as WordPress 6.7+ requires.
add_action('init', function () {
    load_plugin_textdomain('icalendrier', false, basename(dirname(__FILE__)) . '/languages');
});

// Enqueue CSS
function icalendrier_wp_head()
{
    wp_register_style('icalendrier-default', plugins_url('/css/icalendrier.css', __FILE__), array(), ICALENDRIER_VERSION, 'all');
    wp_register_style('icalendrier-alt-1', plugins_url('/css/themes/icalendrier-alt-1.css', __FILE__), array(), ICALENDRIER_VERSION, 'all');
    wp_enqueue_style('icalendrier-default');
    wp_enqueue_style('icalendrier-alt-1');
    // The modern layout's stylesheet is only enqueued when that layout is rendered (see icalendrier_render()).
    wp_register_style('icalendrier-modern', plugins_url('/css/icalendrier-modern.css', __FILE__), array(), ICALENDRIER_VERSION, 'all');
}

function icalendrier_wp_admin_head()
{
    wp_enqueue_style('icalendrier', plugins_url('/css/icalendrier-admin.css', __FILE__), array(), ICALENDRIER_VERSION);
}

add_action('wp_enqueue_scripts', 'icalendrier_wp_head');

add_action('admin_enqueue_scripts', 'icalendrier_wp_admin_head');


/**
 * Parse shortcode or widget attributes and apply defaults.
 *
 * Single source of truth for both entry points. Keys are matched case-insensitively because
 * WordPress lowercases shortcode attribute names while the widget stores them in camelCase.
 *
 * @param array|string $atts Shortcode attributes, or a widget instance. WordPress passes '' for a bare shortcode.
 *
 * @return array{calType: string, language: string, timezone: string|int, showLink: int, showMoon: int, showNameDay: int, bgColor: string|false, style: string, widgetTitle: string}
 */
function icalendrier_get_attributes($atts)
{
    $atts = is_array($atts) ? array_change_key_case($atts, CASE_LOWER) : [];

    $type = $atts['type'] ?? 'comp175';
    if ( ! in_array($type, ['comp175', 'wide300', 'modern'], true)) {
        $type = 'comp175';
    }

    return [
        'calType'     => $type,
        'language'    => $atts['language'] ?? 'en',
        'timezone'    => $atts['timezone'] ?? 0,
        'showLink'    => array_key_exists('showlink', $atts) ? (int)icalendrier_is_on($atts['showlink']) : 1,
        'showMoon'    => array_key_exists('moonphases', $atts) ? (int)icalendrier_is_on($atts['moonphases']) : 1,
        'showNameDay' => array_key_exists('namedays', $atts) ? (int)icalendrier_is_on($atts['namedays']) : 1,
        'bgColor'     => $atts['bgcolor'] ?? false,
        'style'       => $atts['style'] ?? 'default',
        'widgetTitle' => $atts['widgettitle'] ?? '',
    ];
}

/**
 * Legacy name (with its typo) kept for third parties that may call it.
 *
 * @deprecated 1.9 Use icalendrier_get_attributes().
 */
function getAttibutes($atts)
{
    return icalendrier_get_attributes($atts);
}

/**
 * Reads a checkbox or shortcode flag: "on", "1", "true", "yes" and true mean on; everything else off.
 *
 * @param mixed $value
 */
function icalendrier_is_on($value): bool
{
    if (is_bool($value)) {
        return $value;
    }

    return in_array(strtolower(trim((string)$value)), ['on', '1', 'true', 'yes'], true);
}

/**
 * Render the calendar for parsed attributes (see icalendrier_get_attributes()).
 */
function icalendrier_render(array $attributes): string
{
    $iCalendrier = new Icalendrier($attributes['language'], $attributes['timezone']);

    if ('modern' === $attributes['calType']) {
        wp_enqueue_style('icalendrier-modern', plugins_url('/css/icalendrier-modern.css', __FILE__), array(), ICALENDRIER_VERSION, 'all');

        return $iCalendrier->iCalendrierModern($attributes);
    }

    if ('wide300' === $attributes['calType']) {
        return $iCalendrier->iCalendrierWide($attributes);
    }

    return $iCalendrier->iCalendrierComp($attributes);
}


// Shortcode
add_shortcode('icalendrier', 'icalendrier_shortcode');

function icalendrier_shortcode($atts)
{
    return icalendrier_render(icalendrier_get_attributes($atts));
}


/**
 * Class ICalendrier
 */
class ICalendrierWidget extends WP_Widget
{

    /**
     * ICalendrierWidget constructor.
     */
    public function __construct()
    {
        parent::__construct('icalendrier_widget', 'iCalendrier Widget', [
            'classname'   => 'calendar',
            'description' => 'Simple daily calendar'
        ]);
    }

    /**
     * @param $args
     * @param $instance
     */
    public function widget($args, $instance)
    {
        $attributes = icalendrier_get_attributes($instance);
        $title      = apply_filters('widget_title', $attributes['widgetTitle'], $instance, $this->id_base);

        echo $args['before_widget'] ?? '';

        if ('' !== $title) {
            echo ($args['before_title'] ?? '') . $title . ($args['after_title'] ?? '');
        }

        echo icalendrier_render($attributes);

        echo $args['after_widget'] ?? '';
    }

    /**
     * @param $new_instance
     * @param $old_instance
     *
     * @return mixed
     */
    public function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        foreach (['type', 'language', 'timezone', 'widgetTitle', 'bgColor', 'style'] as $key) {
            $instance[$key] = wp_strip_all_tags((string)($new_instance[$key] ?? ''));
        }
        // An unchecked checkbox is absent from the submitted form: that means off.
        $instance['showLink']   = (int)icalendrier_is_on($new_instance['showLink'] ?? '');
        $instance['moonphases'] = (int)icalendrier_is_on($new_instance['moonphases'] ?? '');
        $instance['namedays']   = (int)icalendrier_is_on($new_instance['namedays'] ?? '');

        return $instance;
    }

    /**
     * @param $instance
     */
    public function form($instance)
    {
        $type        = isset($instance['type']) ? esc_attr($instance['type']) : "modern"; // new widgets get the modern layout
        $language    = isset($instance['language']) ? esc_attr($instance['language']) : "en";
        $timezone    = isset($instance['timezone']) ? esc_attr((string)$instance['timezone']) : '0';
        $widgetTitle = isset($instance['widgetTitle']) ? esc_attr($instance['widgetTitle']) : "";
        $showLink    = isset($instance['showLink']) ? icalendrier_is_on($instance['showLink']) : true;
        $showMoon    = isset($instance['moonphases']) ? icalendrier_is_on($instance['moonphases']) : true;
        $showNameDay = isset($instance['namedays']) ? icalendrier_is_on($instance['namedays']) : true;
        $bgColor     = isset($instance['bgColor']) ? esc_attr($instance['bgColor']) : "";
        $style       = isset($instance['style']) ? esc_attr($instance['style']) : "default";
        ?>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('type')); ?>"><?php esc_html_e('MOD_ICAL_CALTYPE_LABEL', 'icalendrier'); ?></label>
            <select class="widefat" id="<?php echo esc_attr($this->get_field_id('type')); ?>" name="<?php echo esc_attr($this->get_field_name('type')); ?>">
                <option value="modern"<?php if ($type == 'modern') {
                    echo " selected=\"selected\"";
                } ?>><?php esc_html_e('MOD_ICAL_CALTYPE_MODERN', 'icalendrier'); ?></option>
                <option value="comp175"<?php if ($type == 'comp175') {
                    echo " selected=\"selected\"";
                } ?>><?php esc_html_e('MOD_ICAL_CALTYPE_COMP175', 'icalendrier'); ?></option>
                <option value="wide300"<?php if ($type == 'wide300') {
                    echo " selected=\"selected\"";
                } ?>><?php esc_html_e('MOD_ICAL_CALTYPE_COMP300', 'icalendrier'); ?></option>
            </select>
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('style')); ?>"><?php esc_html_e('MOD_ICAL_STYLE_LABEL', 'icalendrier'); ?></label>
            <select class="widefat" id="<?php echo esc_attr($this->get_field_id('style')); ?>" name="<?php echo esc_attr($this->get_field_name('style')); ?>">
                <option value="default"<?php if ($style == 'default') {
                    echo " selected=\"selected\"";
                } ?>><?php esc_html_e('MOD_ICAL_STYLE_DEFAULT', 'icalendrier'); ?></option>
                <option value="alt-1"<?php if ($style == 'alt-1') {
                    echo " selected=\"selected\"";
                } ?>><?php esc_html_e('MOD_ICAL_STYLE_ALT_1', 'icalendrier'); ?></option>
            </select>
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('language')); ?>"><?php esc_html_e('MOD_ICAL_LANGUAGE_LABEL', 'icalendrier'); ?></label>
            <select class="widefat" id="<?php echo esc_attr($this->get_field_id('language')); ?>" name="<?php echo esc_attr($this->get_field_name('language')); ?>">
                <option value="da"<?php if ($language == 'da') {
                    echo " selected=\"selected\"";
                } ?>>Dansk
                </option>
                <option value="de"<?php if ($language == 'de') {
                    echo " selected=\"selected\"";
                } ?>>Deutsch
                </option>
                <option value="en"<?php if ($language == 'en') {
                    echo " selected=\"selected\"";
                } ?>>English (US)
                </option>
                <option value="en-GB"<?php if ($language == 'en-GB') {
                    echo " selected=\"selected\"";
                } ?>>English (UK)
                </option>
                <option value="es"<?php if ($language == 'es') {
                    echo " selected=\"selected\"";
                } ?>>Español
                </option>
                <option value="fr"<?php if ($language == 'fr') {
                    echo " selected=\"selected\"";
                } ?>>Français
                </option>
                <option value="it"<?php if ($language == 'it') {
                    echo " selected=\"selected\"";
                } ?>>Italiano
                </option>
                <option value="ro"<?php if ($language == 'ro') {
                    echo " selected=\"selected\"";
                } ?>>Română
                </option>
                <option value="pt"<?php if ($language == 'pt') {
                    echo " selected=\"selected\"";
                } ?>>Português
                </option>
                <option value="pt-BR"<?php if ($language == 'pt-BR') {
                    echo " selected=\"selected\"";
                } ?>>Português (Brasil)
                </option>
                <option value="sv"<?php if ($language == 'sv') {
                    echo " selected=\"selected\"";
                } ?>>Svenska
                </option>
            </select>
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('timezone')); ?>"><?php esc_html_e('MOD_ICAL_TIMEZONE_LABEL', 'icalendrier'); ?></label>
            <select class="widefat" id="<?php echo esc_attr($this->get_field_id('timezone')); ?>" name="<?php echo esc_attr($this->get_field_name('timezone')); ?>">
                <option value="0"<?php if ('0' === $timezone) {
                    echo " selected=\"selected\"";
                } ?>><?php esc_html_e('MOD_ICAL_TIMEZONE_AUTO', 'icalendrier'); ?></option>

                <?php
                // Group identifiers by region; UTC has no region and is listed separately below.
                $regions = array();
                foreach (DateTimeZone::listIdentifiers() as $timezone_identifier) {
                    if ('UTC' === $timezone_identifier) {
                        continue;
                    }
                    $region             = strstr($timezone_identifier, '/', true) ?: $timezone_identifier;
                    $regions[$region][] = $timezone_identifier;
                }
                foreach ($regions as $region => $identifiers) {
                    echo '<optgroup label="' . esc_attr($region) . '">' . PHP_EOL;
                    foreach ($identifiers as $timezone_identifier) {
                        echo '<option value="' . esc_attr($timezone_identifier) . '"' . ($timezone === $timezone_identifier ? ' selected="selected"' : '') . '>' . esc_html($timezone_identifier) . '</option>' . PHP_EOL;
                    }
                    echo '</optgroup>' . PHP_EOL;
                }
                ?>
                <option value="UTC"<?php if ($timezone == 'UTC') {
                    echo " selected=\"selected\"";
                } ?>>UTC
                </option>
            </select>
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('showLink')); ?>"><?php esc_html_e('MOD_ICAL_SHOWLINK_LABEL', 'icalendrier'); ?></label> &nbsp;
            <input id="<?php echo esc_attr($this->get_field_id('showLink')); ?>" name="<?php echo esc_attr($this->get_field_name('showLink')); ?>"
                   type="checkbox" <?php if ($showLink) {
                echo ' checked="checked"';
            } ?> />
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('moonphases')); ?>"><?php esc_html_e('MOD_ICAL_MOONPHASES_LABEL', 'icalendrier'); ?></label> &nbsp;
            <input id="<?php echo esc_attr($this->get_field_id('moonphases')); ?>" name="<?php echo esc_attr($this->get_field_name('moonphases')); ?>"
                   type="checkbox" <?php if ($showMoon) {
                echo ' checked="checked"';
            } ?> />
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('namedays')); ?>"><?php esc_html_e('MOD_ICAL_NAMEDAYS_LABEL', 'icalendrier'); ?></label> &nbsp;
            <input id="<?php echo esc_attr($this->get_field_id('namedays')); ?>" name="<?php echo esc_attr($this->get_field_name('namedays')); ?>"
                   type="checkbox" <?php if ($showNameDay) {
                echo ' checked="checked"';
            } ?> />
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('widgetTitle')); ?>"><?php esc_html_e('MOD_ICAL_WIDGET_TITLE_LABEL', 'icalendrier'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('widgetTitle')); ?>" name="<?php echo esc_attr($this->get_field_name('widgetTitle')); ?>"
                   type="text"
                   value="<?php echo $widgetTitle; ?>"/>
        </p>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('bgColor')); ?>"><?php esc_html_e('MOD_ICAL_BGCOLOR_LABEL', 'icalendrier'); ?></label><br/>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('bgColor')); ?>" name="<?php echo esc_attr($this->get_field_name('bgColor')); ?>"
                   type="text"
                   value="<?php echo $bgColor; ?>"/>
        </p>

        <?php
    }
}

add_action('widgets_init', function () {
    register_widget('ICalendrierWidget');
});
```
