<?php
/*--------------------------------
Get Theme Options
----------------------------------*/
if (!function_exists('glamer_options')) {
	function glamer_options($option = '', $default = null) {
		$defaults = glamer_default_theme_options();
		$options = get_option('glamer_Theme_Option');
		$default = (!isset($default) && isset($defaults[$option])) ? $defaults[$option] : $default;
		return (isset($options[$option])) ? $options[$option] : $default;
	}
}

if (! function_exists('wp_body_open')) :
	/**
	 * Shim for sites older than 5.2.
	 *
	 * @link https://core.trac.wordpress.org/ticket/12563
	 */
	function wp_body_open() {
		do_action('wp_body_open');
	}
endif;

/**
 * Adds custom classes to the array of body classes.
 */
function glamer_body_classes($classes) {
	// Adds a class of hfeed to non-singular pages.
	if (! is_singular()) {
		$classes[] = 'hfeed';
	}

	if (class_exists('WooCommerce')) {
		$classes[] = 'glamer-woo-active';
	} else {
		$classes[] = 'glamer-woo-deactivate';
	}

	//Check Elementor Page Builder Used or not
	$elementor_used = get_post_meta(get_the_ID(), '_elementor_edit_mode', true);

	if (is_archive() || is_search()) {
		$classes[]        = !!$elementor_used ? 'page-builder-not-used' : 'page-builder-not-used';
	} else {
		$classes[]        = !!$elementor_used ? 'page-builder-used' : 'page-builder-not-used';
	}

	return $classes;
}
add_filter('body_class', 'glamer_body_classes');

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function glamer_pingback_header() {
	if (is_singular() && pings_open()) {
		printf('<link rel="pingback" href="%s">', esc_url(get_bloginfo('pingback_url')));
	}
}
add_action('wp_head', 'glamer_pingback_header');


/**
 * Words limit
 */
function glamer_words_limit($text, $limit) {
	$words = explode(' ', $text, ($limit + 1));

	if (count($words) > $limit) {
		array_pop($words);
	}

	return implode(' ', $words);
}


/**
 * Get excluded sidebar list
 */
if (! function_exists('glamer_sidebars')) {
	function glamer_sidebars() {
		$default = esc_html__('Default', 'glamer');
		$options = array($default);
		// set ids of the registered sidebars for exclude
		$exclude = array('glamer-footer-widget');

		global $wp_registered_sidebars;

		if (! empty($wp_registered_sidebars)) {
			foreach ($wp_registered_sidebars as $sidebar) {
				if (! in_array($sidebar['id'], $exclude)) {
					$options[$sidebar['id']] = $sidebar['name'];
				}
			}
		}

		return $options;
	}
}

/**
 * Iframe embed
 */

function glamer_iframe_embed($tags, $context) {
	if ('post' === $context) {
		$tags['iframe'] = array(
			'src'             => true,
			'height'          => true,
			'width'           => true,
			'frameborder'     => true,
			'allowfullscreen' => true,
		);
	}
	return $tags;
}
add_filter('wp_kses_allowed_html', 'glamer_iframe_embed', 10, 2);
/**
 * Allow Html
 */
if (!function_exists('glamer_allow_html')) {
	function glamer_allow_html() {
		return array(
			'a'      => array(
				'href'   => array(),
				'target' => array(),
				'title'  => array(),
				'rel'    => array(),
			),
			'strong' => array(),
			'small'  => array(),
			'span'   => array(
				'style' => array(),
			),
			'p'      => array(),
			'br'     => array(),
			'img'    => array(
				'src'    => array(),
				'title'  => array(),
				'alt'    => array(),
				'width'  => array(),
				'height' => array(),
				'class'  => array(),
			),
			'del'    => array(),
			'ins'    => array(),
			'h1'     => array(),
			'h2'     => array(),
			'h3'     => array(),
			'h4'     => array(),
			'h5'     => array(),
			'h6'     => array(),
			'div'    => array(
				'class' => array(),
				'style' => array(),
			),
			'ul'     => array(),
			'ol'     => array(),
			'li'     => array(),
			'blockquote' => array(),
			'code'   => array(),
			'pre'    => array(),
			'i'      => array(),
			'iframe' => array(
				'src'    => array(),
				'width'  => array(),
				'height' => array(),
				'frameborder' => array(),
				'allow' => array(),
			),
			'table'  => array(),
			'tr'     => array(),
			'td'     => array(),
			'th'     => array(),
			'em'     => array(),
			'b'     => array(),
			'sup'     => array(),
		);
	}
}

