render_block_core_calendar

The timeline below displays how wordpress function render_block_core_calendar has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Server-side rendering of the `core/calendar` block.
 *
 * @package WordPress
 */
/**
 * Renders the `core/calendar` block on server.
 *
 * @param array $attributes The block attributes.
 *
 * @return string Returns the block content.
 */
function render_block_core_calendar($attributes)
{
    global $monthnum, $year;
    // Calendar shouldn't be rendered
    // when there are no published posts on the site.
    if (!block_core_calendar_has_published_posts()) {
        if (is_user_logged_in()) {
            return '<div>' . __('The calendar block is hidden because there are no published posts.') . '</div>';
        }
        return '';
    }
    $previous_monthnum = $monthnum;
    $previous_year = $year;
    if (isset($attributes['month']) && isset($attributes['year'])) {
        $permalink_structure = get_option('permalink_structure');
        if (str_contains($permalink_structure, '%monthnum%') && str_contains($permalink_structure, '%year%')) {
            $monthnum = $attributes['month'];
            $year = $attributes['year'];
        }
    }
    $color_block_styles = array();
    // Text color.
    $preset_text_color = array_key_exists('textColor', $attributes) ? "var:preset|color|{$attributes['textColor']}" : null;
    $custom_text_color = $attributes['style']['color']['text'] ?? null;
    $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
    // Background Color.
    $preset_background_color = array_key_exists('backgroundColor', $attributes) ? "var:preset|color|{$attributes['backgroundColor']}" : null;
    $custom_background_color = $attributes['style']['color']['background'] ?? null;
    $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
    // Generate color styles and classes.
    $styles = wp_style_engine_get_styles(array('color' => $color_block_styles), array('convert_vars_to_classnames' => true));
    $inline_styles = empty($styles['css']) ? '' : sprintf(' style="%s"', esc_attr($styles['css']));
    $classnames = empty($styles['classnames']) ? '' : (' ' . esc_attr($styles['classnames']));
    if (isset($attributes['style']['elements']['link']['color']['text'])) {
        $classnames .= ' has-link-color';
    }
    // Apply color classes and styles to the calendar.
    $calendar = str_replace('<table', '<table' . $inline_styles, get_calendar(true, false));
    $calendar = str_replace('class="wp-calendar-table', 'class="wp-calendar-table' . $classnames, $calendar);
    $wrapper_attributes = get_block_wrapper_attributes();
    $output = sprintf('<div %1$s>%2$s</div>', $wrapper_attributes, $calendar);
    $monthnum = $previous_monthnum;
    $year = $previous_year;
    return $output;
}

WordPress Version: 6.4

/**
 * Server-side rendering of the `core/calendar` block.
 *
 * @package WordPress
 */
/**
 * Renders the `core/calendar` block on server.
 *
 * @param array $attributes The block attributes.
 *
 * @return string Returns the block content.
 */
function render_block_core_calendar($attributes)
{
    global $monthnum, $year;
    // Calendar shouldn't be rendered
    // when there are no published posts on the site.
    if (!block_core_calendar_has_published_posts()) {
        if (is_user_logged_in()) {
            return '<div>' . __('The calendar block is hidden because there are no published posts.') . '</div>';
        }
        return '';
    }
    $previous_monthnum = $monthnum;
    $previous_year = $year;
    if (isset($attributes['month']) && isset($attributes['year'])) {
        $permalink_structure = get_option('permalink_structure');
        if (str_contains($permalink_structure, '%monthnum%') && str_contains($permalink_structure, '%year%')) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $monthnum = $attributes['month'];
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $year = $attributes['year'];
        }
    }
    $color_block_styles = array();
    // Text color.
    $preset_text_color = array_key_exists('textColor', $attributes) ? "var:preset|color|{$attributes['textColor']}" : null;
    $custom_text_color = $attributes['style']['color']['text'] ?? null;
    $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
    // Background Color.
    $preset_background_color = array_key_exists('backgroundColor', $attributes) ? "var:preset|color|{$attributes['backgroundColor']}" : null;
    $custom_background_color = $attributes['style']['color']['background'] ?? null;
    $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
    // Generate color styles and classes.
    $styles = wp_style_engine_get_styles(array('color' => $color_block_styles), array('convert_vars_to_classnames' => true));
    $inline_styles = empty($styles['css']) ? '' : sprintf(' style="%s"', esc_attr($styles['css']));
    $classnames = empty($styles['classnames']) ? '' : (' ' . esc_attr($styles['classnames']));
    if (isset($attributes['style']['elements']['link']['color']['text'])) {
        $classnames .= ' has-link-color';
    }
    // Apply color classes and styles to the calendar.
    $calendar = str_replace('<table', '<table' . $inline_styles, get_calendar(true, false));
    $calendar = str_replace('class="wp-calendar-table', 'class="wp-calendar-table' . $classnames, $calendar);
    $wrapper_attributes = get_block_wrapper_attributes();
    $output = sprintf('<div %1$s>%2$s</div>', $wrapper_attributes, $calendar);
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $monthnum = $previous_monthnum;
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $year = $previous_year;
    return $output;
}

