<?php
/**
 * This file includes the theme functions.
 *
 * @package Organic Horizon
 * @since Organic Horizon 1.0
 */

/*
-------------------------------------------------------------------------------------------------------
	Theme Setup
-------------------------------------------------------------------------------------------------------
*/

if ( ! function_exists( 'organic_horizon_setup' ) ) :

	/** Function organic_horizon_setup */
	function organic_horizon_setup() {

		/*
		* Enable support for translation.
		*/
		load_theme_textdomain( 'organic-horizon', get_template_directory() . '/languages' );

		/*
		* Enable support for RSS feed links to head.
		*/
		add_theme_support( 'automatic-feed-links' );

		/*
		* Enable selective refresh for widgets.
		*/
		add_theme_support( 'customize-selective-refresh-widgets' );

		/*
		* Enable support for post thumbnails.
		*/
		add_theme_support( 'post-thumbnails' );

		add_image_size( 'horizon-featured-large', 2400, 2400, true ); // Large Featured Image.
		add_image_size( 'horizon-featured-medium', 1800, 1800, true ); // Medium Featured Image.
		add_image_size( 'horizon-featured-small', 640, 640, true ); // Small Featured Image.
		add_image_size( 'horizon-featured-square', 1200, 1200, true ); // Square Featured Image.

		/*
		* Enable support for site title tag.
		*/
		add_theme_support( 'title-tag' );

		/*
		* Enable support for custom logo.
		*/
		add_theme_support( 'custom-logo', array(
			'height'      => 400,
			'width'       => 240,
			'flex-height' => true,
			'flex-width'  => true,
		) );

		/*
		* Enable support for custom menus.
		*/
		register_nav_menus( array(
			'main-menu' => esc_html__( 'Main Menu', 'organic-horizon' ),
			'social-menu' => esc_html__( 'Social Menu', 'organic-horizon' ),
		));

		/*
		* Enable support for custom header.
		*/
		register_default_headers( array(
			'default' => array(
			'url'   => get_template_directory_uri() . '/images/default-header.jpg',
			'thumbnail_url' => get_template_directory_uri() . '/images/default-header.jpg',
			'description'   => esc_html__( 'Default Custom Header', 'organic-horizon' ),
			),
		));
		$defaults = array(
			'video' 							=> true,
			'width'								=> 1800,
			'height'							=> 480,
			'flex-height'					=> true,
			'flex-width'					=> true,
			'default-image' 			=> get_template_directory_uri() . '/images/default-header.jpg',
			'header-text'					=> false,
			'uploads'							=> true,
		);
		add_theme_support( 'custom-header', $defaults );

		/*
		* Enable support for custom background.
		*/
		$defaults = array(
			'default-color'          => 'f4f4f4',
		);
		add_theme_support( 'custom-background', $defaults );

	}
endif; // End function organic_horizon_setup.
add_action( 'after_setup_theme', 'organic_horizon_setup' );

/*
-------------------------------------------------------------------------------------------------------
	Register Scripts
-------------------------------------------------------------------------------------------------------
*/

if ( ! function_exists( 'organic_horizon_enqueue_scripts' ) ) {

	/** Function organic_horizon_enqueue_scripts */
	function organic_horizon_enqueue_scripts() {

		// Enqueue Styles.
		wp_enqueue_style( 'organic-horizon-style', get_stylesheet_uri() );
		wp_enqueue_style( 'organic-horizon-style-conditionals', get_template_directory_uri() . '/css/style-conditionals.css', array( 'organic-horizon-style' ), '1.0' );
		wp_enqueue_style( 'organic-horizon-style-mobile', get_template_directory_uri() . '/css/style-mobile.css', array( 'organic-horizon-style' ), '1.0' );
		wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/css/font-awesome.css', array( 'organic-horizon-style' ), '1.0' );

		// Resgister Scripts.
		wp_register_script( 'jquery-sidr', get_template_directory_uri() . '/js/jquery.sidr.js', array( 'jquery' ), '1.0' );
		wp_register_script( 'jquery-fitvids', get_template_directory_uri() . '/js/jquery.fitvids.js', array( 'jquery' ), '1.0' );
		wp_register_script( 'jquery-brightness', get_template_directory_uri() . '/js/jquery.bgBrightness.js', array( 'jquery' ), '1.0' );
		wp_register_script( 'jquery-mousewheel', get_template_directory_uri() . '/js/jquery.mousewheel.js', array( 'jquery' ), '1.0' );

		// Enqueue Scripts.
		wp_enqueue_script( 'hoverIntent' );
		wp_enqueue_script( 'organic-horizon-custom', get_template_directory_uri() . '/js/jquery.custom.js', array( 'jquery', 'jquery-sidr', 'jquery-mousewheel', 'jquery-fitvids', 'jquery-brightness' ), '1.0', true );

		// Load single scripts only on single pages.
		if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
			wp_enqueue_script( 'comment-reply' );
		}
	}
}
add_action( 'wp_enqueue_scripts', 'organic_horizon_enqueue_scripts' );

