説明なし

calendar.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/calendar` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/calendar` block on server.
  9. *
  10. * @param array $attributes The block attributes.
  11. *
  12. * @return string Returns the block content.
  13. */
  14. function render_block_core_calendar( $attributes ) {
  15. global $monthnum, $year;
  16. $previous_monthnum = $monthnum;
  17. $previous_year = $year;
  18. if ( isset( $attributes['month'] ) && isset( $attributes['year'] ) ) {
  19. $permalink_structure = get_option( 'permalink_structure' );
  20. if (
  21. strpos( $permalink_structure, '%monthnum%' ) !== false &&
  22. strpos( $permalink_structure, '%year%' ) !== false
  23. ) {
  24. // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
  25. $monthnum = $attributes['month'];
  26. // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
  27. $year = $attributes['year'];
  28. }
  29. }
  30. $wrapper_attributes = get_block_wrapper_attributes();
  31. $output = sprintf(
  32. '<div %1$s>%2$s</div>',
  33. $wrapper_attributes,
  34. get_calendar( true, false )
  35. );
  36. // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
  37. $monthnum = $previous_monthnum;
  38. // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
  39. $year = $previous_year;
  40. return $output;
  41. }
  42. /**
  43. * Registers the `core/calendar` block on server.
  44. */
  45. function register_block_core_calendar() {
  46. register_block_type_from_metadata(
  47. __DIR__ . '/calendar',
  48. array(
  49. 'render_callback' => 'render_block_core_calendar',
  50. )
  51. );
  52. }
  53. add_action( 'init', 'register_block_core_calendar' );