<?php
/**
 * Theme wrapper
 *
 * @link http://scribu.net/wordpress/theme-wrappers.html
 */

function ya_template_path() {
	return YA_Wrapping::$main_template;
}

function ya_sidebar_path() {
	return YA_Wrapping::sidebar();
}

add_filter('template_include', array('YA_Wrapping', 'wrap'), 99);

/**
 * Page titles
 */
function ya_title() {
	if (is_home()) {
		if (get_option('page_for_posts', true)) {
			echo get_the_title(get_option('page_for_posts', true));
		} else {
			_e('Latest Posts', 'yatheme');
		}
	} elseif (is_archive()) {
		$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
		if ($term) {
			echo $term->name;
		} elseif (is_post_type_archive()) {
			echo get_queried_object()->labels->name;
		} elseif (is_day()) {
			printf(__('Daily Archives: %s', 'yatheme'), get_the_date());
		} elseif (is_month()) {
			printf(__('Monthly Archives: %s', 'yatheme'), get_the_date('F Y'));
		} elseif (is_year()) {
			printf(__('Yearly Archives: %s', 'yatheme'), get_the_date('Y'));
		} elseif (is_author()) {
			printf(__('Author Archives: %s', 'yatheme'), get_the_author());
		} else {
			single_cat_title();
		}
	} elseif (is_search()) {
		printf(__('Search Results for <small>%s</small>', 'yatheme'), get_search_query());
	} elseif (is_404()) {
		_e('Not Found', 'yatheme');
	} else {
		the_title();
	}
}

/**
 * Show an admin notice if .htaccess isn't writable
 */
function ya_htaccess_writable() {
	if (!is_writable(get_home_path() . '.htaccess')) {
		if (current_user_can('administrator')) {
			add_action('admin_notices', create_function('', "echo '<div class=\"error\"><p>" . sprintf(__('Please make sure your <a href="%s">.htaccess</a> file is writable ', 'yatheme'), admin_url('options-permalink.php')) . "</p></div>';"));
		}
	}
}
add_action('admin_init', 'ya_htaccess_writable');

/**
 * Return WordPress subdirectory if applicable
 */
function wp_base_dir() {
	preg_match('!(https?://[^/|"]+)([^"]+)?!', site_url(), $matches);
	if (count($matches) === 3) {
		return end($matches);
	} else {
		return '';
	}
}

/**
 * Opposite of built in WP functions for trailing slashes
 */
function leadingslashit($string) {
	return '/' . unleadingslashit($string);
}

function unleadingslashit($string) {
	return ltrim($string, '/');
}

function add_filters($tags, $function) {
	foreach($tags as $tag) {
		add_filter($tag, $function);
	}
}

function is_element_empty($element) {
	$element = trim($element);
	return empty($element) ? false : true;
}

function is_customize(){
	return isset($_POST['customized']) && ( isset($_POST['customize_messenger_chanel']) || isset($_POST['wp_customize']) );
}

function is_ajax(){
	return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}


/**
 *  Is active sidebar
 * */
function is_active_sidebar_YA($index) {
	global $wp_registered_widgets;
	
	$index = ( is_int($index) ) ? "sidebar-$index" : sanitize_title($index);
	$sidebars_widgets = wp_get_sidebars_widgets();
	if (!empty($sidebars_widgets[$index])) {
		foreach ($sidebars_widgets[$index] as $i => $id) {
			$id_base = preg_replace( '/-[0-9]+$/', '', $id );
			
			if ( isset($wp_registered_widgets[$id]) ) {
				$widget = new WP_Widget($id_base, $wp_registered_widgets[$id]['name']);

				if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $id, $matches ) )
					$number = $matches[1];
					
				$instances = get_option($widget->option_name);
				
				if ( isset($instances) && isset($number) ) {
					$instance = $instances[$number];
					
					if ( isset($instance['widget_display']) && check_wdisplay($instance['widget_display']) == false ) {
						unset($sidebars_widgets[$index][$i]);
					}
				}
			}
		}
		
		if ( empty($sidebars_widgets[$index]) ) return false;
		
	} else return false;
	
	return true;
}


/**
 * Check widget display
 * */
