[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> calendar.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/calendar` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/calendar` block on server.
  10   *
  11   * @param array $attributes The block attributes.
  12   *
  13   * @return string Returns the block content.
  14   */
  15  function render_block_core_calendar( $attributes ) {
  16      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.' ) . '</div>';
  23          }
  24          return '';
  25      }
  26  
  27      $previous_monthnum = $monthnum;
  28      $previous_year     = $year;
  29  
  30      if ( isset( $attributes['month'] ) && isset( $attributes['year'] ) ) {
  31          $permalink_structure = get_option( 'permalink_structure' );
  32          if (
  33              strpos( $permalink_structure, '%monthnum%' ) !== false &&
  34              strpos( $permalink_structure, '%year%' ) !== false
  35          ) {
  36              // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
  37              $monthnum = $attributes['month'];
  38              // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
  39              $year = $attributes['year'];
  40          }
  41      }
  42  
  43      $wrapper_attributes = get_block_wrapper_attributes();
  44      $output             = sprintf(
  45          '<div %1$s>%2$s</div>',
  46          $wrapper_attributes,
  47          get_calendar( true, false )
  48      );
  49  
  50      // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
  51      $monthnum = $previous_monthnum;
  52      // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
  53      $year = $previous_year;
  54  
  55      return $output;
  56  }
  57  
  58  /**
  59   * Registers the `core/calendar` block on server.
  60   */
  61  function register_block_core_calendar() {
  62      register_block_type_from_metadata(
  63          __DIR__ . '/calendar',
  64          array(
  65              'render_callback' => 'render_block_core_calendar',
  66          )
  67      );
  68  }
  69  
  70  add_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   */
  80  function 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( 'wp_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   */
 103  function 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( 'wp_calendar_block_has_published_posts', $has_published_posts );
 107      return $has_published_posts;
 108  }
 109  
 110  /**
 111   * Handler for updating the has published posts flag when a post is deleted.
 112   *
 113   * @param int $post_id Deleted post ID.
 114   */
 115  function block_core_calendar_update_has_published_post_on_delete( $post_id ) {
 116      if ( is_multisite() ) {
 117          return;
 118      }
 119  
 120      $post = get_post( $post_id );
 121  
 122      if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
 123          return;
 124      }
 125  
 126      block_core_calendar_update_has_published_posts();
 127  }
 128  
 129  /**
 130   * Handler for updating the has published posts flag when a post status changes.
 131   *
 132   * @param string  $new_status The status the post is changing to.
 133   * @param string  $old_status The status the post is changing from.
 134   * @param WP_Post $post       Post object.
 135   */
 136  function block_core_calendar_update_has_published_post_on_transition_post_status( $new_status, $old_status, $post ) {
 137      if ( is_multisite() ) {
 138          return;
 139      }
 140  
 141      if ( $new_status === $old_status ) {
 142          return;
 143      }
 144  
 145      if ( 'post' !== get_post_type( $post ) ) {
 146          return;
 147      }
 148  
 149      if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
 150          return;
 151      }
 152  
 153      block_core_calendar_update_has_published_posts();
 154  }
 155  
 156  add_action( 'delete_post', 'block_core_calendar_update_has_published_post_on_delete' );
 157  add_action( 'transition_post_status', 'block_core_calendar_update_has_published_post_on_transition_post_status', 10, 3 );


Generated: Wed Dec 1 01:00:03 2021 Cross-referenced by PHPXref 0.7.1