<?php
define( 'WPCTP_NAME',			'WPCargo Theme Pro' );
define( 'WPCTP',				'wpctp' );
define( 'WPCTP_SEO', 			'wpctp-seo' );
define( 'WPCTP_VERSION', 		'1.2.2' );
define( 'WPCTP_DIR',		 	get_template_directory() );
define( 'WPCTP_INC_DIR',		WPCTP_DIR . '/inc' );
define( 'WPCTP_ADMIN_DIR',		WPCTP_INC_DIR . '/admin' );
define( 'WPCTP_WIDGETS_DIR',	WPCTP_INC_DIR . '/widgets' );
define( 'WPCTP_PT_DIR',			WPCTP_ADMIN_DIR . '/post-types' );
define( 'WPCTP_CUSTOMIZER_DIR',	WPCTP_ADMIN_DIR . '/customizer' );
define( 'WPCTP_TO_DIR',			WPCTP_ADMIN_DIR . '/theme-option' );
define( 'WPCTP_RP_DIR',			WPCTP_ADMIN_DIR . '/required-plugins' );
define( 'WPCTP_IE_DIR',			WPCTP_ADMIN_DIR . '/import-export' );
define( 'WPCTP_URL',			get_template_directory_uri() );
define( 'WPCTP_INC_URL',		WPCTP_URL . '/inc' );
define( 'WPCTP_ADMIN_URL',		WPCTP_INC_URL . '/admin' );
/*define( 'WPCTP_WIDGETS_URL',	WPCTP_INC_URL . '/widgets' );
define( 'WPCTP_CUSTOMIZER_URL',	WPCTP_ADMIN_URL . '/customizer' );
define( 'WPCTP_TO_URL',			WPCTP_ADMIN_URL . '/theme-option' );
define( 'WPCTP_RP_URL',			WPCTP_ADMIN_URL . '/required-plugins' );
define( 'WPCTP_IE_URL',			WPCTP_ADMIN_URL . '/import-export' );*/
define( 'WPCTP_ASSETS_URL', 	WPCTP_URL . '/assets' );
define( 'WPCTP_IMAGES_URL', 	WPCTP_ASSETS_URL . '/images' );
define( 'WPCTP_JS_URL', 		WPCTP_ASSETS_URL . '/js' );
define( 'WPCTP_CSS_URL', 		WPCTP_ASSETS_URL . '/css' );
require ( WPCTP_INC_DIR . '/development-functions.php' );
require ( WPCTP_INC_DIR . '/template-tags.php' );
require ( WPCTP_INC_DIR . '/template-functions.php' );
require ( WPCTP_INC_DIR . '/icon-functions.php' );
require ( WPCTP_INC_DIR . '/theme-setup.php' );
require ( WPCTP_INC_DIR . '/woocommerce.php' );
require ( WPCTP_INC_DIR . '/template-compatibility.php' );
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */

function my_custom_origin_text(){
    return __('City', 'text-domain');
}
add_filter( 'wpcsr_country_label', 'my_custom_origin_text' );

function mycustom_wpcsr_exc_vat_label(){
    return 'exc. GST';
}
add_filter( 'wpcsr_exc_vat_label', 'mycustom_wpcsr_exc_vat_label' );

function mycustom_wpcsr_inc_vat_label(){
    return 'inc. GST';
}
add_filter( 'wpcsr_inc_vat_label', 'mycustom_wpcsr_inc_vat_label' );