/**
 * Next - Prev Post Link
 */
if (!function_exists('glamer_next_prev_post_link')) {
	function glamer_next_prev_post_link() {
		$prev_post = get_previous_post();
		$next_post = get_next_post();
		if (! $prev_post && ! $next_post) {
			return;
		}
?>
		<nav class="glamer-post-navication-thum post-box" role="navigation">
			<h2 class="screen-reader-text"><?php esc_html_e('Post navigation', 'glamer'); ?></h2>
			<div class="nav-links">
				<?php
				if ($prev_post) :
					$prev_post_thumb = get_the_post_thumbnail($prev_post->ID, 'thumbnail');
				?>
					<div class="nav-previous post-thum-nav">

						<div class="nav-holder">
							<?php if ($prev_post_thumb) : ?>
								<div class="nav-thumb">
									<a href="<?php echo esc_url(get_the_permalink($prev_post->ID)); ?>"><?php echo get_the_post_thumbnail($prev_post->ID, 'thumbnail'); ?></a>
								</div>
							<?php endif; ?>
							<a href="<?php echo esc_url(get_the_permalink($prev_post->ID)); ?>" class="nav-label">
								<span class="nav-subtitle"><?php esc_html_e('Previous Post', 'glamer'); ?></span>
							</a>
						</div>

					</div>
				<?php endif; ?>
				<?php
				if ($next_post) :
					$next_post_thumb = get_the_post_thumbnail($next_post->ID, 'thumbnail');
				?>
					<div class="nav-next post-thum-nav">

						<div class="nav-holder">
							<a href="<?php echo esc_url(get_the_permalink($next_post->ID)); ?>" class="nav-label">
								<span class="nav-subtitle"><?php esc_html_e('Next Post', 'glamer'); ?></span>
							</a>
							<?php if ($next_post_thumb) : ?>
								<div class="nav-thumb">
									<a href="<?php echo esc_url(get_the_permalink($next_post->ID)); ?>"><?php echo get_the_post_thumbnail($next_post->ID, 'thumbnail'); ?></a>
								</div>
							<?php endif; ?>
						</div>

					</div>

				<?php endif; ?>
			</div>
		</nav>
<?php
	}
}

/**
 * Check if a post is a custom post type.
 *
 * @param mixed $post Post object or ID
 *
 * @return boolean
 */
function glamer_custom_post_types($post = null) {
	$custom_post_list = get_post_types(array('_builtin' => false));

	// there are no custom post types
	if (empty($custom_post_list)) {
		return false;
	}

	$custom_types     = array_keys($custom_post_list);
	$current_post_type = get_post_type($post);

	// could not detect current type
	if (! $current_post_type) {
		return false;
	}

	return in_array($current_post_type, $custom_types);
}


/**
 * Add span tag in archive list count number
 */
add_filter('get_archives_link', 'glamer_modify_archive_link', 10, 2);

function glamer_modify_archive_link($link_html, $url) {
	// Extract the number from the link HTML
	preg_match('/<\/a>(.*?)<\/li>/', $link_html, $matches);

	if (!empty($matches[1])) {
		$link_html = preg_replace('/<\/a>(.*?)<\/li>/', '<span class="post-count-number">$1</span></a>', $link_html);
	}

	return $link_html;
}

// Added span on category widget
function glamer_add_span_category_count($links) {
	// Use regex to match the <a> tag and the number in parentheses
	$links = preg_replace(
		'/<a([^>]*)>([^<]*)<\/a>\s*\((\d+)\)/',
		'<a$1>$2 <span class="post-count-number">($3)</span></a>',
		$links
	);
	return $links;
}

add_filter('wp_list_categories', 'glamer_add_span_category_count');

if (! function_exists('glamer_posted_on')) :

	function glamer_posted_on() {
		$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
		if (get_the_time('U') !== get_the_modified_time('U')) {
			$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
		}

		$time_string = sprintf(
			$time_string,
			esc_attr(get_the_date(DATE_W3C)),
			esc_html(get_the_date('j M, Y')),  // Format: 15 Sep, 2020
			esc_attr(get_the_modified_date(DATE_W3C)),
			esc_html(get_the_modified_date('j M, Y'))  // Modified date format: 15 Sep, 2020
		);

		echo '<span class="posted-on"><i class="fas fa-clock"></i>' . $time_string . '</span>'; // WPCS: XSS OK.
	}

endif;


/**
 * Prints HTML with meta information for the current author.
 */
