<?php 
defined( 'ABSPATH' ) or die( 'Error' );
function themeRootDir()
{
	return dirname(__FILE__)."/";
}
function dx2bandhelpersincluded()
{
	return true;
}

function getCustomPostObject($post)
{
	switch($post->post_type)
	{
		case "song": 
			require_once themeRootDir()."objects/song.class.php";
			$CustomPost = Song::Get()->ByPost($post); break;
		case "album": 
			require_once themeRootDir()."objects/album.class.php";
			$CustomPost = Album::Get()->ByPost($post); 
		break;
		case "bandmember": 
			require_once themeRootDir()."objects/bandmember.class.php";
			$CustomPost = BandMember::Get()->ByPost($post); 
		break;
		case "gig": 
			require_once themeRootDir()."objects/gig.class.php";
			$CustomPost = Gig::Get()->ByPost($post); 
		break;
		case "gear": 
			require_once themeRootDir()."objects/gear.class.php";
			$CustomPost = Gear::Get()->ByPost($post); 
		break;			
		default: 
			require_once themeRootDir()."objects/coreobject.class.php";
			$CustomPost = CoreObject::Get()->ByPost($post); 
		break;
	}
	return $CustomPost;
}

function seoUrl($string) 
{
    $string = strtolower($string);
    $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
    $string = preg_replace("/[\s-]+/", " ", $string);
    $string = preg_replace("/[\s_]/", "-", $string);
    return $string;
}

function getBaseUrl()
{
	return "/wp-content/themes/banddemo/bandmanager/";
}

function getRelativePluginUrl()
{
	return plugins_url()."/dx2bands/";
}

function getPostBySlug($slug, $posttype = "post")
{
	$post = null;
	try{
		$args = array(
			'post_type' => $posttype,
			'numberposts' => 1,
			'posts_per_page' => 1,
			'post_status' => 'publish',
			'name' => $slug
		);
		$post = new WP_Query($args);
	}
	catch(Exception $e)
	{
		
	}
	return $post;
}

function getMultiplePosts($limit, $posttype="post")
{
	$posts = null;
	try{
		$args=array('post_type' => $posttype,
		  'post_status' => 'publish',
		  'posts_per_page' => $limit);
		$posts = new WP_Query($args);
	}
	catch(Exception $e)
	{
		
	}
	
	return $posts;
}

