CalendarBlock.php
| 1 | <?php |
| 2 | /** |
| 3 | * ParsiDate Calendar Block |
| 4 | * |
| 5 | * Register the Gutenberg block version of the calendar widget |
| 6 | */ |
| 7 | |
| 8 | namespace WPParsidate\Block; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use WPParsidate\Core\Calendar; |
| 13 | use WPParsidate\Helper\Assets; |
| 14 | |
| 15 | class CalendarBlock extends Block { |
| 16 | private const STYLE = 'wp-parsidate-calendar-style'; |
| 17 | |
| 18 | protected string $editorScript = 'wp-parsidate-calendar-editor-script'; |
| 19 | |
| 20 | protected string $blockJsonPath = 'blocks/calendar/block.json'; |
| 21 | |
| 22 | protected string $scriptBaseName = 'calendar-block'; |
| 23 | |
| 24 | protected string $dataVar = 'wppCalendarBlockData'; |
| 25 | |
| 26 | public function registerBlock(): void { |
| 27 | parent::registerBlock(); |
| 28 | |
| 29 | wp_register_style( |
| 30 | self::STYLE, |
| 31 | Assets::url( 'css-admin/calendar-wdiget.min.css' ), |
| 32 | array(), |
| 33 | Assets::getVersion() |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | protected function renderContent( $attributes ): string { |
| 38 | $title = $attributes['title'] ?? ''; |
| 39 | $postType = $attributes['postType'] ?? 'post'; |
| 40 | $theme = $attributes['theme'] ?? 'simple'; |
| 41 | $blockId = 'wp-parsidate-calendar-block-' . md5( $postType . $theme ); |
| 42 | |
| 43 | ob_start(); |
| 44 | |
| 45 | echo '<div ' . get_block_wrapper_attributes() . '>'; |
| 46 | |
| 47 | if ( ! empty( $title ) ) { |
| 48 | echo '<h2 class="wp-block-wp-parsidate-calendar__title">' . esc_html( $title ) . '</h2>'; |
| 49 | } |
| 50 | |
| 51 | do_action( 'wp_parsidate_calendar_block_start', $title, $postType, $theme, $blockId ); |
| 52 | Calendar::printCalendar( $postType, $theme ); |
| 53 | do_action( 'wp_parsidate_calendar_block_end', $title, $postType, $theme, $blockId ); |
| 54 | |
| 55 | echo '</div>'; |
| 56 | |
| 57 | return ob_get_clean(); |
| 58 | } |
| 59 | } |
| 60 |