if (! function_exists('glamer_posted_by')) :

	function glamer_posted_by() {
		$byline = sprintf(
			/* translators: %s: post author. */
			esc_html_x(' %s', 'post author', 'glamer'),
			'<span class="author vcard"><a class="url fn n" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '">' . esc_html(get_the_author()) . '</a></span>'
		);

		echo '<span class="byline"><i class="fas fa-user"></i> ' . $byline . '</span>'; // WPCS: XSS OK.
	}
endif;


/**
 * Prints HTML with meta information for the tags.
 */
if (! function_exists('glamer_post_tags')) :

	function glamer_post_tags() {
		// Hide category and tag text for pages.
		if ('post' === get_post_type()) {

			/* translators: used between list items, there is a space after the comma */
			$tags_list = get_the_tag_list('<li>', '</li><li>', '</li>');
			if ($tags_list) {
				/* translators: 1: list of tags. */
				printf(
					'<div class="post-type-tag">
						<h4 class="post-type-title">' . esc_html__('Tags:', 'glamer') . '</h4>
						<ul>%1$s</ul>
					</div>',
					$tags_list
				); // WPCS: XSS OK.
			}
		}
	}
endif;

/**
 * Prints HTML with meta information for the categories.
 */

if (! function_exists('glamer_post_categories')) :

	function glamer_post_categories() {
		// Hide category and tag text for pages.
		if ('post' === get_post_type()) {

			/* translators: used between list items, there is a space after the comma */
			$categories_list = get_the_category_list(esc_html__(', ', 'glamer'));
			if ($categories_list) {
				/* translators: 1: list of categories. */
				printf('<span class="cat-links">' . esc_html__('%1$s', 'glamer') . '</span>', $categories_list); // WPCS: XSS OK.
			}
		}
	}
endif;

/**
 * Prints post's first category
 */

if (! function_exists('glamer_post_first_category')) :

	function glamer_post_first_category() {

		$post_category_list = get_the_terms(get_the_ID(), 'category');

		$post_first_category = $post_category_list[0];
		if (! empty($post_first_category->slug)) {
			echo '<span class="cat-links"><a href="' . get_term_link($post_first_category->slug, 'category') . '">' . $post_first_category->name . '</a></span>';
		}
	}
endif;

/**
 * Prints HTML with meta information for the comments.
 */
if (! function_exists('glamer_comment_count')) :

	function glamer_comment_count() {
		if (! post_password_required() && (comments_open() || get_comments_number()) && get_comments_number() != 0) {
			echo '<span class="comments-link"><i class="far fa-comments"></i>';
			comments_popup_link('', '' . esc_html__('1', 'glamer') . ' <span class="comment-count-text">' . esc_html__('Comment', 'glamer') . '</span>', '% <span class="comment-count-text">' . esc_html__('Comments', 'glamer') . '</span>');
			echo '</span>';
		}
	}
endif;

if (! function_exists('glamer_comment_count_home')) :
	function glamer_comment_count_home() {
		if (! post_password_required() && (comments_open() || get_comments_number()) && get_comments_number() != 0) {
			comments_popup_link('', '' . esc_html__('1', 'glamer') . ' <span class="tag-title">' . esc_html__('Comment', 'glamer') . '</span>', '% <span class="tag-title">' . esc_html__('Comments', 'glamer') . '</span>');
			echo '</span>';
		}
	}
endif;

/*--------------------------------
COMMENT PAGINATION
----------------------------------*/

if (!function_exists('glamer_comments_pagination')) {
	function glamer_comments_pagination() {
		the_comments_pagination(array(
			'screen_reader_text' => ' ',
			'prev_text'          => '<i class="fa fa-angle-left"></i>',
			'next_text'          => '<i class="fa fa-angle-right"></i>',
			'type'               => 'list',
			'mid_size'           => 1,
		));
	}
}


/*--------------------------------
Allode Megamenu
----------------------------------*/
add_filter('nav_menu_css_class', 'glamer_megamenu_class', 10, 2);
function glamer_megamenu_class($classes, $item) {
	$navmega = get_post_meta($item->ID, 'glamer_navmeta', true);
	if (in_array('menu-item-has-children', $classes) && !empty($navmega)) {
		if (!empty($navmega['glamer_nav_megamenu_show'] == true)) {
			$megamenu = 'mega ';
		} else {
			$megamenu = 'no-mega ';
		}
		$classes[] = $megamenu . $navmega['glamer_nav_mmenu_column'];
	}
	return $classes;
}