Make WordPress Core


Ignore:
Timestamp:
11/08/2021 02:26:27 PM (4 years ago)
Author:
youknowriad
Message:

Block Editor: Update the WordPress Packages based on Gutenberg 11.9 RC1.

This brings the JS packages up to date and is the first step that will allow us
to include the other block editor updates for WordPress 5.9:
FSE infrastrucutre, site editor and global styles.

Props noisysocks.
See #54337.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks/calendar.php

    r49226 r52042  
    1515function render_block_core_calendar( $attributes ) {
    1616    global $monthnum, $year;
     17
     18    // Calendar shouldn't be rendered
     19    // when there are no published posts on the site.
     20    if ( ! block_core_calendar_has_published_posts() ) {
     21        if ( is_user_logged_in() ) {
     22            return '<div>' . __( 'The calendar block is hidden because there are no published posts.', 'gutenberg' ) . '</div>';
     23        }
     24        return '';
     25    }
    1726
    1827    $previous_monthnum = $monthnum;
     
    6069
    6170add_action( 'init', 'register_block_core_calendar' );
     71
     72/**
     73 * Returns whether or not there are any published posts.
     74 *
     75 * Used to hide the calendar block when there are no published posts.
     76 * This compensates for a known Core bug: https://core.trac.wordpress.org/ticket/12016
     77 *
     78 * @return bool Has any published posts or not.
     79 */
     80function block_core_calendar_has_published_posts() {
     81    // Multisite already has an option that stores the count of the published posts.
     82    // Let's use that for multisites.
     83    if ( is_multisite() ) {
     84        return 0 < (int) get_option( 'post_count' );
     85    }
     86
     87    // On single sites we try our own cached option first.
     88    $has_published_posts = get_option( 'gutenberg_calendar_block_has_published_posts', null );
     89    if ( null !== $has_published_posts ) {
     90        return (bool) $has_published_posts;
     91    }
     92
     93    // No cache hit, let's update the cache and return the cached value.
     94    return block_core_calendar_update_has_published_posts();
     95}
     96
     97/**
     98 * Queries the database for any published post and saves
     99 * a flag whether any published post exists or not.
     100 *
     101 * @return bool Has any published posts or not.
     102 */
     103function block_core_calendar_update_has_published_posts() {
     104    global $wpdb;
     105    $has_published_posts = (bool) $wpdb->get_var( "SELECT 1 as test FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1" );
     106    update_option( 'gutenberg_calendar_block_has_published_posts', $has_published_posts );
     107    return $has_published_posts;
     108}
     109
     110// We only want to register these functions and actions when
     111// we are on single sites. On multi sites we use `post_count` option.
     112if ( ! is_multisite() ) {
     113    /**
     114     * Handler for updating the has published posts flag when a post is deleted.
     115     *
     116     * @param int $post_id Deleted post ID.
     117     */
     118    function block_core_calendar_update_has_published_post_on_delete( $post_id ) {
     119        $post = get_post( $post_id );
     120
     121        if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
     122            return;
     123        }
     124
     125        block_core_calendar_update_has_published_posts();
     126    }
     127
     128    /**
     129     * Handler for updating the has published posts flag when a post status changes.
     130     *
     131     * @param string  $new_status The status the post is changing to.
     132     * @param string  $old_status The status the post is changing from.
     133     * @param WP_Post $post       Post object.
     134     */
     135    function block_core_calendar_update_has_published_post_on_transition_post_status( $new_status, $old_status, $post ) {
     136        if ( $new_status === $old_status ) {
     137            return;
     138        }
     139
     140        if ( 'post' !== get_post_type( $post ) ) {
     141            return;
     142        }
     143
     144        if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
     145            return;
     146        }
     147
     148        block_core_calendar_update_has_published_posts();
     149    }
     150
     151    add_action( 'delete_post', 'block_core_calendar_update_has_published_post_on_delete' );
     152    add_action( 'transition_post_status', 'block_core_calendar_update_has_published_post_on_transition_post_status', 10, 3 );
     153}
Note: See TracChangeset for help on using the changeset viewer.