<?php
/**
 * Abstract base class for Smart Post Show Blocks.
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/blocks/includes
 */

namespace SmartPostShow\Blocks;

use SmartPostShow\Blocks\Block_Base;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Post Reading Time Block.
 */
class Post_Meta extends Block_Base {

	/**
	 * Block properties
	 */
	protected function set_block_properties() {
		$this->block_name = 'post-meta';
		$this->styles     = array(
			'sp_smart_post_blocks_css',
			'sp_smart_post_blocks_style',
			'sp_smart_post_blocks_google_fonts',
		);
	}

	/**
	 * Render block
	 *
	 * @param array  $attributes Block attributes.
	 * @param string $content Block content.
	 * @param array  $blocks Inner blocks.
	 * @return string        Rendered HTML.
	 */
	public function render_block( $attributes, $content = '', $blocks = array() ) {

		if ( \SmartPostShow\Blocks\Helper::is_editor_page() ) {
			return '<p style="opacity:0.5; margin:0; padding:8px 0;">Author Meta will appear here</p>';
		}

		global $post;
		if ( ! $post || ! isset( $post->post_content ) ) {
			return '';
		}

		$meta_map = array(
			'author'       => array( $this, 'author' ),
			'date'         => array( $this, 'date' ),
			'comments'     => array( $this, 'comment_counter' ),
			'views'        => array( $this, 'view_count' ),
			'likes'        => array( $this, 'likes' ),
			'reading-time' => array( $this, 'reading_time' ),
			'category'     => array( $this, 'category' ),
			'tag'          => array( $this, 'tag' ),
		);

		$unique_id                   = $attributes['uniqueId'] ?? '';
		$align                       = $attributes['align'] ?? '';
		$additional_cls              = $attributes['advancedAdditionalClass'] ?? '';
		$additional_id               = $attributes['advancedAdditionalID'] ?? '';
		$content_global_typography   = $attributes['globalContentTypography'] ?? '';
		$content_global_class        = $content_global_typography['class'] ?? '';
		$meta_data_all_content_array = $attributes['metaDataAllContentArray'] ?? '';
		$align_width                 = '' !== $align ? ' align' . $align : '';

		ob_start();
		?>
		<div class="sp-smart-post-meta-block">
			<div id="<?php echo esc_attr( $additional_id ); ?>">
				<div id="<?php echo esc_attr( $unique_id ); ?>" class="smart-post-meta-block-wrapper sp-smart-post-wrapper <?php echo esc_attr( $additional_cls . ' ' . $align_width ); ?>">
					<div class="smart-post-meta-block-all-meta <?php echo esc_attr( $content_global_class ); ?> ">
						<?php
						if ( is_array( $meta_data_all_content_array ) ) {
							$total = count( $meta_data_all_content_array );
							$index = 0;

							foreach ( $meta_data_all_content_array as $meta ) {
								++$index;
								if ( empty( $meta['show'] ) ) {
									continue;
								}
								$key = $meta['value'];

								if ( isset( $meta_map[ $key ] ) ) {
									//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
									echo call_user_func( $meta_map[ $key ], $attributes, $post ); // No need for escaping as it's coming from our own functions.
								}
								// Add separator if not last item.
								if ( $index < $total ) {
									echo wp_kses_post( $this->get_separator_html( $attributes ) );
								}
							}
						}
						?>
					</div>
				</div>
			</div>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Render the author metadata.
	 *
	 * @param array   $attributes Block attributes passed from editor.
	 * @param WP_Post $post       The post object.
	 *
	 * @return string HTML output for author meta.
	 */
	public function author( $attributes, $post ) {

		$post_id       = $post->ID;
		$author_style  = $attributes['authorStyle'] ?? 'ShowNameGravatar';
		$show_prefix   = $attributes['showAuthorPrefix'] ?? false;
		$prefix_text   = $attributes['prefixLabelText'] ?? '';
		$meta_icon     = $attributes['metaUserIcon'] ?? '';
		$gravatar_size = isset( $attributes['gravatarSize']['value'] ) ? intval( $attributes['gravatarSize']['value'] ) : 40;
		$icon          = Helper::get_icon_list( $meta_icon );

		// Get post author.
		$author_id   = get_post_field( 'post_author', $post_id );
		$author_name = get_the_author_meta( 'display_name', $author_id );
		$avatar_url  = get_avatar_url( $author_id, array( 'size' => $gravatar_size ) );
		ob_start();
		?>
		<div class="sp-smart-post-author-meta-wrapper">

			<?php if ( 'ShowNameIcon' === $author_style ) : ?>
				<span class="sp-smart-post-author-meta-icon">
					<?php
					if ( ! empty( $icon ) ) :
						?>
						<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">


							<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>


						</svg>
					<?php endif ?>
				</span>
				<?php
			endif;
			?>

			<?php if ( 'ShowNameGravatar' === $author_style || 'showGravatar' === $author_style ) : ?>
				<span class="sp-smart-post-author-meta-icon">
					<img src="<?php echo esc_url( $avatar_url ); ?>" alt="<?php echo esc_attr( $author_name ); ?>" class="sp-smart-post-meta-icon" />
				</span>
			<?php endif; ?>

			<?php if ( $show_prefix && 'showGravatar' !== $author_style ) : ?>
				<span class="sp-smart-post-author-meta-prefix">
					<?php echo esc_html( $prefix_text ); ?>
				</span>
			<?php endif; ?>

			<?php if ( 'showGravatar' !== $author_style ) : ?>
				<span class="sp-smart-post-author-meta-author-name">
					<?php echo esc_html( $author_name ); ?>
				</span>
			<?php endif; ?>

		</div>

		<?php
		return ob_get_clean();
	}

	/**
	 * Render the view metadata.
	 *
	 * @param array   $attributes Block attributes passed from editor.
	 * @param WP_Post $post       The post object.
	 *
	 * @return string HTML output for author meta.
	 */
	public function view_count( $attributes, $post ) {

		$show_icon      = $attributes['showViewIcon'] ?? true;
		$icon_source    = $attributes['viewIconSource'] ?? 'classic';
		$label_show     = $attributes['viewLabelShow'] ?? true;
		$label_text     = $attributes['viewLabelText'] ?? 'Reads';
		$label_position = $attributes['viewLabelPosition'] ?? 'after';
		$post_id        = $post->ID;
		$count          = get_post_meta( $post_id, '_post_views_count', true );
		$count          = ! empty( $count ) ? (int) $count : 0;
		$icon           = Helper::get_icon_list( $icon_source );

		ob_start();
		?>
		<div class="smart-post-view-block-wrapper">
			<div class="sp-smart-post-view-count-wrapper">
				<?php
				if ( $show_icon ) :

					if ( ! empty( $icon ) ) :
						?>
						<span class="sp-smart-post-view-count-icon">
							<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">

								<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>

							</svg>
						</span>
						<?php
					endif;
				endif;
				?>

				<?php if ( $label_show && 'before' === $label_position ) : ?>
					<span class="sp-smart-post-view-count-label">
						<?php echo esc_html( $label_text ); ?>
					</span>
				<?php endif; ?>

				<span class="sp-smart-post-view-count-value">
					<?php echo esc_html( $count ); ?>

				</span>

				<?php if ( $label_show && 'after' === $label_position ) : ?>
					<span class="sp-smart-post-view-count-label">
						<?php echo esc_html( $label_text ); ?>
					</span>
				<?php endif; ?>

			</div>
		</div>
		<?php
		return ob_get_clean();
	}
	/**
	 * Render the view metadata.
	 *
	 * @param array   $attributes Block attributes passed from editor.
	 * @param WP_Post $post       The post object.
	 *
	 * @return string HTML output for author meta.
	 */
	public function comment_counter( $attributes, $post ) {

		$show_icon      = $attributes['showCommentsIcon'] ?? true;
		$icon_source    = $attributes['commentsIconSource'] ?? 'classic';
		$label_show     = $attributes['commentsLabelShow'] ?? true;
		$label_text     = $attributes['commentsLabelText'] ?? 'Reads';
		$label_position = $attributes['commentsLabelPosition'] ?? 'after';
		$post_id        = $post->ID;
		$count          = $post_id ? get_comments_number( $post_id ) : '';
		$icon           = Helper::get_icon_list( $icon_source );

		ob_start();
		?>
		<div class="sps-smart-comment-count">
			<?php
			if ( $show_icon ) :
				if ( ! empty( $icon ) ) :
					?>
					<span class="sp-smart-comment-count-icon">
						<?php if ( ! empty( $icon ) ) { ?>
							<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">

								<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>

							</svg>
						<?php } ?>
					</span>
					<?php
				endif;
			endif;
			?>

			<?php if ( $label_show && 'before' === $label_position ) : ?>
				<span class="sp-smart-post-comment-count-label">
					<?php echo esc_html( $label_text ); ?>
				</span>
			<?php endif; ?>

			<span class="sp-smart-post-view-count-value">
				<?php echo esc_html( $count ); ?>
			</span>

			<?php if ( $label_show && 'after' === $label_position ) : ?>
				<span class="sp-smart-post-comment-count-label">
					<?php echo esc_html( $label_text ); ?>
				</span>
			<?php endif; ?>

		</div>

		<?php
		return ob_get_clean();
	}

	/**
	 * Render the view metadata.
	 *
	 * @param array   $attributes Block attributes passed from editor.
	 * @param WP_Post $post       The post object.
	 *
	 * @return string HTML output for author meta.
	 */
	public function reading_time( $attributes, $post ) {

		$show_icon      = $attributes['showReadingIcon'] ?? true;
		$icon_source    = $attributes['readingIconSource'] ?? 'classic';
		$label_show     = $attributes['readingLabelShow'] ?? true;
		$label_text     = $attributes['readingLabelText'] ?? 'Reads';
		$label_position = $attributes['readingLabelPosition'] ?? 'after';
		$minutes        = $this->calculate_reading_time( $post->post_content );
		$icon           = Helper::get_icon_list( $icon_source );

		ob_start();
		?>
		<div class="smart-post-reading-block-wrapper">
			<div class="sp-smart-post-reading-time-wrapper">
				<?php
				if ( $show_icon ) :
					if ( ! empty( $icon ) ) :
						?>
						<span class="sp-smart-post-reading-time-icon">
							<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">

								<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>

							</svg>
						</span>
						<?php
					endif;
				endif;
				?>

				<?php if ( $label_show && 'before' === $label_position ) : ?>
					<span class="sp-smart-post-reading-count-label">
						<?php echo esc_html( $label_text ); ?>
					</span>
				<?php endif; ?>

				<span class="sp-smart-post-reading-time-value">
					<?php echo esc_html( $minutes ); ?> Min

				</span>

				<?php if ( $label_show && 'after' === $label_position ) : ?>
					<span class="sp-smart-post-reading-count-label">
						<?php echo esc_html( $label_text ); ?>
					</span>
				<?php endif; ?>

			</div>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Render the view metadata.
	 *
	 * @param array   $attributes Block attributes passed from editor.
	 * @param WP_Post $post       The post object.
	 *
	 * @return string HTML output for author meta.
	 */
	public function likes( $attributes, $post ) {

		$show_icon      = $attributes['showLikeIcon'] ?? true;
		$icon_source    = $attributes['likeIconSource'] ?? 'classic';
		$label_show     = $attributes['likeLabelShow'] ?? true;
		$label_text     = $attributes['likeLabelText'] ?? 'Reads';
		$label_position = $attributes['likeLabelPosition'] ?? 'after';
		$post_id        = $post->ID;
		$count          = get_post_meta( $post_id, '_post_like_count', true );
		$count          = ! empty( $count ) ? (int) $count : 0;

		$like_options = \SP_PCP_User_Like::get_pcp_likes_block_button( $post_id );
		$button_class = $like_options['button_class'] ?? '';
		$button_nonce = $like_options['nonce'] ?? '';
		$title        = $like_options['title'] ?? '';
		$icon         = Helper::get_icon_list( $icon_source );

		ob_start();
		?>
		<div class="smart-post-like-block-wrapper pcpl-wrapper">
			<div class="sp-smart-post-like-count-wrapper pcpl-button <?php echo esc_attr( $button_class ); ?>" data-nonce="<?php echo esc_attr( $button_nonce ); ?>" title="<?php echo esc_attr( $title ); ?>" data-post-id="<?php echo esc_attr( $post_id ); ?>" data-single="true">
				<?php
				if ( $show_icon ) :
					if ( ! empty( $icon ) ) :
						?>
						<span class="sp-smart-post-like-count-icon">
							<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">

								<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>

							</svg>
						</span>
						<?php
					endif;
				endif;
				?>

				<?php if ( $label_show && 'before' === $label_position ) : ?>
					<span class="sp-smart-post-like-count-label">
						<?php echo esc_html( $label_text ); ?>
					</span>
				<?php endif; ?>

				<span class="sp-smart-post-like-count-value">
					<?php echo esc_html( $count ); ?>

				</span>

				<?php if ( $label_show && 'after' === $label_position ) : ?>
					<span class="sp-smart-post-like-count-label">
						<?php echo esc_html( $label_text ); ?>
					</span>
				<?php endif; ?>

			</div>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Render the category metadata.
	 *
	 * @param array   $attributes Block attributes passed from editor.
	 * @param WP_Post $post       The post object.
	 *
	 * @return string HTML output for author meta.
	 */
	public function category( $attributes, $post ) {

		$category_custom_separator = $attributes['categoryCustomSeparator'] ?? '';
		$enable_category_heading   = $attributes['enableCategoryHeading'] ?? false;
		$category_heading_label    = $attributes['categoryHeadingLabel'] ?? '';
		$enable_category_icon      = $attributes['enableCategoryIcon'] ?? false;
		$category_icon_style       = $attributes['categoryIconStyle'] ?? 'default';
		$category_separator        = $attributes['categorySeparator'] ?? 'none';
		$specific_color            = $attributes['categorySpecificColor'] ?? 'false';
		$categories                = get_the_category( $post->ID );
		$icon                      = Helper::get_icon_list( $category_icon_style );

		if ( empty( $categories ) ) {
			return '';
		}

		/**
		 * Separator Logic
		 */
		if ( 'none' === $category_separator ) {
			$separator_html = '';
		} elseif ( 'custom-separator' === $category_separator ) {
			$separator_html = $category_custom_separator;
		} else {
			$separator_html = $category_separator;
		}

		ob_start();
		?>
		<div class="sp-smart-post-meta-categories-container">

			<?php if ( $enable_category_heading || $enable_category_icon ) : ?>
				<div class="sp-smart-post-meta-categories-label-container">

					<?php if ( $enable_category_icon ) : ?>
						<span class="sp-smart-post-categories-icon">

							<?php if ( ! empty( $icon ) ) : ?>

								<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">

									<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>

								</svg>
							<?php endif; ?>
						</span>
					<?php endif; ?>

					<?php if ( $enable_category_heading ) : ?>
						<span class="sp-smart-post-categories-label">
							<?php echo esc_html( $category_heading_label ); ?>
						</span>
					<?php endif; ?>

				</div>
			<?php endif; ?>

			<div class="sp-smart-post-meta-category-link-container">

				<?php

				if ( ! empty( $categories ) ) :
					$total = count( $categories );
					$i     = 1;

					foreach ( $categories as $cat ) :

						$cat_link      = get_category_link( $cat->term_id );
						$cat_color     = get_term_meta( $cat->term_id, 'category_color', true );
						$cat_style_css = ( $specific_color && $cat_color ) ? 'style="color: ' . esc_attr( $cat_color ) . ';"' : '';
						?>
						<a href="<?php echo esc_url( $cat_link ); ?>"
							class="sp-smart-post-category-link"
							<?php
							// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
							echo $cat_style_css; // escaped earlier.
							?>
							>

							<?php echo esc_html( $cat->name ); ?>
						</a>

						<?php if ( 'none' !== $category_separator && $i < $total ) : ?>
							<div class="sp-smart-post-meta-category-separator">
								<?php echo esc_html( $separator_html ); ?>
							</div>
						<?php endif; ?>

						<?php
						++$i;
					endforeach;
				endif;
				?>

			</div>

		</div>
		<?php

		return ob_get_clean();
	}

	/**
	 * Render the category metadata.
	 *
	 * @param array   $attributes Block attributes passed from editor.
	 * @param WP_Post $post       The post object.
	 *
	 * @return string HTML output for author meta.
	 */
	public function tag( $attributes, $post ) {

		$tag_custom_separator   = $attributes['tagCustomSeparator'] ?? '';
		$enable_tag_heading     = $attributes['enableTagHeading'] ?? false;
		$category_heading_label = $attributes['tagHeadingLabel'] ?? '';
		$enable_tag_icon        = $attributes['enableTagIcon'] ?? false;
		$tag_icon_style         = $attributes['tagIconStyle'] ?? 'default';
		$tag_separator          = $attributes['tagSeparator'] ?? 'none';
		$tags                   = get_the_tags( $post->ID );
		$icon                   = Helper::get_icon_list( $tag_icon_style );

		if ( empty( $tags ) ) {
			return '';
		}

		/**
		 * Separator Logic
		 */
		if ( 'none' === $tag_separator ) {
			$separator_html = '';
		} elseif ( 'custom-separator' === $tag_separator ) {
			$separator_html = $tag_custom_separator;
		} else {
			$separator_html = $tag_separator;
		}

		ob_start();
		?>
		<div class="sp-smart-post-meta-tags-container">

			<?php if ( $enable_tag_heading || $enable_tag_icon ) : ?>
				<div class="sp-smart-post-meta-tags-label-container">

					<?php if ( $enable_tag_icon ) : ?>
						<span class="sp-smart-post-tags-icon">

							<?php if ( ! empty( $icon ) ) : ?>

								<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">

									<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>

								</svg>
							<?php endif; ?>
						</span>
					<?php endif; ?>

					<?php if ( $enable_tag_heading ) : ?>
						<span class="sp-smart-post-tags-label">
							<?php echo esc_html( $category_heading_label ); ?>
						</span>
					<?php endif; ?>

				</div>
			<?php endif; ?>

			<div class="sp-smart-post-meta-tag-link-container">

				<?php

				if ( ! empty( $tags ) ) :
					$total = count( $tags );
					$i     = 1;

					foreach ( $tags as $tag ) :

						$cat_link = get_tag_link( $tag->term_id );
						?>

						<a href="<?php echo esc_url( $cat_link ); ?>" class="sp-smart-post-tag-link">
							<?php echo esc_html( $tag->name ); ?>
						</a>

						<?php if ( 'none' !== $tag_separator && $i < $total ) : ?>
							<div class="sp-smart-post-meta-tag-separator">
								<?php echo esc_html( $separator_html ); ?>
							</div>
						<?php endif; ?>

						<?php
						++$i;
					endforeach;
				endif;
				?>

			</div>

		</div>
		<?php

		return ob_get_clean();
	}

	/**
	 *
	 * Render the date metadata.
	 *
	 * @param array $attributes block.
	 * @return string HTML out
	 */
	public function date( $attributes ) {

		$date_time_display          = $attributes['dateTimeDisplay'] ?? 'publish-date';
		$enable_publish_date_prefix = $attributes['enablePublishDatePrefix'] ?? false;
		$enable_update_date_prefix  = $attributes['enableUpdateDatePrefix'] ?? false;
		$published_prefix_label     = $attributes['publishedPrefixLabel'] ?? '';
		$update_prefix_label        = $attributes['updatePrefixLabel'] ?? '';
		$date_time_format           = $attributes['dateTimeFormat'] ?? 'F j, Y';
		$show_date_icon             = $attributes['showDateIcon'] ?? false;
		$date_icon_style            = $attributes['dateIconStyle'] ?? 'classic';
		$publish_date               = get_the_date( 'c' );
		$modified_date              = get_the_modified_date( 'c' );
		$icon                       = Helper::get_icon_list( $date_icon_style );

		// Pick right date.
		if ( ( 'auto-date-display' === $date_time_display && $publish_date !== $modified_date )
			|| 'update-date' === $date_time_display
		) {

			$date = $modified_date;
		} else {

			$date = $publish_date;
		}

		// Prefix label.
		$date_label = '';

		if ( 'auto-date-display' === $date_time_display && $publish_date !== $modified_date ) {
			if ( $enable_update_date_prefix ) {
				$date_label = $update_prefix_label;
			}
		} elseif ( $enable_publish_date_prefix ) {
			$date_label = $published_prefix_label;
		}
		$formatted_date = $this->sp_get_format_date( $date, $date_time_format );

		ob_start();
		?>
		<div class="sps-smart-post-meta-date-container">
			<div class="sp-smart-post-meta-date-wrapper">

				<?php if ( $show_date_icon ) : ?>
					<span class="sp-smart-post-date-meta-icon">
						<?php if ( ! empty( $icon ) ) : ?>
							<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">
								<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>
							</svg>
						<?php endif; ?>
					</span>
				<?php endif; ?>

				<?php if ( 'publish-date' === $date_time_display && $enable_publish_date_prefix ) : ?>
					<span class="sp-smart-post-date-meta-prefix">
						<?php echo esc_html( $published_prefix_label ); ?>
					</span>
				<?php endif; ?>

				<?php if ( 'update-date' === $date_time_display && $enable_update_date_prefix ) : ?>
					<span class="sp-smart-post-date-meta-prefix">
						<?php echo esc_html( $update_prefix_label ); ?>
					</span>
				<?php endif; ?>

				<?php if ( 'auto-date-display' === $date_time_display && ! empty( $date_label ) ) : ?>
					<span class="sp-smart-post-date-meta-prefix">
						<?php echo esc_html( $date_label ); ?>
					</span>
				<?php endif; ?>

				<span class="sp-smart-post-date-meta-date-time">
					<?php echo esc_html( $formatted_date ); ?>
				</span>

			</div>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Format a timestamp according to block settings or a custom format.
	 *
	 * Supports predefined format identifiers:
	 *  - 'time_ago'        : human-readable relative time (e.g. "2 hours ago")
	 *  - 'date_format'     : uses WordPress date_format option
	 *  - 'date_time_format': uses WordPress date_format + time_format
	 *  - any valid PHP date format string
	 *
	 * @param int|string $date_string Unix timestamp or date string.
	 * @param string     $date_time_format    Format identifier or PHP date format. Default 'date_format'.
	 * @return string Formatted date string.
	 */
	public static function sp_get_format_date( $date_string, $date_time_format ) {

		if ( empty( $date_string ) ) {
			return '';
		}

		// Remove curly quotes or stray characters.
		$date_string = trim( $date_string, " \t\n\r\0\x0B\"\'“”" );

		$timestamp = strtotime( $date_string );

		if ( ! $timestamp ) {
			return '';
		}

		$wp_date_format = get_option( 'date_format' );
		$wp_time_format = get_option( 'time_format' );

		switch ( $date_time_format ) {

			case 'publish-date':
				return date_i18n( 'M j, Y', $timestamp );

			case 'time-only':
				return date_i18n( 'g:i A', $timestamp );

			case 'full-date':
				return date_i18n( 'F j, Y', $timestamp );

			case 'date-time-short':
				return date_i18n( 'M j, Y g:i A', $timestamp );

			case 'date-time-long':
				return date_i18n( 'F j, Y g:i A', $timestamp );

			case 'day-month-year':
				return date_i18n( 'j M Y', $timestamp );

			case 'day-month-year-time':
				return date_i18n( 'j M Y g:i A', $timestamp );

			case 'time-ago':
				return human_time_diff( $timestamp, current_time( 'timestamp' ) ) . ' ago';

			case 'wp-default-date':
				return date_i18n( $wp_date_format, $timestamp );

			case 'wp-default-datetime':
				return date_i18n( $wp_date_format . ' ' . $wp_time_format, $timestamp );

			default:
				return date_i18n( 'F j, Y', $timestamp );
		}
	}

	/**
	 * Render separator html
	 *
	 * @param array $attributes of the separator.
	 */
	private function get_separator_html( $attributes ) {
		$type   = $attributes['metaSeparator'] ?? 'none';
		$custom = $attributes['metaCustomSeparator'] ?? '';

		if ( 'none' === $type ) {
			return '';
		}
		if ( 'custom' === $type ) {
			return '<span class="sp-smart-post-meta-separator">' . esc_html( $custom ) . '</span>';
		}

		$list = array(
			'normal-space' => '&nbsp;',
			'full-stop'    => '.',
			'straight'     => '|',
			'slash'        => '/',
			'doubleSlash'  => '//',
			'backSlash'    => '\\',
			'dash'         => '-',
		);

		return '<span class="sp-smart-post-meta-separator">' . $list[ $type ] . '</span>';
	}

	/**
	 * Calculate reading time
	 *
	 * @param string $post_content The post content.
	 */
	private function calculate_reading_time( $post_content ) {
		$content_text = wp_strip_all_tags( $post_content );
		$word_count   = str_word_count( $content_text );

		$words_per_minute = 200;
		$minutes          = ceil( $word_count / $words_per_minute );
		return max( 1, $minutes );
	}
}
