<?php
/**
 * TopPic theme functions and definitions
 *
 * When using a child theme you can override certain functions (those wrapped
 * in a function_exists() call) by defining them first in your child theme's
 * functions.php file. The child theme's functions.php file is included before
 * the parent theme's file, so the child theme functions would be used.
 *
 * @link https://codex.wordpress.org/Theme_Development
 * @link https://codex.wordpress.org/Child_Themes
 *
 * Functions that are not pluggable (not wrapped in function_exists()) are
 * instead attached to a filter or action hook.
 *
 * For more information on hooks, actions, and filters,
 * {@link https://codex.wordpress.org/Plugin_API}
 *
 */


/*-----------------------------------------------------------------------------------*/
/* SETS UP THEME DEFAULTS AND REGISTERS SUPPORT FOR VARIOUS WORDPRESS FEATURES
/*-----------------------------------------------------------------------------------*/


if ( !function_exists( 'toppic_setup' ) ) {  
	function toppic_setup() {

		/* MAKE THEME AVAILABLE FOR TRANSLATION */
		
		load_theme_textdomain('toppic', get_template_directory() . '/languages');

		/* ENABLE SUPPORT FOR POST THUMBNAILS ON POSTS AND PAGES */
		
		add_theme_support( 'post-thumbnails' );		

		add_image_size( 'kktfwp-xs', 500, false );
		add_image_size( 'kktfwp-s', 690, false );
		add_image_size( 'kktfwp-l', 1300, false );
		add_image_size( 'kktfwp-m', 1660, false );
		add_image_size( 'kktfwp-xl', 1920, false );
		add_image_size( 'kktfwp-xxl', 2560, false );
		
		/* Really huge image size for 5k HiDPI monitors support (will be used in the srcset attribute only) */
		add_image_size( 'kktfwp-xxxl', 5120, false );
		
		/* WOOCOMMERCE SUPPORT */
		
		add_theme_support( 'woocommerce' );	

		add_theme_support( 'wc-product-gallery-zoom' );
		add_theme_support( 'wc-product-gallery-lightbox' );
		add_theme_support( 'wc-product-gallery-slider' );
		
		/* LET WORDPRESS MANAGE THE DOCUMENT TITLE */
		
		add_theme_support( 'title-tag' );
			
		
		/* POST FORMATS */	

		$format_types =	array(
							'gallery',
							'link',
							'image',
							'quote',
							'video');

		add_theme_support('post-formats', $format_types);

		/* REGISTER MENU */	
		
		add_theme_support( 'nav-menus' );

		register_nav_menus( array(
			'main-menu' 	=> esc_html__( 'Main Navigation', 'toppic' ), // MAIN MENU
			'footer-menu' 	=> esc_html__( 'Footer Navigation', 'toppic' ), // FOOTER MENU
		) );
		
		// Switch default core markup for search form, comment form, and comments to output valid HTML5.
			add_theme_support( 'html5', array(			
				'comment-form',
				'comment-list',
				'gallery',
				'caption'
			) );
			
		/*
		 * This theme styles the visual editor to resemble the theme style,
		 * specifically font, colors, icons, and column width.
		 */
		add_editor_style( array( 'css/editor-style.css', kktfwp_fonts_url() ) );
		
		/* ADD DEFAULT POSTS AND COMMENTS RSS FEED LINKS TO HEAD */
		
		add_theme_support('automatic-feed-links' );	

		// Gutenberg
		add_theme_support( 'responsive-embeds' );
	}
}

add_action('after_setup_theme', 'toppic_setup');

/*-----------------------------------------------------------------------------------*/
/* GET THEME VERSION
/*-----------------------------------------------------------------------------------*/

if ( !function_exists( 'kktfwp_themeData' ) ) {  
	function kktfwp_themeData( $arg = 'Version' ) {
		$themeData = wp_get_theme(get_template());
		
		return $themeData->get( $arg );
	}
}

/*-----------------------------------------------------------------------------------*/
/* ENQUEUE WORDPRESS THEME STYLES WITHIN GUTENBERG
/*-----------------------------------------------------------------------------------*/
 
if ( !function_exists( 'kktfwp_gutenberg_styles' ) ) {  
	function kktfwp_gutenberg_styles() {
		
		// Add custom fonts, used in the Gutenberg editor.
		wp_enqueue_style( 'kktfwp-fonts', kktfwp_fonts_url(), array(), null );
	
		// Load the theme styles within Gutenberg.
		wp_enqueue_style( 'kktfwp-gutenberg', get_theme_file_uri( '/css/gutenberg-style.css' ), false, kktfwp_themeData(), 'all' );
	}
}
add_action( 'enqueue_block_editor_assets', 'kktfwp_gutenberg_styles' );

/*-----------------------------------------------------------------------------------*/
/* OEMBED FILTER
/*-----------------------------------------------------------------------------------*/ 

add_filter( 'embed_oembed_html', 'kktfwp_wrap_oembed_html', 99, 4 );

if ( ! function_exists( 'kktfwp_wrap_oembed_html' ) ) {
	function kktfwp_wrap_oembed_html( $cached_html, $url, $attr, $post_id ) {
		
		if ( ( strpos( $url, 'youtube.com' ) || strpos( $url, 'vimeo.com' ) ) == false  ) return $cached_html;
			
		return '<div class="responsive-oembed-video">' . $cached_html . '</div>';
	}
}

/*-----------------------------------------------------------------------------------*/
/* CHECK IF META PLUGIN INSTALLED
/*-----------------------------------------------------------------------------------*/

if ( !function_exists('rwmb_meta') ) {
	function rwmb_meta() {
		return false;
	}
} else {
	function kktfwp_meta_style() {
		$kktfwp_theme_version = kktfwp_themeData();
		wp_enqueue_style( 'kktfwp-meta-style', get_theme_file_uri( '/css/meta-style.css' ), '', $kktfwp_theme_version );
	}	
	add_action('admin_enqueue_scripts', 'kktfwp_meta_style');
}


