<?php
/**
 * Additional features to allow styling of the templates
 *
 * @package WordPress
 * @subpackage WPCargo_Theme_Pro
 * @since 1.0
 */
/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function wpctp_body_classes( $classes ) {
	$template = get_page_template_slug( get_the_ID() );
	$layout = wpctp_get_option( 'site_layout' );
	// Add class of group-blog to blogs with more than 1 published author.
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}
	// Add class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}
	// Add class if we're viewing the Customizer for easier styling of theme options.
	if ( is_customize_preview() ) {
		$classes[] = 'wpctp-customizer';
	}
	// Add class on front page.
	if ( is_front_page() && 'posts' !== get_option( 'show_on_front' ) ) {
		$classes[] = 'wpctp-front-page';
	}
	// Add a class if there is a custom header.
	if ( has_header_image() ) {
		$classes[] = 'has-header-image';
	}
	if (
		( in_array( $layout, array( 'sidebar-content', 'content-sidebar' ) ) ) ||
		( is_page_template() && ( $template == 'page-sidebar-content.php' ) ) ||
		( is_page_template() && ( $template == 'page-content-sidebar.php' ) )
	) {
		$classes[] = 'has-sidebar';
	}
	if ( $layout ) {
		$classes[] = $layout;
	}
	if (
		( is_page_template() && ( $template == 'page-fullwidth.php' ) )
	) {
		$classes[] = 'full-width-content';
		unset( $classes[array_search('content-sidebar', $classes)] );
		unset( $classes[array_search('sidebar-content', $classes)] );
	}
	if (
		( is_page_template() && ( $template == 'page-sidebar-content.php' ) )
	) {
		$classes[] = 'sidebar-content';
		unset( $classes[array_search('full-width-content', $classes)] );
		unset( $classes[array_search('content-sidebar', $classes)] );
	}
	if (
		( is_page_template() && ( $template == 'page-content-sidebar.php' ) ) 
	) {
		$classes[] = 'content-sidebar';
		unset( $classes[array_search('full-width-content', $classes)] );
		unset( $classes[array_search('sidebar-content', $classes)] );
	}
	$classes[] = 'footer-widgets-' . ( wpctp_get_option( 'footer_widgets_column' ) ? : 4 );
	if( has_post_thumbnail() && ( is_page() || is_single() ) ) {
		$classes[] = 'has-image';
	}else{
		$classes[] = 'no-image';
	}
	if( is_search() ){
		$classes[] = 'archive';
	}
	if( wpctp_get_option( 'page_layout' ) ){
		$classes[] = 'wpctp-' . wpctp_get_option( 'page_layout' );
	}
	return $classes;
}
add_filter( 'body_class', 'wpctp_body_classes' );
/**
 * Count our number of active panels.
 *
 * Primarily used to see if we have any panels active, duh.
 */
function wpctp_panel_count() {
	$panel_count = 0;
	/**
	 * Filter number of front page sections in WPCargo Pro.
	 *
	 * @since WPCargo Pro 1.0
	 *
	 * @param int $num_sections Number of front page sections.
	 */
	$num_sections = apply_filters( 'wpctp_front_page_sections', 4 );
	// Create a setting and control for each of the sections available in the theme.
	for ( $i = 1; $i < ( 1 + $num_sections ); $i++ ) {
		if ( get_theme_mod( 'panel_' . $i ) ) {
			$panel_count++;
		}
	}
	return $panel_count;
}
/**
 * Checks to see if we're on the homepage or not.
 */
