From f9f5eeabd9ba836c2cb1b32fb597b52faf993c32 Mon Sep 17 00:00:00 2001 From: Hasmukh K Mistry Date: Mon, 28 Oct 2024 17:06:00 +0530 Subject: [PATCH] calendar-plus: WIP --- includes/class-calendar-plus-theme-compat.php | 58 ++++++++++++++++--- .../class-calendar-plus-event-shortcode.php | 6 +- .../class-calendar-plus-template-loader.php | 1 - .../shortcodes/event-single.compat.php | 5 +- public/templates/shortcodes/event-single.php | 5 +- 5 files changed, 59 insertions(+), 16 deletions(-) diff --git a/includes/class-calendar-plus-theme-compat.php b/includes/class-calendar-plus-theme-compat.php index c6b83e5..c7572a1 100644 --- a/includes/class-calendar-plus-theme-compat.php +++ b/includes/class-calendar-plus-theme-compat.php @@ -104,6 +104,15 @@ class Calendar_Plus_Theme_Compat { public function __construct() { add_filter( 'template_include', array( $this, 'maybe_replace_content' ), 1 ); + add_action( 'init', function() { + register_block_pattern( + 'calendar-plus/event-single', + array( + 'title' => __( 'Calendar Event Pattern', 'calendar-plus' ), + 'content' => '', + ) + ); + } ); } function maybe_replace_content( $template ) { @@ -118,11 +127,46 @@ function maybe_replace_content( $template ) { if( $source === 'calendar_plus' ) { return $template; } - $event = calendarp_get_event( get_the_ID() ); - $event_post = $event->post; - $event_post->post_content = $shortcodes['single-event']->render_compat( array( 'event_id' => get_the_ID() ) ); - - calendar_plus_reset_post( (array) $event_post ); +// // Solution i am proposing +// add_filter( 'the_content', function( $content ) use ( $shortcodes ) { +// static $cplus_rendered = false; + +// // If already run, return the content unchanged +// if ( $cplus_rendered ) { +// return $content; +// } + +// $cplus_rendered = true; + +// return $shortcodes['single-event']->render_compat( array( 'event_id' => get_the_ID() ) ); +// }, 1, 1 ); + if ( wp_is_block_theme() ) { + add_filter( 'render_block', function( $block_content, $block ) use ( $shortcodes ) { + if ( is_singular( 'calendar_event' ) && $block['blockName'] === 'core/post-content' ) { + // Get the block patterns registry. + $patterns_registry = \WP_Block_Patterns_Registry::get_instance(); + + // Retrieve the block pattern by its name. + $block_pattern = $patterns_registry->get_registered( 'calendar-plus/event-single' ); + + // If the block pattern exists, replace the content. + if ( $block_pattern ) { + $new_content = $shortcodes['single-event']->render_compat( array( 'event_id' => get_the_ID() ) ); // Get the content from the block pattern. + + // Wrap it inside the post-content wrapper to preserve structure. + return '
' . $new_content . '
'; + } + } + + return $block_content; + }, 10, 2 ); + } else { + $event = calendarp_get_event( get_the_ID() ); + $event_post = $event->post; + $event_post->post_content = $shortcodes['single-event']->render_compat( array( 'event_id' => get_the_ID() ) ); + + calendar_plus_reset_post( (array) $event_post ); + } // This small hack allows avoiding duplications of meta add_filter( 'cpschool_post_meta_items', function( $meta, $post_id ) { @@ -178,8 +222,8 @@ function maybe_replace_content( $template ) { } } - if( is_singular( 'calendar_event' ) || is_archive() ) { - return locate_template( array( 'single.php', 'page.php' ) ); + if( ! wp_is_block_theme() && ( is_singular( 'calendar_event' ) || is_archive() ) ) { + return locate_template( array( 'single.php', 'post.php', 'page.php', 'index.php' ) ); } return $template; diff --git a/includes/shortcodes/class-calendar-plus-event-shortcode.php b/includes/shortcodes/class-calendar-plus-event-shortcode.php index 46b3b1f..44738b3 100644 --- a/includes/shortcodes/class-calendar-plus-event-shortcode.php +++ b/includes/shortcodes/class-calendar-plus-event-shortcode.php @@ -38,7 +38,7 @@ public function render( $atts ) { * @return string Shortcode html */ public function render_compat( $atts ) { - return $this->render_attributes( $atts, 'shortcodes/event-single.plain.php' ); + return $this->render_attributes( $atts, 'shortcodes/event-single.compat.php' ); } /** @@ -49,9 +49,7 @@ public function render_compat( $atts ) { * @return string Shortcode output */ private function render_attributes( $atts, $template ) { - if( empty( $atts['event_id'] ) ) { - $event_id = get_the_ID(); - } + $event_id = $atts['event_id'] ?? get_the_ID(); if ( ! $event = calendarp_get_event( $event_id ) ) { return calendarp_is_rest_api_request() diff --git a/public/class-calendar-plus-template-loader.php b/public/class-calendar-plus-template-loader.php index 215135c..45046d7 100644 --- a/public/class-calendar-plus-template-loader.php +++ b/public/class-calendar-plus-template-loader.php @@ -14,7 +14,6 @@ public function load_template( $template ) { $file = ''; if ( is_single() && get_post_type() == 'calendar_event' ) { - $source = calendarp_get_setting( 'single_event_template_source' ); if( $source !== 'calendar_plus' ) { return $template; diff --git a/public/templates/shortcodes/event-single.compat.php b/public/templates/shortcodes/event-single.compat.php index ba5685f..bcbff21 100644 --- a/public/templates/shortcodes/event-single.compat.php +++ b/public/templates/shortcodes/event-single.compat.php @@ -24,8 +24,9 @@ ID ); ?> -
- ID ); ?> +
+ : + ID ); ?>
diff --git a/public/templates/shortcodes/event-single.php b/public/templates/shortcodes/event-single.php index 4379033..6e1606e 100644 --- a/public/templates/shortcodes/event-single.php +++ b/public/templates/shortcodes/event-single.php @@ -31,8 +31,9 @@ ID ); ?> -
- ID ); ?> +
+ : + ID ); ?>