<?php

$relatedPostId = isset($attributes['relatedPostId']) ? $attributes['relatedPostId'] : null;
if (!$relatedPostId) return;

$relatedPost = get_post($relatedPostId) ?? null;
$anchor = isset($attributes['anchor']) && !empty($attributes['anchor']) ? esc_html($attributes['anchor']) : "post-content-container-" .  $attributes['relatedPostId'];

$anchor = isset($attributes['anchor']) && !empty($attributes['anchor']) ? esc_html($attributes['anchor']) : ($relatedPostId ? "post-content-container-" . $relatedPostId : '');
$content = get_the_content(null, null, $relatedPost);

if ($relatedPost) {
	// Temporarily set up the global post variable so the_content works as expected
	global $post;
	$post = $relatedPost;
	setup_postdata($post);

	ob_start();
	the_content();
	$content = ob_get_clean();
	wp_reset_postdata();
}
?>


<section id="<?php echo $anchor  ?>" class="post-content-container <?php echo $anchor ? "has-custom-anchor" : "" ?>" data-related-id="<?php echo $relatedPostId ?>">
	<h2 class="post-content-container__title"><?php echo $relatedPost->post_title ?></h2>
	<?php echo $content; ?>
</section>