<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package Personal_Trainer
 */

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

	// Adds a class with respect to layout selected.
	$layout  = personaltrainer_get_theme_layout();
	$sidebar = personaltrainer_get_sidebar_id();

	if ( 'no-sidebar-full-width' === $layout ) {
		$classes[] = 'no-sidebar full-width-layout';
	} elseif ( 'right-sidebar' === $layout ) {
		if ( '' !== $sidebar ) {
			$classes[] = 'two-columns-layout content-left';
		}
	}

	if ( ! personaltrainer_is_slider_displayed() && '' == personaltrainer_featured_overall_image() && ! personaltrainer_has_header_media_text() ) {
		$classes[] = 'header-media-disabled';
	}

	// Adds a class of (full-width|box) to blogs.
	if ( 'boxed' === get_theme_mod( 'personaltrainer_layout_type' ) ) {
		$classes[] = 'boxed-layout';
	} else {
		$classes[] = 'fluid-layout';
	}

	$phone = get_theme_mod( 'personaltrainer_contact_info_phone', '(123) 456-7890' );
	$email = get_theme_mod( 'personaltrainer_contact_info_email', 'something@domain.com' );

	if ( ! $phone && ! $email ) {
		$classes[] = 'contact-header-disabled';
	}

	$header_media_title    = get_theme_mod( 'personaltrainer_header_media_title' );
	$header_media_text     = get_theme_mod( 'personaltrainer_header_media_text' );
	$header_media_url      = get_theme_mod( 'personaltrainer_header_media_url', '' );
	$header_media_url_text = get_theme_mod( 'personaltrainer_header_media_url_text' );

	if ( is_front_page() && ! $header_media_title && ! $header_media_text && ! $header_media_url && ! $header_media_url_text ) {
		$classes[] = 'no-header-media-text';
	}

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

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function personaltrainer_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'personaltrainer_pingback_header' );

/**
 * Remove first post from blog as it is already show via recent post template
 */
function personaltrainer_alter_home( $query ) {
	if ( $query->is_home() && $query->is_main_query() ) {
		$cats = get_theme_mod( 'personaltrainer_front_page_category' );

		if ( is_array( $cats ) && ! in_array( '0', $cats ) ) {
			$query->query_vars['category__in'] = $cats;
		}
	}
}
add_action( 'pre_get_posts', 'personaltrainer_alter_home' );

/**
 * Function to add Scroll Up icon
 */
function personaltrainer_scrollup() {
	$disable_scrollup = get_theme_mod( 'personaltrainer_disable_scrollup' );

	if ( $disable_scrollup ) {
		return;
	}

	echo '
		<div class="scrollup">
			<a href="#masthead" id="scrollup" class="fa fa-sort-asc" aria-hidden="true"><span class="screen-reader-text">' . esc_html__( 'Scroll Up', 'personal-trainer' ) . '</span></a>
		</div>' ;
}
add_action( 'wp_footer', 'personaltrainer_scrollup', 1 );

if ( ! function_exists( 'personaltrainer_content_nav' ) ) :
	/**
	 * Display navigation/pagination when applicable
	 *
	 * @since Personal Trainer 0.1
	 */
	function personaltrainer_content_nav() {
		global $wp_query;

		// Don't print empty markup in archives if there's only one page.
		if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) ) {
			return;
		}

		$pagination_type = get_theme_mod( 'personaltrainer_pagination_type', 'default' );

		/**
		 * Check if navigation type is Jetpack Infinite Scroll and if it is enabled, else goto default pagination
		 * if it's active then disable pagination
		 */
		if ( ( 'infinite-scroll' === $pagination_type ) && class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'infinite-scroll' ) ) {
			return false;
		}

		if ( 'numeric' === $pagination_type && function_exists( 'the_posts_pagination' ) ) {
			the_posts_pagination( array(
				'prev_text'          => esc_html__( 'Previous page', 'personal-trainer' ),
				'next_text'          => esc_html__( 'Next page', 'personal-trainer' ),
				'before_page_number' => '<span class="meta-nav screen-reader-text">' . esc_html__( 'Page', 'personal-trainer' ) . ' </span>',
			) );
		} else {
			the_posts_navigation();
		}
	}
endif; // personaltrainer_content_nav

/**
 * Check if a section is enabled or not based on the $value parameter
 * @param  string $value Value of the section that is to be checked
 * @return boolean return true if section is enabled otherwise false
 */
function personaltrainer_check_section( $value ) {
	global $wp_query;

	// Get Page ID outside Loop
	$page_id = absint( $wp_query->get_queried_object_id() );

	// Front page displays in Reading Settings
	$page_for_posts = absint( get_option( 'page_for_posts' ) );

	return ( 'entire-site' == $value  || ( ( is_front_page() || ( is_home() && $page_for_posts !== $page_id ) ) && 'homepage' == $value ) );
}

/**
 * Return the first image in a post. Works inside a loop.
 * @param [integer] $post_id [Post or page id]
 * @param [string/array] $size Image size. Either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32).
 * @param [string/array] $attr Query string or array of attributes.
 * @return [string] image html
 *
 * @since Personal Trainer 0.1
 */

function personaltrainer_get_first_image( $postID, $size, $attr, $src = false ) {
	ob_start();

	ob_end_clean();

	$image 	= '';

	$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_post_field( 'post_content', $postID ) , $matches );

	if( isset( $matches[1][0] ) ) {
		//Get first image
		$first_img = $matches[1][0];

		if ( $src ) {
			//Return url of src is true
			return $first_img;
		}

		return '<img class="pngfix wp-post-image" src="' . $first_img . '">';
	}

	return false;
}

function personaltrainer_get_theme_layout() {
	$layout = get_theme_mod( 'personaltrainer_default_layout', 'right-sidebar' );

	if ( is_home() || is_archive() || is_search() ) {
		$layout = get_theme_mod( 'personaltrainer_homepage_archive_layout', 'no-sidebar-full-width' );
	}

	return $layout;
}

function personaltrainer_get_sidebar_id() {
	$sidebar = '';

	$layout = personaltrainer_get_theme_layout();

	if ( 'no-sidebar-full-width' === $layout ) {
		return $sidebar;
	}

	if ( is_active_sidebar( 'sidebar-1' ) ) {
		$sidebar = 'sidebar-1'; // Primary Sidebar.
	}

	return $sidebar;
}

/**
 * Featured content posts
 */
function personaltrainer_get_featured_posts() {
	$number = get_theme_mod( 'personaltrainer_featured_content_number', 3 );

	$post_list    = array();

	$args = array(
		'posts_per_page'      => $number,
		'post_type'           => 'post',
		'ignore_sticky_posts' => 1, // ignore sticky posts.
	);

	// Get valid number of posts.
	$args['post_type'] = 'featured-content';

	for ( $i = 1; $i <= $number; $i++ ) {
		$post_id = get_theme_mod( 'personaltrainer_featured_content_cpt_' . $i );

		if ( $post_id ) {
			$post_list = array_merge( $post_list, array( $post_id ) );
		}
	}

	$args['post__in'] = $post_list;
	$args['orderby']  = 'post__in';

	$featured_posts = get_posts( $args );

	return $featured_posts;
}