function check_wdisplay ($widget_display){
	
	$YA_Menu_Checkbox = new YA_Menu_Checkbox;
	
	if ( isset($widget_display['display_select']) && $widget_display['display_select'] == 'if_selected' ) {
		
		if (isset($widget_display['checkbox'])) {
			
			if (isset($widget_display['checkbox']['users'])) {
				global $user_ID;
				
				foreach ($widget_display['checkbox']['users'] as $key => $value) {
					
					if ( ($key == 'login' && $user_ID) || ($key == 'logout' && !$user_ID) ){
						
						if (isset($widget_display['checkbox']['menus'])) {
							
							foreach ($widget_display['checkbox']['menus'] as $menu_id => $item_ids) {
								
								if ( $YA_Menu_Checkbox->is_menu_item_active($menu_id, $item_ids) ) return true;
							}
						}
					}
				}
			}
			
			return false;
			
		} else return false ;
		
	} elseif ( isset($widget_display['display_select']) && $widget_display['display_select'] == 'if_no_selected' ) {
		
		if (isset($widget_display['checkbox'])) {
			
			if (isset($widget_display['checkbox']['users'])) {
				global $user_ID;
				
				foreach ($widget_display['checkbox']['users'] as $key => $value) {
					if ( ($key == 'login' && $user_ID) || ($key == 'logout' && !$user_ID) ) return false;
				}
			}
			
			if (isset($widget_display['checkbox']['menus'])) {
							
				foreach ($widget_display['checkbox']['menus'] as $menu_id => $item_ids) {
					
					if ( $YA_Menu_Checkbox->is_menu_item_active($menu_id, $item_ids) ) return false;
				}
			}
		}
	}
	
	return true ;
}



function check_wdisplayxxx ($widget_display){
		
	if ( isset($widget_display['display_select']) && $widget_display['display_select'] == 'if_selected' ) {
		
		if (isset($widget_display['checkbox'])) {
			
			if (isset($widget_display['checkbox']['users'])) {
				global $user_ID;
				
				foreach ($widget_display['checkbox']['users'] as $key => $value) {
					if ( ($key == 'login' && $user_ID) || ($key == 'logout' && !$user_ID) ){
					
						if (isset($widget_display['checkbox']['ganeral'])) {
							foreach ($widget_display['checkbox']['ganeral'] as $key => $value) {
								$is = 'is_'.$key;
								if ( $is() === true ) return true;
							}
						}
						
						if (isset($widget_display['checkbox']['cats'])) {
							foreach ($widget_display['checkbox']['cats'] as $catid => $cat_name) {
								if ( is_category($catid) === true ) return true;
							}
						}
						
						if (isset($widget_display['checkbox']['pages'])) {
							foreach ($widget_display['checkbox']['pages'] as $pageid => $page_name) {
								if ( is_page($pageid) === true ) return true;
							}
						}
					}
				}
			}
			
			return false;
			
		} else return false ;
		
	} elseif ( isset($widget_display['display_select']) && $widget_display['display_select'] == 'if_no_selected' ) {
		
		if (isset($widget_display['checkbox'])) {
			
			if (isset($widget_display['checkbox']['users'])) {
				global $user_ID;
				
				foreach ($widget_display['checkbox']['users'] as $key => $value) {
					if ($key == 'login' && $user_ID) return false;
					if ($key == 'logout' && !$user_ID) return false;
				}
			}
			
			if (isset($widget_display['checkbox']['ganeral'])) {
				foreach ($widget_display['checkbox']['ganeral'] as $key => $value) {
					$is = 'is_'.$key;
					if ( $is() === true ) return false;
				}
			}
			
			if (isset($widget_display['checkbox']['cats'])) {
				foreach ($widget_display['checkbox']['cats'] as $catid => $cat_name) {
					if ( is_category($catid) === true ) return false;
				}
			}
			
			if (isset($widget_display['checkbox']['pages'])) {
				foreach ($widget_display['checkbox']['pages'] as $pageid => $page_name) {
					if ( is_page($pageid) === true ) return false;
				}
			}
		}
	} else return true;
	
	return true ;
}

	
	
/**
 * Get Social share
 * */
