<?php

/**
 * Functions which enhance the theme by hooking into WordPress
 * @package Glaciers
 * @since 1.0.0
 */

/* TABLE of CONTENTS
   ==================================================== 
   BODY OPEN
   BODY CLASSES
   SITE LOGO & TITLE
   SITE TAGLINE
   HEADER BRANDING
   MAIN NAVIGATION
   MODIFY SEARCH FORM
   ARCHIVE TITLE PREFIX
   ARCHIVE TITLES
   PAGE HEADERS
   DISPLAY A SINGLE POST CATEGORY
   DISPLAY FEATURED or CATEGORY LABEL
   META DATE
   UPDATED POST DATE
   META AUTHOR
   ENTRY FOOTER
   POST THUMBNAILS
   AUTHOR BIO
   CHECK IF POST AUTHOR HAS COMMENT
   MODIFY the COMMENT FORM
   ADD A TITLE TO POSTS MISSING TITLES
   FILTER THE EXCERPT LENGTH
   FILTER THE EXCERPT SUFFIX
   CREATE A CONTINUE READING LINK FOR EXCERPTS
   MOVE READ MORE LINK OUTSIDE OF PARAGRAPHS
   DISPLAY SVG ICONS IN MENU
   CONVERT HEX to RGBA
   INLINE COLOUR PRESETS
   BLOG  NAVIGATION
   POST NAVIGATION
   MULTIPAGE NAVIGATION
   ====================================================*/


/* BODY OPEN
 @since 1.0.0
   ==================================================== */
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;


/* SET CONTENT WIDTH
@since 1.0.0
   ==================================================== */
$GLOBALS['content_width'] = 1140;

if (!function_exists('glaciers_content_width')) :
	function glaciers_content_width()
	{
		$content_width = $GLOBALS['content_width'];
		// Check if the page has a sidebar.
		if (is_active_sidebar('left-sidebar') || is_active_sidebar('right-sidebar') || is_active_sidebar('blog-sidebar')) {
			$content_width = 775;
		}
		$GLOBALS['content_width'] = apply_filters('glaciers_content_width', $content_width);
	}
endif;
add_action('template_redirect', 'glaciers_content_width', 0);


/* BODY CLASSES
@since 1.0.0
   ==================================================== */
