calendar.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. $custom_class_name = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className'];
  31. $align_class_name = empty( $attributes['align'] ) ? '' : ' ' . "align{$attributes['align']}";
  32. $output = sprintf(
  33. '<div class="%1$s">%2$s</div>',
  34. esc_attr( 'wp-block-calendar' . $custom_class_name . $align_class_name ),
  35. get_calendar( true, false )
  36. );
  37. // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
  38. $monthnum = $previous_monthnum;
  39. // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
  40. $year = $previous_year;
  41. return $output;
  42. }
  43. /**
  44. * Registers the `core/calendar` block on server.
  45. */
  46. function register_block_core_calendar() {
  47. register_block_type(
  48. 'core/calendar',
  49. array(
  50. 'attributes' => array(
  51. 'align' => array(
  52. 'type' => 'string',
  53. 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
  54. ),
  55. 'className' => array(
  56. 'type' => 'string',
  57. ),
  58. 'month' => array(
  59. 'type' => 'integer',
  60. ),
  61. 'year' => array(
  62. 'type' => 'integer',
  63. ),
  64. ),
  65. 'render_callback' => 'render_block_core_calendar',
  66. )
  67. );
  68. }
  69. add_action( 'init', 'register_block_core_calendar' );