<?php
/**
 * Hi, thank you for purchasing our template. From this file (functions.php) is included and started the whole framework,
 * and I will do my best to comment all the files. Below are included few useful notices, please read them:
 */


/**
 * Constant Definition
 * ===================
 * - theses constants are important for proper framework set-up
 * !!!!! DO NOT CHANGE WHEN CUSTOMIZING !!!!!!
 */
add_theme_support( 'post-thumbnails' );

global $fprinter;		// printer is class common to all templates ( printing titles, time, comments and other stuff )

////////////////////////////////////////////////////////////////////////////////
// FRAMEWORK INIT
////////////////////////////////////////////////////////////////////////////////
define('THEMENAME', 'London Creative Plus');
define('THEMENAMELOW','lcp');
// DEMO or RELEASE ?
//if( isset( $_SERVER['SCRIPT_URI'] ) && strpos($_SERVER['SCRIPT_URI'],'http://demo.freshface.net/file/') !== false )
//	define('IS_DEMO_CONTENT', true); //!!!!! DO NOT CHANGE !!!!!!
//else
	define('IS_DEMO_CONTENT', false);//
define ('IS_USER_VERSION', false);

define('DEFAULT_CATEGORY_TEMPLATE', 'blog/blog-sidebar.php');
define('DEFAULT_SINGLE_TEMPLATE', 'single-blog/single-sidebar.php');

/**
 * !!!!AUTHOR DEFINITION!!!!
 * if you want to change the themename and author for your client, do it here NOT in "Constant Definition" :)
 */
define('CUSTOM_THEMENAME', 'London Creative Plus');
define('CUSTOM_AUTHORNAME', 'freshface');
require get_template_directory().'/framework/init.php';

add_theme_support( 'automatic-feed-links' );

////////////////////////////////////////////////////////////////////////////////
// MENU REGISTRATION
////////////////////////////////////////////////////////////////////////////////
fEnv::addNavigationMenu( __('Navigation', 'lcp-lang'), 'navigation');

////////////////////////////////////////////////////////////////////////////////
// CUSTOM FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

/*
 * Prints logo. If you dont set logo in Theme Options, print default
 */
function lcp_print_header_logo() {
	$logo_show = fOpt::Get('header', 'logo-show');
	if( 0 == $logo_show ) return;
	
	$logo_url = fOpt::Get('header', 'logo-url');
	if( empty( $logo_url ) ) {
		$logo_url = get_template_directory_uri().'/skins/'.fOpt::Get('skins', 'theme-color-skin').'/gfx/logo.png';
	}
	
	echo '<div id="logo">';
		echo '<a href="'. get_home_url() .'">';
			echo '<img src="'.$logo_url.'" alt="" id="lclogo" />';
		echo '</a>';
	echo '</div><!--  END "logo"  //-->';
}

function lcp_print_header_text_right() {
	$text_right = fOpt::Get('header', 'text-right');
	if( empty( $text_right ) ) return;
	$text_right_with_br = str_replace("\n", '<br />', $text_right);
	echo '<div id="header_contacts">'.$text_right_with_br.'</div>';
}

function lcp_header_nav_menu_empty() {
	echo '<div style="position: relative; font-style: italic; color: red; margin: 15px 0 15px 0;">Hello world! Please create a Top Navigation Menu and assign it in "Appereance &rarr; Menus &rarr; Theme Locations"</div>';
}

if ( ! isset( $content_width ) ) $content_width = 960;
function ffgtd() {
	return 'wgenglish';
}


//define('CUSTOM_VERSION', '1.0');
////////////////////////////////////////////////////////////////////////////////
// FRESHPANEL INCLUDING
////////////////////////////////////////////////////////////////////////////////
/*
 include 'freshwork/freshpanel/freshpanel.php';     // theme options panel
 include 'freshwork/freshslider/freshslider.php';     // theme options panel                                  
 include 'freshwork/writepanels.php';               // page & post writepanels
 include 'freshwork/writepanels-category.php';               // page & post writepanels
 include 'freshwork/widgets.php';               // page & post writepanels
 //include 'freshwork/shortcodes.php';               // page & post writepanels */