/*-----------------------------------------------------------------------------------*/
/* REQUIRED PLUGINS
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_register_required_plugins' ) ) {
	function kktfwp_register_required_plugins() {

		/**
		 * Array of plugin arrays. Required keys are name and slug.
		 * If the source is NOT from the .org repo, then source is also required.
		 */
		$plugins = array(

			array(
				'name'                     => esc_html__( 'Like This', 'toppic' ), // The plugin name
				'slug'                     => 'roses-like-this', // The plugin slug (typically the folder name)
				'source'                   => get_template_directory() . '/includes/plugins/roses-like-this.zip', 
				'required'                 => false, // The plugin source
				'version'                  => '1.6.4', // E.g. 1.0.0. If set, the active plugin must be this version or higher, otherwise a notice is presented
				'force_activation'         => false, // If true, plugin is activated upon theme activation and cannot be deactivated until theme switch
				'force_deactivation'       => false, // If true, plugin is deactivated upon theme switch, useful for theme-specific plugins
				'external_url'             => '' // If set, overrides default API URL and points to an external URL
			),
			array(
				'name'                     => esc_html__( 'Gallery Meta Boxes', 'toppic' ), // The plugin name
				'slug'                     => 'meta-box', // The plugin slug (typically the folder name)
				'source'                   => get_template_directory() . '/includes/plugins/meta-box.zip', 
				'required'                 => true, // The plugin source
				'version'                  => '17.3', // E.g. 1.0.0. If set, the active plugin must be this version or higher, otherwise a notice is presented
				'force_activation'         => false, // If true, plugin is activated upon theme activation and cannot be deactivated until theme switch
				'force_deactivation'       => false, // If true, plugin is deactivated upon theme switch, useful for theme-specific plugins
				'external_url'             => '' // If set, overrides default API URL and points to an external URL
			),
			array(
				'name'          		=> esc_html__( 'WPBakery Page Builder', 'toppic' ), // The plugin name
				'slug'          		=> 'js_composer', // The plugin slug (typically the folder name)
				'source'           		=> get_template_directory() . '/includes/plugins/js_composer.zip', // The plugin source
				'required'         		=> false, // If false, the plugin is only 'recommended' instead of required
				'version'               => '7.4', // E.g. 1.0.0. If set, the active plugin must be this version or higher, otherwise a notice is presented
				'force_activation'      => false, // If true, plugin is activated upon theme activation and cannot be deactivated until theme switch
				'force_deactivation'    => false, // If true, plugin is deactivated upon theme switch, useful for theme-specific plugins
				'external_url'          => '' // If set, overrides default API URL and points to an external URL
			),
			array(
				'name'          		=> esc_html__( 'Kotofey Custom Post Types', 'toppic' ), // The plugin name
				'slug'          		=> 'kk-cpt', // The plugin slug (typically the folder name)
				'source'           		=> get_template_directory() . '/includes/plugins/kk-cpt.zip', // The plugin source
				'required'         		=> true, // If false, the plugin is only 'recommended' instead of required
				'version'               => '1.3', // E.g. 1.0.0. If set, the active plugin must be this version or higher, otherwise a notice is presented
				'force_activation'      => false, // If true, plugin is activated upon theme activation and cannot be deactivated until theme switch
				'force_deactivation'    => false, // If true, plugin is deactivated upon theme switch, useful for theme-specific plugins
				'external_url'          => '' // If set, overrides default API URL and points to an external URL
			),
			array(
				'name'          		=> esc_html__( 'Contact From 7', 'toppic' ), // The plugin name
				'slug'          		=> 'contact-form-7', // The plugin slug (typically the folder name)
				'required'         		=> false, // If false, the plugin is only 'recommended' instead of required
				'version'               => '', // E.g. 1.0.0. If set, the active plugin must be this version or higher, otherwise a notice is presented
			)
		);

		/**
		 * Array of configuration settings. Amend each line as needed.
		 * If you want the default strings to be available under your own theme domain,
		 * leave the strings uncommented.
		 * Some of the strings are added into a sprintf, so see the comments at the
		 * end of each line for what each argument will be.
		 */
		$config = array(
			'id'           => 'tgmpa',                 // Unique ID for hashing notices for multiple instances of TGMPA.
			'domain'    => 'KK_TF',
			'default_path' => '',                      // Default absolute path to pre-packaged plugins.
			'menu'         => 'tgmpa-install-plugins', // Menu slug.
			'has_notices'  => true,                    // Show admin notices or not.
			'dismissable'  => false,                    // If false, a user cannot dismiss the nag message.
			'dismiss_msg'  => '',                      // If 'dismissable' is false, this message will be output at top of nag.
			'is_automatic' => true,                   // Automatically activate plugins after installation or not.
			'message'      => '',                      // Message to output right before the plugins table.
			'strings'      => array(
				'page_title'                      => __( 'Install Required Plugins', 'toppic' ),
				'menu_title'                      => __( 'Install Plugins', 'toppic' ),
				'installing'                      => __( 'Installing Plugin: %s', 'toppic' ), // %s = plugin name.
				'oops'                            => __( 'Something went wrong with the plugin API.', 'toppic' ),
				'notice_can_install_required'     => _n_noop( 'This theme requires the following plugin: %1$s.', 'This theme requires the following plugins: %1$s.', 'toppic' ), // %1$s = plugin name(s).
				'notice_can_install_recommended'  => _n_noop( 'This theme recommends the following plugin: %1$s.', 'This theme recommends the following plugins: %1$s.', 'toppic' ), // %1$s = plugin name(s).
				'notice_cannot_install'           => _n_noop( 'Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.', 'Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.', 'toppic' ), // %1$s = plugin name(s).
				'notice_can_activate_required'    => _n_noop( 'The following required plugin is currently inactive: %1$s.', 'The following required plugins are currently inactive: %1$s.', 'toppic' ), // %1$s = plugin name(s).
				'notice_can_activate_recommended' => _n_noop( 'The following recommended plugin is currently inactive: %1$s.', 'The following recommended plugins are currently inactive: %1$s.', 'toppic' ), // %1$s = plugin name(s).
				'notice_cannot_activate'          => _n_noop( 'Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.', 'Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.', 'toppic' ), // %1$s = plugin name(s).
				'notice_ask_to_update'            => _n_noop( 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.', 'toppic' ), // %1$s = plugin name(s).
				'notice_cannot_update'            => _n_noop( 'Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.', 'Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.', 'toppic' ), // %1$s = plugin name(s).
				'install_link'                    => _n_noop( 'Begin installing plugin', 'Begin installing plugins', 'toppic' ),
				'activate_link'                   => _n_noop( 'Begin activating plugin', 'Begin activating plugins', 'toppic' ),
				'return'                          => __( 'Return to Required Plugins Installer', 'toppic' ),
				'plugin_activated'                => __( 'Plugin activated successfully.', 'toppic' ),
				'complete'                        => __( 'All plugins installed and activated successfully. %s', 'toppic' ), // %s = dashboard link.
				'nag_type'                        => 'updated' // Determines admin notice type - can only be 'updated', 'update-nag' or 'error'.
			)
		);

		tgmpa( $plugins, $config );

	}
}

add_action( 'tgmpa_register', 'kktfwp_register_required_plugins' );

 
/*-----------------------------------------------------------------------------------*/
/* INCLUDE NEEDED FILES
/*-----------------------------------------------------------------------------------*/
 
require_once get_theme_file_path( '/includes/migrating.php' );
require_once get_theme_file_path( '/includes/img-filters.php' );
require_once get_theme_file_path( '/includes/class-tgm-plugin-activation.php' );
require_once get_theme_file_path( '/includes/class-kk-get-options.php' );
require_once get_theme_file_path( '/extends/theme-meta-boxes.php' );
require_once get_theme_file_path( '/includes/one-click-import.php' );
require_once get_theme_file_path( '/includes/class_check_ver.php' );


