<?php
namespace Vidplex;

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Helpers
 *
 * Provides utility functions and common helper methods used
 * across the plugin.
 *
 * Includes functions for formatting, data manipulation, and
 * other reusable operations.
 */
class Helpers {



	/**
	 * The single instance of the Init class.
	 *
	 * This static variable holds the single instance of the class and ensures
	 * that only one instance of the class is created (Singleton pattern).
	 *
	 * @var Init|null The single instance of the Init class or null if not instantiated.
	 */
	private static $instance = null;

	/**
	 * Retrieves the single instance of the Init class.
	 *
	 * This method implements the Singleton pattern, ensuring that only
	 * one instance of the Init class is created and used throughout
	 * the application. If the instance doesn't exist yet, it will be created.
	 *
	 * @return Init The single instance of the Init class.
	 */
	public static function get_instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Filters a list of videos based on provided arguments.
	 *
	 * Applies filtering criteria such as channel, status, or date
	 * to return a subset of the video list.
	 *
	 * @param array $videos List of video records to filter.
	 * @param array $args   Optional associative array of filter parameters.
	 * @return array Filtered list of videos.
	 */
	public static function video_filters($videos, $args = array(
		'order'   => 'desc',
		'orderBy' => 'date',
		'exclude' => array(),
	)) {
		$order    = strtolower( $args['order'] ?? 'desc' );
		$order_by = strtolower( $args['orderBy'] ?? 'date' );

		if ( empty( $videos ) || ! is_array( $videos ) ) {
			return array();
		}

		usort(
			$videos,
			function ( $a, $b ) use ( $order_by, $order ) {
				// Determine the key to sort.
				$key1 = 'title' === $order_by ? $a['video_title'] : $a['published_at'];
				$key2 = 'title' === $order_by ? $b['video_title'] : $b['published_at'];

				// If sorting by date, convert to timestamp.
				if ( 'date' === $order_by ) {
					$key1 = strtotime( $key1 );
					$key2 = strtotime( $key2 );
				}

				if ( $key1 === $key2 ) {
					return 0;
				}

				if ( 'asc' === $order ) {
					return ( $key1 < $key2 ) ? -1 : 1;
				} else {
					return ( $key1 > $key2 ) ? -1 : 1;
				}
			}
		);

		if ( ! empty( $args['exclude'] ) ) {
			$exclude = $args['exclude'];
			$videos  = array_filter(
				$videos,
				function ( $video ) use ( $exclude ) {
					return ! in_array( $video['video_id'], $exclude, true );
				}
			);
		}

		return $videos;
	}

