<?php
/**
 * Green Nursery functions and definitions
 * @package Green Nursery
 */

if ( ! function_exists( 'green_nursery_after_theme_support' ) ) :

	function green_nursery_after_theme_support() {
		
		add_theme_support( 'automatic-feed-links' );

		add_theme_support(
			'custom-background',
			array(
				'default-color' => 'ffffff',
			)
		);

		$GLOBALS['content_width'] = apply_filters( 'green_nursery_content_width', 1140 );
		
		add_theme_support( 'post-thumbnails' );

		add_theme_support(
			'custom-logo',
			array(
				'height'      => 270,
				'width'       => 90,
				'flex-height' => true,
				'flex-width'  => true,
			)
		);
		
		add_theme_support( 'title-tag' );

        load_theme_textdomain( 'green-nursery', get_template_directory() . '/languages' );

		add_theme_support(
			'html5',
			array(
				'search-form',
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
				'script',
				'style',
			)
		);

		add_theme_support( 'post-formats', array(
			'video',  
			'audio',  
			'gallery',
			'quote',  
			'image',  
			'link',   
			'status', 
			'aside',  
			'chat',   
		) );
		
		add_theme_support( 'align-wide' );
		add_theme_support( 'responsive-embeds' );
        add_theme_support( 'woocommerce' );
		add_theme_support( 'wp-block-styles' );

		require get_template_directory() . '/inc/metabox.php';
		require get_template_directory() . '/inc/homepage-setup/homepage-setup-settings.php';

		if (! defined( 'GREEN_NURSERY_BUY_NOW' ) ){
			define('GREEN_NURSERY_BUY_NOW',__('https://www.omegathemes.com/products/nursery-wordpress-theme','green-nursery'));
		}
		if (! defined( 'GREEN_NURSERY_SUPPORT_FREE' ) ){
			define('GREEN_NURSERY_SUPPORT_FREE',__('https://wordpress.org/support/theme/green-nursery/','green-nursery'));
		}
		if (! defined( 'GREEN_NURSERY_DEMO_PRO' ) ){
			define('GREEN_NURSERY_DEMO_PRO',__('https://layout.omegathemes.com/green-nursery/','green-nursery'));
		}
		if (! defined( 'GREEN_NURSERY_LITE_DOCS_PRO' ) ){
			define('GREEN_NURSERY_LITE_DOCS_PRO',__('https://layout.omegathemes.com/steps/green-nursery/','green-nursery'));
		}
		if (! defined('GREEN_NURSERY_BUNDLE_BUTTON') ){
			define('GREEN_NURSERY_BUNDLE_BUTTON',__('https://www.omegathemes.com/products/wp-theme-bundle','green-nursery'));
		}
	}

endif;

add_action( 'after_setup_theme', 'green_nursery_after_theme_support' );

/**
 * Register and Enqueue Styles.
 */
function green_nursery_register_styles() {

	wp_enqueue_style( 'dashicons' );

    $green_nursery_theme_version = wp_get_theme()->get( 'Version' );
	$green_nursery_fonts_url = green_nursery_fonts_url();
    if( $green_nursery_fonts_url ){
    	require get_theme_file_path( 'lib/custom/css/wptt-webfont-loader.php' );
        wp_enqueue_style(
			'green-nursery-google-fonts',
			wptt_get_webfont_url( $green_nursery_fonts_url ),
			array(),
			$green_nursery_theme_version
		);
    }
	wp_enqueue_style( 'green-nursery-style', get_stylesheet_uri(), array(), $green_nursery_theme_version );

	wp_enqueue_style( 'green-nursery-style', get_stylesheet_uri() );
	require get_parent_theme_file_path( '/custom_css.php' );
	wp_add_inline_style( 'green-nursery-style',$green_nursery_custom_css );
	wp_enqueue_style(
		'fontawesome-css',
		get_template_directory_uri() . '/assets/css/fontawesome-all.css',
		array(),'4.5.0'
	);

	$green_nursery_css = '';

	if ( get_header_image() ) :

		$green_nursery_css .=  '
			.header-image-box{
				background-image: url('.esc_url(get_header_image()).') !important;
				-webkit-background-size: cover !important;
				-moz-background-size: cover !important;
				-o-background-size: cover !important;
				background-size: cover !important;
				height: 450px;
				display: flex;
				align-items: center;
			}';

	endif;

	wp_add_inline_style( 'green-nursery-style', $green_nursery_css );

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

	wp_enqueue_script( 'imagesloaded' );
    wp_enqueue_script( 'masonry' );
	wp_enqueue_script( 'green-nursery-custom', get_template_directory_uri() . '/lib/custom/js/theme-custom-script.js', array('jquery'), '', 1);
	wp_localize_script(
        'green-nursery-custom',
        'ajax_obj',
        array(
            'ajaxurl' => admin_url('admin-ajax.php')
        )
    );

    // Global Query
    if( is_front_page() ){

    	$green_nursery_posts_per_page = absint( get_option('posts_per_page') );
        $green_nursery_c_paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
        $green_nursery_posts_args = array(
            'posts_per_page'        => $green_nursery_posts_per_page,
            'paged'                 => $green_nursery_c_paged,
        );
        $posts_qry = new WP_Query( $green_nursery_posts_args );
        $max = $posts_qry->max_num_pages;

    }else{
        global $wp_query;
        $max = $wp_query->max_num_pages;
        $green_nursery_c_paged = ( get_query_var( 'paged' ) > 1 ) ? get_query_var( 'paged' ) : 1;
    }

    $green_nursery_default = green_nursery_get_default_theme_options();
    $green_nursery_pagination_layout = get_theme_mod( 'green_nursery_pagination_layout',$green_nursery_default['green_nursery_pagination_layout'] );
}