WordPress Version: 6.2

/**
 * Server-side rendering of the `core/calendar` block.
 *
 * @package WordPress
 */
/**
 * Renders the `core/calendar` block on server.
 *
 * @param array $attributes The block attributes.
 *
 * @return string Returns the block content.
 */
function render_block_core_calendar($attributes)
{
    global $monthnum, $year;
    // Calendar shouldn't be rendered
    // when there are no published posts on the site.
    if (!block_core_calendar_has_published_posts()) {
        if (is_user_logged_in()) {
            return '<div>' . __('The calendar block is hidden because there are no published posts.') . '</div>';
        }
        return '';
    }
    $previous_monthnum = $monthnum;
    $previous_year = $year;
    if (isset($attributes['month']) && isset($attributes['year'])) {
        $permalink_structure = get_option('permalink_structure');
        if (str_contains($permalink_structure, '%monthnum%') && str_contains($permalink_structure, '%year%')) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $monthnum = $attributes['month'];
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $year = $attributes['year'];
        }
    }
    $color_block_styles = array();
    // Text color.
    $preset_text_color = array_key_exists('textColor', $attributes) ? "var:preset|color|{$attributes['textColor']}" : null;
    $custom_text_color = _wp_array_get($attributes, array('style', 'color', 'text'), null);
    $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
    // Background Color.
    $preset_background_color = array_key_exists('backgroundColor', $attributes) ? "var:preset|color|{$attributes['backgroundColor']}" : null;
    $custom_background_color = _wp_array_get($attributes, array('style', 'color', 'background'), null);
    $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
    // Generate color styles and classes.
    $styles = wp_style_engine_get_styles(array('color' => $color_block_styles), array('convert_vars_to_classnames' => true));
    $inline_styles = empty($styles['css']) ? '' : sprintf(' style="%s"', esc_attr($styles['css']));
    $classnames = empty($styles['classnames']) ? '' : (' ' . esc_attr($styles['classnames']));
    if (isset($attributes['style']['elements']['link']['color']['text'])) {
        $classnames .= ' has-link-color';
    }
    // Apply color classes and styles to the calendar.
    $calendar = str_replace('<table', '<table' . $inline_styles, get_calendar(true, false));
    $calendar = str_replace('class="wp-calendar-table', 'class="wp-calendar-table' . $classnames, $calendar);
    $wrapper_attributes = get_block_wrapper_attributes();
    $output = sprintf('<div %1$s>%2$s</div>', $wrapper_attributes, $calendar);
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $monthnum = $previous_monthnum;
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $year = $previous_year;
    return $output;
}

WordPress Version: 6.1

/**
 * Server-side rendering of the `core/calendar` block.
 *
 * @package WordPress
 */
/**
 * Renders the `core/calendar` block on server.
 *
 * @param array $attributes The block attributes.
 *
 * @return string Returns the block content.
 */
function render_block_core_calendar($attributes)
{
    global $monthnum, $year;
    // Calendar shouldn't be rendered
    // when there are no published posts on the site.
    if (!block_core_calendar_has_published_posts()) {
        if (is_user_logged_in()) {
            return '<div>' . __('The calendar block is hidden because there are no published posts.') . '</div>';
        }
        return '';
    }
    $previous_monthnum = $monthnum;
    $previous_year = $year;
    if (isset($attributes['month']) && isset($attributes['year'])) {
        $permalink_structure = get_option('permalink_structure');
        if (str_contains($permalink_structure, '%monthnum%') && str_contains($permalink_structure, '%year%')) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $monthnum = $attributes['month'];
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $year = $attributes['year'];
        }
    }
    $wrapper_attributes = get_block_wrapper_attributes();
    $output = sprintf('<div %1$s>%2$s</div>', $wrapper_attributes, get_calendar(true, false));
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $monthnum = $previous_monthnum;
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $year = $previous_year;
    return $output;
}

WordPress Version: 5.9

/**
 * Server-side rendering of the `core/calendar` block.
 *
 * @package WordPress
 */