/*-----------------------------------------------------------------------------------*/
/* VC SETUP
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_vcSetAsTheme' ) ) {
	function kktfwp_vcSetAsTheme() {
		vc_set_as_theme( $disable_updater = true );
		require_once get_theme_file_path( '/includes/vc-func.php' );
	}
}

add_action( 'vc_before_init', 'kktfwp_vcSetAsTheme' );

add_action('admin_init', function()
{
    if(is_admin()) {
        setcookie('vchideactivationmsg', '1', strtotime('+3 years'), '/');
        setcookie('vchideactivationmsg_vc11', (defined('WPB_VC_VERSION') ? WPB_VC_VERSION : '1'), strtotime('+3 years'), '/');
    }
});


/*-----------------------------------------------------------------------------------*/
/* POSTS ID HELPER
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_postid' ) ) {
	function kktfwp_postid() {
		
		$postid = get_the_ID();

		if (is_home() || is_search() || is_category() || is_tag() || is_date() || is_author() || is_404()) {
			$postid = get_option( 'page_for_posts' );
		} else if ( is_tax( 'portfolio-type' )) {
			$postid = kktfwp_portfolioID();
		} else if ( is_tax( 'gallery-type' )) {
			$postid = kktfwp_galleryID();
		}  else if ( function_exists('is_shop') && is_shop() ) {
			$postid = get_option( 'woocommerce_shop_page_id' );
		}
			
		return $postid;
	}
}


/*-----------------------------------------------------------------------------------*/
/* REGISTERING SIDEBARS
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_widgets_init' ) ) {
	function kktfwp_widgets_init() {

		$args = array(
			'before_widget'=>'<div class="widget %2$s" id="%1$s">',
			'after_widget'=>'</div>',
			'before_title'=>'<h5 class="widget-title">',
			'after_title'=>'</h5>'
		);
		
		// BLOG SIDEBAR
		
		register_sidebar(
			array_merge( $args,
				array(
					'name'			=> esc_html__( 'Blog Sidebar', 'toppic' ),
					'id'			=> 'kk-blog-sidebar',
					'description'	=> esc_html__( 'Widgets in this area will be shown on all posts and blog page.', 'toppic' )
				)
			)
		);
			
		// PAGE SIDEBAR
		
		register_sidebar(
			array_merge( $args,
				array(
					'name'			=> esc_html__( 'Page Sidebar', 'toppic' ),
					'id'			=> 'kk-page-sidebar',
					'description'	=> esc_html__( 'Widgets in this area will be shown on all pages.', 'toppic' )
				)
			)
		);
		
		// PORTFOLIO SIDEBAR
		
		register_sidebar(
			array_merge( $args,
				array(
					'name'			=> esc_html__( 'Portfolio Sidebar', 'toppic' ),
					'id'			=> 'kk-portfolio-sidebar',
					'description'	=> esc_html__( 'Widgets in this area will be shown on all single portfolio pages.', 'toppic' )
				)
			)
		);

	}
}

add_action('widgets_init', 'kktfwp_widgets_init');


/*-----------------------------------------------------------------------------------*/
/* ENQUEUE SCRIPTS AND STYLES
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_get_jquery' ) ) {
	function kktfwp_get_jquery() {
		$postid = kktfwp_postid();
		
		$bg_post_meta = get_post_meta($postid, '_custom_bg_type_value', true);
		$kktfwp_theme_version = kktfwp_themeData();
				
		if( empty( $bg_post_meta ) ) {
			$bg_post_meta = 'Default';
		}
		
				
		/**
		* Enqueue Google Fonts.
		*/
		wp_enqueue_style( 'kktfwp-gfonts', kktfwp_fonts_url(), '', $kktfwp_theme_version);

		/* Stylesheets */
		
		wp_enqueue_style( 'kktfwp-icons', get_theme_file_uri( '/css/fontello.css' ), '', $kktfwp_theme_version );
		wp_enqueue_style( 'kktfwp-reset', get_theme_file_uri( '/css/reset.css' ), '', $kktfwp_theme_version );
		wp_enqueue_style( 'kktfwp-default', get_theme_file_uri( '/css/default.css' ), '', $kktfwp_theme_version );
		wp_enqueue_style( 'kktfwp-style', get_stylesheet_uri(), array(), $kktfwp_theme_version );
		wp_enqueue_style( 'royalslider', get_theme_file_uri(  '/css/royalslider.css' ), '', $kktfwp_theme_version );
		wp_enqueue_style( 'royalslider-skin',  get_theme_file_uri( '/css/rs-default.css' ), '', $kktfwp_theme_version );
		wp_enqueue_style( 'kktfwp-lightbox',  get_theme_file_uri( '/css/lightbox.css' ), '', $kktfwp_theme_version );
		wp_enqueue_style( 'kktfwp-woocommerce',  get_theme_file_uri( '/css/woocommerce.css' ), '', $kktfwp_theme_version );
		
		if ( get_theme_mod( 'kktfwp_responsive', 1 ) ) {
			wp_enqueue_style( 'kktfwp-responsive', get_theme_file_uri( '/css/responsive.css' ), '', $kktfwp_theme_version);
		}
		
		
		/* Scripts */
		wp_enqueue_script( 'kktfwp-parallax', get_theme_file_uri( '/js/jquery.parallax.custom.js' ), array( 'jquery' ), $kktfwp_theme_version, true );					
		wp_enqueue_script( 'jquery-mtree', get_theme_file_uri( '/js/jquery.mtree.js' ), array( 'jquery' ), $kktfwp_theme_version, true );					
		wp_enqueue_script( 'kktfwp-tipsy', get_theme_file_uri( '/js/jquery.tipsy.js' ), array( 'jquery' ), $kktfwp_theme_version, true );					
		wp_enqueue_script( 'jquery-transit', get_theme_file_uri( '/js/jquery.transit.min.js' ), array( 'jquery' ), $kktfwp_theme_version, true );					
		wp_enqueue_script( 'scrollMonitor-js', get_theme_file_uri( '/js/scrollMonitor.js' ), array( 'jquery' ), $kktfwp_theme_version, true );					
		wp_enqueue_script( 'jquery-touchswipe', get_theme_file_uri( '/js/jquery.touchswipe.js' ), array( 'jquery' ), $kktfwp_theme_version, true );
		wp_enqueue_script( 'kktfwp-modernizr', get_theme_file_uri( '/js/jquery.modernizr.custom.js' ), array( 'jquery' ), $kktfwp_theme_version, true );
		
		if ( in_array( get_theme_mod( 'kktfwp_menu_style', 'kk-menu-default' ), array( 'kk-menu-slide-right', 'kk-menu-fullscreen' ) ) ) {
			wp_enqueue_script( 'kktfwp-bg-check', get_theme_file_uri( '/js/jquery.background-check.min.js' ), array( 'jquery' ), $kktfwp_theme_version, true );
			wp_enqueue_script( 'jquery-classie', get_theme_file_uri( '/js/jquery.classie.js' ), array( 'jquery' ), $kktfwp_theme_version, true );
			wp_enqueue_script( 'jquery-dlmenu', get_theme_file_uri( '/js/jquery.dlmenu.js' ), array( 'jquery' ), $kktfwp_theme_version, true );
		}
		
		/* Remove Visual Composer prettyPhoto lightbox */		
		wp_deregister_script( 'prettyphoto' );
		
		if (!is_admin()){
			if ( is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
				wp_enqueue_script( 'comment-reply' );
		}		
		
		if ( ( $bg_post_meta === 'Video' ) && ( get_post_meta( $postid, '_gallery_layout_value', true ) !== 'Fullscreen') && ( get_post_meta( $postid, '_gallery_layout_value', true) !== 'Fullscreen-Grid' ) ) {
		
			if ( ( get_theme_mod( 'kktfwp_content_width', 'kk-boxed' ) === 'kk-boxed' ) || ( is_page_template('template-homepage-slider.php' ) ) ) {	
				$kkCheckBg = esc_url( get_post_meta($postid, '_bg_video_value', true) );
			
				if (strpos($kkCheckBg, 'youtube') > 0) {
					wp_enqueue_script('kktfwp-bgvideo', get_theme_file_uri( '/js/jquery.bgVideo.js' ), array( 'jquery' ), $kktfwp_theme_version, true);
				}
			}
		}
		
		if ((( $bg_post_meta === 'Default' ) || ( $bg_post_meta === 'Custom Slideshow')) && ( get_post_meta($postid, '_gallery_layout_value', true) !== 'Fullscreen' ) && (get_post_meta($postid, '_gallery_layout_value', true ) !== 'Fullscreen-Grid' )) {
		
			if ( get_theme_mod( 'kktfwp_slider_type', 'kk_royal' ) === 'kk_royal' ) {
				wp_enqueue_script('jquery-royalslider', get_theme_file_uri( '/js/jquery.royalslider.min.js' ), array( 'jquery' ), $kktfwp_theme_version, true);
			} else {		
				wp_enqueue_script('kktfwp-kenb', get_theme_file_uri( '/js/jquery.kenb.js' ), array( 'jquery' ), $kktfwp_theme_version, true);	
			}
		
		} else if ( get_post_meta($postid, '_gallery_layout_value', true) === 'Fullscreen' ) {	
			wp_enqueue_script('jquery-royalslider', get_theme_file_uri( '/js/jquery.royalslider.min.js' ), array( 'jquery' ), $kktfwp_theme_version, true);
		}
		
		if( is_home() || is_front_page() || is_archive() || is_search() || has_post_format('gallery') || is_singular('portfolio') || is_page_template('template-home-demo.php') || is_page_template('template-home-demo-2.php') ) {	
			if(!is_page_template('template-homepage-slider.php')) {		
				wp_enqueue_script('jquery-royalslider', get_theme_file_uri( '/js/jquery.royalslider.min.js' ), array( 'jquery' ), $kktfwp_theme_version, true);
			}			
		}
		
		if ( is_page_template('template-portfolio.php') || is_page_template('template-gallery.php') || is_tax( 'portfolio-type' ) || is_tax( 'gallery-type' ) ) {
				wp_enqueue_script('jquery-mixitup', get_theme_file_uri( '/js/jquery.mixitup.min.js' ), array( 'jquery' ), $kktfwp_theme_version, true);
		}
		
		if ( is_page_template('template-homepage-grid.php') || in_array( get_post_meta($postid, '_gallery_layout_value', true), array( '2-column', '3-column', '4-column', 'Thumbnails' ) ) && get_post_meta($postid, '_is_masonry_value', true) === 'Yes' || get_post_meta($postid, '_gallery_layout_value', true) === 'Mosaic' || get_post_meta($postid, '_gallery_layout_value', true) === 'Fullscreen-Grid' && get_post_meta($postid, '_grid_type_value', true) === 'Random' ) {
			wp_enqueue_script('masonry');
		}
		
		
		if(get_post_meta($postid, '_gallery_layout_value', true) !== 'Fullscreen' && !is_page_template('template-homepage.php')) {
			wp_enqueue_script( 'jquery-magnific-popup', get_theme_file_uri( '/js/jquery.magnific-popup.min.js' ), array( 'jquery' ), $kktfwp_theme_version, true);	
		}
				
		wp_enqueue_script( 'kktfwp-custom', get_theme_file_uri( '/js/jquery.custom.js' ), array( 'jquery' ), $kktfwp_theme_version, true );	
		wp_enqueue_script( 'lazysizes-js', get_theme_file_uri( '/js/lazysizes.min.js' ), array(), $kktfwp_theme_version, true);	
		
		
		/* Move an array to jQuery */
		
		KkGetOptions::init();
		$kk_data = KkGetOptions::TplConditions();
		wp_localize_script( 'kktfwp-custom', 'kk_js_data', $kk_data );
		
		if ( rwmb_meta( 'kk_slider_auto' ) ) {
			
			$postSliderData = array();
			$postSliderData['state'] = rwmb_meta( 'kk_slider_auto' );
			$postSliderData['delay'] = rwmb_meta( 'kk_sliderDelay' );
			
			wp_localize_script( 'kktfwp-custom', 'kk_postSlider', $postSliderData );
		}
		
					
		if ( post_password_required() && has_post_thumbnail() ) {
			$passImg = wp_get_attachment_image_src(get_post_thumbnail_id(kktfwp_postid()), 'featured-image-thumb');
			wp_localize_script( 'kktfwp-custom', 'kk_js_passImg', array('img_url' => esc_url( $passImg[0] ) ) );
		}
		
	}
}

add_action('wp_enqueue_scripts', 'kktfwp_get_jquery');


/*-----------------------------------------------------------------------------------*/
/* REGISTER GOOGLE FONTS
/* Create your own kktfwp_fonts_url() function to override in a child theme
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_fonts_url' ) ) {
	function kktfwp_fonts_url( $fonts = array() ) {
		$fonts_url 		= '';
		$fonts     		= $fonts;
		$subsets   		= 'latin,latin-ext';
		$variants  		= ':100,200,300,400,500,600,700';
		
		$fonts[] = 'Montserrat'.$variants;
		$fonts[] = 'Open Sans'.$variants;
		
		$fonts = array_unique($fonts);

		if ( $fonts ) {
			$fonts_url = add_query_arg( array(
				'family' => urlencode( implode( '|', $fonts ) ),
				'subset' => urlencode( $subsets ),
			), 'https://fonts.googleapis.com/css' );
		}

		return esc_url( $fonts_url );
	}
}


/*-----------------------------------------------------------------------------------*/
/* CHROME MOBILE BACKGROUND
/*-----------------------------------------------------------------------------------*/

if( ! function_exists( 'kktfwp_chrome_bg' ) ) {
	function kktfwp_chrome_bg() { 
	
		$bg = '#252525';
		
		if ( get_theme_mod( 'kktfwp_skin', 'light' ) === 'dark' ) {
			$bg = '#252525';			
		}

		return printf( '<meta name="theme-color" content="%s">', $bg );
	}
}

add_action('wp_head', 'kktfwp_chrome_bg');


/*-----------------------------------------------------------------------------------*/	
/* MAX CONTENT WIDTH
/*-----------------------------------------------------------------------------------*/	

