[ 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  // We only want to register these functions and actions when
 111  // we are on single sites. On multi sites we use `post_count` option.
 112  if ( ! 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  }


Generated: Sat Jun 25 01:00:03 2022 Cross-referenced by PHPXref 0.7.1