function glaciers_body_classes($classes)
{
	$glaciers_boxed = apply_filters('glaciers_boxed', esc_attr(get_theme_mod('glaciers_boxed', false)));
	$glaciers_hide_search_modal = apply_filters('glaciers_hide_search_modal', esc_attr(get_theme_mod('glaciers_hide_search_modal', false)));
	$glaciers_featured_img_below = apply_filters('glaciers_featured_img_below', esc_attr(get_theme_mod('glaciers_featured_img_below', false)));
	$glaciers_blog_style = apply_filters('glaciers_blog_layout', esc_attr(get_theme_mod('glaciers_blog_style', 'classic-right')));
	$glaciers_post_style = apply_filters('glaciers_post_layout', esc_attr(get_theme_mod('glaciers_post_style', 'classic-right')));
	$glaciers_footer_style = apply_filters('glaciers_footer_layout', esc_attr(get_theme_mod('glaciers_footer_style', 'footer1')));

	// Adds a class of hfeed to non-singular pages.
	if (!is_singular()) {
		$classes[] = 'hfeed';
	}

	// Blog style classes
	if ($glaciers_blog_style && is_home() || is_archive()) {
		$classes[] = 'blog-' . esc_attr($glaciers_blog_style);
	}

	// Post style classes
	if ($glaciers_post_style && is_single()) {
		$classes[] = 'single-' . esc_attr($glaciers_post_style);
	}

	// Post meta field: custom heading
	if (get_post_meta(get_the_ID(), 'Custom Heading', true)) {
		$classes[] = 'custom-heading';
	}

	// Page excerpt class
	if (is_page() && has_excerpt()) {
		$classes[] = 'has-excerpt';
	}

	// Show Search modal class
	if ($glaciers_hide_search_modal && $glaciers_hide_search_modal !== false) {
		$classes[] = 'hide-search-modal';
	}

	// Boxed class
	if ($glaciers_boxed && $glaciers_boxed !== false) {
		$classes[] = 'boxed';
	}

	// Check whether the current page is the default page template
	if (basename(get_page_template()) === 'page.php' && is_page()) {
		$classes[] = 'template-right';
	}

	// Check whether the current page is the left column template
	if (is_page_template(array('templates/template-left.php'))) {
		$classes[] = 'template-left';
	}

	// Check whether the current page is the full width template
	if (is_page_template(array('templates/template-full-width.php'))) {
		$classes[] = 'template-full';
	}

	// Check whether the current page is the full width template
	if (is_page_template(array('templates/template-wide.php'))) {
		$classes[] = 'template-wide';
	}

	// Check whether the current page is the short width template
	if (is_page_template(array('templates/template-short-width.php'))) {
		$classes[] = 'template-short';
	}

	// Check whether the current page is the left column template
	if (is_page_template(array('templates/template-blank.php'))) {
		$classes[] = 'template-blank';
	}

	// Adds a class of no-sidebar when there is no sidebar present.
	if (!is_active_sidebar('blog-sidebar') ||  !is_active_sidebar('left-sidebar') || !is_active_sidebar('right-sidebar')) {
		$classes[] = 'no-sidebar';
	}

	// Check for post thumbnail
	if (
		is_single() && has_post_thumbnail()
		|| is_page() && has_post_thumbnail()
	) {
		$classes[] = 'has-post-thumbnail';
	} else {
		$classes[] = 'no-post-thumbnail';
	}

	// Footer style
	if ($glaciers_footer_style === 'footer1') {
		$classes[] = esc_attr($glaciers_footer_style);
	}

	// Check whether we're in the customizer preview
	if (is_customize_preview()) {
		$classes[] = 'customizer-preview';
	}

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


/* SITE LOGO & TITLE
@since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_site_identity')) :
	function glaciers_site_identity()
	{

		if (has_custom_logo()) {
			the_custom_logo();
		}

		// Check whether the site title is activated in the customizer.
		$glaciers_hide_site_title = get_theme_mod('glaciers_hide_site_title', false);
		if (false === $glaciers_hide_site_title) {

			printf(
				'<%1$s class="site-title"><a class="navbar-brand" href="%2$s" rel="home">%3$s</a></%1$s>',
				is_front_page() && is_home() ? 'h1' : 'p',
				esc_url(home_url('/')),
				esc_html(get_bloginfo('name'))
			);
		}
	}
endif;


/* SITE TAGLINE
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_site_tagline')) :
	function glaciers_site_tagline()
	{

		$description  = get_bloginfo('description', 'display');
		// Check whether the site title is activated in the customizer.
		$glaciers_hide_tagline = get_theme_mod('glaciers_hide_tagline', false);


		if ($description) {
			if (false === $glaciers_hide_tagline) {
				echo '<p class="site-description">' . esc_html($description) . '</p>';
			}
		}
	}
endif;


/* HEADER BRANDING
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_header_branding')) :
	function glaciers_header_branding()
	{

		$description  = get_bloginfo('description', 'display');
		echo '<div class="site-branding">';
		glaciers_site_identity();
		glaciers_site_tagline();
		echo '</div>';
	}
endif;


/* MAIN NAVIGATION
   ====================================================*/
if (!function_exists('glaciers_mainnav')) :
	function glaciers_mainnav()
	{

		$glaciers_header_layout = apply_filters('glaciers_header_layout', get_theme_mod('glaciers_header_layout', 'header1'));
?>
		<span class="toggle-wrap">
			<button class="navbar-toggler collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#headernav" aria-controls="headernav" aria-expanded="false" aria-label="Toggle navigation">
				<span class="navbar-toggler-icon"><?php glaciers_theme_svg('nav'); ?></span>
			</button>
		</span>

		<nav id="headernav" class="<?php echo esc_attr($glaciers_header_layout); ?> navbar navbar-collapse collapse" aria-label="navbar">

			<?php
			if (has_nav_menu('primary')) {
				wp_nav_menu(array(
					'menu_id'              => 'mainmenu',
					'theme_location' => 'primary',
					'container'         => 'ul',
					'menu_class'     => 'navbar-nav',
					'link_before'  => '<span>',
					'link_after'   => '</span>',
				));
			} else {
				echo '<ul class="navbar-nav">';
				wp_list_pages(array(
					'match_menu_classes' 	=> true,
					'title_li' 				=> false,
					'link_before'  => '<span>',
					'link_after'   => '</span>',
				));
				echo '</ul>';
			}
			?>

			<?php // Are the options activated in the customizer.		
			$glaciers_hide_search_modal = get_theme_mod('glaciers_hide_search_modal', false);
			if (false === $glaciers_hide_search_modal) { ?>
				<button type="button" class="searchModal-btn" data-bs-toggle="modal" data-bs-target="#searchModal"><span class="visually-hidden"><?php esc_html_e('Search', 'glaciers'); ?></span><?php glaciers_theme_svg('search'); ?></button>
			<?php } ?>

		</nav>





	<?php
	}
endif;

/* MODIFY SEARCH FORM
 @since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_search_form')) :
	function glaciers_search_form($form)
	{
		$form = '
      <form  method="get" class="search-form" action="' . esc_url(home_url('/')) . '">
        <div class="search-wrap input-group">
            <input type="search" class="search-field" placeholder="' . esc_attr_x('Type keywords...', 'placeholder', 'glaciers') . '" value="' . get_search_query() . '" name="s" title="' . esc_attr_x('Search for:', '', 'glaciers') . '" />
          <button type="submit" class="button">' . glaciers_get_theme_svg('search') . '</button>
        </div>
			</form>';
		return $form;
	}
endif;
add_filter('get_search_form', 'glaciers_search_form');


/* ARCHIVE TITLE PREFIX
	Styles the archive title prefix with a span
	@since 1.0.0
   ==================================================== */
function glaciers_prefix_archive_title($title)
{

	$regex = apply_filters(
		'glaciers_prefix_the_archive_title_regex',
		array(
			'pattern'     => '/(\A[^\:]+\:)/',
			'replacement' => '<span class="archive-prefix colour">$1</span>',
		)
	);

	if (empty($regex)) {
		return $title;
	}
	return preg_replace($regex['pattern'], $regex['replacement'], $title);
}

add_filter('get_the_archive_title', 'glaciers_prefix_archive_title');

/* ARCHIVE TITLES
	Change how archive titles are displayed
	@since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_archive_title')) :
	function glaciers_archive_title($title)
	{

		// Check if this is set to show or hide the archive prefix
		$glaciers_hide_prefix_archive = get_theme_mod('glaciers_hide_prefix_archive', false);

		// If enabled - the prefix archive label is hidden
		if (true === $glaciers_hide_prefix_archive) {
			if (is_category()) {
				return single_cat_title('', false);
			} elseif (is_author()) {
				return get_the_author();
			}
		}
		return $title;
	}
endif;
add_filter('get_the_archive_title', 'glaciers_archive_title', 10, 1);


/* PAGE HEADERS
	Load the appropriate page headers
	@since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_page_headers')) :
	function glaciers_page_headers()
	{
		if (is_archive()) {
			echo '<section class="container"><header id="archive-header">';
			the_archive_title('<h1 class="archive-title">', '</h1>');
			the_archive_description('<div class="archive-description page-excerpt lead">', '</div>');

			// action hook for any content after the archive title and description
			if (!is_category() && !is_author()) {
				do_action('glaciers_after_archive_description');
			}
			echo '</header></section>';
		}
	}
endif;
add_action('glaciers_page_header', 'glaciers_page_headers');



/* DISPLAY A SINGLE POST CATEGORY
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_first_category')) :
	function glaciers_first_category()
	{
		$category = get_the_category();
		$first_category = $category[0];

		echo sprintf(
			'<span class="post-meta">' . esc_html__('In ', 'glaciers') . '<a href="%s">%s</a></span>',
			get_category_link($first_category),
			$first_category->name
		); // phpcs:ignore WordPress.Security.EscapeOutput
	}
endif;


/* DISPLAY FEATURED or CATEGORY LABEL
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_featured_badge')) :
	function glaciers_featured_badge()
	{
		$glaciers_featured_label_text = get_theme_mod('glaciers_featured_label_text', esc_html__('Featured', 'glaciers'));

		if (is_sticky() && is_home() && !is_paged()) {
			echo '<span class="featured-badge">', wp_kses_post($glaciers_featured_label_text), '</span>';
		}
	}
endif;


/* META DATE
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_posted_on')) {

	function glaciers_posted_on()
	{
		$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';

		$time_string = sprintf(
			$time_string,
			esc_attr(get_the_date(DATE_W3C)),
			esc_html(get_the_date())
		);
		echo '<span class="posted-on post-meta">';
		printf(
			/* translators: %s: Publish date. */
			esc_html__('On %s', 'glaciers'),
			$time_string // phpcs:ignore WordPress.Security.EscapeOutput
		);
		echo '</span>';
	}
}

