<?php

/**
 * thim functions and definitions
 *
 * @package thim
 */
/**
 * Set the content width based on the theme's design and stylesheet.
 */
if ( ! isset( $content_width ) ) {
	$content_width = 640; /* pixels */
}

define( 'THIM_DIR', trailingslashit( get_template_directory() ) );
define( 'THIM_URI', trailingslashit( get_template_directory_uri() ) );
define( 'THIM_VERSION', '1.18' );

if ( ! function_exists( 'thim_setup' ) ) :

	/**
	 * 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 thim_setup() {

		/*
		  * Make theme available for translation.
		  * Translations can be filed in the /languages/ directory.
		  * If you're building a theme based on thim, use a find and replace
		  * to change 'cakeart' to the name of your theme in all the template files
		  */
		load_theme_textdomain( 'cakeart', THIM_DIR . '/languages' );

		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );

		/*
		  * Enable support for Post Thumbnails on posts and pages.
		  *
		  * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
		  */
		add_theme_support( 'post-thumbnails' );

		/*
		* Declare WooCommerce support
		*
		*/
		add_theme_support( 'woocommerce' );
		add_theme_support( 'wc-product-gallery-lightbox' );

		// This theme uses wp_nav_menu() in tưo location.
		register_nav_menus( array(
			'primary' => esc_html__( 'Primary Menu', 'cakeart' ),
		) );

		/*
		  * Switch default core markup for search form, comment form, and comments
		  * to output valid HTML5.
		  */
		add_theme_support( 'html5', array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
		) );

		/*
		  * Enable support for Post Formats.
		  * See http://codex.wordpress.org/Post_Formats
		  */
		add_theme_support( 'post-formats', array(
			'aside',
			'image',
			'video',
			'quote',
			'link',
			'gallery',
			'audio'
		) );

		add_theme_support( "title-tag" );
		// Set up the WordPress core custom background feature.
		add_theme_support( 'custom-background', apply_filters( 'thim_custom_background_args', array(
			'default-color' => 'ffffff',
			'default-image' => '',
		) ) );
		add_editor_style();
	}

endif; // thim_setup
add_action( 'after_setup_theme', 'thim_setup' );

/**
 * Register widget area.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_sidebar
 */