function get_social() {
	global $post;
	
	$social['social-share'] = ya_options()->getCpanelValue('social-share');
	$social['social-share-fb'] = ya_options()->getCpanelValue('social-share-fb');
	$social['social-share-tw'] = ya_options()->getCpanelValue('social-share-tw');
	$social['social-share-in'] = ya_options()->getCpanelValue('social-share-in');
	$social['social-share-go'] = ya_options()->getCpanelValue('social-share-go');
	//$social['social-share-pi'] = ya_options()->getCpanelValue('social-share-pi');
	
	if (!$social['social-share']) return false;
	
	//$options = $this->get_wp_social_share_options();
	$permalinked = urlencode(get_permalink($post->ID));
	$spermalink = get_permalink($post->ID);
	$title = urlencode($post->post_title);
	$stitle = $post->post_title;
	
	$data = '<div class="social-share">';
	$data .= '<style type="text/css">
				.social-share{
					display: table;
				    margin: 5px;
				    width: 100%;
				}
				.social-share-item{
					float: left;
				}
				.social-share-fb{
					margin-right: 25px;
                }
			</style>';
	
	if ($social['social-share-fb']) {
		$data .='<div class="social-share-fb social-share-item" >';
		$data .= '<div id="fb-root"></div>
					<script>(function(d, s, id) {
					  var js, fjs = d.getElementsByTagName(s)[0];
					  if (d.getElementById(id)) return;
					  js = d.createElement(s); js.id = id;
					  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
					  fjs.parentNode.insertBefore(js, fjs);
					}(document, \'script\', \'facebook-jssdk\'));</script>';
		$data .= '<div class="fb-like" data-href="'.$spermalink.'" data-send="true" data-layout="button_count" data-width="200" data-show-faces="false"></div>';
		$data .= '</div> <!--Facebook Button-->';
	}
		
	if ($social['social-share-tw']) {
		$data .='<div class="social-share-twitter social-share-item" >
					<a href="https://twitter.com/share" class="twitter-share-button" data-url="'. $spermalink .'" data-text="'.$stitle.'" data-count="horizontal">Tweet</a><script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
				</div> <!--Twitter Button-->';
	}
	
	if ($social['social-share-go']) {
		$data .= '<div class="social-share-google-plus social-share-item">
					<!-- Place this tag where you want the +1 button to render -->
					<div class="g-plusone" data-size="medium" data-href="'. $permalinked .'"></div>
		
					<!-- Place this render call where appropriate -->
					<script type="text/javascript">
					  (function() {
						var po = document.createElement("script"); po.type = "text/javascript"; po.async = true;
						po.src = "https://apis.google.com/js/plusone.js";
						var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(po, s);
					  })();
					</script>
				</div> <!--google plus Button-->';
	}
	
	if ($social['social-share-in']) {
		$data .= '<div class="social-share-linkedin social-share-item">
					<script src="http://platform.linkedin.com/in.js" type="text/javascript"></script>
					<script type="IN/Share" data-url="'. $permalinked .'" data-counter="right"></script>
				</div> <!--linkedin Button-->';
	}

//	if ($social['social-share-pi']) {
//		$data .= '<div class="social-share-pinterest social-share-item">
//					<a href="//pinterest.com/pin/create/button/?url='.$permalinked.'" data-pin-do="buttonPin" data-pin-config="beside"><img src="#" alt="test"></a>
//					<script type="text/javascript" src="//assets.pinterest.com/js/pinit.js"></script>
//				</div> <!--pinterest Button-->';
//	}
	$data .= '</div>';
	echo $data;

}


/**
 * Use Bootstrap's media object for listing comments
 *
 * @link http://twitter.github.com/bootstrap/components.html#media
 */

function ya_get_avatar($avatar) {
	$avatar = str_replace("class='avatar", "class='avatar pull-left media-object", $avatar);
	return $avatar;
}
add_filter('get_avatar', 'ya_get_avatar');

function ya_custom_direction(){
	global $wp_locale;
	$opt_direction = ya_options()->getCpanelValue('text_direction');
	$opt_direction = strtolower($opt_direction);
	if ( in_array($opt_direction, array('ltr', 'rtl')) ){
		$wp_locale->text_direction = $opt_direction;
	} else {
		// default by $wp_locale->text_direction;
	}
}
add_filter( 'wp', 'ya_custom_direction' );

function ya_navbar_class(){
	$classes = array( 'navbar' );

	if ( 'static' != ya_options()->getCpanelValue('navbar_position') )
		$classes[]	=	ya_options()->getCpanelValue('navbar_position');

	if ( ya_options()->getCpanelValue('navbar_inverse') )
		$classes[]	=	'navbar-inverse';

	apply_filters( 'ya_navbar_classes', $classes );

	echo 'class="' . join( ' ', $classes ) . '"';
}

function ya_content_class(){
	$classes = array( 'content' );
	
	$all_sidebars = array_merge(ya_sidebar_left(), ya_sidebar_right());
	
	$all_sidebars = array_unique($all_sidebars);
	
	$span = 12;
	foreach ($all_sidebars as $sb){
		if ( !is_active_sidebar_YA($sb) ){
			continue;
		}
		$sb_expand_field = 'sidebar_'.$sb.'_expand';
		$sb_expand_value = ya_options()->getCpanelValue($sb_expand_field);
		$span -= (int)$sb_expand_value;
	}
	if ($span <= 0){
		$classes[] = 'span12';
	} else {
		$classes[] = 'span'.$span;
	}
	
	echo 'class="' . join( ' ', $classes ) . '"';
}

/**
 * Count Page Hits in WordPress
 * */
function count_page_hits() {
   if(is_singular()) {
      global $post;
      $count = get_post_meta($post->ID, 'count_page_hits', true);
      $newcount = $count + 1;

      update_post_meta($post->ID, 'count_page_hits', $newcount);
   }
}
add_action('wp_head', 'count_page_hits');


function ya_sidebar_left(){
	$layout = ya_options()->getCpanelValue('theme_layout');
	$side_left = array();
	
	if ( preg_match('/sm/', $layout) ){
		$side_left[] = 'primary';
	} else if ( preg_match('/[l|r]+.?m/', $layout) ){
		for ( $i = 0; $i < strlen($layout); $i++ ){
			if ($layout[$i]=='m') break;
			if ($layout[$i]=='l') $side_left[] = 'left';
			if ($layout[$i]=='r') $side_left[] = 'right';
		}
	}
	return apply_filters('ya_sidebar_left', $side_left);
}

function ya_sidebar_right(){
	$layout = ya_options()->getCpanelValue('theme_layout');
	$side_right = array();

	if ( preg_match('/ms/', $layout) ){
		$side_right[] = 'primary';
	} else if ( preg_match('/m.?[l|r]+/', $layout) ){
		$push = 0;
		for ( $i = 0; $i < strlen($layout); $i++ ){
			if ($layout[$i]=='m') {
				$push = 1;
				continue;
			} else {
				if ( $push ){
					if ($layout[$i]=='l') $side_right[] = 'left';
					if ($layout[$i]=='r') $side_right[] = 'right';
				}
			}
		}
	}
	return apply_filters('ya_sidebar_right', $side_right);
}


function ya_typography_css(){
	$styles = '';
	if ( ya_options()->getCpanelValue('google_webfonts') ):
		
		$webfonts_assign = ya_options()->getCpanelValue('webfonts_assign');
		$styles = '<style>';
		if ( $webfonts_assign == 'headers' ){
			$styles .= 'h1, h2, h3, h4, h5, h6 {';
		} else if ( $webfonts_assign == 'custom' ){
			$custom_assign = ya_options()->getCpanelValue('webfonts_custom');
			$custom_assign = trim($custom_assign);
			if (!$custom_assign) return '';
			$styles .= $custom_assign . ' {';
		} else {
			$styles .= 'body, input, button, select, textarea, .search-query {';
		}
		$styles .= 'font-family: ' . ya_options()->getCpanelValue('google_webfonts') . ';}</style>';
	endif;
	return $styles;
}

function ya_typography_css_cache(){
	$data = get_transient( 'ya_typography_css' );
	//if ( $data === false ) {
		$data = ya_typography_css();
		set_transient( 'ya_typography_css', $data, 3600 * 24 );
	//}
	echo $data;
}
add_action( 'wp_head', 'ya_typography_css_cache', 12, 0 );

function ya_typography_css_cache_reset(){
	delete_transient( 'ya_typography_css' );
	ya_typography_css_cache();
}
//add_action( 'customize_preview_init', 'ya_typography_css_cache_reset' );


function ya_typography_webfonts(){
	if ( ya_options()->getCpanelValue('google_webfonts') ):
		$webfont				= ya_options()->getCpanelValue('google_webfonts');
		$webfont_weight			= ya_options()->getCpanelValue('webfonts_weight');
		$webfont_character_set	= ya_options()->getCpanelValue('webfonts_character_set');
		
		$f = strlen($webfont);
		if ($f > 3){
			$webfontname = str_replace( ' ', '+', $webfont );
			return '<link href="http://fonts.googleapis.com/css?family=' . $webfontname . ':' . $webfont_weight . '&subset=' . $webfont_character_set . '" rel="stylesheet">';
		}
	endif;
}

function ya_typography_webfonts_cache(){
	$data = get_transient( 'ya_typography_webfont' );
	//if ( $data === false ) {
		$data = ya_typography_webfonts();
		set_transient( 'ya_typography_webfont', $data, 0 );
	//}
	echo $data;
}
add_action( 'wp_head', 'ya_typography_webfonts_cache', 11, 0 );


function ya_typography_webfonts_cache_reset(){
	delete_transient( 'ya_typography_webfont' );
	ya_typography_webfonts_cache();
}
//add_action( 'customize_preview_init', 'ya_typography_webfonts_cache_reset' );


function ya_custom_header_scripts() {
	if ( ya_options()->getCpanelValue('advanced_head') ){
		echo ya_options()->getCpanelValue('advanced_head');
	}
}
add_action( 'wp_head', 'ya_custom_header_scripts', 200 );

function add_favicon(){
	if ( ya_options()->getCpanelValue('favicon') ){
		echo '<link rel="shortcut icon" href="' . ya_options()->getCpanelValue('favicon') . '" />';
	}
}
add_action('wp_head', 'add_favicon');
function get_entry_content_asset( $post_id )
	{
		global $post;
		$post = get_post( $post_id );
		
		$content = apply_filters ("the_content", $post->post_content);
		//print_r($content);
		$value=preg_match('/<div class=\"entry\-content\-asset\">(.*?)<\/div>/s',$content,$results);
		if($value){
			return $results[0];
		}else{
			return $content;
		}
	}