add_action( 'wp_enqueue_scripts', 'green_nursery_register_styles',200 );

function green_nursery_admin_enqueue_scripts_callback() {
    if ( ! did_action( 'wp_enqueue_media' ) ) {
    wp_enqueue_media();
    }
    wp_enqueue_script('green-nursery-uploaderjs', get_stylesheet_directory_uri() . '/lib/custom/js/uploader.js', array(), "1.0", true);
}
add_action( 'admin_enqueue_scripts', 'green_nursery_admin_enqueue_scripts_callback' );

/**
 * Register navigation menus uses wp_nav_menu in five places.
 */
function green_nursery_menus() {

	$green_nursery_locations = array(
		'green-nursery-primary-menu'  => esc_html__( 'Primary Menu', 'green-nursery' ),
	);

	register_nav_menus( $green_nursery_locations );
}

add_action( 'init', 'green_nursery_menus' );

add_filter('loop_shop_columns', 'green_nursery_loop_columns');
if (!function_exists('green_nursery_loop_columns')) {
	function green_nursery_loop_columns() {
		$green_nursery_columns = get_theme_mod( 'green_nursery_per_columns', 3 );
		return $green_nursery_columns;
	}
}

add_filter( 'loop_shop_per_page', 'green_nursery_per_page', 20 );
function green_nursery_per_page( $green_nursery_cols ) {
  	$green_nursery_cols = get_theme_mod( 'green_nursery_product_per_page', 9 );
	return $green_nursery_cols;
}

function green_nursery_products_args( $green_nursery_args ) {

    $green_nursery_args['posts_per_page'] = get_theme_mod( 'green_nursery_custom_related_products_number', 6 );

    $green_nursery_args['columns'] = get_theme_mod( 'green_nursery_custom_related_products_number_per_row', 3 );

    return $green_nursery_args;
}

require get_template_directory() . '/inc/custom-header.php';
require get_template_directory() . '/classes/class-svg-icons.php';
require get_template_directory() . '/classes/class-walker-menu.php';
require get_template_directory() . '/inc/customizer/customizer.php';
require get_template_directory() . '/inc/custom-functions.php';
require get_template_directory() . '/inc/template-tags.php';
require get_template_directory() . '/classes/body-classes.php';
require get_template_directory() . '/inc/widgets/widgets.php';
require get_template_directory() . '/inc/pagination.php';
require get_template_directory() . '/lib/breadcrumbs/breadcrumbs.php';
require get_template_directory() . '/lib/custom/css/dynamic-style.php';
require get_template_directory() . '/inc/general.php';
require get_template_directory() . '/inc/homepage-setup/whizzie.php';

function green_nursery_remove_customize_register() {
    global $wp_customize;

    $wp_customize->remove_setting( 'display_header_text' );
    $wp_customize->remove_control( 'display_header_text' );

}

add_action( 'customize_register', 'green_nursery_remove_customize_register', 11 );

function green_nursery_radio_sanitize(  $green_nursery_input, $green_nursery_setting  ) {
	$green_nursery_input = sanitize_key( $green_nursery_input );
	$green_nursery_choices = $green_nursery_setting->manager->get_control( $green_nursery_setting->id )->choices;
	return ( array_key_exists( $green_nursery_input, $green_nursery_choices ) ? $green_nursery_input : $green_nursery_setting->default );
}