if ( ! function_exists( 'kktfwp_content_width' ) ) {
	function kktfwp_content_width() {
		$GLOBALS['content_width'] = apply_filters( 'kktfwp_content_width', 2560 );
	}
}

add_action( 'after_setup_theme', 'kktfwp_content_width', 0 );

if ( ! function_exists( 'kktfwp_max_srcset_image_width' ) ) {
	function kktfwp_max_srcset_image_width( $w, $sizes = array() ) {
		return 5120;
	}
}
add_filter( 'max_srcset_image_width', 'kktfwp_max_srcset_image_width', 10, 2 );


/*-----------------------------------------------------------------------------------*/
/* BACKGROUND VIDEO
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_bgVideo' ) ) {
	function kktfwp_bgVideo( $postid ) {
	
		if ( get_theme_mod( 'kktfwp_content_width', 'kk-boxed' ) === 'kk-boxed' || is_page_template('template-homepage-slider.php') ) {
			$bgUrl = trim(get_post_meta($postid, '_bg_video_value', true));
			$fallbackImg = get_post_meta($postid, '_bg_image_fallback_value', true);
			
			$kk_data = array();
			$kk_data['muted'] = rwmb_meta( 'kk_muteVid' , false, $postid );
			
			if (strpos($bgUrl, 'youtube') > 0) {

			parse_str( parse_url( esc_url( $bgUrl ), PHP_URL_QUERY ), $array );	  
				$kk_data['video_source'] = $array['v'];
				$kk_data['helper'] = true;				
			} else {			
				$kk_data['video_source'] = esc_url( $bgUrl );				
			}

			$kk_data['fallback_img'] = esc_url( $fallbackImg );
			
			wp_localize_script( 'kktfwp-custom', 'kk_js_video', $kk_data );
		}
	}
}


/*-----------------------------------------------------------------------------------*/
/* PHOTO PROOFING AJAX HANDLER
/*-----------------------------------------------------------------------------------*/

add_action( 'wp_ajax_nopriv_kktfwp_proofing', 'kktfwp_proofing' );
add_action( 'wp_ajax_kktfwp_proofing', 'kktfwp_proofing' );

if ( !function_exists( 'kktfwp_proofing' ) ) {
	function kktfwp_proofing() {

		check_ajax_referer( 'proof-nonce', '_wpnonce' );
							
		$data = wp_get_attachment_metadata( $_POST['image_id'] );
		
		if ( isset($data['image_meta']['proof_status'] ) && $data['image_meta']['proof_status'] === 'checked' ) {
			$data['image_meta']['proof_status'] = 'unchecked';
		} else {
			$data['image_meta']['proof_status'] = 'checked';
		}	
				
		wp_update_attachment_metadata( $_POST['image_id'], $data );
		
		$new_status = wp_get_attachment_metadata( $_POST['image_id'] );			
		wp_send_json_success( $new_status['image_meta']['proof_status'] );
	}
}



/*-----------------------------------------------------------------------------------*/
/* FULLSCREEN GALLERY
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_galleryFS' ) ) {
	function kktfwp_galleryFS() {
		$postid = get_the_ID();
		$attachments = rwmb_meta( 'kk_imgadv', 'type=image_advanced', $post_id = $postid );	
	
		echo '<div class="royalSlider_fullscreen type-fullscreen-gallery rsKK"></div>';
		
		if ( get_post_meta($postid, '_full_style_value', true) === 'Fade' ) {
			$kk_galleryFS = 'fade';
		} else if ( get_post_meta($postid, '_full_style_value', true) === 'Move' ) {
			$kk_galleryFS = 'move';
		} else {
			$kk_galleryFS = get_theme_mod( 'kktfwp_fs_gallery', 'fade' );
		}
		
		if ( get_post_meta($postid, '_scale_mode_value', true) === 'Fill' ) {
			$kk_galleryScale = 'fill';
		} else if ( get_post_meta($postid, '_scale_mode_value', true) === 'Fit' ) {
			$kk_galleryScale = 'fit';
		}
		
		if ( get_post_meta($postid, '_near_by_value', true) === 'Yes' ) {
			$kk_near_by = 1;
		} else {
			$kk_near_by = 0;
		}
		
		$kk_autoplay = get_theme_mod( 'kktfwp_fs_gallery_autoplay', 0 );
		$kk_loop = get_theme_mod( 'kktfwp_fs_gallery_loop', 1 );
		
		// Built an array for slider
		
		$kk_data = array();
		$kk_data['galText'] = esc_html__( 'Drag or Click', 'toppic' );
		$kk_data['menuStyle'] = get_theme_mod( 'kktfwp_menu_style', 'kk-menu-default' );
		$kk_data['transitionType'] = $kk_galleryFS;
		$kk_data['galleryScale'] = $kk_galleryScale;
		$kk_data['visibleNearBy'] = $kk_near_by;
		$kk_data['galleryLoop'] = $kk_loop;
		$kk_data['transitionSpeed'] = get_theme_mod( 'kktfwp_fs_gallery_transition', '900');
		$kk_data['autoplay'] = array(
			'enabled' => $kk_autoplay,
			'delay' => get_theme_mod( 'kktfwp_fs_gallery_lenght', '4000'),
		);
		$kk_data['slides'] = array_merge($attachments);		
		
		// Move an array to jQuery
		
		if (!empty($attachments)) {
			wp_localize_script( 'kktfwp-custom', 'kk_js_gallery', $kk_data );
		}
	}
}


/*-----------------------------------------------------------------------------------*/
/* BACKGROUND SLIDESHOW
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_bgSlideshow' ) ) { 
	function kktfwp_bgSlideshow() {  
		// Check layout style. Enable slideshow if style 'Boxed' or it's a frontpage
		if ( get_theme_mod( 'kktfwp_content_width', 'kk-boxed' ) === 'kk-boxed' || is_page_template( 'template-homepage-slider.php' ) ) {	
			if ( get_theme_mod( 'kktfwp_slider_type', 'kk_royal' ) === 'kk_royal' ) {
				kktfwp_slideshowType_1();
			} elseif( get_theme_mod( 'kktfwp_slider_type', 'kk_royal' ) === 'ken_b') {
				kktfwp_slideshowType_2();
			}
		}  
	}
}
 
if ( ! function_exists( 'kktfwp_slideshowType_1' ) ) { 
	function kktfwp_slideshowType_1() {

		$postid = kktfwp_postid();

		$kk_data = array();

		$bg_post_meta = get_post_meta($postid, '_custom_bg_type_value', true);
		
		if( empty( $bg_post_meta ) ) {
			$bg_post_meta = 'Default';
		}
		
		$args = array(
			'post_type' => 'slider',
			'posts_per_page' => -1
		);	
		$query = new WP_Query( $args );
		
		if ( $query->have_posts() && $bg_post_meta === 'Default' ) {
		
			echo '<div class="royalSlider_fullscreen"></div>';
			
			$kk_data['ifDefault'] = '1';
			$attachments = array();
			
			while ( $query->have_posts()): $query->the_post();
			
			$full = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
			
			$attachments[get_post_thumbnail_id()] = array( 
												'image' => $full[0],
												'title' => get_post_meta(get_the_ID(), '_slide_title_value', true)
											);						
			endwhile;				
			
		} elseif ( rwmb_meta( 'kk_homegrid', 'type=image_advanced', $postid ) && $bg_post_meta === 'Custom Slideshow' ) {
			
			echo '<div class="royalSlider_fullscreen"></div>';
			
			$attachments = rwmb_meta( 'kk_homegrid', 'type=image_advanced', $post_id = $postid );	
			
		} else {
			kktfwp_bg_image();
		}
		
		wp_reset_postdata();
		
		if ( !empty( $attachments ) ) {			
			// Built an array for slider
			$kk_data['menuStyle'] = get_theme_mod( 'kktfwp_menu_style', 'kk-menu-default' );
			$kk_data['randomizeSlides'] = get_theme_mod( 'kktfwp_slider_random', 0 );
			$kk_data['transitionType'] = get_theme_mod( 'kktfwp_slider_effect', 'fade' );
			$kk_data['transitionSpeed'] = get_theme_mod( 'kktfwp_slider_transition', '900' );
			$kk_data['autoplay'] = array(
				'delay' => get_theme_mod( 'kktfwp_slider_lenght', '4000' ),
			);
			$kk_data['slides'] = array_merge($attachments);		
			
			// Move an array to jQuery		
			wp_localize_script( 'kktfwp-custom', 'kk_js_fs_slideshow', $kk_data );
		}
	}
}
	
	
if ( ! function_exists( 'kktfwp_slideshowType_2' ) ) { 	
	function kktfwp_slideshowType_2() {

		$postid = kktfwp_postid();
		
		// Built an array for slider		
		$kk_data = array();
		
		$bg_post_meta = get_post_meta($postid, '_custom_bg_type_value', true);
			
		if( empty( $bg_post_meta ) ) {
			$bg_post_meta = 'Default';
		}
		
		$args = array(
			'post_type' => 'slider',
			'posts_per_page' => -1
		);	
		$query = new WP_Query( $args );
	
		if( $query->have_posts() && $bg_post_meta === 'Default' ) {
			
			$kk_data['ifDefault'] = '1';
			
			while ( $query->have_posts()): $query->the_post();									
				$full = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');						
				$attachments[get_post_thumbnail_id()] = array( 
											'image' => $full[0],
											'title' => get_post_meta(get_the_ID(), '_slide_title_value', true)
										);									
			endwhile;
			 
			
		} elseif ( $bg_post_meta === 'Custom Slideshow' ) {		
		
			$attachments = rwmb_meta( 'kk_homegrid', 'type=image_advanced', $post_id = $postid );	
			
		} else {	
		
			kktfwp_bg_image();
			
		} 
			
		wp_reset_postdata();
		
		if ( !empty( $attachments ) ) {
			// Built an array for slider		
			$kk_data['fps'] = get_theme_mod( 'kktfwp_kenb_fps', '60' );
			$kk_data['delay'] = get_theme_mod( 'kktfwp_kenb_lenght', '4000' );
			$kk_data['transitionSpeed'] = get_theme_mod( 'kktfwp_kenb_transition', '1000' );
			$kk_data['zoom'] = get_theme_mod( 'kktfwp_kenb_zoom', '1.2' );
			$kk_data['slides'] = array_merge($attachments);
			
			// Move an array to jQuery		
			wp_localize_script( 'kktfwp-custom', 'kk_js_fsk_slideshow', $kk_data );
		}
	}
}



/*-----------------------------------------------------------------------------------*/
/* ADD EXTRA CLASSES TO THE BODY CLASS 
/*-----------------------------------------------------------------------------------*/