////////////////////////////////////////////////////////////////////////////////
// THEME INIT
////////////////////////////////////////////////////////////////////////////////
 register_sidebar( array(
	 'name' => __( 'Global Sidebar' , ffgtd()),
	 'id' => THEMENAMELOW . '-global',
  		'before_widget' => '<div class="widget">',
  		'after_widget' => '</div>',
  		'before_title' => '	<h3 class="widget_title">',
  		'after_title' => '</h3>',) );
 
 
 register_sidebar( array(
	 'name' => __( 'Home'  , ffgtd()),
	 'id' => THEMENAMELOW . '-home',
  		'before_widget' => '<div class="widget">',
  		'after_widget' => '</div>',
  		'before_title' => '	<h3 class="widget_title">',
  		'after_title' => '</h3>',) );
 
 
 
 register_sidebar( array(
 		'name' => __( 'Blog Sidebar'  , ffgtd()),
    	'id' => THEMENAMELOW . '-blog',
  		'before_widget' => '<div class="widget">',
  		'after_widget' => '</div>',
  		'before_title' => '	<h3 class="widget_title">',
  		'after_title' => '</h3>',) );
 
 
 register_sidebar( array(
	 'name' => __( 'Blog Single Sidebar'  , ffgtd()),
	 'id' => THEMENAMELOW . '-blog-single',
  		'before_widget' => '<div class="widget">',
  		'after_widget' => '</div>',
  		'before_title' => '	<h3 class="widget_title">',
  		'after_title' => '</h3>',) );
 
 
 register_sidebar( array(
	 'name' => __( 'Portfolio Single Sidebar'  , ffgtd()),
	 'id' => THEMENAMELOW . '-portfolio-single',
  		'before_widget' => '<div class="widget">',
  		'after_widget' => '</div>',
  		'before_title' => '	<h3 class="widget_title">',
  		'after_title' => '</h3>',) );
 

 register_sidebar( array(
	 'name' => __( 'Page'  , ffgtd()),
	 'id' => THEMENAMELOW . '-page',
  		'before_widget' => '<div class="widget">',
  		'after_widget' => '</div>',
  		'before_title' => '	<h3 class="widget_title">',
  		'after_title' => '</h3>',) );
 

 register_sidebar( array(
 'name' => __('Search' , ffgtd()),
 'id' => THEMENAMELOW . '-search', 
  		'before_widget' => '<div class="widget">',
  		'after_widget' => '</div>',
  		'before_title' => '	<h3 class="widget_title">',
  		'after_title' => '</h3>',
 ) );
 

 $custom_sidebar_collection = new fSidebarCollection();
 foreach( $custom_sidebar_collection->getSidebars() as $id => $name ) {
 	register_sidebar(array(
 	'name' => $name,
 	'id' => THEMENAMELOW.'custom'.$id,
 	'description' => 'This is a custom sidebar',
 	'before_widget' => '<div class="widget">',
	'after_widget' => '</div>',
	'before_title' => '	<h3 class="widget_title">',
	'after_title' => '</h3>',
 	));
}
 
 

 function my_get_posts( $query ) {


//   $query->set( 'post_type', array( 'post', 'blog', 'portfolio') );
//echo $query->get('post_type'); 
	/* foreach($query->get('post_type') as $neco)
   {
      echo $neco;
   }  */
  return $query;
}



add_action( 'after_setup_theme', 'lcp_setup' );
//add_action('init', 'init_post_types');
  
function lcp_setup()
{
  // suport functions
  add_theme_support( 'nav-menus' );

}