function thim_widgets_init() {

	include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

	if ( function_exists( 'wpfp_widget_control' ) ) {
		global $wp_registered_widget_controls;
		if ( isset( $wp_registered_widget_controls['wpfp-most_favorited_posts'] ) && ! isset( $wp_registered_widget_controls['wpfp-most_favorited_posts']['id_base'] ) && function_exists( 'wpfp_widget_control' ) ) {
			$wp_registered_widget_controls['wpfp-most_favorited_posts']['id_base'] = 'wpfp';
		}
	}

	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar Blog', 'cakeart' ),
		'id'            => 'sidebar-blog',
		'description'   => esc_html__( 'Sidebar Blog', 'cakeart' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Toolbar Left', 'cakeart' ),
		'id'            => 'top_left_menu',
		'description'   => esc_html__( 'Toolbar Left', 'cakeart' ),
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget'  => '</li>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
	register_sidebar( array(
		'name'          => esc_html__( 'Toolbar Right', 'cakeart' ),
		'id'            => 'top_right_menu',
		'description'   => esc_html__( 'Toolbar Right', 'cakeart' ),
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget'  => '</li>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Menu Right', 'cakeart' ),
		'id'            => 'menu-right',
		'description'   => esc_html__( 'Menu Right', 'cakeart' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	if ( is_plugin_active( 'thim-recipe/recipe.php' ) ) {
		register_sidebar( array(
			'name'          => esc_html__( 'Sidebar Recipes', 'cakeart' ),
			'id'            => 'sidebar-recipes',
			'description'   => esc_html__( 'Sidebar Recipes', 'cakeart' ),
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
		) );
	}

	if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
		register_sidebar( array(
			'name'          => esc_html__( 'Sidebar Shop', 'cakeart' ),
			'id'            => 'sidebar-shop',
			'description'   => esc_html__( 'Sidebar Shop', 'cakeart' ),
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
		) );
	}

	register_sidebar( array(
		'name'          => esc_html__( 'Footer Top 1', 'cakeart' ),
		'id'            => 'sidebar-bottom-left',
		'description'   => esc_html__( 'Footer Top 1', 'cakeart' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
	register_sidebar( array(
		'name'          => esc_html__( 'Footer Top 2', 'cakeart' ),
		'id'            => 'sidebar-bottom-right',
		'description'   => esc_html__( 'Footer Top 2', 'cakeart' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Footer 1', 'cakeart' ),
		'id'            => 'footer-bottom-1',
		'description'   => esc_html__( 'Footer 1', 'cakeart' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Footer 2', 'cakeart' ),
		'id'            => 'footer-bottom-2',
		'description'   => esc_html__( 'Footer 2', 'cakeart' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
	register_sidebar( array(
		'name'          => esc_html__( 'Footer 3', 'cakeart' ),
		'id'            => 'footer-bottom-3',
		'description'   => esc_html__( 'Footer 3', 'cakeart' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
	register_sidebar( array(
		'name'          => esc_html__( 'Footer 4', 'cakeart' ),
		'id'            => 'footer-bottom-4',
		'description'   => esc_html__( 'Footer 4', 'cakeart' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
}

add_action( 'widgets_init', 'thim_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function thim_scripts() {

	global $theme_options_data;

	wp_enqueue_style( 'thim-style', get_stylesheet_uri(), array(), THIM_VERSION );

	if ( is_file( CAKEART_UPLOADS_FOLDER . CAKEART_FILE_NAME ) ) {
		wp_enqueue_style( 'cakeart-options', CAKEART_UPLOADS_URL . CAKEART_FILE_NAME, array(), filemtime( CAKEART_UPLOADS_FOLDER . CAKEART_FILE_NAME ) );
	}else{
		wp_enqueue_style( 'Roboto-Slab-googlefonts','https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@300;400;500;600&display=swap', array(), '' );
		wp_enqueue_style( 'Roboto-googlefonts','https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,500;1,500&display=swap', array(), '' );
		wp_enqueue_style( 'cakeart-options', THIM_URI . 'default.css', array(), THIM_VERSION );
 	}

	wp_enqueue_script( 'thim-main', THIM_URI . 'js/main.min.js', array(
		'jquery',
		'framework-bootstrap'
	), '', true );


	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}

	wp_enqueue_script( 'jquery-cookie' );
	wp_enqueue_script( 'prettyPhoto' );
	wp_enqueue_script( 'prettyPhoto-init' );
	wp_enqueue_style( 'woocommerce_prettyPhoto_css' );
	wp_enqueue_script( 'thim-custom-script', THIM_URI . 'js/custom-script.js', array( 'jquery' ), '', true );
	wp_enqueue_script( 'thim-slider-carosel', THIM_URI . 'js/slider-carosel.js', array(
		'jquery',
		'wpb_composer_front_js'
	), '', true );

	if ( is_rtl() || ( isset( $theme_options_data['thim_enable_rtl'] ) && $theme_options_data['thim_enable_rtl'] == '1' ) ) {
		wp_enqueue_style( 'thim-css-rtl', THIM_URI . 'rtl.css', array() );
	}

}

add_action( 'wp_enqueue_scripts', 'thim_scripts' );

function thim_add_custom_attachment_script() {

	wp_register_script( 'custom-attachment-script', THIM_URI . 'js/custom-attachment.js' );
	wp_enqueue_script( 'custom-attachment-script' );

} // end add_custom_attachment_script
add_action( 'admin_enqueue_scripts', 'thim_add_custom_attachment_script' );

// Require library
require THIM_DIR . 'inc/libs/class-tgm-plugin-activation.php';
require THIM_DIR . 'inc/libs/theme-wrapper.php';
require THIM_DIR . 'inc/libs/tax-meta-class/Tax-meta-class.php';
require THIM_DIR . 'inc/libs/export-front-page.php';

// Require plugins
if ( is_admin() && current_user_can( 'manage_options' ) ) {
	require THIM_DIR . 'inc/admin/plugins-require.php';
}

// require
require THIM_DIR . 'inc/custom-functions.php';

/**
 * Custom template tags for this theme.
 */
require THIM_DIR . 'inc/template-tags.php';

/**
 * Custom functions that act independently of the theme templates.
 */
require THIM_DIR . 'inc/extras.php';

// dislay setting layout
require THIM_DIR . 'inc/wrapper-before-after.php';

// Includes widgets
require THIM_DIR . 'inc/widgets/widgets.php';

require THIM_DIR . 'inc/header/logo.php';

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

if ( is_plugin_active( 'thim-framework/tp-framework.php' ) ) {

	require THIM_DIR . 'inc/admin/customize-options.php';

	require THIM_DIR . 'inc/tax-meta.php';
}

/**
 * Implement the Custom Header feature.
 */

if ( class_exists( 'WooCommerce' ) ) {
	require THIM_DIR . '/woocommerce/woocommerce.php';
}
require THIM_DIR . 'inc/admin/demo-data-oneclick.php';