add_filter( 'body_class' , 'kktfwp_body_class' );

if ( ! function_exists( 'kktfwp_body_class' ) ) { 
	function kktfwp_body_class( $classes ) {

		$postid = kktfwp_postid();
		
		$classes[] = 'kk_vc';
		
		$content_style = get_theme_mod( 'kktfwp_content_width', 'kk-boxed' );
		
		switch ( $content_style ) {
			case 'kk-boxed':
				$classes[] = 'kk-content-boxed';
				break;
			case 'kk-full':
				$classes[] = 'kk-content-fullwidth';
				if ( rwmb_meta( 'kk_title_type', 'type=select', $postid ) === 'parallax' && get_post_meta( $postid, '_gallery_layout_value', true ) !== 'Fullscreen' ) {

					$classes[] = 'kk-parallax-title';
					wp_localize_script( 'kktfwp-custom', 'kk_title_parallax', array('is_active' => true) );
					
					if ( rwmb_meta( 'kk_title_size', 'type=select', $postid ) === 'full' ) {
						$classes[] = 'kk-parallax-title-fullheight';
						wp_localize_script( 'kktfwp-custom', 'kk_title_height', array('is_active' => true) );
					}
				}
				break;	
		}
		
		$menu_style = get_theme_mod( 'kktfwp_menu_style', 'kk-menu-default' );
		
		switch ( $menu_style ) {
			case 'kk-menu-default':
				$classes[] = 'kk_menu_default';
				break;
			case 'kk-menu-classic':
				$classes[] = 'kk_menu_classic';
				break;
			case 'kk-menu-default-hidden':
				$classes[] = 'kk_menu_default_hidden';
				break;
			case 'kk-menu-slide-right':
				$classes[] = 'kk_menu_slide_right';
				break;
			case 'kk-menu-left-boxed':
				$classes[] = 'kk_menu_left_boxed';
				break;
			case 'kk-menu-fullscreen':
				$classes[] = 'kk_menu_fullscreen';
				break;
		}	
		
		if ( rwmb_meta( 'kk_proof' ) === 'proofing' ) {
			$classes[] = 'kk-photo-proofing';		
		}
		
		if ( get_theme_mod( 'kktfwp_borders', 0 ) && get_theme_mod( 'kktfwp_menu_style', 'kk-menu-default' ) === 'kk-menu-slide-right' )  {
			$classes[] = 'kk-is-borders';
		}
		
		if ( in_array( get_post_meta($postid, '_gallery_layout_value', true), array( '2-column', '3-column', '4-column', 'Thumbnails' ) ) && get_post_meta($postid, '_is_masonry_value', true) == 'Yes' || get_post_meta($postid, '_gallery_layout_value', true) == 'Mosaic' ) {
			$classes[] = 'kk-masonry';
		}
		
		if ( post_password_required($postid) && !is_front_page() && !is_archive() ) {
			$classes[] = 'password-protected';
		}
		
		if ( get_theme_mod( 'kktfwp_skin', 'light' ) === 'dark' ) {
			$classes[] = 'dark-skin';
		}
		
		// return the $classes array
		return $classes;	
	}
}

/*-----------------------------------------------------------------------------------*/
/* COMMENTS NAVIGATION
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_comment_nav' ) ) {
	function kktfwp_comment_nav() {
		// Are there comments to navigate through?
		if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) {
		?>
		<nav class="navigation comment-navigation" role="navigation">
			<div class="com-nav-links">
				<?php
					if ( $prev_link = get_previous_comments_link( esc_html__( 'Older Comments', 'toppic' ) ) ) {
						printf( '<div class="com-nav-previous">%s</div>', $prev_link );
					}

					if ( $next_link = get_next_comments_link( esc_html__( 'Newer Comments', 'toppic' ) ) ) {
						printf( '<div class="com-nav-next">%s</div>', $next_link );
					}
				?>
			</div><!-- .nav-links -->
		</nav><!-- .comment-navigation -->
		<?php
		}
	}
}

/*-----------------------------------------------------------------------------------*/
/* PROJECTS NAVIGATION
/*-----------------------------------------------------------------------------------*/  

if ( !function_exists( 'kktfwp_project_nav' ) ) {
  
	function kktfwp_project_nav() {
	
		$next = 'get_next_post_link';
		$prev = 'get_previous_post_link';
		
		if ( get_theme_mod( 'kktfwp_project_nav', 0 ) ) {
			$next = 'get_previous_post_link';
			$prev = 'get_next_post_link';
		}

		echo '<div class="proj-nav">					
			<div class="proj-prev">'.
				$prev( '%link', '<i class="kk-icon-left-open-big"></i><div class="proj-nav-title"><span class="proj-title">%title</span><span class="proj-label">'. esc_html__( 'Prev Project', 'toppic' ) .'</span></div>', true, '', 'portfolio-type' ).'
			</div>
			<div class="proj-next">'.
				$next( '%link', '<div class="proj-nav-title"><span class="proj-title">%title</span><span class="proj-label">'. esc_html__( 'Next Project', 'toppic' ) .'</span></div><i class="kk-icon-right-open-big"></i>', true, '', 'portfolio-type' ).'
			</div>
		</div>';
	}
}


/*-----------------------------------------------------------------------------------*/
/* FILTERS
/*-----------------------------------------------------------------------------------*/

add_filter('widget_text', 'do_shortcode');
add_filter('the_excerpt', 'do_shortcode');

if ( ! function_exists( 'kktfwp_filter_wp_page_menu_args' ) ) {
	function kktfwp_filter_wp_page_menu_args($args) {
		$args['show_home'] = true;
		return $args;
	}
}
add_filter('wp_page_menu_args', 'kktfwp_filter_wp_page_menu_args');


if ( ! function_exists( 'kktfwp_search_all' ) ) {
	function kktfwp_search_all($query){
		if($query->is_search) $query->set('post_type', array('post, page, portfolio, gallery'));
		return $query;
	}
}

add_filter('the_search_query', 'kktfwp_search_all');


if ( ! function_exists( 'kktfwp_more_link' ) ) {
	function kktfwp_more_link( $more_link, $more_link_text ) {
		return '<a class="more-link" href="'. get_permalink() . '">'. esc_html__('Read more','toppic') .'</a>';
	}
}

add_filter( 'the_content_more_link', 'kktfwp_more_link', 10, 2 );