/* UPDATED POST DATE
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_updated')) :
	function glaciers_updated()
	{
		printf(
			/* translators: %s: For the updated date. */
			'<span class="posted-update post-meta">' .	esc_html__('Updated on %s', 'glaciers') . '</span>',
			get_the_modified_date()
		);
	}
endif;

/* META AUTHOR
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_posted_by')) :
	function glaciers_posted_by()
	{

		if (!get_the_author_meta('description') && post_type_supports(get_post_type(), 'author')) {
			echo '<span class="byline post-meta">';
			printf(
				/* translators: %s: Author name. */
				esc_html__('By %s', 'glaciers'),
				'<a href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '" rel="author">' . esc_html(get_the_author()) . '</a>'
			);
			echo '</span>';
		}
	}
endif;


/* META COMMENT COUNT & LINK
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_comment_link')) :
	function glaciers_comment_link()
	{
		if (!post_password_required() && (comments_open() || get_comments_number())) {
			echo '<span class="comments-link post-meta">';
			/* translators: %s: Name of current post. Only visible to screen readers. */
			comments_popup_link(sprintf(__('Make a comment <span class="screen-reader-text">on %s</span>', 'glaciers'), get_the_title()));

			echo '</span>';
		}
	}
endif;


/* ENTRY FOOTER
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_entry_footer')) :

	// Prints HTML with meta information for the categories, tags and comments.
	function glaciers_entry_footer()
	{

		// Is the option to hide the edit link activated in the customizer.
		$glaciers_hide_edit = get_theme_mod('glaciers_hide_edit', false);
		$glaciers_hide_post_footer = get_theme_mod('glaciers_hide_post_footer', false);
		$glaciers_hide_post_categories = get_theme_mod('glaciers_hide_post_categories', false);
		$glaciers_hide_post_tags = get_theme_mod('glaciers_hide_post_tags', false);

		if (false === $glaciers_hide_post_footer) {
			echo '<footer class="entry-footer">';

			// 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__(', ', 'glaciers'));
				if ($categories_list && false === $glaciers_hide_post_categories) {
					/* translators: 1: list of categories. */
					printf('<span class="cat-links">' . glaciers_get_theme_svg('doc') . esc_html__('Posted in: %1$s', 'glaciers') . '</span>', $categories_list); // phpcs:ignore WordPress.Security.EscapeOutput
				}

				/* translators: used between list items, there is a space after the comma */
				$tags_list = get_the_tag_list('', esc_html_x(', ', 'list item separator', 'glaciers'));
				if ($tags_list && false === $glaciers_hide_post_tags) {
					/* translators: 1: list of tags. */
					printf('<span class="tags-links">' . glaciers_get_theme_svg('tag') . esc_html__('Tagged: %1$s', 'glaciers') . '</span>', $tags_list); // phpcs:ignore WordPress.Security.EscapeOutput
				}
			}

			if (false === $glaciers_hide_edit) {
				edit_post_link(
					sprintf(
						wp_kses(
							/* translators: %s: Name of current post. Only visible to screen readers */
							__('Open <span class="screen-reader-text">%s</span>', 'glaciers'),
							array(
								'span' => array(
									'class' => array(),
								),
							)
						),
						wp_kses_post(get_the_title())
					),
					'<span class="edit-link">' . glaciers_get_theme_svg('edit') . esc_html__('Edit Post: ', 'glaciers'),
					'</span>'
				);
			}

			echo '</footer>';
		}
	}