function wpctp_setup() {
	/*
	 * Make theme available for translation.
	 * If you're building a theme based on WPCargo Pro, use a find and replace
	 * to change WPCTP to the name of your theme in all the template files.
	 */
	load_theme_textdomain('wpctp', get_template_directory() . '/languages');

	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );
	/*
	 * Let WordPress manage the document title.
	 * By adding theme support, we declare that this theme does not use a
	 * hard-coded <title> tag in the document head, and expect WordPress to
	 * provide it for us.
	 */
	add_theme_support( 'title-tag' );
	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 *
	 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
	 */
	add_theme_support( 'post-thumbnails' );
	add_image_size( 'wpctp-featured-image', 2000, 1200, true );
	add_image_size( 'wpctp-thumbnail-avatar', 100, 100, true );
	add_image_size( 'home-testimonial-cover', 265, 300, TRUE );
	add_image_size( 'author-thumb', 70, 70, TRUE );
	add_image_size( 'service', 550, 310, TRUE );
	// Set the default content width.
	$GLOBALS['content_width'] = 525;
	// This theme uses wp_nav_menu() in two locations.
	register_nav_menus( array(
		'primary'   => __( 'Primary Navigation Menu', 'wpctp' ),
//		'secondary' => __( 'Secondary Navigation Menu', 'wpctp' ),
//		'top'    	=> __( 'Top Menu', 'wpctp' ),
//		'social' 	=> __( 'Social Links Menu', 'wpctp' ),
	) );
	add_theme_support( 'html5', array(
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
	) );
	add_theme_support( 'post-formats', array(
		'aside',
		'image',
		'video',
		'quote',
		'link',
		'gallery',
		'audio',
	) );
	add_theme_support( 'customize-selective-refresh-widgets' );
}
add_action( 'after_setup_theme', 'wpctp_setup' );
function wpctp_excerpt_more( $link ) {
	if ( is_admin() ) {
		return $link;
	}
	$link = sprintf( '<p class="link-more"><a href="%1$s" class="more-link">%2$s</a></p>',
		esc_url( get_permalink( get_the_ID() ) ),
		/* translators: %s: Name of current post */
		sprintf( __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'wpctp' ), get_the_title( get_the_ID() ) )
	);
	return ' &hellip; ' . $link;
}
add_filter( 'excerpt_more', 'wpctp_excerpt_more' );
/**
 * Handles JavaScript detection.
 *
 * Adds a `js` class to the root `<html>` element when JavaScript is detected.
 *
 * @since WPCargo Pro 1.0
 */
function wpctp_javascript_detection() {
	echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
}
add_action( 'wp_head', 'wpctp_javascript_detection', 0 );
/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function wpctp_pingback_header() {
	if ( is_singular() && pings_open() ) {
		printf( '<link rel="pingback" href="%s">' . "\n", get_bloginfo( 'pingback_url' ) );
	}
}
add_action( 'wp_head', 'wpctp_pingback_header' );
/**
 * Enqueue scripts and styles.
 */
