Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions includes/class-calendar-plus-theme-compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 '<div class="entry-content wp-block-post-content calendar-event-content">' . $new_content . '</div>';
}
}

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 ) {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions includes/shortcodes/class-calendar-plus-event-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

/**
Expand All @@ -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()
Expand Down
1 change: 0 additions & 1 deletion public/class-calendar-plus-template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions public/templates/shortcodes/event-single.compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
<?php calendarp_event_categories_list( $event->ID ); ?>
</div>

<div class="event-meta-item event-ical" style="overflow: hidden">
<?php calendarp_event_ical_file_button( $event->ID ); ?>
<div class="event-meta-item event-calendars" style="overflow: hidden">
<?php _e( 'Add to', 'calendar-plus' ); ?>:
<?php calendarp_event_add_to_calendars_links( $event->ID ); ?>
</div>
</div>
</div>
Expand Down
5 changes: 3 additions & 2 deletions public/templates/shortcodes/event-single.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
<?php calendarp_event_categories_list( $event->ID ); ?>
</div>

<div class="event-meta-item event-ical">
<?php calendarp_event_ical_file_button( $event->ID ); ?>
<div class="event-meta-item event-calendars" style="overflow: hidden">
<?php _e( 'Add to', 'calendar-plus' ); ?>:
<?php calendarp_event_add_to_calendars_links( $event->ID ); ?>
</div>
</div>
</div>
Expand Down