	/**
	 * Renders a video grid or carousel.
	 *
	 * Generates the HTML output for displaying videos in either a
	 * grid or carousel layout based on the specified type and attributes.
	 *
	 * @param string $type       Layout type ('grid' or 'carousel').
	 * @param array  $videos     List of video records to display.
	 * @param array  $attributes Additional rendering options (e.g., columns, autoplay).
	 * @return void Outputs the rendered grid or carousel directly.
	 */
	public static function grid_carousel_render( $type, $videos, $attributes ) {
		$attributes = (array) $attributes;

		switch ( $type ) {
			case 'custom':
				ob_start();
				if ( ( 'default' === $attributes['layout'] && 'grid' === $attributes['displayType'] ) || 'list' === $attributes['layout'] ) {
					foreach ( $videos as $video ) {
						$classes   = array();
						$classes[] = 'grid__item';
						$classes[] = 'layout__item';
						$classes[] = 'frame-align__' . $attributes['videoStyles']['align'];
						$classes[] = isset( $attributes['itemStyles']['shadow']['enabled'] ) && filter_var( $attributes['itemStyles']['shadow']['enabled'], FILTER_VALIDATE_BOOLEAN ) ? 'has-box-shadow' : '';
						$classes[] = isset( $attributes['itemStyles']['shadowHover']['enabled'] ) && filter_var( $attributes['itemStyles']['shadowHover']['enabled'], FILTER_VALIDATE_BOOLEAN ) ? 'has-hover-box-shadow' : '';
						?>
						<div class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', array_values( $classes ) ) ) ); ?>" data-video-id="<?php echo esc_attr( $video['id'] ); ?>">
							<?php
							if ( ! vidplex_premium_access() || ( isset( $attributes['popup']['enabled'] ) && ! $attributes['popup']['enabled'] ) ) {
								?>
								<div class="iframe-loader">
								</div>
								<?php
							}

							if ( vidplex_premium_access() && isset( $attributes['popup']['enabled'] ) && $attributes['popup']['enabled'] ) {
								$thumbnail_url = 'https://img.youtube.com/vi/' . $video['id'] . '/hqdefault.jpg';
								switch ( $attributes['videoStyles']['resolution'] ) {
									case 'default':
										$thumbnail_url = 'https://img.youtube.com/vi/' . $video['id'] . '/default.jpg';
										break;

									case 'max':
										$thumbnail_url = 'https://img.youtube.com/vi/' . $video['id'] . '/maxresdefault.jpg';
										break;

									default:
										break;
								}

								?>
								<figure class="video__thumbnail">
									<img src="<?php echo esc_url( $thumbnail_url ); ?>" />
									<i class="play__icon">
										<svg fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 330 330">
											<path d="M37.728,328.12c2.266,1.256,4.77,1.88,7.272,1.88c2.763,0,5.522-0.763,7.95-2.28l240-149.999
													c4.386-2.741,7.05-7.548,7.05-12.72c0-5.172-2.664-9.979-7.05-12.72L52.95,2.28c-4.625-2.891-10.453-3.043-15.222-0.4
													C32.959,4.524,30,9.547,30,15v300C30,320.453,32.959,325.476,37.728,328.12z" />
										</svg>
									</i>
									<span class="thumbnail__overlay"></span>
								</figure>
								<?php
							}
							?>
						</div>
						<?php
					}
				}

				if ( 'default' === $attributes['layout'] && 'carousel' === $attributes['displayType'] ) {
					?>
					<ul class="swiper-wrapper">
						<?php
						foreach ( $videos as $video ) {
							$classes   = array();
							$classes[] = 'swiper-slide';
							$classes[] = 'slide__item';
							$classes[] = 'layout__item';
							$classes[] = 'frame-align__' . $attributes['videoStyles']['align'];
							$classes[] = isset( $attributes['itemStyles']['shadow']['enabled'] ) && filter_var( $attributes['itemStyles']['shadow']['enabled'], FILTER_VALIDATE_BOOLEAN ) ? 'has-box-shadow' : '';
							$classes[] = isset( $attributes['itemStyles']['shadowHover']['enabled'] ) && filter_var( $attributes['itemStyles']['shadowHover']['enabled'], FILTER_VALIDATE_BOOLEAN ) ? 'has-hover-box-shadow' : '';
							?>
							<li class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', array_values( $classes ) ) ) ); ?>" data-video-id="<?php echo esc_attr( $video['id'] ); ?>">
								<?php
								if ( ! vidplex_premium_access() || ( isset( $attributes['popup']['enabled'] ) && ! $attributes['popup']['enabled'] ) ) {
									?>
									<div class="iframe-loader">
									</div>
									<?php
								}

								if ( vidplex_premium_access() && isset( $attributes['popup']['enabled'] ) && $attributes['popup']['enabled'] ) {
									$thumbnail_url = 'https://img.youtube.com/vi/' . $video['id'] . '/hqdefault.jpg';
									switch ( $attributes['videoStyles']['resolution'] ) {
										case 'default':
											$thumbnail_url = 'https://img.youtube.com/vi/' . $video['id'] . '/default.jpg';
											break;

										case 'max':
											$thumbnail_url = 'https://img.youtube.com/vi/' . $video['id'] . '/maxresdefault.jpg';
											break;

										default:
											break;
									}
									?>
									<figure class="video__thumbnail">
										<img src="<?php echo esc_url( $thumbnail_url ); ?>" />
										<i class="play__icon">
											<svg fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 330 330">
												<path d="M37.728,328.12c2.266,1.256,4.77,1.88,7.272,1.88c2.763,0,5.522-0.763,7.95-2.28l240-149.999
													c4.386-2.741,7.05-7.548,7.05-12.72c0-5.172-2.664-9.979-7.05-12.72L52.95,2.28c-4.625-2.891-10.453-3.043-15.222-0.4
													C32.959,4.524,30,9.547,30,15v300C30,320.453,32.959,325.476,37.728,328.12z" />
											</svg>
										</i>
										<span class="thumbnail__overlay"></span>
									</figure>
									<?php
								}
								?>
							</li>
							<?php
						}
						?>
					</ul>
					<?php
				}

				return ob_get_clean();

			case 'channel':
				ob_start();
				if ( ( 'default' === $attributes['layout'] && 'grid' === $attributes['displayType'] ) || 'list' === $attributes['layout'] ) {
					foreach ( $videos as $video ) {
						$meta = json_decode( $video['statistics'], true );

						$classes   = array();
						$classes[] = 'grid__item';
						$classes[] = 'layout__item';
						$classes[] = 'frame-align__' . $attributes['videoStyles']['align'];
						$classes[] = isset( $attributes['itemStyles']['shadow']['enabled'] ) && filter_var( $attributes['itemStyles']['shadow']['enabled'], FILTER_VALIDATE_BOOLEAN ) ? 'has-box-shadow' : '';
						$classes[] = isset( $attributes['itemStyles']['shadowHover']['enabled'] ) && filter_var( $attributes['itemStyles']['shadowHover']['enabled'], FILTER_VALIDATE_BOOLEAN ) ? 'has-hover-box-shadow' : '';
						?>
						<div class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', array_values( $classes ) ) ) ); ?>" data-video-id="<?php echo esc_attr( $video['video_id'] ); ?>">
							<?php
							if ( ! vidplex_premium_access() || ( isset( $attributes['popup']['enabled'] ) && ! $attributes['popup']['enabled'] ) ) {
								?>
								<div class="iframe-loader">
								</div>
								<?php
							}

							if ( vidplex_premium_access() && isset( $attributes['popup']['enabled'] ) && $attributes['popup']['enabled'] ) {
								$thumbnail_url = 'https://img.youtube.com/vi/' . $video['video_id'] . '/hqdefault.jpg';
								switch ( $attributes['videoStyles']['resolution'] ) {
									case 'default':
										$thumbnail_url = 'https://img.youtube.com/vi/' . $video['video_id'] . '/default.jpg';
										break;

									case 'max':
										$thumbnail_url = 'https://img.youtube.com/vi/' . $video['video_id'] . '/maxresdefault.jpg';
										break;

									default:
										break;
								}

								?>
								<figure class="video__thumbnail">
									<img src="<?php echo esc_url( $thumbnail_url ); ?>" />
									<i class="play__icon">
										<svg fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 330 330">
											<path d="M37.728,328.12c2.266,1.256,4.77,1.88,7.272,1.88c2.763,0,5.522-0.763,7.95-2.28l240-149.999
													c4.386-2.741,7.05-7.548,7.05-12.72c0-5.172-2.664-9.979-7.05-12.72L52.95,2.28c-4.625-2.891-10.453-3.043-15.222-0.4
													C32.959,4.524,30,9.547,30,15v300C30,320.453,32.959,325.476,37.728,328.12z" />
										</svg>
									</i>
									<span class="thumbnail__overlay"></span>
								</figure>
								<?php
							}

							if ( isset( $attributes['content']['title'] ) && filter_var( $attributes['content']['title'], FILTER_VALIDATE_BOOLEAN ) ) {
								$allowed_tags = array(
									'h1',
									'h2',
									'h3',
									'h4',
									'h5',
									'h6',
								);

								$title_tag = isset( $attributes['content']['titleTag'] ) && in_array( $attributes['content']['titleTag'], $allowed_tags, true ) ? $attributes['content']['titleTag'] : 'h3';

								printf( '<%1$s class="video__title">%2$s</%1$s>', esc_attr( $title_tag ), esc_html( $video['video_title'] ) );
							}

							if ( vidplex_premium_access() && isset( $attributes['content']['meta'] ) && filter_var( $attributes['content']['meta'] ) && ( intval( $meta['likeCount'] ) > 0 || intval( $meta['viewCount'] ) > 0 || intval( $meta['commentCount'] ) > 0 ) ) {
								?>
								<ul class="video__meta">
									<?php
									if ( intval( $meta['likeCount'] ) > 0 ) {
										?>
										<li class="meta">
											<i>
												<svg
													width="24"
													height="24"
													viewBox="0 0 24 24"
													fill="none"
													xmlns="http://www.w3.org/2000/svg">
													<path
														d="M7 11L11 2C11.7956 2 12.5587 2.31607 13.1213 2.87868C13.6839 3.44129 14 4.20435 14 5V9H19.66C19.9499 8.99672 20.2371 9.0565 20.5016 9.17522C20.7661 9.29393 21.0016 9.46873 21.1919 9.68751C21.3821 9.90629 21.5225 10.1638 21.6033 10.4423C21.6842 10.7207 21.7035 11.0134 21.66 11.3L20.28 20.3C20.2077 20.7769 19.9654 21.2116 19.5979 21.524C19.2304 21.8364 18.7623 22.0055 18.28 22H7M7 11V22M7 11H4C3.46957 11 2.96086 11.2107 2.58579 11.5858C2.21071 11.9609 2 12.4696 2 13V20C2 20.5304 2.21071 21.0391 2.58579 21.4142C2.96086 21.7893 3.46957 22 4 22H7"
														stroke="currentColor"
														stroke-width="1.25"
														stroke-linecap="round"
														stroke-linejoin="round" />
												</svg>
											</i>
											<?php echo esc_html( vidplex_format_number( $meta['likeCount'] ) ); ?>
										</li>
										<?php
									}

									if ( intval( $meta['viewCount'] ) > 0 ) {
										?>
										<li class="meta">
											<i>
												<svg
													width="24"
													height="24"
													viewBox="0 0 24 24"
													fill="none"
													xmlns="http://www.w3.org/2000/svg">
													<path
														d="M1 12C1 12 5 4 12 4C19 4 23 12 23 12C23 12 19 20 12 20C5 20 1 12 1 12Z"
														stroke="currentColor"
														stroke-width="1.25"
														stroke-linecap="round"
														stroke-linejoin="round" />
													<path
														d="M12 15C13.6569 15 15 13.6569 15 12C15 10.3431 13.6569 9 12 9C10.3431 9 9 10.3431 9 12C9 13.6569 10.3431 15 12 15Z"
														stroke="currentColor"
														stroke-width="1.25"
														stroke-linecap="round"
														stroke-linejoin="round" />
												</svg>
											</i>
											<?php echo esc_html( vidplex_format_number( $meta['viewCount'] ) ); ?>
										</li>
										<?php
									}

									if ( intval( $meta['commentCount'] ) > 0 ) {
										?>
										<li class="meta">
											<i>
												<svg
													width="24"
													height="24"
													viewBox="0 0 24 24"
													fill="none"
													xmlns="http://www.w3.org/2000/svg">
													<path
														d="M21 15C21 15.5304 20.7893 16.0391 20.4142 16.4142C20.0391 16.7893 19.5304 17 19 17H7L3 21V5C3 4.46957 3.21071 3.96086 3.58579 3.58579C3.96086 3.21071 4.46957 3 5 3H19C19.5304 3 20.0391 3.21071 20.4142 3.58579C20.7893 3.96086 21 4.46957 21 5V15Z"
														stroke="currentColor"
														stroke-width="1.25"
														stroke-linecap="round"
														stroke-linejoin="round" />
												</svg>
											</i>
											<?php echo esc_html( vidplex_format_number( $meta['commentCount'] ) ); ?>
										</li>
										<?php
									}
									?>
								</ul>
								<?php
							}

							if ( isset( $attributes['content']['description'] ) && filter_var( $attributes['content']['description'], FILTER_VALIDATE_BOOLEAN ) ) {
								?>
								<p class="video__description"><?php echo esc_html( vidplex_create_youtube_excerpt( $video['video_description'], $attributes['content']['excerpt'] ) ); ?></p>
								<?php
							}
							?>
						</div>
						<?php
					}
				}

				if ( 'default' === $attributes['layout'] && 'carousel' === $attributes['displayType'] ) {
					?>
					<ul class="swiper-wrapper">
						<?php
						foreach ( $videos as $video ) {
							$meta = json_decode( $video['statistics'], true );

							$classes   = array();
							$classes[] = 'swiper-slide';
							$classes[] = 'slide__item';
							$classes[] = 'layout__item';
							$classes[] = 'frame-align__' . $attributes['videoStyles']['align'];
							$classes[] = isset( $attributes['itemStyles']['shadow']['enabled'] ) && filter_var( $attributes['itemStyles']['shadow']['enabled'], FILTER_VALIDATE_BOOLEAN ) ? 'has-box-shadow' : '';
							$classes[] = isset( $attributes['itemStyles']['shadowHover']['enabled'] ) && filter_var( $attributes['itemStyles']['shadowHover']['enabled'], FILTER_VALIDATE_BOOLEAN ) ? 'has-hover-box-shadow' : '';
							?>
							<li class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', array_values( $classes ) ) ) ); ?>" data-video-id="<?php echo esc_attr( $video['video_id'] ); ?>">
								<?php
								if ( ! vidplex_premium_access() || ( isset( $attributes['popup']['enabled'] ) && ! $attributes['popup']['enabled'] ) ) {
									?>
									<div class="iframe-loader">
									</div>
									<?php
								}

								if ( vidplex_premium_access() && isset( $attributes['popup']['enabled'] ) && $attributes['popup']['enabled'] ) {
									$thumbnail_url = 'https://img.youtube.com/vi/' . $video['video_id'] . '/hqdefault.jpg';
									switch ( $attributes['videoStyles']['resolution'] ) {
										case 'default':
											$thumbnail_url = 'https://img.youtube.com/vi/' . $video['video_id'] . '/default.jpg';
											break;

										case 'max':
											$thumbnail_url = 'https://img.youtube.com/vi/' . $video['video_id'] . '/maxresdefault.jpg';
											break;

										default:
											break;
									}
									?>
									<figure class="video__thumbnail">
										<img src="<?php echo esc_url( $thumbnail_url ); ?>" />
										<i class="play__icon">
											<svg fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 330 330">
												<path d="M37.728,328.12c2.266,1.256,4.77,1.88,7.272,1.88c2.763,0,5.522-0.763,7.95-2.28l240-149.999
													c4.386-2.741,7.05-7.548,7.05-12.72c0-5.172-2.664-9.979-7.05-12.72L52.95,2.28c-4.625-2.891-10.453-3.043-15.222-0.4
													C32.959,4.524,30,9.547,30,15v300C30,320.453,32.959,325.476,37.728,328.12z" />
											</svg>
										</i>
										<span class="thumbnail__overlay"></span>
									</figure>
									<?php
								}

								if ( isset( $attributes['content']['title'] ) && filter_var( $attributes['content']['title'], FILTER_VALIDATE_BOOLEAN ) ) {
									$allowed_tags = array(
										'h1',
										'h2',
										'h3',
										'h4',
										'h5',
										'h6',
									);

									$title_tag = isset( $attributes['content']['titleTag'] ) && in_array( $attributes['content']['titleTag'], $allowed_tags, true ) ? $attributes['content']['titleTag'] : 'h3';

									printf( '<%1$s class="video__title">%2$s</%1$s>', esc_attr( $title_tag ), esc_html( $video['video_title'] ) );
								}

								if ( vidplex_premium_access() && isset( $attributes['content']['meta'] ) && filter_var( $attributes['content']['meta'] ) && ( intval( $meta['likeCount'] ) > 0 || intval( $meta['viewCount'] ) > 0 || intval( $meta['commentCount'] ) > 0 ) ) {
									?>
									<ul class="video__meta">
										<?php
										if ( intval( $meta['likeCount'] ) > 0 ) {
											?>
											<li class="meta">
												<i>
													<svg
														width="24"
														height="24"
														viewBox="0 0 24 24"
														fill="none"
														xmlns="http://www.w3.org/2000/svg">
														<path
															d="M7 11L11 2C11.7956 2 12.5587 2.31607 13.1213 2.87868C13.6839 3.44129 14 4.20435 14 5V9H19.66C19.9499 8.99672 20.2371 9.0565 20.5016 9.17522C20.7661 9.29393 21.0016 9.46873 21.1919 9.68751C21.3821 9.90629 21.5225 10.1638 21.6033 10.4423C21.6842 10.7207 21.7035 11.0134 21.66 11.3L20.28 20.3C20.2077 20.7769 19.9654 21.2116 19.5979 21.524C19.2304 21.8364 18.7623 22.0055 18.28 22H7M7 11V22M7 11H4C3.46957 11 2.96086 11.2107 2.58579 11.5858C2.21071 11.9609 2 12.4696 2 13V20C2 20.5304 2.21071 21.0391 2.58579 21.4142C2.96086 21.7893 3.46957 22 4 22H7"
															stroke="currentColor"
															stroke-width="1.25"
															stroke-linecap="round"
															stroke-linejoin="round" />
													</svg>
												</i>
												<?php echo esc_html( vidplex_format_number( $meta['likeCount'] ) ); ?>
											</li>
											<?php
										}

										if ( intval( $meta['viewCount'] ) > 0 ) {
											?>
											<li class="meta">
												<i>
													<svg
														width="24"
														height="24"
														viewBox="0 0 24 24"
														fill="none"
														xmlns="http://www.w3.org/2000/svg">
														<path
															d="M1 12C1 12 5 4 12 4C19 4 23 12 23 12C23 12 19 20 12 20C5 20 1 12 1 12Z"
															stroke="currentColor"
															stroke-width="1.25"
															stroke-linecap="round"
															stroke-linejoin="round" />
														<path
															d="M12 15C13.6569 15 15 13.6569 15 12C15 10.3431 13.6569 9 12 9C10.3431 9 9 10.3431 9 12C9 13.6569 10.3431 15 12 15Z"
															stroke="currentColor"
															stroke-width="1.25"
															stroke-linecap="round"
															stroke-linejoin="round" />
													</svg>
												</i>
												<?php echo esc_html( vidplex_format_number( $meta['viewCount'] ) ); ?>
											</li>
											<?php
										}

										if ( intval( $meta['commentCount'] ) > 0 ) {
											?>
											<li class="meta">
												<i>
													<svg
														width="24"
														height="24"
														viewBox="0 0 24 24"
														fill="none"
														xmlns="http://www.w3.org/2000/svg">
														<path
															d="M21 15C21 15.5304 20.7893 16.0391 20.4142 16.4142C20.0391 16.7893 19.5304 17 19 17H7L3 21V5C3 4.46957 3.21071 3.96086 3.58579 3.58579C3.96086 3.21071 4.46957 3 5 3H19C19.5304 3 20.0391 3.21071 20.4142 3.58579C20.7893 3.96086 21 4.46957 21 5V15Z"
															stroke="currentColor"
															stroke-width="1.25"
															stroke-linecap="round"
															stroke-linejoin="round" />
													</svg>
												</i>
												<?php echo esc_html( vidplex_format_number( $meta['commentCount'] ) ); ?>
											</li>
											<?php
										}
										?>
									</ul>
									<?php
								}

								if ( isset( $attributes['content']['description'] ) && filter_var( $attributes['content']['description'], FILTER_VALIDATE_BOOLEAN ) ) {
									?>
									<p class="video__description"><?php echo esc_html( vidplex_create_youtube_excerpt( $video['video_description'], $attributes['content']['excerpt'] ) ); ?></p>
									<?php
								}
								?>
							</li>
							<?php
						}
						?>
					</ul>
					<?php
				}

				return ob_get_clean();

			case 'gallery':
				ob_start();
				if ( 'custom' === $attributes['videoSource'] ) {
					foreach ( $videos as $index => $video ) {
						$thumbnail_url = 'https://img.youtube.com/vi/' . $video['id'] . '/hqdefault.jpg';
						switch ( $attributes['videoStyles']['resolution'] ) {
							case 'default':
								$thumbnail_url = 'https://img.youtube.com/vi/' . $video['id'] . '/default.jpg';
								break;

							case 'max':
								$thumbnail_url = 'https://img.youtube.com/vi/' . $video['id'] . '/maxresdefault.jpg';
								break;

							default:
								break;
						}

						$classes   = array();
						$classes[] = 'video__item';
						$classes[] = 0 === $index ? 'is-active' : '';
						?>
						<li class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', array_values( $classes ) ) ) ); ?>" data-video-id="<?php echo esc_attr( $video['id'] ); ?>">
							<figure class="video__thumbnail">
								<img src="<?php echo esc_url( $thumbnail_url ); ?>" />
								<i class="yt-play-icon">
									<svg
										height="100%"
										version="1.1"
										viewBox="0 0 68 48"
										width="100%">
										<path
											class="ytp-large-play-button-bg"
											d="M66.52,7.74c-0.78-2.93-2.49-5.41-5.42-6.19C55.79,.13,34,0,34,0S12.21,.13,6.9,1.55 C3.97,2.33,2.27,4.81,1.48,7.74C0.06,13.05,0,24,0,24s0.06,10.95,1.48,16.26c0.78,2.93,2.49,5.41,5.42,6.19 C12.21,47.87,34,48,34,48s21.79-0.13,27.1-1.55c2.93-0.78,4.64-3.26,5.42-6.19C67.94,34.95,68,24,68,24S67.94,13.05,66.52,7.74z"
											fill="#f03"></path>
										<path
											d="M 45,24 27,14 27,34"
											fill="#fff"></path>
									</svg>
								</i>
							</figure>
						</li>
						<?php
					}
				} elseif ( 'channel' === $attributes['videoSource'] ) {
					foreach ( $videos as $index => $video ) {
						$meta = json_decode( $video['statistics'], true );

						$thumbnail_url = 'https://img.youtube.com/vi/' . $video['video_id'] . '/hqdefault.jpg';
						switch ( $attributes['videoStyles']['resolution'] ) {
							case 'default':
								$thumbnail_url = 'https://img.youtube.com/vi/' . $video['video_id'] . '/default.jpg';
								break;

							case 'max':
								$thumbnail_url = 'https://img.youtube.com/vi/' . $video['video_id'] . '/maxresdefault.jpg';
								break;

							default:
								break;
						}

						$classes   = array();
						$classes[] = 'video__item';
						$classes[] = 0 === $index ? 'is-active' : '';
						?>
						<li class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', array_values( $classes ) ) ) ); ?>" data-video-id="<?php echo esc_attr( $video['video_id'] ); ?>">
							<figure class="video__thumbnail">
								<img src="<?php echo esc_url( $thumbnail_url ); ?>" />
								<i class="yt-play-icon">
									<svg
										height="100%"
										version="1.1"
										viewBox="0 0 68 48"
										width="100%">
										<path
											class="ytp-large-play-button-bg"
											d="M66.52,7.74c-0.78-2.93-2.49-5.41-5.42-6.19C55.79,.13,34,0,34,0S12.21,.13,6.9,1.55 C3.97,2.33,2.27,4.81,1.48,7.74C0.06,13.05,0,24,0,24s0.06,10.95,1.48,16.26c0.78,2.93,2.49,5.41,5.42,6.19 C12.21,47.87,34,48,34,48s21.79-0.13,27.1-1.55c2.93-0.78,4.64-3.26,5.42-6.19C67.94,34.95,68,24,68,24S67.94,13.05,66.52,7.74z"
											fill="#f03"></path>
										<path
											d="M 45,24 27,14 27,34"
											fill="#fff"></path>
									</svg>
								</i>
							</figure>
							<?php
							if ( ( isset( $attributes['content']['title'] ) && filter_var( $attributes['content']['title'], FILTER_VALIDATE_BOOLEAN ) ) || ( isset( $attributes['content']['meta'] ) && filter_var( $attributes['content']['meta'], FILTER_VALIDATE_BOOLEAN ) ) || ( isset( $attributes['content']['description'] ) && filter_var( $attributes['content']['description'], FILTER_VALIDATE_BOOLEAN ) ) ) {
								?>
							<div class="video__details">
								<?php
								if ( isset( $attributes['content']['title'] ) && filter_var( $attributes['content']['title'], FILTER_VALIDATE_BOOLEAN ) ) {
									$allowed_tags = array(
										'h1',
										'h2',
										'h3',
										'h4',
										'h5',
										'h6',
									);

									$title_tag = isset( $attributes['content']['titleTag'] ) && in_array( $attributes['content']['titleTag'], $allowed_tags, true ) ? $attributes['content']['titleTag'] : 'h3';

									printf( '<%1$s class="video__title">%2$s</%1$s>', esc_attr( $title_tag ), esc_html( $video['video_title'] ) );
								}

								if ( vidplex_premium_access() && isset( $attributes['content']['meta'] ) && filter_var( $attributes['content']['meta'] ) && ( intval( $meta['likeCount'] ) > 0 || intval( $meta['viewCount'] ) > 0 || intval( $meta['commentCount'] ) > 0 ) ) {
									?>
									<ul class="video__meta">
										<?php
										if ( intval( $meta['likeCount'] ) > 0 ) {
											?>
											<li class="meta">
												<i>
													<svg
														width="24"
														height="24"
														viewBox="0 0 24 24"
														fill="none"
														xmlns="http://www.w3.org/2000/svg">
														<path
															d="M7 11L11 2C11.7956 2 12.5587 2.31607 13.1213 2.87868C13.6839 3.44129 14 4.20435 14 5V9H19.66C19.9499 8.99672 20.2371 9.0565 20.5016 9.17522C20.7661 9.29393 21.0016 9.46873 21.1919 9.68751C21.3821 9.90629 21.5225 10.1638 21.6033 10.4423C21.6842 10.7207 21.7035 11.0134 21.66 11.3L20.28 20.3C20.2077 20.7769 19.9654 21.2116 19.5979 21.524C19.2304 21.8364 18.7623 22.0055 18.28 22H7M7 11V22M7 11H4C3.46957 11 2.96086 11.2107 2.58579 11.5858C2.21071 11.9609 2 12.4696 2 13V20C2 20.5304 2.21071 21.0391 2.58579 21.4142C2.96086 21.7893 3.46957 22 4 22H7"
															stroke="currentColor"
															stroke-width="1.25"
															stroke-linecap="round"
															stroke-linejoin="round" />
													</svg>
												</i>
												<?php echo esc_html( vidplex_format_number( $meta['likeCount'] ) ); ?>
											</li>
											<?php
										}

										if ( intval( $meta['viewCount'] ) > 0 ) {
											?>
											<li class="meta">
												<i>
													<svg
														width="24"
														height="24"
														viewBox="0 0 24 24"
														fill="none"
														xmlns="http://www.w3.org/2000/svg">
														<path
															d="M1 12C1 12 5 4 12 4C19 4 23 12 23 12C23 12 19 20 12 20C5 20 1 12 1 12Z"
															stroke="currentColor"
															stroke-width="1.25"
															stroke-linecap="round"
															stroke-linejoin="round" />
														<path
															d="M12 15C13.6569 15 15 13.6569 15 12C15 10.3431 13.6569 9 12 9C10.3431 9 9 10.3431 9 12C9 13.6569 10.3431 15 12 15Z"
															stroke="currentColor"
															stroke-width="1.25"
															stroke-linecap="round"
															stroke-linejoin="round" />
													</svg>
												</i>
												<?php echo esc_html( vidplex_format_number( $meta['viewCount'] ) ); ?>
											</li>
											<?php
										}

										if ( intval( $meta['commentCount'] ) > 0 ) {
											?>
											<li class="meta">
												<i>
													<svg
														width="24"
														height="24"
														viewBox="0 0 24 24"
														fill="none"
														xmlns="http://www.w3.org/2000/svg">
														<path
															d="M21 15C21 15.5304 20.7893 16.0391 20.4142 16.4142C20.0391 16.7893 19.5304 17 19 17H7L3 21V5C3 4.46957 3.21071 3.96086 3.58579 3.58579C3.96086 3.21071 4.46957 3 5 3H19C19.5304 3 20.0391 3.21071 20.4142 3.58579C20.7893 3.96086 21 4.46957 21 5V15Z"
															stroke="currentColor"
															stroke-width="1.25"
															stroke-linecap="round"
															stroke-linejoin="round" />
													</svg>
												</i>
												<?php echo esc_html( vidplex_format_number( $meta['commentCount'] ) ); ?>
											</li>
											<?php
										}
										?>
									</ul>
									<?php
								}

								if ( isset( $attributes['content']['description'] ) && filter_var( $attributes['content']['description'], FILTER_VALIDATE_BOOLEAN ) ) {
									?>
									<p class="video__description"><?php echo esc_html( vidplex_create_youtube_excerpt( $video['video_description'], $attributes['content']['excerpt'] ) ); ?></p>
									<?php
								}
								?>
							</div>
							<?php } ?>
						</li>
						<?php
					}
				}
				return ob_get_clean();

			default:
				return '';
		}
	}