endif;

add_action('glaciers_after_post_entry_content', 'glaciers_entry_footer');

/* POST THUMBNAILS
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_post_thumbnail')) :
	function glaciers_post_thumbnail()
	{
		if (post_password_required() || is_attachment() || !has_post_thumbnail()) {
			return;
		}

		$caption = get_the_post_thumbnail_caption();
	?>

		<figure class="featured-media">

			<?php if (is_singular()) : ?>

				<div class="post-thumbnail">
					<?php the_post_thumbnail(
						'post-thumbnail',
						array('class' => 'featured-thumbnail', 'alt' => get_the_title())
					);
					?>
				</div><!-- .post-thumbnail -->

			<?php else : ?>

				<a class="post-thumbnail-link" href="<?php the_permalink(); ?>" aria-hidden="true">
					<?php the_post_thumbnail('post-thumbnail', array('alt' => get_the_title())); ?>
				</a>

			<?php endif; // End is_singular() 

			if ($caption) {
			?>

				<figcaption class="wp-caption-text"><?php echo wp_kses_post($caption); ?></figcaption>

			<?php
			}
			?>

		</figure>

<?php
	}
endif;


/* POST AUTHOR BIO
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_author_post_bio_info')) :
	function glaciers_author_post_bio_info()
	{

		echo '<div class="post-author-info">';
		printf(
			'<div class="post-author-avatar">%s</div>',
			get_avatar(get_the_author_meta('user_email'), 100)
		);
		echo '<div class="post-author-description"><div class="author-bio">';
		printf(
			'<h3 class="post-author-heading">%s</h3>',
			get_the_author()
		);
		the_author_meta('description');
		echo '</div>' . glaciers_get_theme_svg('arrow-right-circle') . '<a class="post-author-link" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '" rel="author">';
		printf(
			/* translators: %s: For the author name. */
			esc_html__('View all posts by %s', 'glaciers'),
			get_the_author()
		);
		echo '</a></div></div>';
	}