function wpctp_is_frontpage() {
	return ( is_front_page() && ! is_home() );
}
function wpctp_get_option( $key, $echo = false ) {
 	$options = get_option( 'wpctp-settings' );
 	$output = ! empty( $options[$key] ) ? $options[$key] : '';
	if( $echo ) :
		echo $output;
		return;
	endif;
	return $output;
}
function wpctp_reset(){
	if( isset( $_REQUEST['reset'] ) && 'true' === $_REQUEST['reset'] ){
		return true;
	}
	return false;
}
function wpctp_get_logo(){
	if( wpctp_get_option( 'logo' ) && ! wpctp_reset() ){
		return wpctp_get_option( 'logo' );
	}
	return WPCTP_IMAGES_URL . '/wpcargo-theme-pro.png';
}
function wpctp_get_sticky_logo(){
	if( wpctp_get_option( 'sticky_logo' ) && ! wpctp_reset() ){
		return wpctp_get_option( 'sticky_logo' );
	}
	return WPCTP_IMAGES_URL . '/wpcargo-theme-pro.png';
}
function wpctp_get_transparent_logo(){
	if( wpctp_get_option( 'transparent_logo' ) && ! wpctp_reset() ){
		return wpctp_get_option( 'transparent_logo' );
	}
	return WPCTP_IMAGES_URL . '/wpcargo-theme-pro-transparent.png';
}
function wpctp_get_font_family( $option_name ){
	$font_family = json_decode( wpctp_get_option( $option_name ), true );
	if( empty( $font_family ) ){
		return 'Roboto Condensed';
	}
	return $font_family['family'];
}
function wpctp_get_google_font_url( $font_array ) {
	if ( count( $font_array ) > 0 ) {
		$font_array = wpctp_get_google_font( $font_array );
		$base_url = '';
		$font_familys = array();
		$subsets = array();
		foreach( $font_array as $font ) {
			if ( isset( $font['family'] ) ){
				$font_familys[] = str_replace( ' ', '+', $font['family'] ) . ':' . implode( ',', array_unique( $font['variants'] ) );
				$subsets = array_merge( $subsets, array_unique( $font['subsets'] ) );
			}
		}
		if ( count( $font_familys ) > 0 ) {
			$base_url .= implode('|', $font_familys );
		}
		if ( count( $subsets ) > 0) {
			$base_url .= '&subset=' . implode( ',', $subsets );
		}
		if ( $base_url != '' ) {
	  		return '//fonts.googleapis.com/css?family=' . $base_url;
		}
	}
	return null;
}
function wpctp_get_google_font( $font_array ){
	$fonts = array();		 
	foreach ( $font_array as $font ){	
		if( !empty( $font ) ){
			if ( !array_key_exists( $font['family'], $fonts ) ){
				$fonts[$font['family']] = $font;
			} else {
				$fonts[$font['family']]['variants'] = array_merge( $fonts[$font['family']]['variants'], $font['variants'] );
				$fonts[$font['family']]['subsets'] = array_merge( $fonts[$font['family']]['subsets'], $font['subsets'] );
			}
		}
	
	}
	return $fonts;
}
function wpctp_pixels_css(){
	return array( 'font-size', 'border-width', 'border-top-width', 'border-bottom-width', 'border-left-width', 'border-right-width', 'width', 'height', 'padding', 'padding-top', 'padding-bottom', 'padding-left', 'padding-right', 'margin', 'margin-top', 'margin-bottom', 'margin-left', 'margin-right' );
}
function wpctp_get_css( $properties, $key ){
	$options = get_option( 'wpctp-settings' );
	$value = wpctp_get_option( $key );
	if(
		( $properties == 'background-image' ) ||
		( $properties == 'font-family' )
	){
		$value = $key;
	}
	if(
		isset( $options[$key] ) && empty( $value ) && 
		in_array( $properties, array( 'border-top-color', 'border-bottom-color', 'border-left-color', 'border-right-color', 'border-color', 'background-color', 'color' ) )
	){
		$value = 'transparent';
	}
	if( 
		( empty( $value ) || ( $value == wpctp_get_default( $key ) ) ) &&
		( $properties != 'font-family' )
	){
		return;
	}
	if(
		( $properties == 'background-image' ) &&
		$value
	){
		return "{$properties}:url('{$value}');";
	}
	$pixels = wpctp_pixels_css();
	$default = array_merge( $pixels, array( 'font-family' ) );
	if( 
		in_array( $properties, $pixels ) && 
		( wpctp_get_default( $key ) != $value ) &&
		$value
	){
		return "{$properties}:{$value}px;";
	}
	if(
		( ! in_array( $properties, $default ) ) &&
		$value
	){
		return "{$properties}:{$value};";
	}
	if(
		( $properties == 'font-family' ) &&
		$value
	){
		return "{$properties}:'{$value}';";
	}
}
function wpctp_get_styles( $selector, $styles ){
	$styles = array_filter( $styles );
	if( $styles ){echo "$selector{";}
	foreach( $styles as $style ){
		if( ! empty( $style ) ){
			echo $style;
		}
	}
	if( $styles ){echo "}";}
}
function wpctp_image( $post_id, $index = 0, $size = 'full' ){
	$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
	return $image[$index];
}
function wpctp_do_post_title() {
	global $wp_query;
	$_title = get_the_title();
	if( $wp_query->is_archive ){
		$_title = $wp_query->queried_object->name;
	}
	if( $wp_query->is_404 ){
		$_title = __( 'Not found, error 404', 'wpctp' );
	}
	if( $wp_query->is_search ){
		$_title = __( 'Search Results for: ' . $wp_query->query['s'], 'wpctp' );
	}
	$title = apply_filters( 'wpctp_post_title_text', $_title );
	if ( 0 === mb_strlen( $title ) )
		return;
	//* Wrap in H1 on singular pages
	$wrap = 'h1';
	//* Also, if HTML5 with semantic headings, wrap in H1
	$wrap = apply_filters( 'wpctp_entry_title_wrap', $wrap );
	//* Build the output
	$output = wpctp_markup( array(
		'html5'   => "<{$wrap} %s>",
		'xhtml'   => sprintf( '<%s class="entry-title">%s</%s>', $wrap, $title, $wrap ),
		'context' => 'entry-title',
		'echo'    => false,
	) );
	$output .= wpctp_html5() ? "{$title}</{$wrap}>" : '';
	echo apply_filters( 'wpctp_post_title_output', "$output \n" );
}
function wpctp_is_home(){
	if( get_option('show_on_front') == 'page' && ( get_option('page_on_front') == get_the_ID() )){
		return true;
	}
	return false;
}
function wpctp_get_option_settings(){
	$defaults = array();
	foreach( wpctp_default_args() as $option ){
		if( ! empty( $option['sub_tab'] ) ){
			foreach( $option['sub_tab'] as $id => $fields ){
				foreach( $fields['fields'] as $_id => $field ){
					if( ! empty( $field['default'] ) ){
						$defaults[$_id] = $field['default'];
					}
				}
			}
		}
	}
	return $defaults;
}
function wpctp_get_default( $key ){
	$options = wpctp_default_args();
	$defaults = array();
	foreach( $options as $option ){
		if( ! empty( $option['sub_tab'] ) ){
			foreach( $option['sub_tab'] as $id => $fields ){
				foreach( $fields['fields'] as $_id => $field ){
					if( isset( $field['default'] ) ){
						$defaults[$_id] = $field['default'];
					}
				}
			}
		}
	}
	if( isset( $defaults[$key] ) ) {
		return $defaults[$key];
	}
	return;
}
function wpctp_get( $key ){
	if( isset( $_GET[$key] ) ) {
		return $_GET[$key];
	}
	return;
}