<?php
if ( ! function_exists( 'expert_fitness_trainer_setup' ) ) :
function expert_fitness_trainer_setup() {

// Root path/URI.
define( 'EXPERT_FITNESS_TRAINER_PARENT_DIR', get_template_directory() );
define( 'EXPERT_FITNESS_TRAINER_PARENT_URI', get_template_directory_uri() );

// Root path/URI.
define( 'EXPERT_FITNESS_TRAINER_PARENT_INC_DIR', EXPERT_FITNESS_TRAINER_PARENT_DIR . '/inc');
define( 'EXPERT_FITNESS_TRAINER_PARENT_INC_URI', EXPERT_FITNESS_TRAINER_PARENT_URI . '/inc');

	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'wc-product-gallery-zoom' );
	add_theme_support( 'wc-product-gallery-slider' );

	/*
	 * Let WordPress manage the document title.
	 */
	add_theme_support( 'title-tag' );

	add_theme_support( 'responsive-embeds' );
	
	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 */
	add_theme_support( 'post-thumbnails' );
	
	//Add selective refresh for sidebar widget
	add_theme_support( 'customize-selective-refresh-widgets' );
	
	/*
	 * Make theme available for translation.
	 */

	load_theme_textdomain( 'expert-fitness-trainer', get_stylesheet_directory() . '/languages' );
		
	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary'  => esc_html__( 'Primary Menu', 'expert-fitness-trainer' ),
	) );

	/*
	 * 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',
	) );
		
	add_theme_support('custom-logo');

	/*
	 * WooCommerce Plugin Support
	 */
	add_theme_support( 'woocommerce' );
	
	// Gutenberg wide images.
	add_theme_support( 'align-wide' );
	
	
	//Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'expert_fitness_trainer_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );
	
	/*
	 * This theme styles the visual editor to resemble the theme style,
	 * specifically font, colors, icons, and column width.
	 */
	add_editor_style( array( 'assets/css/editor-style.css', expert_fitness_trainer_google_font_url() ) );

    add_theme_support( 'custom-header', apply_filters( 'expert_fitness_trainer_custom_header_args', array(
		'default-image'          => get_template_directory_uri() . '/assets/images/custom-header.png',
		'default-text-color'     => 'ffffff',
		'width'                  => 2000, 
		'height'                 => 200,
		'flex-width'    		 => true,
		'flex-height'    		 => true,
        'uploads'            => true,
	)));


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


    //  --------------------------------------------- ENQUEUE ----------------------------------------------------- //

    /**
     * Implement the Custom Header feature.
     */
    require_once get_template_directory() . '/inc/custom-header.php';

    /**
     * Demo Import
     */
    require get_parent_theme_file_path( '/demo-import/demo-import-settings.php' );

    	if ( ! defined( 'EXPERT_FITNESS_TRAINER_FREE_THEME_URL' ) ) {
		define( 'EXPERT_FITNESS_TRAINER_FREE_THEME_URL', 'https://www.seothemesexpert.com/products/free-fitness-wordpress-theme' );
	}
	if ( ! defined( 'EXPERT_FITNESS_TRAINER_PRO_THEME_URL' ) ) {
		define( 'EXPERT_FITNESS_TRAINER_PRO_THEME_URL', 'https://www.seothemesexpert.com/products/gym-trainer-website-template' );
	}
	if ( ! defined( 'EXPERT_FITNESS_TRAINER_FREE_DOCS_THEME_URL' ) ) {
		define( 'EXPERT_FITNESS_TRAINER_FREE_DOCS_THEME_URL', 'https://demo.seothemesexpert.com/documentation/expert-fitness-trainer/' );
	}
	if ( ! defined( 'EXPERT_FITNESS_TRAINER_DEMO_THEME_URL' ) ) {
		define( 'EXPERT_FITNESS_TRAINER_DEMO_THEME_URL', 'https://demo.seothemesexpert.com/expert-fitness-trainer/' );
	}
	if ( ! defined( 'EXPERT_FITNESS_TRAINER_RATE_THEME_URL' ) ) {
		define( 'EXPERT_FITNESS_TRAINER_RATE_THEME_URL', 'https://wordpress.org/support/theme/expert-fitness-trainer/reviews/#new-post' );
	}
	if ( ! defined( 'EXPERT_FITNESS_TRAINER_SUPPORT_THEME_URL' ) ) {
		define( 'EXPERT_FITNESS_TRAINER_SUPPORT_THEME_URL', 'https://wordpress.org/support/theme/expert-fitness-trainer/' );
	}
	if ( ! defined( 'EXPERT_FITNESS_TRAINER_THEME_BUNDLE_URL' ) ) {
		define( 'EXPERT_FITNESS_TRAINER_THEME_BUNDLE_URL', 'https://www.seothemesexpert.com/products/wordpress-theme-bundle' );
	}
    if ( ! defined( 'EXPERT_FITNESS_TRAINER_DEMO_IMPORT_URL' ) ) {
        define( 'EXPERT_FITNESS_TRAINER_DEMO_IMPORT_URL', esc_url( admin_url( 'themes.php?page=expertfitnesstrainer-wizard' ) ) );
    }
}
endif;
add_action( 'after_setup_theme', 'expert_fitness_trainer_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function expert_fitness_trainer_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'expert_fitness_trainer_content_width', 1170 );
}
add_action( 'after_setup_theme', 'expert_fitness_trainer_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */

function expert_fitness_trainer_widgets_init() {
	
	register_sidebar( array(
		'name' => __( 'Sidebar Widget Area', 'expert-fitness-trainer' ),
		'id' => 'expert-fitness-trainer-sidebar-primary',
		'description' => __( 'The Primary Widget Area', 'expert-fitness-trainer' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s wow fadeInUp">',
		'after_widget' => '</aside>',
		'before_title' => '<h4 class="widget-title">',
		'after_title' => '</h4><div class="title"><span class="shap"></span></div>',
	) );
	
	register_sidebar( array(
		'name' => __( 'Footer Widget Area', 'expert-fitness-trainer' ),
		'id' => 'expert-fitness-trainer-footer-widget-area',
		'description' => __( 'The Footer Widget Area', 'expert-fitness-trainer' ),
		'before_widget' => '<div class="footer-widget col-lg-3 col-sm-6 wow fadeIn" data-wow-delay="0.5s"><aside id="%1$s" class="widget %2$s">',
		'after_widget' => '</aside></div>',
		'before_title' => '<h5 class="widget-title w-title">',
		'after_title' => '</h5><span class="shap"></span>',
	) );
}
add_action( 'widgets_init', 'expert_fitness_trainer_widgets_init' );

/**
 * All Styles & Scripts.
 */

require_once get_template_directory() . '/inc/enqueue.php';

/**
 * Nav Walker fo Bootstrap Dropdown Menu.
 */

require_once get_template_directory() . '/inc/class-wp-bootstrap-navwalker.php';

/**
 * Custom template tags for this theme.
 */
require_once get_template_directory() . '/inc/template-tags.php';

/**
 * Custom functions that act independently of the theme templates.
 */
require_once get_template_directory() . '/inc/extras.php';

/**
 * Customizer additions.
 */
require_once get_template_directory() . '/inc/customizer.php';

require_once get_template_directory() . '/inc/fonts.php';

require_once get_template_directory() . '/wptt-webfont-loader.php';


function expert_fitness_trainer_custom_controls() {
	load_template( trailingslashit( get_template_directory() ) . '/inc/customizer/customizer-custom-controls.php' );
}
add_action( 'customize_register', 'expert_fitness_trainer_custom_controls' );

// Sanitize the input
function expert_fitness_trainer_sanitize_sidebar_position( $expert_fitness_trainer_input ) {
    $expert_fitness_trainer_valid = array( 'right', 'left' );

    if ( in_array( $expert_fitness_trainer_input, $expert_fitness_trainer_valid ) ) {
        return $expert_fitness_trainer_input;
    } else {
        return 'right';
    }
}

add_filter( 'nav_menu_link_attributes', 'expert_fitness_trainer_dropdown_data_attribute', 20, 3 );
/**
 * Use namespaced data attribute for Bootstrap's dropdown toggles.
 *
 * @param array    $atts HTML attributes applied to the item's `<a>` element.
 * @param WP_Post  $item The current menu item.
 * @param stdClass $args An object of wp_nav_menu() arguments.
 * @return array
 */
function expert_fitness_trainer_dropdown_data_attribute( $atts, $item, $args ) {
    if ( is_a( $args->walker, 'WP_Bootstrap_Navwalker' ) ) {
        if ( array_key_exists( 'data-toggle', $atts ) ) {
            unset( $atts['data-toggle'] );
            $atts['data-bs-toggle'] = 'dropdown';
        }
    }
    return $atts;
}

function expert_fitness_trainer_remove_theme_customizer_setting($wp_customize) {
    // Remove the setting
    $wp_customize->remove_setting('display_header_text');
    // Remove the control
    $wp_customize->remove_control('display_header_text');
}
add_action('customize_register', 'expert_fitness_trainer_remove_theme_customizer_setting', 20); 
// Use a priority greater than the one used for adding the setting


// Set the number of products per row to 3 on the shop page
add_filter('loop_shop_columns', 'expert_fitness_trainer_custom_shop_loop_columns');

if (!function_exists('expert_fitness_trainer_custom_shop_loop_columns')) {
    function expert_fitness_trainer_custom_shop_loop_columns() {
        // Retrieve the number of columns from theme customizer setting (default: 3)
        $expert_fitness_trainer_columns = get_theme_mod('expert_fitness_trainer_custom_shop_per_columns', 3);
        return $expert_fitness_trainer_columns;
    }
}

// Set the number of products per page on the shop page
add_filter('loop_shop_per_page', 'expert_fitness_trainer_custom_shop_per_page', 20);

if (!function_exists('expert_fitness_trainer_custom_shop_per_page')) {
    function expert_fitness_trainer_custom_shop_per_page($expert_fitness_trainer_products_per_page) {
        // Retrieve the number of products per page from theme customizer setting (default: 9)
        $expert_fitness_trainer_products_per_page = get_theme_mod('expert_fitness_trainer_custom_shop_product_per_page', 9);
        return $expert_fitness_trainer_products_per_page;
    }
}

function expert_fitness_trainer_sanitize_number_absint( $number, $setting ) {
    // Ensure $number is an absolute integer (whole number, zero or greater).
    $number = absint( $number );

    // If the input is an absolute integer, return it; otherwise, return the default
    return ( $number ? $number : $setting->default );
}

/**
 * Generate Google fonts URL.
 */
function expert_fitness_trainer_google_font_url() {
    $expert_fitness_trainer_font_family = array(
        'Fira Sans Condensed:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900',
        'Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900'
    );

    $expert_fitness_trainer_query_args = array(
        'family' => rawurlencode(implode('|', $expert_fitness_trainer_font_family)),
    );

    return add_query_arg($expert_fitness_trainer_query_args, 'https://fonts.googleapis.com/css');
}

/**
 * Enqueue theme styles and scripts.
 */
function expert_fitness_trainer_scripts_styles() {
    // Enqueue Google Fonts
    wp_enqueue_style('expert-fitness-trainer-google-fonts', expert_fitness_trainer_google_font_url(), array(), null);
    
    // Enqueue other styles and scripts as needed
    wp_enqueue_style('expert-fitness-trainer-main-style', get_stylesheet_uri(), array(), '1.0.0');
    // Add more enqueuing here if needed
}
add_action('wp_enqueue_scripts', 'expert_fitness_trainer_scripts_styles');

function expert_fitness_trainer_fonts_scripts() {
	$expert_fitness_trainer_headings_font = esc_html(get_theme_mod('expert_fitness_trainer_headings_text'));
	$expert_fitness_trainer_body_font = esc_html(get_theme_mod('expert_fitness_trainer_body_text'));

	if( $expert_fitness_trainer_headings_font ) {
		wp_enqueue_style( 'expert-fitness-trainer-headings-fonts', '//fonts.googleapis.com/css?family='. $expert_fitness_trainer_headings_font );
	} else {
		wp_enqueue_style( 'expert-fitness-trainer-source-sans', '//fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic');
	}
	if( $expert_fitness_trainer_body_font ) {
		wp_enqueue_style( 'expert-fitness-trainer-body-fonts', '//fonts.googleapis.com/css?family='. $expert_fitness_trainer_body_font );
	} else {
		wp_enqueue_style( 'expert-fitness-trainer-source-body', '//fonts.googleapis.com/css?family=Source+Sans+Pro:400,300,400italic,700,600');
	}
}
add_action( 'wp_enqueue_scripts', 'expert_fitness_trainer_fonts_scripts' );

/**
 * Enqueue theme color style.
 */
function expert_fitness_trainer_customize_css() {
    $expert_fitness_trainer_dynamic_color = get_theme_mod( 'expert_fitness_trainer_dynamic_color_one', '#EB1749' );
    $expert_fitness_trainer_custom_css = ":root { --color-primary1: {$expert_fitness_trainer_dynamic_color}; }";
    wp_add_inline_style( 'expert-fitness-trainer-style', $expert_fitness_trainer_custom_css );
}
add_action( 'wp_enqueue_scripts', 'expert_fitness_trainer_customize_css' );

/**
 * Enqueue theme copyright alignment style.
 */
function expert_fitness_trainer_copyright_alignment_option() {
    // Get the alignment setting from the theme customizer.
    $expert_fitness_trainer_copyright_alignment = get_theme_mod('expert_fitness_trainer_copyright_alignment', 'center');

    // Start building the CSS string for the alignment.
    $expert_fitness_trainer_copyright_alignment_css = '
        .copyright-text, .footer-copyright, .footer-copyright a, p.copyright-text {
            text-align: ' . esc_attr($expert_fitness_trainer_copyright_alignment) . ';
        }
    ';

    // Add the inline style to the theme's main stylesheet.
    wp_add_inline_style('expert-fitness-trainer-style', $expert_fitness_trainer_copyright_alignment_css);
}

add_action('wp_enqueue_scripts', 'expert_fitness_trainer_copyright_alignment_option');

// Helper function to get page ID by slug
function get_page_id_by_slug($expert_fitness_trainer_slug) {
    $expert_fitness_trainer_page = get_page_by_path($expert_fitness_trainer_slug); // Get the page by slug
    return $expert_fitness_trainer_page ? $expert_fitness_trainer_page->ID : 0;   // Return the page ID or 0 if not found
}

add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );

// Show Admin Notice
function expert_fitness_trainer_promo_notice() {
    if (get_option('expert_fitness_trainer_notice_dismissed')) {
        return;
    }

    if (isset($_GET['page']) && in_array($_GET['page'], ['expertfitnesstrainer-wizard'])) {
        return;
    }
    

    $expert_fitness_trainer_theme = wp_get_theme();
    $expert_fitness_trainer_theme_title = $expert_fitness_trainer_theme->get( 'Name' );
    $expert_fitness_trainer_theme_version = $expert_fitness_trainer_theme->get( 'Version' );

    ?>
    <div class="notice expert-fitness-trainer-notice is-dismissible">
        <div>
            <h3>
                <span><?php echo esc_html( '(Version: ' . $expert_fitness_trainer_theme_version . ')' ); ?></span><br>
                <?php esc_html_e( 'Thank you for choosing,', 'expert-fitness-trainer'); ?>
                <?php echo esc_html( $expert_fitness_trainer_theme_title ); ?> <?php esc_html_e( '!!!', 'expert-fitness-trainer'); ?>
            </h3>
            <p><?php esc_html_e( 'Welcome! You can now easily start building your website using our beautiful and user-friendly themes. No need to worry about setup — with our one-click demo importer, your site can look just like the demo in minutes. All the tools and information you need to get started are right here!', 'expert-fitness-trainer'); ?></p>
            <a class="button button-primary expert-fitness-trainer-buy-now" href="<?php echo esc_url( EXPERT_FITNESS_TRAINER_DEMO_IMPORT_URL ); ?>" target="_blank">
                <?php esc_html_e( 'One Click Demo Import', 'expert-fitness-trainer') ?>
            </a>
            <a class="button button-primary expert-fitness-trainer-bundle-button" target="_blank" href="<?php echo esc_url( EXPERT_FITNESS_TRAINER_THEME_BUNDLE_URL ); ?>">
                <?php echo esc_html__( 'Get All 60+ Themes @ $79', 'expert-fitness-trainer' ); ?>
            </a>
        </div>
        <div class="expert-fitness-trainer-image-wrap">
            <img src="<?php echo esc_url(get_stylesheet_directory_uri() . '/screenshot.png'); ?>">
        </div>
        <canvas id="expert-fitness-trainer-notice-confetti"></canvas>
    </div>
    <?php
}
add_action('admin_notices', 'expert_fitness_trainer_promo_notice');

add_action('wp_ajax_expert_fitness_trainer_dismiss_notice', 'expert_fitness_trainer_dismiss_notice');
function expert_fitness_trainer_dismiss_notice() {
    update_option('expert_fitness_trainer_notice_dismissed', true);
    wp_send_json_success();
}

add_action('after_switch_theme', 'expert_fitness_trainer_reset_notice_on_activation');
function expert_fitness_trainer_reset_notice_on_activation() {
    delete_option('expert_fitness_trainer_notice_dismissed');
}

function expert_fitness_trainer_enqueue_admin_assets() {
    wp_enqueue_script(
        'expert-fitness-trainer-confetti',
        get_template_directory_uri() . '/demo-import/assets/js/confetti/confetti.min.js',
        ['jquery'],
        null,
        true
    );

    wp_enqueue_script(
        'expert-fitness-trainer-notice-confetti-js',
        get_template_directory_uri() . '/demo-import/assets/js/plugin-update-notice.js',
        ['expert-fitness-trainer-confetti'],
        null,
        true
    );

    wp_enqueue_script(
        'expert-fitness-trainer-notice-dismiss',
        get_template_directory_uri() . '/demo-import/assets/js/notice-dismiss.js',
        ['jquery'],
        null,
        true
    );
}
add_action('admin_enqueue_scripts', 'expert_fitness_trainer_enqueue_admin_assets');