/**
 * Renders the `core/calendar` block on server.
 *
 * @param array $attributes The block attributes.
 *
 * @return string Returns the block content.
 */
function render_block_core_calendar($attributes)
{
    global $monthnum, $year;
    // Calendar shouldn't be rendered
    // when there are no published posts on the site.
    if (!block_core_calendar_has_published_posts()) {
        if (is_user_logged_in()) {
            return '<div>' . __('The calendar block is hidden because there are no published posts.') . '</div>';
        }
        return '';
    }
    $previous_monthnum = $monthnum;
    $previous_year = $year;
    if (isset($attributes['month']) && isset($attributes['year'])) {
        $permalink_structure = get_option('permalink_structure');
        if (strpos($permalink_structure, '%monthnum%') !== false && strpos($permalink_structure, '%year%') !== false) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $monthnum = $attributes['month'];
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $year = $attributes['year'];
        }
    }
    $wrapper_attributes = get_block_wrapper_attributes();
    $output = sprintf('<div %1$s>%2$s</div>', $wrapper_attributes, get_calendar(true, false));
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $monthnum = $previous_monthnum;
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $year = $previous_year;
    return $output;
}

WordPress Version: 5.6

/**
 * Server-side rendering of the `core/calendar` block.
 *
 * @package WordPress
 */
/**
 * Renders the `core/calendar` block on server.
 *
 * @param array $attributes The block attributes.
 *
 * @return string Returns the block content.
 */
function render_block_core_calendar($attributes)
{
    global $monthnum, $year;
    $previous_monthnum = $monthnum;
    $previous_year = $year;
    if (isset($attributes['month']) && isset($attributes['year'])) {
        $permalink_structure = get_option('permalink_structure');
        if (strpos($permalink_structure, '%monthnum%') !== false && strpos($permalink_structure, '%year%') !== false) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $monthnum = $attributes['month'];
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $year = $attributes['year'];
        }
    }
    $wrapper_attributes = get_block_wrapper_attributes();
    $output = sprintf('<div %1$s>%2$s</div>', $wrapper_attributes, get_calendar(true, false));
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $monthnum = $previous_monthnum;
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $year = $previous_year;
    return $output;
}

WordPress Version: 5.3

/**
 * Server-side rendering of the `core/calendar` block.
 *
 * @package WordPress
 */
/**
 * Renders the `core/calendar` block on server.
 *
 * @param array $attributes The block attributes.
 *
 * @return string Returns the block content.
 */
function render_block_core_calendar($attributes)
{
    global $monthnum, $year;
    $previous_monthnum = $monthnum;
    $previous_year = $year;
    if (isset($attributes['month']) && isset($attributes['year'])) {
        $permalink_structure = get_option('permalink_structure');
        if (strpos($permalink_structure, '%monthnum%') !== false && strpos($permalink_structure, '%year%') !== false) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $monthnum = $attributes['month'];
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $year = $attributes['year'];
        }
    }
    $custom_class_name = empty($attributes['className']) ? '' : (' ' . $attributes['className']);
    $align_class_name = empty($attributes['align']) ? '' : (' ' . "align{$attributes['align']}");
    $output = sprintf('<div class="%1$s">%2$s</div>', esc_attr('wp-block-calendar' . $custom_class_name . $align_class_name), get_calendar(true, false));
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $monthnum = $previous_monthnum;
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $year = $previous_year;
    return $output;
}

WordPress Version: 5.2

/**
 * Server-side rendering of the `core/calendar` block.
 *
 * @package WordPress
 */
/**
 * Renders the `core/calendar` block on server.
 *
 * @param array $attributes The block attributes.
 *
 * @return string Returns the block content.
 */
function render_block_core_calendar($attributes)
{
    global $monthnum, $year;
    $previous_monthnum = $monthnum;
    $previous_year = $year;
    if (isset($attributes['month']) && isset($attributes['year'])) {
        $permalink_structure = get_option('permalink_structure');
        if (strpos($permalink_structure, '%monthnum%') !== false && strpos($permalink_structure, '%year%') !== false) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $monthnum = $attributes['month'];
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
            $year = $attributes['year'];
        }
    }
    $custom_class_name = empty($attributes['className']) ? '' : (' ' . $attributes['className']);
    $align_class_name = empty($attributes['align']) ? '' : (' ' . "align{$attributes['align']}");
    return sprintf('<div class="%1$s">%2$s</div>', esc_attr('wp-block-calendar' . $custom_class_name . $align_class_name), get_calendar(true, false));
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $monthnum = $previous_monthnum;
    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
    $year = $previous_year;
}