endif;


/* CHECK IF POST AUTHOR HAS COMMENT
 @since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_is_comment_by_post_author')) :
	function glaciers_is_comment_by_post_author($comment = null)
	{

		if (is_object($comment) && $comment->user_id > 0) {

			$user = get_userdata($comment->user_id);
			$post = get_post($comment->comment_post_ID);

			if (!empty($user) && !empty($post)) {

				return $comment->user_id === $post->post_author;
			}
		}
		return false;
	}
endif;


/* MODIFY the COMMENT FORM
 @since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_comment_form_default_fields')) :
	function glaciers_comment_form_default_fields($fields)
	{
		$commenter = wp_get_current_commenter();

		$req      = get_option('require_name_email');
		$aria_req = ($req ? " aria-required='true'" : '');
		$html_req = ($req ? " required='required'" : '');
		$html5    = 'html5';

		$fields['author'] = '
      <div class="row gx-1"><div class="col-md-4"><p class="comment-form-author">
        <input id="author" name="author" type="text" placeholder="' . esc_attr__('Name', 'glaciers') . (esc_attr($req) ? ' *' : '') . '" value="' . esc_attr($commenter['comment_author']) . '" size="30"' . esc_attr($aria_req) . esc_attr($html_req) . ' />
      </p></div>';

		$fields['email'] = '
      <div class="col-md-4"><p class="comment-form-email">
        <input id="email" name="email" ' . (esc_attr($html5) ? 'type="email"' : 'type="text"') . ' placeholder="' . esc_attr__('Email', 'glaciers') . (esc_attr($req) ? ' *' : '') . '" value="' . esc_attr($commenter['comment_author_email']) . '" size="30" aria-describedby="email-notes"' . esc_attr($aria_req) . esc_attr($html_req)  . ' />
      </p></div>';

		$fields['url'] = '
      <div class="col-md-4"><p class="comment-form-url">
        <input id="url" name="url" ' . (esc_attr($html5) ? 'type="url"' : 'type="text"') . ' placeholder="' . esc_attr__('Website', 'glaciers') . '" value="' . esc_attr($commenter['comment_author_url']) . '" size="30" />
      </p></div></div>';

		return $fields;
	}
endif;
add_filter('comment_form_default_fields', 'glaciers_comment_form_default_fields', 10, 1);


/* ADD A TITLE TO POSTS MISSING TITLES
	When a post is missing a title, a default title will be used.
	@since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_post_title')) {
	function glaciers_post_title($title)
	{
		return '' === $title ? esc_html_x('Untitled', 'Added to posts and pages that are missing titles', 'glaciers') : $title;
	}
}

add_filter('the_title', 'glaciers_post_title');


/* FILTER THE EXCERPT LENGTH
	Customizable excerpt length
	@since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_excerpt_length')) {
	function glaciers_excerpt_length($length)
	{

		if (is_admin()) {
			return $length;
		}

		$excerpt_length = esc_attr(get_theme_mod('glaciers_excerpt_length', '25'));
		return $excerpt_length; // phpcs:ignore WordPress.Security.EscapeOutput
	}
}
add_filter('excerpt_length', 'glaciers_excerpt_length', 99);



/* FILTER THE EXCERPT SUFFIX
	Replaces the default [...] with a &hellip; (three dots)
	@since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_excerpt_more')) :
	function glaciers_excerpt_more()
	{
		return '&hellip;';
	}
	add_filter('excerpt_more', 'glaciers_excerpt_more');
endif;


/* CREATE A CONTINUE READING LINK FOR EXCERPTS
@since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_read_more_link')) :
	function glaciers_read_more_link()
	{
		$glaciers_readmore_text = esc_html(get_theme_mod('glaciers_readmore_text', esc_html__('Read More', 'glaciers')));
		echo '<p class="more-link-wrapper">' . glaciers_get_theme_svg('arrow-right-circle-fill') . '<a class="more-link" href="' . esc_url(get_permalink()) . '">' . wp_kses_post($glaciers_readmore_text) . '</a></p>';
	}
endif;


/* MOVE READ MORE LINK OUTSIDE OF PARAGRAPHS
	Move the 'continue reading' link outside of paragraph.
	@since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_move_more_link')) :
	function glaciers_move_more_link()
	{
		$glaciers_readmore_text = esc_html(get_theme_mod('glaciers_readmore_text', esc_html__('Read More', 'glaciers')));
		return '<p><a class="more-link" href="' . esc_url(get_permalink()) . '">' . wp_kses_post($glaciers_readmore_text) . '</a></p>';
	}
	add_filter('the_content_more_link', 'glaciers_move_more_link');
endif;


/* INSERT FORMATS TO DROP DOWN - CLASSIC EDITOR
 @since 1.0.0
   ==================================================== */