/*-----------------------------------------------------------------------------------*/
/* COMMENTS
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'toppic_comment' ) ) {
	function toppic_comment( $comment, $args, $depth ) {
		
		switch ( $comment->comment_type ) :		
		case 'pingback' :
		case 'trackback' : ?>
				<div class="pingback-entry"><span class="pingback-heading"><?php esc_html_e( 'Pingback:', 'toppic' ); ?></span> <?php comment_author_link(); ?></div>
		<?php 
		break;
		default : ?>
			<li id="comment-<?php comment_ID()?>" class="comment wrapper">
				<article class="comment-body">
					<div class="kk-gravatar vcard">	
						<?php echo get_avatar($comment, 60)?>
					</div>
					<div class="kk-comment-wrapper">	
						<div class="comment-info">				
							<div class="response-info">
								<strong class="comment-author"><?php echo get_comment_author_link()?></strong>&nbsp;&nbsp;&mdash;&nbsp;&nbsp;<?php printf( esc_html__('%1$s at %2$s','toppic'),get_comment_date(),get_comment_time())?>
							</div>				
						</div>
						<div class="comment-text">
							<?php if($comment->comment_approved=='0') {
									esc_html_e( 'Your comment is awaiting moderation.', 'toppic' );
								} else { 
									comment_text();
									comment_reply_link(array_merge($args, array('depth'=>$depth, 'max_depth'=>$args['max_depth'], 'after' => '<i class="kk kk-icon-reply"></i>')));
							} ?>
						</div>
					</div>
				</article>
			</li>
			<?php
		endswitch;
	}
}


/*-----------------------------------------------------------------------------------*/
/* TITLE TYPE
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_title_type' ) ) {
	function kktfwp_title_type() {
	
		$postid = kktfwp_postid();
		
		$title_type = rwmb_meta( 'kk_title_type', '', $postid );

		if( empty( $title_type ) ||  get_theme_mod( 'kktfwp_content_width', 'kk-boxed' ) === 'kk-boxed' ) {
			$title_type = 'default';
		}	

		return $title_type;		
	}
}

/*-----------------------------------------------------------------------------------*/
/* PARALLAX HEADER
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_parallax' ) ) {
	function kktfwp_parallax() {
	
		$postid = kktfwp_postid();
		
		$hero_bg_img = false;
		$hero_bg_vid = false;
		
		// BG Image		
		$hero_bg_img = rwmb_meta( 'kk_title_bg', '', $postid );	
				
		if ( is_array ($hero_bg_img) ) {			
			$hero_bg_img = array_shift($hero_bg_img);		
		}
		
		
		// BG Video
		$hero_bg_vid = rwmb_meta( 'kk_herovid', '', $postid );
		$muteVideo = 'muted';
		
		if ( rwmb_meta( 'kk_muteVidP', '', $postid ) === '1' ) {
			$muteVideo = 'muted';
		} else if ( rwmb_meta( 'kk_muteVidP', '', $postid ) === '0' ) {
			$muteVideo = '';
		}
		
		if ( is_array($hero_bg_vid) ) {			
			$hero_bg_vid = array_shift( $hero_bg_vid );
		}
		
		$out = '';
		
		if ( $hero_bg_vid ) {
			$out .= '<div id="kk-parallax-video" class="kk-parallax-video">';
				$out .= '<video id="kk-html5-video" class="kk-html5-video kktfwp-adjust-me" loop autoplay playsinline preload="none" '.esc_attr( $muteVideo ).'><source src="'. esc_url( $hero_bg_vid['url'] ).'" type="video/'.esc_attr( $hero_bg_vid['fileformat'] ).'" /></video>';		
			$out .= '</div>';
		}
		
		if ( $hero_bg_img ) {
			$out .= '<div id="kk-parallax-bg" class="kk-parallax-bg kk-hidden">';				
				$out .= '<img id="kk-img-check" src="'. esc_url( $hero_bg_img['full_url'] ) .'" alt="'. esc_attr( $hero_bg_img['alt'] ) .'" />';
			$out .= '</div>';
		}
		
		return $out;		
	}
}

if ( ! function_exists( 'kktfwp_parallax_style' ) ) {
	function kktfwp_parallax_style() {
	
		$postid = kktfwp_postid();

		$bg_title = rwmb_meta( 'kk_title_bg', 'type=image_advanced', $postid );
		
		if( !empty( $bg_title ) ) {
		
			$bg_title_img = array_shift($bg_title);
			
			$out = sprintf( '.kk-parallax-wrapper > .kk-parallax-bg { background-image: url("%1$s"); }', 
				esc_url( $bg_title_img['full_url'] )
			);

			wp_add_inline_style( 'kktfwp-style', $out );					
		
		} else {		
			return;		
		}		
	}
}

add_action( 'wp_enqueue_scripts', 'kktfwp_parallax_style' );



/*-----------------------------------------------------------------------------------*/
/* OPEN WORDPRESS IMAGES IN LIGHTBOX
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_add_rel_to_gallery' ) ) {
	function kktfwp_add_rel_to_gallery($link, $id = null) {
		$id = intval( $id );
		$_post = get_post( $id );
		$post_title = esc_attr( $_post->post_title );
		$link = str_replace("'><img", "' class=\"kk-lightbox\" data-title=\"".$post_title."\"><img", $link);
		return $link;
	}
}

add_filter('wp_get_attachment_link', 'kktfwp_add_rel_to_gallery', 10, 2);


/*-----------------------------------------------------------------------------------*/
/* HELPERS
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_BackLink' ) ) {
	function kktfwp_BackLink()
	{
			$get_posts = new WP_Query;
			$ppages = array();
			$pages = $get_posts->query('post_type=page&posts_per_page=-1');
			foreach ($pages as $page) {
				$post_custom = get_post_custom($page->ID);
				if ($post_custom['_wp_page_template'][0] == 'template-portfolio.php') {
					$ppages[] = $page->ID;
				}
			}
			if (!empty($ppages)) {
				echo '<span class="all-l" title="' . esc_html__('View all projects', 'toppic') . '"><a href="' . get_permalink($ppages[0]) . '"></a></span>';
			}
	}
}

if ( ! function_exists( 'kktfwp_portfolioID' ) ) {
	function kktfwp_portfolioID()
	{
			$ppages = array();
			
			$pages = get_pages(

				 array(
					'meta_key' => '_wp_page_template',
					'meta_value' => 'template-portfolio.php'
				)
			);
			
			foreach ($pages as $page) {			
					$ppages[] = $page->ID;
			}
			
			if (!empty($ppages)) {
				return $ppages[0];
			}
	}
}

if ( ! function_exists( 'kktfwp_galleryID' ) ) {
	function kktfwp_galleryID()
	{
			$ppages = array();
			
			$pages = get_pages(

				 array(
					'meta_key' => '_wp_page_template',
					'meta_value' => 'template-gallery.php'
				)
			);
			foreach ($pages as $page) {
					$ppages[] = $page->ID;
			}
			if (!empty($ppages)) {
				return $ppages[0];
			}
	}
}

if ( ! function_exists( 'kktfwp_BlogLink' ) ) {
	function kktfwp_BlogLink()
	{
			$posts_page_id = get_option( 'page_for_posts');
			$site_url = home_url() ;
			
			echo '<div class="all-l" title="' . esc_html__( 'View all posts', 'toppic' ) . '"><a href="'.$site_url.'/?page_id=' . $posts_page_id . '"></a></div>';

	}
}


/*-----------------------------------------------------------------------------------*/
/* PAGINATION
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_page_pagination' ) ) {
	function kktfwp_page_pagination($pages = '', $range = 2) {

		echo "<div class='pagination' id='kk-pagination'>"; ?>
			<div class="alignleft"><?php next_posts_link(esc_html__( '&laquo; Previous Entries','toppic' ) ) ?></div>
			<div class="alignright"><?php previous_posts_link(esc_html__( 'Next Entries &raquo;','toppic' ) ) ?></div>
		 <?php echo "</div>\n";
	}
}


/*-----------------------------------------------------------------------------------*/
/* SOCIAL PROFILES
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_social_profiles' ) ) {
	function kktfwp_social_profiles() {

		$opArray = get_theme_mod( 'kktfwp_social' );
		
		if ( !$opArray ) return;
			
		$output = '';
		
		foreach( $opArray as $key => $value ) {
			
			if ( $value !== '' ) {
				$output .= '<li><a class="kk-icon-'. esc_attr( $key ) .'" title="'. ucfirst( esc_attr( $key ) ) .'" target="_blank" href="'. ( $key === 'skype' ? 'callto:'.esc_attr( $value ) : esc_url( $value ) ) .'"></a></li>';
			}			
		}
		
		if ( $output !== '' ) {
			return '<ul class="social">' .$output. '</ul>';
		} else {
			return;
		}		
	}
}


/*-----------------------------------------------------------------------------------*/
/* DISABLING COMMENTS BY DEFAULT
/*-----------------------------------------------------------------------------------*/

add_filter( 'wp_insert_post_data', 'kktfwp_default_page_comments_off' );

if ( ! function_exists( 'kktfwp_default_page_comments_off' ) ) {
	function kktfwp_default_page_comments_off( $data ) {
		if ( in_array( $data['post_type'], array( 'page', 'portfolio', 'gallery' ) ) && $data['post_status'] == 'auto-draft' ) {
			$data['comment_status'] = 0;
			$data['ping_status'] = 0;
		}
		return $data;
	}
  }

/*-----------------------------------------------------------------------------------*/
/* IMAGES QUALITY
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_jpeg_quality_callback' ) ) {
	function kktfwp_jpeg_quality_callback( $arg ) {
		return (int)85;
	}
}

add_filter('jpeg_quality', 'kktfwp_jpeg_quality_callback');


/*-----------------------------------------------------------------------------------*/
/* BACKGROUND SINGLE IMAGE
/*-----------------------------------------------------------------------------------*/

if ( !function_exists( 'kktfwp_bg_image' ) ) {
	function kktfwp_bg_image() {		
				
		$postid = kktfwp_postid();	
		
		if ( in_array( get_post_meta( $postid, '_custom_bg_type_value', true) , array( 'Video', 'Custom Slideshow' ) ) ) return;
		
		$bg_image = ( !empty(get_post_meta( $postid, '_bg_image_value', true )) && get_post_meta( $postid, '_custom_bg_type_value', true) === 'Image' ) ? get_post_meta( $postid, '_bg_image_value', true ) : wp_get_attachment_image_url( get_theme_mod( 'kktfwp_bg_image' ), 'kktfwp-xxl' ) ;
		
		if ( !!$bg_image ) {
			printf( '<div class="kktfwp-bgImage"><img src="%1$s" alt="%2$s background image" /></div>',
				$bg_image,
				get_bloginfo( 'name' )
			);
		}
	}
}