// NOTICE FUNCTION

function green_nursery_admin_notice() { 
    global $pagenow;
    $theme_args = wp_get_theme();
    $meta = get_option( 'green_nursery_admin_notice' );
    $name = $theme_args->get( 'Name' );
    $current_screen = get_current_screen();

    if ( ! $meta ) {
        if ( is_network_admin() ) {
            return;
        }

        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        if ( $current_screen->base != 'appearance_page_greennursery-wizard' ) {
            ?>
            <div class="notice notice-success notice-content">
                <h2><?php esc_html_e('Welcome! Thank you for choosing Green Nursery. Let’s Set Up Your Website!', 'green-nursery') ?> </h2>
                <p><?php esc_html_e('Before you dive into customization, let’s go through a quick setup process to ensure everything runs smoothly. Click below to start setting up your website in minutes!', 'green-nursery') ?> </p>
                <div class="info-link">
                    <a href="<?php echo esc_url( admin_url( 'themes.php?page=greennursery-wizard' ) ); ?>"><?php esc_html_e('Get Started with Green Nursery', 'green-nursery'); ?></a>
					<a href="#" class="notice-bundle" id="bundleTrigger"><?php esc_html_e('Get All WP Themes', 'green-nursery'); ?></a>
					<div class="bundle-overlay" id="bundlePopup">
						<div class="bundle-modal">
							<span class="close-popup">&times;</span>
							<div class="sale-badge"><?php esc_html_e('🔥 MEGA SALE', 'green-nursery'); ?></div>
							<h2><?php esc_html_e('Unlock All Premium WP Themes', 'green-nursery'); ?></h2>
							<p> <?php esc_html_e('Unlock the Full Potential of Your Website with Our 50+ WordPress Themes Bundle', 'green-nursery'); ?></p>
							<ul>
								<li><?php esc_html_e('✔ Expert Support', 'green-nursery'); ?></li>
								<li><?php esc_html_e('✔ Premium Theme ZIP File', 'green-nursery'); ?></li>
								<li><?php esc_html_e('✔ Ready-to-Use Website Templates', 'green-nursery'); ?></li>
								<li><?php esc_html_e('✔ Demo Content (Included in the file)', 'green-nursery'); ?></li>
								<li><?php esc_html_e('✔ Access to the Premium Support Forum', 'green-nursery'); ?></li>
							</ul>
							<a href="<?php echo esc_url( GREEN_NURSERY_BUNDLE_BUTTON ); ?>" target="_blank" class="bundle-cta"><?php esc_html_e('Get the Bundle Now →', 'green-nursery'); ?></a>
						</div>
					</div>
                </div>
                <p class="dismiss-link"><strong><a href="?green_nursery_admin_notice=1"><?php esc_html_e( 'Dismiss', 'green-nursery' ); ?></a></strong></p>
            </div>
            <?php
        }
    }
}
add_action( 'admin_notices', 'green_nursery_admin_notice' );

if ( ! function_exists( 'green_nursery_update_admin_notice' ) ) :
/**
 * Updating admin notice on dismiss
 */
function green_nursery_update_admin_notice() {
    if ( isset( $_GET['green_nursery_admin_notice'] ) && $_GET['green_nursery_admin_notice'] == '1' ) {
        update_option( 'green_nursery_admin_notice', true );
    }
}
endif;
add_action( 'admin_init', 'green_nursery_update_admin_notice' );

add_action( 'wp_ajax_get_product_rating', 'green_nursery_get_product_rating' );
add_action( 'wp_ajax_nopriv_get_product_rating', 'green_nursery_get_product_rating' );

function green_nursery_get_product_rating() {
    if ( ! isset($_POST['product_id']) ) {
        wp_send_json_error( [ 'message' => 'No product ID given' ] );
    }

    $product_id = intval( $_POST['product_id'] );
    $product    = wc_get_product( $product_id );

    if ( ! $product ) {
        wp_send_json_error( [ 'message' => 'Invalid product' ] );
    }

    $average      = $product->get_average_rating();
    $review_count = $product->get_review_count();

    wp_send_json_success( [
        'average'      => $average,
        'review_count' => $review_count,
    ] );
}

add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );

add_action( 'init', function() {
    update_option( 'elementor_onboard_wizard_completed', 'yes' );
});