if (!function_exists('insert_formats_to_editor')) :
	function insert_formats_to_editor($init_array)
	{
		// Define the style_formats array
		$style_formats = array(
			// Each array child is a format with it's own settings

			array(
				'title' => esc_html__('Extra Small', 'glaciers'),
				'inline' => 'span',
				'classes' => 'has-extra-small-font-size',
				'wrapper' => true
			),

			array(
				'title' => esc_html__('Small', 'glaciers'),
				'inline' => 'span',
				'classes' => 'has-small-font-size',
				'wrapper' => true
			),
			array(
				'title' => esc_html__('Medium', 'glaciers'),
				'inline' => 'span',
				'classes' => 'has-medium-font-size',
				'wrapper' => true
			),

			array(
				'title' => esc_html__('Large', 'glaciers'),
				'inline' => 'span',
				'classes' => 'has-large-font-size',
				'wrapper' => true
			),
			array(
				'title' => esc_html__('Extra Large', 'glaciers'),
				'inline' => 'span',
				'classes' => 'has-extra-large-font-size',
				'wrapper' => true
			),
			array(
				'title' => esc_html__('Huge', 'glaciers'),
				'inline' => 'span',
				'classes' => 'has-huge-font-size',
				'wrapper' => true
			),
			array(
				'title' => esc_html__('Gigantic', 'glaciers'),
				'block' => 'span',
				'classes' => 'has-gigantic-font-size',
				'wrapper' => true
			)
		);
		// Insert the array, JSON ENCODED, into 'style_formats'
		$init_array['style_formats'] = json_encode($style_formats);

		return $init_array;
	}
endif;
add_filter('tiny_mce_before_init', 'insert_formats_to_editor');


/* ADD STYLE DROP DOWN TO CLASSIC EDITOR
 @since 1.0.0
   ==================================================== */
if (!function_exists('glaciers_mce_buttons_2')) :
	function glaciers_mce_buttons_2($buttons)
	{
		array_unshift($buttons, 'styleselect');
		return $buttons; // phpcs:ignore WordPress.Security.EscapeOutput
	}
endif;
add_filter('mce_buttons_2', 'glaciers_mce_buttons_2');


/* DISPLAY SVG ICONS IN MENU
@since 1.0.0
   ==================================================== */
function glaciers_nav_menu_social_icons($item_output, $item, $depth, $args)
{
	// Change SVG icon inside social links menu if there is supported URL.
	if ('social' === $args->theme_location) {
		$svg = Glaciers_SVG_Icons::get_social_link_svg($item->url);
		if (empty($svg)) {
			$svg = glaciers_get_theme_svg('link');
		}
		$item_output = str_replace($args->link_after, '</span>' . $svg, $item_output);
	}

	return $item_output; // phpcs:ignore WordPress.Security.EscapeOutput
}

add_filter('walker_nav_menu_start_el', 'glaciers_nav_menu_social_icons', 10, 4);