function validateMetaSave()
{
	// Check if our nonce is set.
	if (!isset($_POST['customNBFieldsSaveMetaBoxNonce'])) return false;
	
	// Verify that the nonce is valid.
	if (!wp_verify_nonce($_POST['customNBFieldsSaveMetaBoxNonce'], 'customNBFieldsSaveMetaBoxData')) return;
	
	// If this is an autosave, our form has not been submitted, so we don't want to do anything.
	if (defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
	
	
	// Check the user's permissions.
	if (isset( $_POST['post_type'] ) && 'page' == $_POST['post_type']) 
	{
		if (!current_user_can( 'edit_page', $post_id )) return false;
	} 
	else 
	{
		if (!current_user_can( 'edit_post', $post_id ))return false;
	}
	
	//submission is valid and can continue
	return true;
}

function saveMetaField($post_id, $fieldname)
{
	if(isset($_POST[$fieldname])) 
	{
		update_post_meta($post_id, $fieldname, sanitize_text_field($_POST[$fieldname]));
		//echo "added $fieldname <br>";
	}
}

function isLandscape($image)
{
	list($width, $height) = getimagesize($image);
	//echo "Width : ".$width." - Height : ".$height;
	if ($width > $height) {
		return true;
	} else {
		return false;
	}
}

function getBMImageDir()
{
	return "/wp-content/themes/banddemo/bandmanager/images/";
}

function youtubeUrlToEmbed($string) {
    // return preg_replace(
        // "/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
        // "<iframe width='560' height='315' src=\"//www.youtube.com/embed/$2\" frameborder='0' allowfullscreen></iframe>",
        // $url
    // );
	$search     = '/youtube\.com\/watch\?v=([a-zA-Z0-9]+)/smi';
	$replace    = "youtube.com/embed/$1";    
	$url = preg_replace($search,$replace,$string);
	return "<div class='yt-video-container'><iframe width='560' height='315' src='".$url."' frameborder='0' allowfullscreen></iframe></div>";
}

function getPrettyDate($yyyymmdd, $showyear = true)
{
	if($showyear == true) return date("F jS Y", strtotime($yyyymmdd));
	else return date("F jS", strtotime($yyyymmdd));
	
}

function generateCustomizerCSS()
{
	$css = "";
	ob_start();
	if ( is_admin_bar_showing() ) echo ".navbar-fixed-top{margin-top:32px !important;}"; ?>
	@media (max-width: 768px) {
		<?php if (is_admin_bar_showing()) { echo ".navbar-fixed-top{margin-top:46px;}";}?>
		#bs-example-navbar-collapse-1{background-color: <?php echo get_theme_mod('bandtheme_collapsedmenubg_color');?> !important;}
	}
	@media (max-width: 600px) {
		<?php if (is_admin_bar_showing()) { echo ".navbar-fixed-top{margin-top:47px !important;} .top-nav-collapse{margin-top:0px !important;}";}?>
	}	
	<?php 
		$menubgcolor =  get_theme_mod('bandtheme_menu_color');
		if(trim($menubgcolor) == "") $menubgcolor = "transparent";
		$menubordercolor = get_theme_mod('header_menu_borderbottom_color');
		if(trim($menubordercolor) == "") $menubordercolor = "transparent";
	?>
	.navbar-default{background-color: <?php echo $menubgcolor;?> !important;}
	.navbar-nav li a:hover, .navbar-toggle:hover {color:<?php echo get_theme_mod('theme_menu_text_hover_color');?> !important;}
	.navbar-toggle{ border-color:<?php echo get_theme_mod('bandtheme_mobile_menu_color');?> !important;}
	.navbar-toggle:hover{background-color:<?php echo get_theme_mod('bandtheme_mobile_menu_color');?> !important;}	
	footer{background-color:<?php echo get_theme_mod('footer_bg_color');?>; border-top:<?php echo get_theme_mod("footer_border_top_size");?>px solid <?php echo get_theme_mod("footer_border_top_color");?>;margin-top:15px;}
	footer, footer p{color:<?php echo get_theme_mod('footer_text_color');?>;}
	footer a{color:<?php echo get_theme_mod('footer_link_color');?>;}
	#brandhomelink{color:<?php echo get_theme_mod("bandtheme_sitetitle_color");?>}
	.top-nav-collapse{background-color: <?php echo get_theme_mod('bandtheme_collapsedmenubg_color');?> !important; color:<?php echo get_theme_mod('bandtheme_collapsedmenutext_color');?>}
	.top-nav-collapse a, .top-nav-collapse #brandhomelink{color:<?php echo get_theme_mod('bandtheme_collapsedmenutext_color');?> !important;}
	.navbar-default .navbar-nav a{color:<?php echo get_theme_mod('bandthemeheadertheme_menutext_color');?> !important;}
	body, #content, #primary{background-color:<?php echo get_theme_mod('body_bg_color');?> !important; color:<?php echo get_theme_mod('body_text_color');?> !important;}
	#postpagenav .page-numbers{color:<?php echo get_theme_mod('body_text_color');?> !important; background-color: <?php echo get_theme_mod('body_bg_color');?> !important; border: 1px solid <?php echo get_theme_mod('body_text_color');?> !important;}
	#postpagenav .nav-links .current{background-color:<?php echo get_theme_mod('body_text_color');?> !important; color:<?php echo get_theme_mod('body_bg_color');?> !important;}
	article a, .entry-content a{color:<?php echo get_theme_mod('body_link_color');?>;}
	article a:hover, .entry-content a:hover, #postbreadcrumbs a:hover{color:<?php echo get_theme_mod('body_link_hover_color');?> !important;}
	article h1, article h2, article h3, article h4, article h5{color:<?php echo get_theme_mod('body_heading_color');?>;}
	.navbar-nav .active a{background-color:<?php echo get_theme_mod('header_active_link_color');?> !important; color:<?php echo get_theme_mod('header_active_link_text_color');?> !important;}
	#sidebar h2{font-size:<?php echo get_theme_mod('sidebar_heading_size');?>; color:<?php echo get_theme_mod('sidebar_heading_color');?>;}
	#sidebar a{color:<?php echo get_theme_mod('sidebar_link_color');?>;}
	#sidebar a:hover{color: <?php echo get_theme_mod('sidebar_link_hover_color');?>;}
	#sidebar{color:<?php echo get_theme_mod('sidebar_text_color');?>; background-color:<?php echo get_theme_mod('sidebar_bg_color');?>;}	
	#postbreadcrumbs a{color:<?php echo get_theme_mod('body_text_color');?> !important;}
	<?php if(get_theme_mod("theme_fullwidth_body") == "true") echo "#content{width:100%;}"; 
	$css = ob_get_clean();
	return $css;
}