	/**
	 * Retrieve advanced plugin settings.
	 *
	 * This function fetches the 'vidplex-settings' option from the WordPress database.
	 * If no settings exist, it returns default values.
	 *
	 * @return array {
	 *     @type int    $video_refresh_interval Interval between automatic video refreshes in seconds. Default: 15 minutes.
	 *     @type int    $videos_per_fetch       Number of videos to fetch per API call. Default: 5.
	 *     @type int    $max_videos_per_channel Maximum number of videos stored per channel. Default: 50.
	 *     @type bool   $optimize_db_storage    Whether to optimize database storage. Default: true.
	 *     @type bool   $cascade_delete_videos  Whether deleting a channel deletes its videos. Default: true.
	 *     @type bool   $cleanup_multiple_data  Whether to clean up duplicate or multiple entries. Default: true.
	 *     @type bool   $api_quota_protection   Enable API quota protection. Default: true.
	 *     @type bool   $log_api_errors         Whether to log API errors. Default: true.
	 * }
	 */
	public static function advanced_settings() {
		$settings = (array) get_option(
			'vidplex-settings',
			array(
				'video_refresh_interval' => 'hourly',
				'videos_per_fetch'       => 20,
				'max_videos_per_channel' => 50,

				'long_shorts_as_video'   => false,

				'cascade_delete_videos'  => true,
				'cleanup_multiple_data'  => true,

				'api_quota_protection'   => true,
				'log_api_errors'         => true,
			),
			true
		);

		return $settings;
	}
}