/* CONVERT HEX to RGBA
@since 1.0.0
   ==================================================== */
if (!function_exists('hex2rgba')) :
	function hex2rgba($color, $opacity = 1, $css = false)
	{
		if (empty($color))
			return;

		$color = str_replace('#', '', $color);

		if (strlen($color) == 6) {
			$r = hexdec($color[0] . $color[1]);
			$g = hexdec($color[2] . $color[3]);
			$b = hexdec($color[4] . $color[5]);
		} elseif (strlen($color) == 3) {
			$r = hexdec($color[0] . $color[0]);
			$g = hexdec($color[1] . $color[1]);
			$b = hexdec($color[2] . $color[2]);
		} else {
			return false;
		}

		$opacity = floatval($opacity);

		if ($css)
			return 'rgba( ' . esc_attr($r) . ', ' . esc_attr($g) . ', ' . esc_attr($b) . ', ' . esc_attr($opacity) . ' )';
		else
			return compact(esc_attr($r), esc_attr($g), esc_attr($b), esc_attr($opacity));
	}
endif;


/* INLINE COLOUR PRESETS
   ====================================================*/
if (!function_exists('glaciers_colour_presets')) :
	function glaciers_colour_presets()
	{

		$glaciers_presets = get_theme_mod('glaciers_presets', 'preset1');
		switch (esc_attr($glaciers_presets)) {

			case "preset6":
				echo '--glaciers-primary: #2ac5b1;
					--glaciers-secondary: #c45829;
					--glaciers-tertiary: #0d786a;
					--glaciers-body: #e9e9e9;';
				break;

			case "preset5":
				echo '--glaciers-primary: #3a82cf;
					--glaciers-secondary: #cf983a;
					--glaciers-tertiary: #82560a;
					--glaciers-body: #e9e9e9;';
				break;

			case "preset4":
				echo '--glaciers-primary: #e54b4b;
					--glaciers-secondary: #14993a;
					--glaciers-tertiary: #4ce677;
					--glaciers-body: #e9e9e9;';
				break;

			case "preset3":
				echo '--glaciers-primary: #cb9d15;
					--glaciers-secondary: #1435cc;
					--glaciers-tertiary: #001780;
					--glaciers-body: #e9e9e9;';
				break;

			case "preset2":
				echo '--glaciers-primary: #a0c339;
					--glaciers-secondary: #9738c2;
					--glaciers-tertiary: #540b75;
					--glaciers-body: #e9e9e9;';
				break;

			default:
				echo '--glaciers-primary: #35a0a7;
					--glaciers-secondary: #a66335;
					--glaciers-tertiary: #592b0b;
					--glaciers-body: #e9e9e9;';
		}
	}
endif;


/* BLOG  NAVIGATION
Navigation for the blog
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_blog_nav')) :
	function glaciers_blog_nav()
	{
		the_posts_pagination(array(
			'prev_text'          => is_rtl() ? glaciers_get_theme_svg('caret-right') : glaciers_get_theme_svg('caret-left'),
			'next_text'          =>  is_rtl() ? glaciers_get_theme_svg('caret-left') :  glaciers_get_theme_svg('caret-right'),
			'before_page_number' => ''
		));
	}
endif;


/* POST NAVIGATION
Navigation for full posts.
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_post_pagination')) :
	function glaciers_post_pagination()
	{

		the_post_navigation(array(
			'next_text' => '<span class="nav-meta">' . (is_singular('post') ? esc_html__('Next post', 'glaciers') : esc_html__('Next', 'glaciers')) . '</span> <span class="post-title">%title</span>',
			'prev_text' => '<span class="nav-meta">' . (is_singular('post') ? esc_html__('Previous post', 'glaciers') : esc_html__('Previous', 'glaciers')) . '</span> <span class="post-title">%title</span>',
		));
	}
endif;

/* MULTIPAGE NAVIGATION
Navigation for splitting posts or pages into more than one page.
@since 1.0.0
==================================================== */
if (!function_exists('glaciers_multipage_pagination')) :
	function glaciers_multipage_pagination()
	{

		wp_link_pages(array(
			'before'      => '<div class="multi-page-links"><span class="multi-page-links-title">' . esc_html__('Pages:', 'glaciers') . '</span>',
			'after'       => '</div>',
			'link_before' => '<span>',
			'link_after'  => '</span>',
			'pagelink'    => '%',
			'separator'   => false,
		));
	}
endif;