/*-----------------------------------------------------------------------------------*/
/* CONVERTS A HEX VALUE TO RGB
/*
/* @param string $color The original color, in 3- or 6-digit hexadecimal form.
/* @return array Array containing RGB (red, green, and blue) values for the given
/*               HEX code, empty array otherwise.
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_hex2rgb' ) ) {

	function kktfwp_hex2rgb( $color ) {
		$color = trim( $color, '#' );

		if ( strlen( $color ) === 3 ) {
			$r = hexdec( substr( $color, 0, 1 ).substr( $color, 0, 1 ) );
			$g = hexdec( substr( $color, 1, 1 ).substr( $color, 1, 1 ) );
			$b = hexdec( substr( $color, 2, 1 ).substr( $color, 2, 1 ) );
		} else if ( strlen( $color ) === 6 ) {
			$r = hexdec( substr( $color, 0, 2 ) );
			$g = hexdec( substr( $color, 2, 2 ) );
			$b = hexdec( substr( $color, 4, 2 ) );
		} else {
			return array();
		}

		return array( 'red' => $r, 'green' => $g, 'blue' => $b );
	}
}

/*-----------------------------------------------------------------------------------*/
/* MENU COLOR
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_menuColor' ) ) {
	function kktfwp_menuColor() {

		$color = kktfwp_hex2rgb( get_theme_mod( 'kktfwp_menu_bg_color' ) );
		
		if ( empty( $color ) ) {
			return;
		}
		
		if ( in_array( get_theme_mod( 'kktfwp_menu_style', 'kk-menu-default' ), array( 'kk-menu-slide-right', 'kk-menu-default', 'kk-menu-classic', 'kk-menu-default-hidden' ) ) ) {
		
			$color_rgb = vsprintf( 'rgb( %1$s, %2$s, %3$s)', $color );
			
			return $color_rgb;
		
		} else {
		
			$color[] = absint( get_theme_mod( 'kktfwp_menu_opacity' ) ) / 100;
			
			$color_rgb = vsprintf( 'rgba( %1$s, %2$s, %3$s, %4$s)', $color );
			
			return $color_rgb;
			
		}
	}
}


/*-----------------------------------------------------------------------------------*/
/* WOOCOMMERCE
/*-----------------------------------------------------------------------------------*/

// Default image sizes
if ( ! function_exists( 'kktfwp_default_woo_image_dimensions' ) ) {
	function kktfwp_default_woo_image_dimensions() {
	
		global $pagenow;
	 
		if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {
			return;
		}
		
		$catalog = array(
			'width' => '800', // px
			'height'=> '800', // px
			'crop' => 1 // true
		);

		$single = array(
			'width' => '1000', // px
			'height'=> '1000', // px
			'crop' => 0 // true
		);

		$thumbnail = array(
			'width' => '300', // px
			'height'=> '300', // px
			'crop' => 1 // true
		);

		// Image sizes
		update_option( 'shop_catalog_image_size', $catalog ); // Product category thumbs
		update_option( 'shop_single_image_size', $single ); // Single product image
		update_option( 'shop_thumbnail_image_size', $thumbnail ); // Image gallery thumbs
	}
}

add_action( 'after_switch_theme', 'kktfwp_default_woo_image_dimensions', 1 );


// Wrap shop loop

if ( ! function_exists( 'kktfwp_woo_before_shop_loop' ) ) {
	function kktfwp_woo_before_shop_loop() {
		echo '<div class="divider clear"></div><div class="kk-woo-shop-wrapper">';
	}
}

add_action('woocommerce_before_shop_loop', 'kktfwp_woo_before_shop_loop');

if ( ! function_exists( 'kktfwp_woo_after_shop_loop' ) ) {
	function kktfwp_woo_after_shop_loop() {
		echo '</div>';
	}
}

add_action('woocommerce_after_shop_loop', 'kktfwp_woo_after_shop_loop');


// Move filter
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
add_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 30 );

remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
add_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 20 );


// Change number or products per row

add_filter('loop_shop_columns', 'kktfwp_loop_columns');

if ( ! function_exists( 'kktfwp_loop_columns' ) ) {
	function kktfwp_loop_columns() {
		return 3; // 3 products per row
	}
} 


// Redefine woocommerce_output_related_products()

add_filter( 'woocommerce_output_related_products_args', 'kktfwp_related_products_args' );
if ( ! function_exists( 'kktfwp_related_products_args' ) ) {
	function kktfwp_related_products_args( $args ) {
		$args['posts_per_page'] = 3;
		$args['columns'] = 3; 
		return $args;
	}
}


// Move ratings
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
add_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_rating', 5 );


// Move price on single product page
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 27 );


// Wrap product element
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_result_count', 20 );
add_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 30 );

if ( ! function_exists( 'kk_woo_before_shop_loop_item_title' ) ) {
	function kk_woo_before_shop_loop_item_title() {
		echo '<div class="kk-woo-shop-item">';
	}
}

add_action('woocommerce_before_shop_loop', 'kk_woo_before_shop_loop_item_title');

if ( ! function_exists( 'kktfwp_woo_after_shop_loop_item_title' ) ) {
	function kktfwp_woo_after_shop_loop_item_title() {
		echo '</div>';
	}
}

add_action('woocommerce_after_shop_loop', 'kktfwp_woo_after_shop_loop_item_title');


// Single product page

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_before_single_product', 'woocommerce_template_single_title', 1 );

if ( ! function_exists( 'kktfwp_woo_before_single_product_1' ) ) {
	function kktfwp_woo_before_single_product_1() {
		echo '<div class="inner-content">';
	}
}

add_action('woocommerce_before_single_product', 'kktfwp_woo_before_single_product_1', 4);

if ( ! function_exists( 'kktfwp_woo_after_single_product_2' ) ) {
	function kktfwp_woo_after_single_product_2() {
		echo '</div>';
	}
}

add_action('woocommerce_after_single_product', 'kktfwp_woo_after_single_product_2', 6);

if ( ! function_exists( 'kktfwp_woo_before_single_product_3' ) ) {
	function kktfwp_woo_before_single_product_3() {
		echo '<div class="divider clear"></div>';
	}
}

add_action('woocommerce_before_single_product', 'kktfwp_woo_before_single_product_3', 2);


// Remove product description heading (in tabs)

add_filter( 'woocommerce_product_description_heading', '__return_false', 10 );


// Remove upsell block

remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );

if ( ! function_exists( 'kktfwp_woocommerce_after_single_product_summary_1' ) ) {
	function kktfwp_woocommerce_after_single_product_summary_1() {
		echo '<div class="kk-woo-related">';
	}
}

add_action('woocommerce_after_single_product_summary', 'kktfwp_woocommerce_after_single_product_summary_1', 19);

if ( ! function_exists( 'kktfwp_woocommerce_after_single_product_summary_2' ) ) {
	function kktfwp_woocommerce_after_single_product_summary_2() {
		echo '</div>';
	}
}

add_action('woocommerce_after_single_product_summary', 'kktfwp_woocommerce_after_single_product_summary_2', 21);

/**
 * Displays the optional custom logo.
 * Set default theme logo if the custom logo is not available. *
 * @since TopPic 1.8
 */
 
if ( !function_exists ( 'kktfwp_check_ssl' ) ) {
	
	function kktfwp_check_ssl( $path ) {
			
		if ( is_ssl() ) {
			$url = parse_url( $path );

			if( $url['scheme'] !== 'https' ){
			   $path = preg_replace( '/^http:/i', 'https:', $path );
			}
		}
		
		return $path;
	}
}