/*
-------------------------------------------------------------------------------------------------------
	Theme Updater
-------------------------------------------------------------------------------------------------------
*/

/** Function organic_horizon_theme_updater */
function organic_horizon_theme_updater() {
	require( get_template_directory() . '/updater/theme-updater.php' );
}
add_action( 'after_setup_theme', 'organic_horizon_theme_updater' );

/*
-------------------------------------------------------------------------------------------------------
	Category ID to Name
-------------------------------------------------------------------------------------------------------
*/

/**
 * Changes category IDs to names.
 *
 * @param array $id IDs for categories.
 * @return array
 */

if ( ! function_exists( 'organic_horizon_cat_id_to_name' ) ) :

function organic_horizon_cat_id_to_name( $id ) {
	$cat = get_category( $id );
	if ( is_wp_error( $cat ) ) {
		return false; }
	return $cat->cat_name;
}
endif;

/*
-------------------------------------------------------------------------------------------------------
	Posted On Function
-------------------------------------------------------------------------------------------------------
*/

if ( ! function_exists( 'organic_horizon_posted_on' ) ) :

/** Function organic_horizon_posted_on */
function organic_horizon_posted_on() {
	if ( get_the_modified_time() != get_the_time() ) {
		printf( __( '<span class="%1$s">Updated:</span> %2$s', 'organic-horizon' ),
			'meta-prep meta-prep-author',
			sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
				esc_url( get_permalink() ),
				esc_attr( get_the_modified_time() ),
				esc_attr( get_the_modified_date() )
			)
		);
	} else {
		printf( __( '<span class="%1$s">Posted:</span> %2$s', 'organic-horizon' ),
			'meta-prep meta-prep-author',
			sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
				esc_url( get_permalink() ),
				esc_attr( get_the_time() ),
				get_the_date()
			)
		);
	}
}
endif;

/*
------------------------------------------------------------------------------------------------------
	Content Width
------------------------------------------------------------------------------------------------------
*/

if ( ! isset( $content_width ) ) { $content_width = 760; }

if ( ! function_exists( 'organic_horizon_content_width' ) ) :

/** Function organic_horizon_content_width */
function organic_horizon_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'organic_horizon_content_width', 760 );
}
endif;
add_action( 'after_setup_theme', 'organic_horizon_content_width', 0 );

/*
-------------------------------------------------------------------------------------------------------
	Comments Function
-------------------------------------------------------------------------------------------------------
*/

