render_block_core_calendar( array $attributes ): string
- Since
- 5.2.0
- Source
wp-includes/blocks/calendar.php:20
core/calendar block on server.Compatibility
- WordPress
- since 5.2.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$attributesarray- The block attributes.
Return value
string- Returns the block content.
Performance profile
How much work a call to render_block_core_calendar() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 10–115
- Plugin surface
- None
- Called by
- 0
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 133.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()one call below render_block_core_calendar() - cacheobject cache
wp_cache_get()one call below render_block_core_calendar() - serializeserialisation
maybe_unserialize()one call below render_block_core_calendar()
Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost render_block_core_calendar() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!block_core_calendar_has_published_posts() && !is_user_logged_in() | 10 | block_core_calendar_has_published_posts(), is_user_logged_in() |
!block_core_calendar_has_published_posts() && is_user_logged_in() | 15 | block_core_calendar_has_published_posts(), is_user_logged_in(), __() |
block_core_calendar_has_published_posts() && !isset($attributes) && empty($styles) | 82–93 | block_core_calendar_has_published_posts(), color(), get_calendar(), get_block_wrapper_attributes(), sprintf() |
block_core_calendar_has_published_posts() && !isset($attributes) | 86–99 | block_core_calendar_has_published_posts(), color(), esc_attr(), get_calendar(), get_block_wrapper_attributes(), sprintf() |
block_core_calendar_has_published_posts() && isset($attributes) && empty($styles) | 90–105 | block_core_calendar_has_published_posts(), get_option(), color(), get_calendar(), get_block_wrapper_attributes(), sprintf() |
block_core_calendar_has_published_posts() && !isset($attributes) && !empty($styles) | 92–103 | block_core_calendar_has_published_posts(), color(), esc_attr(), esc_attr(), get_calendar(), get_block_wrapper_attributes(), sprintf() |
block_core_calendar_has_published_posts() && isset($attributes) | 94–111 | block_core_calendar_has_published_posts(), get_option(), color(), esc_attr(), get_calendar(), get_block_wrapper_attributes(), sprintf() |
block_core_calendar_has_published_posts() && isset($attributes) && !empty($styles) | 100–115 | block_core_calendar_has_published_posts(), get_option(), color(), esc_attr(), esc_attr(), get_calendar(), get_block_wrapper_attributes(), sprintf() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 133 | 10–115 | 15 | |
| 8.5 | 133 | 10–115 | 15 | |
| 8.4 | 133 | 10–115 | 15 | 15 fewer instructions than PHP 8.3 |
| 8.3 | 148 | 10–130 | 15 | |
| 8.2 | 148 | 10–130 | 15 | |
| 8.1 | 148 | 10–130 | 15 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 151 | 10–133 | 15 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 9
- block_core_calendar_has_published_posts()Returns whether or not there are any published posts.
- is_user_logged_in()Determines whether the current visitor is a logged in user.
- __()Retrieves the translation of $text.
- get_option()Retrieves an option value based on an option name.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_style_engine_get_styles()Global public interface method to generate styles from a single style object, e.g. the value of a block's attributes.style object or the top level styles in theme.json.
- esc_attr()Escaping for HTML attributes.
- get_calendar()Displays calendar with days that have posts as links.
- get_block_wrapper_attributes()Generates a string of attributes by applying to the current block being rendered all of the features that the block supports.
Source code
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;}Changelog
Introduced in 5.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/blocks/calendar.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.