/*-----------------------------------------------------------------------------------*/
/* CUSTOM LOGO FALLBACK FOR OLD VERSION
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_the_custom_logo_fallback' ) ) {
	function kktfwp_the_custom_logo_fallback() {
		
		$logo = get_option( 'toppic_theme_logo_main' );
		$logo_light = get_option( 'toppic_theme_logo_main_light' );
		$logo_retina = get_option( 'toppic_theme_logo_main_retina' );
		$logo_retina_light = get_option( 'toppic_theme_logo_main_light_retina' );

		if ( empty( $logo ) && empty( $logo_light ) ) {
			$logo = get_theme_file_uri( '/images/logo.png' );
		} else if ( empty( $logo ) && !empty( $logo_light ) ) {
			$logo = $logo_light;
		}		
		
		if ( empty( $logo_light ) && empty( $logo ) ) {
			$logo_light = get_theme_file_uri( '/images/logo-light.png' );
		} else if ( !empty( $logo ) && empty( $logo_light ) ) {
			$logo_light = $logo;
		}
		
		if ( empty( $logo_retina ) ) {
			$logo_retina = $logo;
		}
		if ( empty( $logo_retina_light ) ) {
			$logo_retina_light = $logo_light;
		}
		
		$info = getimagesize( $logo );	
		$html = '';
		
		if ( in_array( get_theme_mod( 'kktfwp_menu_style', 'kk-menu-default' ), array( 'kk-menu-slide-right', 'kk-menu-fullscreen', 'kk-menu-default', 'kk-menu-classic', 'kk-menu-default-hidden', 'kk-menu-left-boxed' ) ) ) {
			
			$html .= '<img class="logo-light" src="'. esc_url( kktfwp_check_ssl( $logo_light ) ) .'" width="'. esc_attr( $info[0] ) .'" height="'. esc_attr( $info[1] ). '" alt="'. esc_attr( get_bloginfo( 'name', 'display' ) ) .'">';
		
			$html .= '<img class="logo-light2x" src="'. esc_url( kktfwp_check_ssl( $logo_retina_light ) ) .'" width="'. esc_attr( $info[0] ) .'" height="'. esc_attr( $info[1] ). '" alt="'. esc_attr( get_bloginfo( 'name', 'display' ) ) .'">';				
		}
		
		if ( in_array( get_theme_mod( 'kktfwp_menu_style', 'kk-menu-default' ), array( 'kk-menu-slide-right', 'kk-menu-fullscreen', 'kk-menu-classic' ) ) ) {
			
			$html .= '<img class="logo-dark" src="'. esc_url( kktfwp_check_ssl( $logo ) ) .'" width="'. esc_attr( $info[0] ) .'" height="'. esc_attr( $info[1] ). '" alt="'. esc_attr( get_bloginfo( 'name', 'display' ) ) .'">';
			
			$html .= '<img class="logo-dark2x" src="'. esc_url( kktfwp_check_ssl( $logo_retina ) ) .'" width="'. esc_attr( $info[0] ) .'" height="'. esc_attr( $info[1] ). '" alt="'. esc_attr( get_bloginfo( 'name', 'display' ) ) .'">';			
		}
		
		echo '<a href="'. esc_url( home_url( '/' ) ) .'" class="custom-logo-link" rel="home">'. $html .'</a>';
	}
}


/*-----------------------------------------------------------------------------------*/
/* CUSTOM LOGO FOR NEW VERSION
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'kktfwp_the_custom_logo' ) ) {
	function kktfwp_the_custom_logo() {
		
		$old_logo_check = ( !empty( get_option( 'toppic_theme_logo_main' ) || get_option( 'toppic_theme_logo_main_light' ) || get_option( 'toppic_theme_logo_main_retina' ) || get_option( 'toppic_theme_logo_main_light_retina' ) ) ) ? true : false;
		
		$new_logo_check = ( !empty( get_theme_mod( 'kktfwp_logo' ) || get_theme_mod( 'kktfwp_logo2x' ) || get_theme_mod( 'kktfwp_logo_dark' ) || get_theme_mod( 'kktfwp_logo_dark2x' ) ) ) ? true : false;
		
		if ( $new_logo_check || !$old_logo_check ) {
			if ( ! $new_logo_check ) {
				printf( '<a href="%s" id="logo--text" class="custom-logo-link" rel="home">%s</a>', 
					esc_url( home_url( '/' ) ), 
					esc_html( get_bloginfo( 'name' ) )
				);			
			} else {
				
				$logo 			= ( !empty( get_theme_mod( 'kktfwp_logo' ) ) ) ? get_theme_mod( 'kktfwp_logo' ) : get_theme_mod( 'kktfwp_logo2x' );
				$logo2x 		= ( !empty( get_theme_mod( 'kktfwp_logo2x' ) ) ) ? get_theme_mod( 'kktfwp_logo2x' ) : get_theme_mod( 'kktfwp_logo' );
				$logo_dark 		= ( !empty( get_theme_mod( 'kktfwp_logo_dark' ) ) ) ? get_theme_mod( 'kktfwp_logo_dark' ) : get_theme_mod( 'kktfwp_logo_dark2x' );
				$logo_dark2x 	= ( !empty( get_theme_mod( 'kktfwp_logo_dark2x' ) ) ) ? get_theme_mod( 'kktfwp_logo_dark2x' ) : get_theme_mod( 'kktfwp_logo_dark' );

				if ( empty( $logo ) ) {
					$logo = $logo_dark;
					$logo2x = $logo_dark2x;
				} elseif ( empty( $logo_dark ) ) {
					$logo_dark = $logo;
					$logo_dark2x = $logo2x;
				}
				
				$logo_atts = wp_get_attachment_image_src( $logo, 'full' );
				$logo2x_url = wp_get_attachment_image_url( $logo2x, 'full' );
				
				if ( ! in_array( get_theme_mod( 'kktfwp_menu_style', 'kk-menu-default' ), array( 'kk-menu-slide-right', 'kk-menu-fullscreen', 'kk-menu-classic' ) ) ) {
					
					printf ( '<a href="%1$s" id="logo--img" class="custom-logo-link" rel="home"><img class="logo-light" src="%2$s" width="%3$s" height="%4$s" alt="%5$s"><img class="logo-light2x" src="%6$s" width="%3$s" height="%4$s" alt="%5$s"></a>',
						esc_url( home_url( '/' ) ),
						esc_url( kktfwp_check_ssl( $logo_atts[0] ) ),
						esc_attr( $logo_atts[1] ),
						esc_attr( $logo_atts[2] ),
						esc_attr( kktfwp_themeData('Name') ),
						esc_url( kktfwp_check_ssl( $logo2x_url ) )
					);
					
				} else {
					
					$logo_dark_url = wp_get_attachment_image_url( $logo_dark, 'full' );
					$logo_dark2x_url = wp_get_attachment_image_url( $logo_dark2x, 'full' );
				
					printf ( '<a href="%1$s" id="logo--img" class="custom-logo-link" rel="home"><img class="logo-light" src="%2$s" width="%3$s" height="%4$s" alt="%5$s"><img class="logo-light2x" src="%6$s" width="%3$s" height="%4$s" alt="%5$s"><img class="logo-dark" src="%7$s" width="%3$s" height="%4$s" alt="%5$s"><img class="logo-dark2x" src="%8$s" width="%3$s" height="%4$s" alt="%5$s"></a>',
						esc_url( home_url( '/' ) ),
						esc_url( kktfwp_check_ssl( $logo_atts[0] ) ),
						esc_attr( $logo_atts[1] ),
						esc_attr( $logo_atts[2] ),
						esc_attr( kktfwp_themeData('Name') ),
						esc_url( kktfwp_check_ssl( $logo2x_url ) ),
						esc_url( kktfwp_check_ssl( $logo_dark_url ) ),
						esc_url( kktfwp_check_ssl( $logo_dark2x_url ) )
					);
				}				
			}
			
			if ( $old_logo_check ) {
				delete_option( 'toppic_theme_logo_main' );
				delete_option( 'toppic_theme_logo_main_light' );
				delete_option( 'toppic_theme_logo_main_retina' );
				delete_option( 'toppic_theme_logo_main_light_retina' );
			}	
				
		} elseif ( $old_logo_check ) {
			kktfwp_the_custom_logo_fallback();				
		}
	}
}

/*-----------------------------------------------------------------------------------*/
/* CUSTOMIZER SETTINGS
/*-----------------------------------------------------------------------------------*/

require_once get_theme_file_path( '/includes/customizer/class-kktfwp-settings.php' );
require_once get_theme_file_path( '/includes/customizer/class-kktfwp-inline-css.php' );


/*-----------------------------------------------------------------------------------*/
/* wp_body_open backwards compatibility
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( 'wp_body_open' ) ) {
    function wp_body_open() {
        do_action( 'wp_body_open' );
    }
}


/*-----------------------------------------------------------------------------------*/
/* GET FILTER CATEGORIES
/*-----------------------------------------------------------------------------------*/
  
if ( !function_exists( 'kktfwp_filter' ) ) {  
	function kktfwp_filter( $args = array(), $taxonomy ) {
	
		$query = new WP_Query( $args );
									
		while ( $query->have_posts() ) : $query->the_post();
	
		$terms =  get_the_terms( get_the_id(), $taxonomy );
		
		if( isset( $terms ) && is_array( $terms ) ) {	
			foreach( $terms as $term ) {
				$termsArr[] = array (
						'slug' => $term->slug,
						'name' => $term->name,
					);
			}
		}	

		endwhile; wp_reset_postdata();
		
		if( isset( $termsArr ) ) {	
		
			$num = array_count_values(array_map(function($item) {
				return $item['slug'];
			}, $termsArr));
			
			$termsArr = array_map("unserialize", array_unique(array_map("serialize", $termsArr)));
		
			return $termsArr;
		}
		
		return;	
	}
}


/*-----------------------------------------------------------------------------------*/
/* KSES ALLOWED ARGS
/*-----------------------------------------------------------------------------------*/  

if ( ! function_exists( 'kktfwp_kses_allowed_args' ) ) {
	function kktfwp_kses_allowed_args() {
		
		$kses_args = array(
			'h1'     => array(
				'style' => array(),
				'class' => array(),
				'id' => array()
			),
			'h2'     => array(
				'style' => array(),
				'class' => array(),
				'id' => array()
			),
			'h3'     => array(
				'style' => array(),
				'class' => array(),
				'id' => array()
			),
			'h4'     => array(
				'style' => array(),
				'class' => array(),
				'id' => array()
			),
			'h5'     => array(
				'style' => array(),
				'class' => array(),
				'id' => array()
			),
			'h6'     => array(
				'style' => array(),
				'class' => array(),
				'id' => array()
			),
			'p'     => array(
				'style' => array(),
				'class' => array(),
				'id' => array()
			),
			'a'      => array(
					'href'  => array(),
					'title' => array(),
					'target'=> array()
			),
			'br'     => array(),
			'em'     => array(),
			'span'   => array(
				'style' => array(),
				'class'  => array(),
				'id' => array()
			),
			'strong' => array(),
			'img'    => array(
					'src'   => array(),
					'alt' => array(),
					'style'=> array(),
					'class'  => array(),
					'title' => array()
			)
		);
		
		return $kses_args;
	}
}