$portfolio = new ffCustomPost('portfolio');
$portfolio->getArgs()
				->isPublic(true)
				->publiclyQueryable(true)
				->showUI(true)
				->showInMenu(true)
				->queryVar(true)
				->capabilityType('post')
				->taxonomyAdd('category')
				->taxonomyAdd('post_tag')
				->labels()
					->name( __('Portfolio', ffgtd()) )
					->singularName(__('Portfolio', ffgtd()) )
					->addNew(__('Add New', ffgtd()) )
					->addNewItem(__('Add New Portfolio', ffgtd()) )
					->editItem(__('Edit Portfolio', ffgtd()) )
					->newItem(__('New Portfolio', ffgtd()) )
					->allItems(__('All Portfolio', ffgtd()) )
					->viewItems(__('View Portfolio', ffgtd()) )
					->searchItems(__('Search Portfolio', ffgtd()) )
					->notFound(__('No Portfolio found', ffgtd()) )
					->notFoundInThrash(__('No Portfolio found in Trash', ffgtd()) )
					->parent_item_colon('')
					->menuName(__('Portfolio', ffgtd()) );

$portfolio->getArgs()->supports()
						->author()
						//->comments()
						->customFields()
						->editor()
						->excerpt()
						->pageAttributes()
						->revisions()
						->thumbnail()
						->title()
						->trckbacks();

function post_in_child_cat( $cats, $_post = null )
{
	foreach ( (array) $cats as $cat ) {
		// get_term_children() accepts integer ID only
		$descendants = get_term_children( (int) $cat, 'category');
		if ( $descendants && in_category( $descendants, $_post ) )
			return true;
	}
	return false;
}

function lcp_option($name)
{
    return htmlspecialchars_decode(stripslashes(get_option($name)));
}	

function the_category_filter($thelist,$separator=' ') {  
     if(!defined('WP_ADMIN')) {  
            //Category Names to exclude  
            $exclude = array('slider');  
      
            $cats = explode($separator,$thelist);  
            $newlist = array();  
            foreach($cats as $cat) {  
                $catname = trim(strip_tags($cat));  
              if(!in_array($catname,$exclude))  
                   $newlist[] = $cat;  
           }  
           return implode($separator,$newlist);  
       } else {  
           return $thelist;  
       }  
   }  
add_filter('the_category','the_category_filter', 10, 2);  

$is_main_loop = true;
function change_posts_per_page( $query ) {
	global $is_main_loop;

	if( !$is_main_loop ) return;
	$is_main_loop = false;
	
	if( $query->is_home ) {
		$categories = fOpt::Get('homepage', 'category-id');
		if( $categories != 'all' || $categories == null );
			$query->set('cat', $categories);
	}

	if( $query->is_home || $query->is_category || $query->is_search ||$query->is_author || $query->is_archive ) {
		$ppp = fEnv::getCategoryOption( 'posts_per_page' );
		if( $ppp != 0 && $ppp != '0' && $ppp != '')
			$query->set('posts_per_page', fEnv::getCategoryOption( 'posts_per_page' ) );

		$currentPostTypes = $query->get('post_type');
		if( empty( $currentPostTypes ) ) {
			$query->set('post_type', array('post', 'portfolio'));
		}
		else {
			if( is_string($currentPostTypes) ) $currentPostTypes = array($currentPostTypes);
			$currentPostTypes = array_merge( $currentPostTypes, array( 'portfolio' ) );
		}

	}

}
add_filter( 'pre_get_posts', 'change_posts_per_page' );

add_action('wp_head', 'print_header_styles', 999 );
function print_header_styles() {
	print_custom_css();
}

add_action('wp_footer', 'print_footer_styles', 999);
function print_footer_styles() {
	print_custom_javascript();
}

function print_custom_javascript() {
	$css = fOpt::Get('customcode', 'custom-javascript');

	if( strpos($css, '<script') === false ) {
		$css = '<script type="text/javascript">'.$css.'</script>';
	}

	echo $css;
}

function print_custom_css() {
	$css = fOpt::Get('customcode', 'custom-css');
	
	if( strpos($css, '<style') === false ) {
		$css = '<style type="text/css">'.$css.'</style>';
	}
	
	echo $css;
}

function lcp_print_pages() {
	wp_link_pages();
}

function lcp_pagination() {
	paginate_links();
}

if( function_exists('is_admin') and is_admin() ){
    require_once dirname( __FILE__ ) . '/install/install.php';
}