if ( ! function_exists( 'organic_horizon_comment' ) ) :

	/**
	 * Setup our comments for the theme.
	 *
	 * @param array $comment IDs for categories.
	 * @param array $args Comment arguments.
	 * @param array $depth Level of replies.
	 */
	function organic_horizon_comment( $comment, $args, $depth ) {
		switch ( $comment->comment_type ) :
			case 'pingback' :
			case 'trackback' :
		?>
		<li class="post pingback">
		<p><?php esc_html_e( 'Pingback:', 'organic-horizon' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( esc_html__( 'Edit', 'organic-horizon' ), '<span class="edit-link">', '</span>' ); ?></p>
	<?php
		break;
			default :
		?>
		<li <?php comment_class(); ?> id="<?php echo esc_attr( 'li-comment-' . get_comment_ID() ); ?>">

		<article id="<?php echo esc_attr( 'comment-' . get_comment_ID() ); ?>" class="comment">
			<footer class="comment-meta">
				<div class="comment-author vcard">
					<?php
						$avatar_size = 72;
					if ( '0' != $comment->comment_parent ) {
						$avatar_size = 48; }

						echo get_avatar( $comment, $avatar_size );

						/* translators: 1: comment author, 2: date and time */
						printf( __( '%1$s <br/> %2$s <br/>', 'organic-horizon' ),
							sprintf( '<span class="fn">%s</span>', wp_kses_post( get_comment_author_link() ) ),
							sprintf( '<a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a>',
								esc_url( get_comment_link( $comment->comment_ID ) ),
								get_comment_time( 'c' ),
								/* translators: 1: date, 2: time */
								sprintf( esc_html__( '%1$s, %2$s', 'organic-horizon' ), get_comment_date(), get_comment_time() )
							)
						);
						?>
					</div><!-- END .comment-author .vcard -->
				</footer>

				<div class="comment-content">
					<?php if ( '0' == $comment->comment_approved ) : ?>
					<em class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'organic-horizon' ); ?></em>
					<br />
				<?php endif; ?>
					<?php comment_text(); ?>
					<div class="reply">
					<?php comment_reply_link( array_merge( $args, array( 'reply_text' => esc_html__( 'Reply', 'organic-horizon' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
					</div><!-- .reply -->
					<?php edit_comment_link( esc_html__( 'Edit', 'organic-horizon' ), '<span class="edit-link">', '</span>' ); ?>
				</div>

			</article><!-- #comment-## -->

		<?php
		break;
		endswitch;
	}
endif; // Ends check for organic_horizon_comment().

/*
-------------------------------------------------------------------------------------------------------
	Custom Excerpt
-------------------------------------------------------------------------------------------------------
*/

/**
 * Replaces "[...]" (appended to automatically generated excerpts) with ... and
 * a 'Continue reading' link.
 *
 * @since Organic Horizon 1.0
 *
 * @return string 'Continue reading' link prepended with an ellipsis.
 */
	function organic_horizon_excerpt_more( $more ) {
		return '...';
	}
add_filter( 'excerpt_more', 'organic_horizon_excerpt_more' );

/*
-------------------------------------------------------------------------------------------------------
	Add Excerpt To Pages
-------------------------------------------------------------------------------------------------------
*/

/**
 * Add excerpt to pages.
 */

add_action( 'init', 'organic_horizon_page_excerpts' );
function organic_horizon_page_excerpts() {
	add_post_type_support( 'page', 'excerpt' );
}

/*
-------------------------------------------------------------------------------------------------------
	Custom Page Links
-------------------------------------------------------------------------------------------------------
*/

/**
 * Adds custom page links to pages.
 *
 * @param array $args for page links.
 * @return array
 */

if ( ! function_exists( 'organic_horizon_wp_link_pages_args_prevnext_add' ) ) :

function organic_horizon_wp_link_pages_args_prevnext_add( $args ) {
	global $page, $numpages, $more, $pagenow;

	if ( ! $args['next_or_number'] == 'next_and_number' ) {
		return $args; }

	$args['next_or_number'] = 'number'; // Keep numbering for the main part.
	if ( ! $more ) {
		return $args; }

	if ( $page -1 ) { // There is a previous page.
		$args['before'] .= _wp_link_page( $page -1 )
			. $args['link_before']. $args['previouspagelink'] . $args['link_after'] . '</a>'; }

	if ( $page < $numpages ) { // There is a next page.
		$args['after'] = _wp_link_page( $page + 1 )
			. $args['link_before'] . $args['nextpagelink'] . $args['link_after'] . '</a>'
			. $args['after']; }

	return $args;
}
endif;
add_filter( 'wp_link_pages_args', 'organic_horizon_wp_link_pages_args_prevnext_add' );

/*
-------------------------------------------------------------------------------------------------------
	Body Class
-------------------------------------------------------------------------------------------------------
*/

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */

if ( ! function_exists( 'organic_horizon_body_class' ) ) :

function organic_horizon_body_class( $classes ) {

	global $post;

	if ( $post ) {
		$page_template = get_page_template_slug($post->ID);
	} else {
		$page_template = false;
	}

	$header_image = get_header_image();
	$post_pages = is_home() || is_archive() || is_search() || is_attachment();

	if ( is_singular() ) {
		$classes[] = 'singular'; }

	if ( function_exists( 'the_custom_logo' ) && has_custom_logo() ) {
		$classes[] = 'horizon-has-logo'; }

	if ( is_page_template( 'template-landing.php' ) ) {
		$classes[] = 'horizon-landing-page'; }

	if ( is_page_template( 'template-portfolio.php' ) ) {
		$classes[] = 'horizon-portfolio-page'; }

	if ( is_page_template( 'template-testimonials.php' ) ) {
		$classes[] = 'horizon-testimonials-page'; }

	if ( 'blank' != get_theme_mod( 'organic_horizon_site_tagline' ) ) {
		$classes[] = 'horizon-desc-active';
	} else {
		$classes[] = 'horizon-desc-inactive';
	}

	if ( ! has_nav_menu( 'social-menu' ) ) {
		$classes[] = 'horizon-no-social-menu'; }

	if ( is_singular() && ! has_post_thumbnail() ) {
		$classes[] = 'horizon-no-img'; }

	if ( is_singular() && has_post_thumbnail() ) {
		$classes[] = 'horizon-has-img'; }

	if ( $post_pages && ! empty( $header_image ) || is_page() && ! has_post_thumbnail() && ! empty( $header_image ) ) {
		$classes[] = 'horizon-header-active';
	} else {
		$classes[] = 'horizon-header-inactive';
	}

	if ( is_header_video_active() && has_header_video() ) {
		$classes[] = 'horizon-header-video-active';
	} else {
		$classes[] = 'horizon-header-video-inactive';
	}

	if ( is_singular() ) {
		$classes[] = 'horizon-singular';
	}

	if ( class_exists( 'Organic_Widgets' ) && $page_template == 'templates/organic-custom-template.php' ) {
		$classes[] = 'organic-custom-template';
	} else {
		$classes[] = 'not-organic-custom-template';
	}

	if ( '' != get_theme_mod( 'background_image' ) ) {
		// This class will render when a background image is set
		// regardless of whether the user has set a color as well.
		$classes[] = 'horizon-background-image';
	} else if ( ! in_array( get_background_color(), array( '', get_theme_support( 'custom-background', 'default-color' ) ), true ) ) {
		// This class will render when a background color is set
		// but no image is set. In the case the content text will
		// Adjust relative to the background color.
		$classes[] = 'horizon-relative-text';
	}

	return $classes;
}
endif;
add_action( 'body_class', 'organic_horizon_body_class' );

/*
-------------------------------------------------------------------------------------------------------
	Includes
-------------------------------------------------------------------------------------------------------
*/

require_once( get_template_directory() . '/customizer/customizer.php' );
require_once( get_template_directory() . '/includes/style-options.php' );
require_once( get_template_directory() . '/includes/typefaces.php' );
require_once( get_template_directory() . '/includes/plugin-activation.php' );
require_once( get_template_directory() . '/includes/plugin-activation-class.php' );

/*
-------------------------------------------------------------------------------------------------------
	Load Jetpack File
-------------------------------------------------------------------------------------------------------
*/

if ( class_exists( 'Jetpack' ) ) {
	require get_template_directory() . '/jetpack/jetpack-setup.php';
}

/*
-------------------------------------------------------------------------------------------------------
	Load WooCommerce File
-------------------------------------------------------------------------------------------------------
*/

if ( class_exists( 'Woocommerce' ) ) {
	require get_template_directory() . '/includes/woocommerce-setup.php';
}