function wpctp_scripts() {
	// Theme stylesheet.
	wp_enqueue_style( 'wpctp', get_template_directory_uri() .'/style.css' );
	wp_enqueue_style( 'wpctp', get_template_directory_uri() .'/assets/mobile-menu.css' );
	// Load the Internet Explorer 9 specific stylesheet, to fix display issues in the Customizer.
	if ( is_customize_preview() ) {
		wp_enqueue_style( 'bh-ie9', WPCTP_CSS_URL . '/ie9.css', array( 'wpctp' ), '1.0' );
		wp_style_add_data( 'bh-ie9', 'conditional', 'IE 9' );
	}
	// Load the Internet Explorer 8 specific stylesheet.
	wp_enqueue_style( 'bh-ie8',  WPCTP_CSS_URL . '/ie8.css', array( 'wpctp' ), '1.0' );
	wp_style_add_data( 'bh-ie8', 'conditional', 'lt IE 9' );
	// Load the html5 shiv.
	wp_enqueue_script( 'html5', WPCTP_JS_URL . '/html5.js', array(), '3.7.3' );
	wp_script_add_data( 'html5', 'conditional', 'lt IE 9' );
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
	// REGISTER SCRIPT
	wp_register_script( 'wpctp-mobile-menu', WPCTP_JS_URL .'/mobile-menu.js', array( 'jquery' ), false );
	wp_register_script( 'wpctp-script', WPCTP_JS_URL .'/script.js', array( 'jquery' ), false );
	wp_register_script( 'wpctp-bxslider', WPCTP_JS_URL .'/bxslider.min.js', array( 'jquery' ), false );
	wp_enqueue_script( 'wpctp-mobile-menu' );
 	wp_enqueue_script( 'wpctp-bxslider' );
 	wp_enqueue_script( 'wpctp-script' );
	// ENQUEUE STYLES
	$google_body_default = array(
		'family' => 'Raleway',
		'variants' => array('300','400','700', '900'),
		'subsets' => array('latin')
	);
	$font_array = array(
		'body_font_family' => json_decode(wpctp_get_option('body_font_family')?:json_encode($google_body_default), true),
		'menu_font_family' => json_decode(wpctp_get_option('menu_font_family'), true),
		'headings_font_family' => json_decode(wpctp_get_option('headings_font_family'), true),
	);
	wp_enqueue_style( 'wpctp-google-font', wpctp_get_google_font_url( $font_array ) );
	if( ! wp_style_is( 'wpctp-font-awesome-min' ) ){
		wp_enqueue_style( 'wpctp-font-awesome-min', WPCTP_CSS_URL . '/font-awesome.min.css' );
	}
	if( ! wp_style_is( 'wpctp-font-awesome-min' ) ){
		wp_enqueue_style( 'wpctp-flaticon-min', WPCTP_CSS_URL . '/flaticon.min.css' );
	}
}
add_action( 'wp_enqueue_scripts', 'wpctp_scripts' );
add_action( 'load-themes.php', 'wpctp_load_themes' );
function wpctp_load_themes(){
    global $pagenow, $tgmpa;
	if( isset( $tgmpa->plugins['wordpress-seo'] ) ) {
		unset( $tgmpa->plugins['wordpress-seo'] );
	}
	$plugins = false;
	foreach ( $tgmpa->plugins as $key => $plugin ) {
		if ( is_plugin_active( wpkeyvalue( $plugin, 'file_path' ) ) ) {
			continue;
		}
		$plugins = true;
	}
	if( ! get_option( 'wpctp-settings' ) && ( isset( $_GET['activated'] ) == true ) ) :
		update_option( 'wpctp-settings', wpctp_get_option_settings() );
		wpctp_load_footer_widgets();
	endif;
	$widgets  = get_option( 'sidebars_widgets' );
	$wysijaid = '';
	if( isset( $widgets['wp_inactive_widgets'] ) && ( isset( $_GET['activated'] ) == true ) ) {
		foreach( $widgets['wp_inactive_widgets'] as $inactive_widget ){
			$wysija = strpos( $inactive_widget, 'wysija' );
			if( $wysija === '' ){
				continue;
			}
			$wysijaid = $inactive_widget;
			break;
		}
	}
	if( $wysijaid && empty( $widgets['footer-widgets-4'] ) && ( isset( $_GET['activated'] ) == true ) ) {
		$widgets['footer-widgets-4'] = array( $wysijaid );
		update_option( 'sidebars_widgets', $widgets );
	}
    if ( 
		( "themes.php" == $pagenow ) && 
		is_admin() && 
		isset( $_GET['activated'] ) &&
		( wpctp_get( 'page' ) !== 'wpctp-install-plugins' ) &&
		$plugins
	) {
        wp_redirect( get_bloginfo( 'url' ) . '/wp-admin/themes.php?page=wpctp-install-plugins' );
		exit;
    }
}
add_action( 'widgets_init', 'wpctp_widgets_init' );
function wpctp_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Primary Sidebar', 'wpctp' ),
		'id'            => 'primary-sidebar',
		'description'   => __( 'Add widgets here to appear in your sidebar on posts, archive, and pages.', 'wpctp' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
	$footer_widgets = 4;
	if( wpctp_get_option( 'footer_widgets_column' ) ){
		$footer_widgets = wpctp_get_option( 'footer_widgets_column' );	
	}
	$x = 1;
	while( $x <= $footer_widgets ){
		register_sidebar( array(
			'name'          => __( "Footer Widgets {$x}", 'wpctp' ),
			'id'            => "footer-widgets-{$x}",
			'description'   => __( "Add widgets here to appear in your footer widgets {$x} section.", 'wpctp' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );
		$x++;
	}
}
add_action( 'widgets_init', 'wpctp_load_widgets' );
function wpctp_load_widgets() {
	register_widget( 'WPCTP_Testimonials_Widget' );
	register_widget( 'WPCTP_Sliders_Widget' );
	register_widget( 'WPCTP_Slider_Search_Widget' );
}
add_action( 'import_end', 'wpctp_import_end' );
function wpctp_import_end() {
	global $wpdb;
	$theme_slug = get_option( 'stylesheet' );
	//if( $theme_slug == 'wpcargo-pro' ){
		$prefix = wpstdclass( $wpdb, 'prefix' );
		$primary_menu_id = $wpdb->get_var( "SELECT {$prefix}terms.term_id FROM {$prefix}terms LEFT JOIN {$prefix}term_taxonomy ON {$prefix}term_taxonomy.term_id = {$prefix}terms.term_id WHERE {$prefix}terms.name = 'WPCargo Main Menu' AND {$prefix}term_taxonomy.taxonomy = 'nav_menu'" );
		$front_page_id = $wpdb->get_var( "SELECT ID FROM {$prefix}posts WHERE post_title = 'WPCargo Home' AND post_name = 'home' AND post_status = 'publish'" );
		$ql_nav_id = $wpdb->get_var( "SELECT {$prefix}terms.term_id FROM {$prefix}terms LEFT JOIN {$prefix}term_taxonomy ON {$prefix}term_taxonomy.term_id = {$prefix}terms.term_id WHERE {$prefix}terms.name = 'Quick Links' AND {$prefix}term_taxonomy.taxonomy = 'nav_menu'" );
		$theme_mods = get_option( 'theme_mods_' . $theme_slug );
		if( ! isset( $theme_mods['nav_menu_locations']['primary'] ) && $primary_menu_id ){
			$theme_mods['nav_menu_locations'] = array( 'primary' => $primary_menu_id );
			update_option( 'theme_mods_' . $theme_slug, $theme_mods );
		}
		if( 
			( get_option( 'show_on_front' ) != 'page' ) && 
			! get_option( 'page_on_front' ) && 
			$front_page_id 
		){
			update_option( 'show_on_front', 'page' );	
			update_option( 'page_on_front', $front_page_id );		
		}
		$nav_menu = get_option( 'widget_nav_menu' );
		if( $nav_menu ){
			foreach( $nav_menu as $key => $menu ){
				if( 
					( $menu['title'] === "QUICK LINKS" ) &&
					! isset( $menu['nav_menu'] ) &&
					$ql_nav_id
				){
					$nav_menu[$key]['nav_menu'] = $ql_nav_id;
					update_option( 'widget_nav_menu', $nav_menu );
					break;
				}
			}
		}
	//}
}
add_action( 'admin_init', 'update_testimonials_slider_meta' );
function update_testimonials_slider_meta(){
	global $wpdb;
	$theme = wp_get_theme();
	if ( version_compare( '1.1.5', $theme->get( 'Version' ), '<=' ) ) {
		return;
	}
	$prefix = wpstdclass( $wpdb, 'prefix' );
	$ids = $wpdb->get_col( "SELECT post_id FROM {$prefix}postmeta WHERE meta_key IN( 'counrty' ) AND meta_value != '' GROUP BY post_id" );
	if( $ids ){
		foreach( $ids as $id ){
			if( get_post_meta( $id, 'counrty', true ) && ! get_post_meta( $id, 'country', true ) ){
				update_post_meta( $id, 'country', get_post_meta( $id, 'counrty', true ) );
			}else{
				continue;
			}
		}
	}
}
add_action( 'wp_head', 'wpctp_wp_head_styles' );
function wpctp_wp_head_styles(){
	if( wpctp_get_option( 'site_options' ) ){
 	echo "<!-- WPCargo Theme Pro Inline Styles -->\n";
 	echo '<style type="text/css" media="screen">' . "\n";
	if( has_post_thumbnail() ){
		wpctp_get_styles(
			'.has-image .header-image',
			array(
				wpctp_get_css( 'background-image', wp_get_attachment_image_url( get_post_meta( get_the_ID(), '_thumbnail_id', true ), 'full' ) ),
			)
		);
	}
	wpctp_get_styles(
		'body,p',
		array(
			wpctp_get_css( 'font-family', wpctp_get_font_family( 'body_font_family' ) ),
			wpctp_get_css( 'font-size', 'body_font_size' ),
		)
	);
	wpctp_get_styles(
		'h1',
		array(
			wpctp_get_css( 'font-family', wpctp_get_font_family( 'headings_font_family' ) ),
			wpctp_get_css( 'font-size', 'h1_font_size' ),
		)
	);
	wpctp_get_styles(
		'h2',
		array(
			wpctp_get_css( 'font-family', wpctp_get_font_family( 'headings_font_family' ) ),
			wpctp_get_css( 'font-size', 'h2_font_size' ),
		)
	);
	wpctp_get_styles(
		'h3',
		array(
			wpctp_get_css( 'font-family', wpctp_get_font_family( 'headings_font_family' ) ),
			wpctp_get_css( 'font-size', 'h3_font_size' ),
		)
	);
	wpctp_get_styles(
		'h4',
		array(
			wpctp_get_css( 'font-family', wpctp_get_font_family( 'headings_font_family' ) ),
			wpctp_get_css( 'font-size', 'h4_font_size' ),
		)
	);
	wpctp_get_styles(
		'h5',
		array(
			wpctp_get_css( 'font-family', wpctp_get_font_family( 'headings_font_family' ) ),
			wpctp_get_css( 'font-size', 'h5_font_size' ),
		)
	);
	wpctp_get_styles(
		'h6',
		array(
			wpctp_get_css( 'font-family', wpctp_get_font_family( 'headings_font_family' ) ),
			wpctp_get_css( 'font-size', 'h6_font_size' ),
		)
	);
	if( 
		( 'fullwidth' === wpctp_get_option( 'page_layout' ) ) &&
		( wpctp_get_default( 'content_width' ) != wpctp_get_option( 'content_width' ) ) 
	){
		$wrap = ( wpctp_get_option( 'content_width' ) + 60 );
		printf( ".wrap{width:%spx;margin-left: auto;margin-right: auto;}", $wrap );
	}
	if( 
		( 'boxed' === wpctp_get_option( 'page_layout' ) ) && 
		( wpctp_get_default( 'content_width' ) != wpctp_get_option( 'content_width' ) ) 
	){
		$wrap = ( wpctp_get_option( 'content_width' ) + 60 );
		printf( "body.wpctp-boxed #page{width:%spx;margin-left: auto;margin-right: auto;overflow: hidden;}", $wrap );
	}
	wpctp_get_styles(
		'.site-header .main-navigation',
		array(
			wpctp_get_css( 'font-family', wpctp_get_font_family( 'menu_font_family' ) ),
			wpctp_get_css( 'font-size', 'menu_font_size' ),
		)
	);
	wpctp_get_styles(
		'.site-footer',
		array(
			wpctp_get_css( 'background-color', 'footer_bc_color' ),
			wpctp_get_css( 'font-size', 'footer_font_size' ),
			wpctp_get_css( 'color', 'footer_color' ),
		)
	);
	wpctp_get_styles(
		'.site-footer .border',
		array(
			wpctp_get_css( 'border-bottom-color', 'footer_border_color_1' ),
			wpctp_get_css( 'border-top-color', 'footer_border_color_2' ),
		)
	);
	wpctp_get_styles(
		'.footer-widgets',
		array(
			wpctp_get_css( 'background-color', 'fw_bc_color' ),
			wpctp_get_css( 'color', 'fw_color' ),
			wpctp_get_css( 'font-size', 'fw_font_size' ),
		)
	);
	wpctp_get_styles(
		'.footer-widgets a',
		array(
			wpctp_get_css( 'color', 'fw_color' ),
		)
	);
	wpctp_get_styles(
		'.footer-widgets .widget-title:after',
		array(
			wpctp_get_css( 'border-top-color', 'fw_widget_title_border_color' ),
		)
	);
	wpctp_get_styles(
		'.footer-widgets .widget_wysija input[type="text"]',
		array(
			wpctp_get_css( 'border-color', 'fw_color' ),
			wpctp_get_css( 'background-color', 'fw_bc_color' ),
		)
	);
	wpctp_get_styles(
		'.footer-widgets .widget-title',
		array(
			wpctp_get_css( 'font-size', 'fw_title_font_size' ),
			wpctp_get_css( 'color', 'fw_widget_title_color' ),
		)
	);
 	echo "\n</style>\n";
	}
}
function wpctp_mobile_memu_element() {
    ?>
    <div id="hamburger" style="display: none;"> 
    	<div class="menu-bar1 menu-bar"></div> 
    	<div class="menu-bar2 menu-bar"></div> 
    	<div class="menu-bar3 menu-bar"></div> 
    </div>
    <?php
}
add_action( 'after_search_icon', 'wpctp_mobile_memu_element' );

function wpcscustom_dashboard_redirect(){
	if( !function_exists( 'wpcfe_admin_page' ) ){
		return false;
	}
	if( is_home() || is_front_page() ){
		wp_redirect( get_the_permalink( wpcfe_admin_page() ) );
        die;
	}
    
}
add_action( 'template_redirect', 'wpcscustom_dashboard_redirect' );
