<?php
/**
 * Functions hooked to core hooks
 *
 * @package Blorigan
 */

if ( ! function_exists( 'blorigan_customize_search_form' ) ) :

	/**
	 * Customize search form.
	 *
	 * @since 1.0.0
	 *
	 * @return string The search form HTML output.
	 */
	function blorigan_customize_search_form() {
		$form = '<form role="search" method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
			<label>
			<span class="screen-reader-text">' . esc_html_x( 'Search for:', 'label', 'blorigan' ) . '</span>
			<input type="search" class="search-field" placeholder="' . esc_attr__( 'Search&hellip;', 'blorigan' ) . '" value="' . get_search_query() . '" name="s" title="' . esc_attr_x( 'Search for:', 'label', 'blorigan' ) . '" />
			</label>
			<input type="submit" class="search-submit" value="&#xf002;" /></form>';

		return $form;
	}

endif;

add_filter( 'get_search_form', 'blorigan_customize_search_form', 15 );

if ( ! function_exists( 'blorigan_exclude_category_in_blog_page' ) ) :

	/**
	 * Exclude category in blog page.
	 *
	 * @since 1.0.0
	 *
	 * @param WP_Query $query WP_Query instance.
	 * @return WP_Query Modified instance.
	 */
	function blorigan_exclude_category_in_blog_page( $query ) {
		if ( $query->is_home() && $query->is_main_query() ) {
			$exclude_categories = blorigan_get_option( 'exclude_categories' );
			if ( ! empty( $exclude_categories ) ) {
				$categories_raw = explode( ',', $exclude_categories );
				$cats           = array();
				if ( ! empty( $categories_raw ) ) {
					foreach ( $categories_raw as $c ) {
						if ( absint( $c ) > 0 ) {
							$cats[] = absint( $c );
						}
					}
					if ( ! empty( $cats ) ) {
						$exclude_text = '';
						$exclude_text = '-' . implode( ',-', $cats );
						$query->set( 'cat', $exclude_text );
					}
				}
			}
		}

		return $query;
	}

endif;

add_filter( 'pre_get_posts', 'blorigan_exclude_category_in_blog_page' );

if ( ! function_exists( 'blorigan_implement_excerpt_length' ) ) :

	/**
	 * Implement excerpt length.
	 *
	 * @since 1.0.0
	 *
	 * @param int $length The number of words.
	 * @return int Excerpt length.
	 */
	function blorigan_implement_excerpt_length( $length ) {
		if ( is_admin() ) {
			return $length;
		}

		$excerpt_length = blorigan_get_option( 'excerpt_length' );

		if ( absint( $excerpt_length ) > 0 ) {
			$length = absint( $excerpt_length );
		}

		return $length;
	}

endif;

add_filter( 'excerpt_length', 'blorigan_implement_excerpt_length', 999 );

if ( ! function_exists( 'blorigan_implement_read_more' ) ) :

	/**
	 * Implement read more in excerpt.
	 *
	 * @since 1.0.0
	 *
	 * @param string $more The string shown within the more link.
	 * @return string The excerpt.
	 */
	function blorigan_implement_read_more( $more ) {
		if ( is_admin() ) {
			return $more;
		}

		$more = '&hellip;';
		return $more;
	}

endif;

add_filter( 'excerpt_more', 'blorigan_implement_read_more' );

if ( ! function_exists( 'blorigan_custom_body_class' ) ) :

	/**
	 * Custom body class.
	 *
	 * @since 1.0.0
	 *
	 * @param string|array $input One or more classes to add to the class list.
	 * @return array Array of classes.
	 */
	function blorigan_custom_body_class( $input ) {
		// Adds a class of group-blog to blogs with more than 1 published author.
		if ( is_multi_author() ) {
			$input[] = 'group-blog';
		}

		// Header layout.
		$input[] = 'header-layout-1';

		// Archive layout.
		$archive_layout = blorigan_get_option( 'archive_layout' );
		$archive_layout = apply_filters( 'blorigan_filter_theme_archive_layout', $archive_layout );
		$input[]        = 'archive-layout-' . esc_attr( $archive_layout );

		// Carousel display.
		$featured_slider_enable_carousel = blorigan_get_option( 'featured_slider_enable_carousel' );
		$featured_slider_carousel_number = blorigan_get_option( 'featured_slider_carousel_number' );
		$number                          = ( false === $featured_slider_enable_carousel ) ? 1 : $featured_slider_carousel_number;
		$input[]                         = 'carousel-display-' . absint( $number );

		// Global layout.
		$global_layout = blorigan_get_option( 'global_layout' );
		$global_layout = apply_filters( 'blorigan_filter_theme_global_layout', $global_layout );

		$input[] = 'global-layout-' . esc_attr( $global_layout );

		// Common class for three columns.
		switch ( $global_layout ) {
			case 'three-columns':
				$input[] = 'three-columns-enabled';
				break;

			default:
				break;
		}

		return $input;
	}

endif;

add_filter( 'body_class', 'blorigan_custom_body_class' );

if ( ! function_exists( 'blorigan_custom_content_width' ) ) :

	/**
	 * Custom content width.
	 *
	 * @since 1.0.0
	 */
	function blorigan_custom_content_width() {
		global $content_width;

		$global_layout = blorigan_get_option( 'global_layout' );
		$global_layout = apply_filters( 'blorigan_filter_theme_global_layout', $global_layout );

		switch ( $global_layout ) {
			case 'no-sidebar':
				$content_width = 1140;
				break;

			case 'three-columns':
				$content_width = 555;
				break;

			case 'left-sidebar':
			case 'right-sidebar':
				$content_width = 749;
				break;

			default:
				break;
		}
	}

endif;

add_filter( 'template_redirect', 'blorigan_custom_content_width' );

if ( ! function_exists( 'blorigan_footer_goto_top' ) ) :

	/**
	 * Go to top.
	 *
	 * @since 1.0.0
	 */
	function blorigan_footer_goto_top() {
		$go_to_top_status = blorigan_get_option( 'go_to_top_status' );
		if ( true === $go_to_top_status ) {
			echo '<a href="#page" class="scrollup" id="btn-scrollup"><i class="fa fa-angle-up"></i></a>';
		}
	}

endif;

add_action( 'wp_footer', 'blorigan_footer_goto_top', 20 );
