Edit File by line
/home/zeestwma/richards.../wp-inclu.../blocks
File: calendar.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Server-side rendering of the `core/calendar` block.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Renders the `core/calendar` block on server.
[8] Fix | Delete
*
[9] Fix | Delete
* @since 5.2.0
[10] Fix | Delete
*
[11] Fix | Delete
* @global int $monthnum.
[12] Fix | Delete
* @global int $year.
[13] Fix | Delete
*
[14] Fix | Delete
* @param array $attributes The block attributes.
[15] Fix | Delete
*
[16] Fix | Delete
* @return string Returns the block content.
[17] Fix | Delete
*/
[18] Fix | Delete
function render_block_core_calendar( $attributes ) {
[19] Fix | Delete
global $monthnum, $year;
[20] Fix | Delete
[21] Fix | Delete
// Calendar shouldn't be rendered
[22] Fix | Delete
// when there are no published posts on the site.
[23] Fix | Delete
if ( ! block_core_calendar_has_published_posts() ) {
[24] Fix | Delete
if ( is_user_logged_in() ) {
[25] Fix | Delete
return '<div>' . __( 'The calendar block is hidden because there are no published posts.' ) . '</div>';
[26] Fix | Delete
}
[27] Fix | Delete
return '';
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
$previous_monthnum = $monthnum;
[31] Fix | Delete
$previous_year = $year;
[32] Fix | Delete
[33] Fix | Delete
if ( isset( $attributes['month'] ) && isset( $attributes['year'] ) ) {
[34] Fix | Delete
$permalink_structure = get_option( 'permalink_structure' );
[35] Fix | Delete
if (
[36] Fix | Delete
str_contains( $permalink_structure, '%monthnum%' ) &&
[37] Fix | Delete
str_contains( $permalink_structure, '%year%' )
[38] Fix | Delete
) {
[39] Fix | Delete
$monthnum = $attributes['month'];
[40] Fix | Delete
$year = $attributes['year'];
[41] Fix | Delete
}
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
$color_block_styles = array();
[45] Fix | Delete
[46] Fix | Delete
// Text color.
[47] Fix | Delete
$preset_text_color = array_key_exists( 'textColor', $attributes ) ? "var:preset|color|{$attributes['textColor']}" : null;
[48] Fix | Delete
$custom_text_color = $attributes['style']['color']['text'] ?? null;
[49] Fix | Delete
$color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
[50] Fix | Delete
[51] Fix | Delete
// Background Color.
[52] Fix | Delete
$preset_background_color = array_key_exists( 'backgroundColor', $attributes ) ? "var:preset|color|{$attributes['backgroundColor']}" : null;
[53] Fix | Delete
$custom_background_color = $attributes['style']['color']['background'] ?? null;
[54] Fix | Delete
$color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
[55] Fix | Delete
[56] Fix | Delete
// Generate color styles and classes.
[57] Fix | Delete
$styles = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) );
[58] Fix | Delete
$inline_styles = empty( $styles['css'] ) ? '' : sprintf( ' style="%s"', esc_attr( $styles['css'] ) );
[59] Fix | Delete
$classnames = empty( $styles['classnames'] ) ? '' : ' ' . esc_attr( $styles['classnames'] );
[60] Fix | Delete
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
[61] Fix | Delete
$classnames .= ' has-link-color';
[62] Fix | Delete
}
[63] Fix | Delete
// Apply color classes and styles to the calendar.
[64] Fix | Delete
$calendar = str_replace( '<table', '<table' . $inline_styles, get_calendar( true, false ) );
[65] Fix | Delete
$calendar = str_replace( 'class="wp-calendar-table', 'class="wp-calendar-table' . $classnames, $calendar );
[66] Fix | Delete
[67] Fix | Delete
$wrapper_attributes = get_block_wrapper_attributes();
[68] Fix | Delete
$output = sprintf(
[69] Fix | Delete
'<div %1$s>%2$s</div>',
[70] Fix | Delete
$wrapper_attributes,
[71] Fix | Delete
$calendar
[72] Fix | Delete
);
[73] Fix | Delete
[74] Fix | Delete
$monthnum = $previous_monthnum;
[75] Fix | Delete
$year = $previous_year;
[76] Fix | Delete
[77] Fix | Delete
return $output;
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/**
[81] Fix | Delete
* Registers the `core/calendar` block on server.
[82] Fix | Delete
*
[83] Fix | Delete
* @since 5.2.0
[84] Fix | Delete
*/
[85] Fix | Delete
function register_block_core_calendar() {
[86] Fix | Delete
register_block_type_from_metadata(
[87] Fix | Delete
__DIR__ . '/calendar',
[88] Fix | Delete
array(
[89] Fix | Delete
'render_callback' => 'render_block_core_calendar',
[90] Fix | Delete
)
[91] Fix | Delete
);
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
add_action( 'init', 'register_block_core_calendar' );
[95] Fix | Delete
[96] Fix | Delete
/**
[97] Fix | Delete
* Returns whether or not there are any published posts.
[98] Fix | Delete
*
[99] Fix | Delete
* Used to hide the calendar block when there are no published posts.
[100] Fix | Delete
* This compensates for a known Core bug: https://core.trac.wordpress.org/ticket/12016
[101] Fix | Delete
*
[102] Fix | Delete
* @since 5.9.0
[103] Fix | Delete
*
[104] Fix | Delete
* @return bool Has any published posts or not.
[105] Fix | Delete
*/
[106] Fix | Delete
function block_core_calendar_has_published_posts() {
[107] Fix | Delete
// Multisite already has an option that stores the count of the published posts.
[108] Fix | Delete
// Let's use that for multisites.
[109] Fix | Delete
if ( is_multisite() ) {
[110] Fix | Delete
return 0 < (int) get_option( 'post_count' );
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
// On single sites we try our own cached option first.
[114] Fix | Delete
$has_published_posts = get_option( 'wp_calendar_block_has_published_posts', null );
[115] Fix | Delete
if ( null !== $has_published_posts ) {
[116] Fix | Delete
return (bool) $has_published_posts;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
// No cache hit, let's update the cache and return the cached value.
[120] Fix | Delete
return block_core_calendar_update_has_published_posts();
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* Queries the database for any published post and saves
[125] Fix | Delete
* a flag whether any published post exists or not.
[126] Fix | Delete
*
[127] Fix | Delete
* @since 5.9.0
[128] Fix | Delete
*
[129] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[130] Fix | Delete
*
[131] Fix | Delete
* @return bool Has any published posts or not.
[132] Fix | Delete
*/
[133] Fix | Delete
function block_core_calendar_update_has_published_posts() {
[134] Fix | Delete
global $wpdb;
[135] Fix | Delete
$has_published_posts = (bool) $wpdb->get_var( "SELECT 1 as test FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1" );
[136] Fix | Delete
update_option( 'wp_calendar_block_has_published_posts', $has_published_posts );
[137] Fix | Delete
return $has_published_posts;
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
// We only want to register these functions and actions when
[141] Fix | Delete
// we are on single sites. On multi sites we use `post_count` option.
[142] Fix | Delete
if ( ! is_multisite() ) {
[143] Fix | Delete
/**
[144] Fix | Delete
* Handler for updating the has published posts flag when a post is deleted.
[145] Fix | Delete
*
[146] Fix | Delete
* @since 5.9.0
[147] Fix | Delete
*
[148] Fix | Delete
* @param int $post_id Deleted post ID.
[149] Fix | Delete
*/
[150] Fix | Delete
function block_core_calendar_update_has_published_post_on_delete( $post_id ) {
[151] Fix | Delete
$post = get_post( $post_id );
[152] Fix | Delete
[153] Fix | Delete
if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
[154] Fix | Delete
return;
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
block_core_calendar_update_has_published_posts();
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
/**
[161] Fix | Delete
* Handler for updating the has published posts flag when a post status changes.
[162] Fix | Delete
*
[163] Fix | Delete
* @since 5.9.0
[164] Fix | Delete
*
[165] Fix | Delete
* @param string $new_status The status the post is changing to.
[166] Fix | Delete
* @param string $old_status The status the post is changing from.
[167] Fix | Delete
* @param WP_Post $post Post object.
[168] Fix | Delete
*/
[169] Fix | Delete
function block_core_calendar_update_has_published_post_on_transition_post_status( $new_status, $old_status, $post ) {
[170] Fix | Delete
if ( $new_status === $old_status ) {
[171] Fix | Delete
return;
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
if ( 'post' !== get_post_type( $post ) ) {
[175] Fix | Delete
return;
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
[179] Fix | Delete
return;
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
block_core_calendar_update_has_published_posts();
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
add_action( 'delete_post', 'block_core_calendar_update_has_published_post_on_delete' );
[186] Fix | Delete
add_action( 'transition_post_status', 'block_core_calendar_update_has_published_post_on_transition_post_status', 10, 3 );
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function