<?php
/**
 * Theme functions and definitions
 *
 * @package organic_grocery
 */

// enque files
if ( ! function_exists( 'organic_grocery_enqueue_styles' ) ) :
	/**
	 * Load assets.
	 *
	 * @since 1.0.0
	 */
	function organic_grocery_enqueue_styles() {
		wp_enqueue_style( 'organic-farm-style-parent', get_template_directory_uri() . '/style.css' );
		wp_enqueue_style( 'organic-grocery-style', get_stylesheet_directory_uri() . '/style.css', array( 'organic-farm-style-parent' ), '1.0.0' );
		// Theme Customize CSS.
		require get_parent_theme_file_path( 'inc/extra_customization.php' );
		wp_add_inline_style( 'organic-grocery-style',$organic_farm_custom_style );

		require get_theme_file_path( 'inc/extra_customization.php' );
		wp_add_inline_style( 'organic-grocery-style',$organic_farm_custom_style );

		// blocks css
        wp_enqueue_style( 'organic-grocery-block-style', get_theme_file_uri( '/assets/css/blocks.css' ), array( 'organic-grocery-style' ), '1.0' );
	}
endif;
add_action( 'wp_enqueue_scripts', 'organic_grocery_enqueue_styles', 99 );

// theme setup
function organic_grocery_setup() {
	load_theme_textdomain( 'organic-grocery', get_template_directory() . '/languages' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( "responsive-embeds" );
	add_theme_support( "wp-block-styles" );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'title-tag' );
	add_theme_support('custom-background',array(
		'default-color' => 'ffffff',
	));
	add_image_size( 'organic-grocery-featured-image', 2000, 1200, true );
	add_image_size( 'organic-grocery-thumbnail-avatar', 100, 100, true );

	$GLOBALS['content_width'] = 525;
	register_nav_menus( array(
		'primary' => __( 'Primary Menu', 'organic-grocery' ),
	) );

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

	// Add theme support for Custom Logo.
	add_theme_support( 'custom-logo', array(
		'width'       => 250,
		'height'      => 250,
		'flex-width'  => true,
		'flex-height' => true,
	) );
	/*
	* This theme styles the visual editor to resemble the theme style,
	* specifically font, colors, and column width.
	*/
	add_editor_style( array( 'assets/css/editor-style.css', organic_farm_fonts_url() ) );

	if ( ! defined( 'ORGANIC_FARM_SUPPORT' ) ) {
	define('ORGANIC_FARM_SUPPORT',__('https://wordpress.org/support/theme/organic-grocery','organic-grocery'));
	}
	if ( ! defined( 'ORGANIC_FARM_REVIEW' ) ) {
		define('ORGANIC_FARM_REVIEW',__('https://wordpress.org/support/theme/organic-grocery/reviews/#new-post','organic-grocery'));
	}
	if ( ! defined( 'ORGANIC_FARM_LIVE_DEMO' ) ) {
	define('ORGANIC_FARM_LIVE_DEMO',__('https://trial.ovationthemes.com/organic-grocery/','organic-grocery'));
	}
	if ( ! defined( 'ORGANIC_FARM_BUY_PRO' ) ) {
	define('ORGANIC_FARM_BUY_PRO',__('https://www.ovationthemes.com/products/organic-grocery-wordpress-theme','organic-grocery'));
	}
	if ( ! defined( 'ORGANIC_FARM_PRO_DOC' ) ) {
	define('ORGANIC_FARM_PRO_DOC',__('https://trial.ovationthemes.com/docs/ot-organic-grocery-pro-doc/','organic-grocery'));
	}
	if ( ! defined( 'ORGANIC_FARM_FREE_DOC' ) ) {
	define('ORGANIC_FARM_FREE_DOC',__('https://trial.ovationthemes.com/docs/ot-organic-grocery-free-doc/','organic-grocery'));
	}
	if ( ! defined( 'ORGANIC_FARM_THEME_NAME' ) ) {
	define('ORGANIC_FARM_THEME_NAME',__('Premium Grocery Theme','organic-grocery'));
	}
}
add_action( 'after_setup_theme', 'organic_grocery_setup' );

// header setup
function organic_grocery_custom_header_setup() {
    add_theme_support( 'custom-header', apply_filters( 'organic_grocery_custom_header_args', array(
        'default-image'          => get_parent_theme_file_uri( '/assets/images/header-img-4.jpg' ),
        'default-text-color'     => 'fff',
        'header-text'            => false,
        'width'                  => 1200,
        'height'                 => 80,
        'flex-width'             => true,
        'flex-height'            => true,
        'wp-head-callback'       => 'organic_farm_header_style',
    ) ) );
}
add_action( 'after_setup_theme', 'organic_grocery_custom_header_setup' );

// widgets
function organic_grocery_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Sidebar', 'organic-grocery' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Add widgets here to appear in your sidebar on blog posts and archive pages.', 'organic-grocery' ),
		'before_widget' => '<section id="%1$s" class="widget wow zoomIn %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<div class="widget_container"><h3 class="widget-title">',
		'after_title'   => '</h3></div>',
	) );

	register_sidebar( array(
		'name'          => __( 'Page Sidebar', 'organic-grocery' ),
		'id'            => 'sidebar-2',
		'description'   => __( 'Add widgets here to appear in your pages and posts', 'organic-grocery' ),
		'before_widget' => '<section id="%1$s" class="widget wow zoomIn %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<div class="widget_container"><h3 class="widget-title">',
		'after_title'   => '</h3></div>',
	) );

	register_sidebar( array(
		'name'          => __( 'Sidebar 3', 'organic-grocery' ),
		'id'            => 'sidebar-3',
		'description'   => __( 'Add widgets here to appear in your sidebar on blog posts and archive pages.', 'organic-grocery' ),
		'before_widget' => '<section id="%1$s" class="widget wow zoomIn %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<div class="widget_container"><h3 class="widget-title">',
		'after_title'   => '</h3></div>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer 1', 'organic-grocery' ),
		'id'            => 'footer-1',
		'description'   => __( 'Add widgets here to appear in your footer.', 'organic-grocery' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer 2', 'organic-grocery' ),
		'id'            => 'footer-2',
		'description'   => __( 'Add widgets here to appear in your footer.', 'organic-grocery' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer 3', 'organic-grocery' ),
		'id'            => 'footer-3',
		'description'   => __( 'Add widgets here to appear in your footer.', 'organic-grocery' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer 4', 'organic-grocery' ),
		'id'            => 'footer-4',
		'description'   => __( 'Add widgets here to appear in your footer.', 'organic-grocery' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
}
add_action( 'widgets_init', 'organic_grocery_widgets_init' );

// remove sections
function organic_grocery_customize_register() {
  	global $wp_customize;
	$wp_customize->remove_section( 'organic_farm_pro' );
	$wp_customize->remove_section( 'organic_farm_product_box_section' );

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

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

	$wp_customize->remove_setting( 'organic_farm_primary_color' );
	$wp_customize->remove_control( 'organic_farm_primary_color' );
	$wp_customize->remove_setting( 'organic_farm_top_bg_color' );
	$wp_customize->remove_control( 'organic_farm_top_bg_color' );
	$wp_customize->remove_setting( 'organic_farm_secondary_color' );
	$wp_customize->remove_control( 'organic_farm_secondary_color' );
	$wp_customize->remove_setting( 'organic_farm_heading_color' );
	$wp_customize->remove_control( 'organic_farm_heading_color' );
	$wp_customize->remove_setting( 'organic_farm_primary_fade' );
	$wp_customize->remove_control( 'organic_farm_primary_fade' );
	$wp_customize->remove_setting( 'organic_farm_slider_overlay' );
	$wp_customize->remove_control( 'organic_farm_slider_overlay' );
	$wp_customize->remove_setting( 'organic_farm_slider_heading_color' );
	$wp_customize->remove_control( 'organic_farm_slider_heading_color' );
	$wp_customize->remove_setting( 'organic_farm_slider_excerpt_color' );
	$wp_customize->remove_control( 'organic_farm_slider_excerpt_color' );
}
add_action( 'customize_register', 'organic_grocery_customize_register', 11 );

// Footer Text
function organic_grocery_copyright_link() {
    $organic_grocery_footer_text = get_theme_mod('organic_grocery_footer_text', esc_html__('Organic Grocery WordPress Theme', 'organic-grocery'));
    $organic_grocery_credit_link = esc_url('https://www.ovationthemes.com/products/free-grocery-wordpress-theme');

    echo '<a href="' . $organic_grocery_credit_link . '" target="_blank">' . esc_html($organic_grocery_footer_text) . '<span class="footer-copyright">' . esc_html__(' By Ovation Themes', 'organic-grocery') . '</span></a>';
}

// customizer
function organic_grocery_customize( $wp_customize ) {

	wp_enqueue_style('customizercustom_css', esc_url( get_stylesheet_directory_uri() ). '/assets/css/customizer.css');

	require get_theme_file_path( 'inc/custom-control.php' );

	// pro section
	$wp_customize->add_section('organic_grocery_pro', array(
		'title'    => __('🔒 Unlock Premium Features', 'organic-grocery'),
		'priority' => 1,
	));
	$wp_customize->add_setting('organic_grocery_pro', array(
		'default'           => null,
		'sanitize_callback' => 'sanitize_text_field',
	));
	$wp_customize->add_control(new Organic_Grocery_Pro_Control($wp_customize, 'organic_grocery_pro', array(
		'label'    => __('GROCERY PREMIUM', 'organic-grocery'),
		'section'  => 'organic_grocery_pro',
		'settings' => 'organic_grocery_pro',
		'priority' => 1,
	)));

	//slider section
	$wp_customize->add_setting('organic_grocery_slider_heading_color', array(
	    'default' => '#ffffff',
	    'sanitize_callback' => 'sanitize_hex_color',
	    'transport' => 'refresh',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'organic_grocery_slider_heading_color', array(
	    'section' => 'organic_farm_slider_section',
	    'label' => esc_html__('Slider Title Color', 'organic-grocery'),
	 	'priority'    => 2,
	)));

	$wp_customize->add_setting(
		'organic_grocery_slider_excerpt_show_hide',
		array(
			'type'                 => 'option',
			'capability'           => 'edit_theme_options',
			'theme_supports'       => '',
			'default'              => '1',
			'transport'            => 'refresh',
			'sanitize_callback'    => 'organic_farm_callback_sanitize_switch',
		)
	);
	$wp_customize->add_control(
		new Organic_Grocery_Customizer_Customcontrol_Switch(
			$wp_customize,
			'organic_grocery_slider_excerpt_show_hide',
			array(
				'settings'        => 'organic_grocery_slider_excerpt_show_hide',
				'section'         => 'organic_farm_slider_section',
				'label'           => __( 'Show Hide excerpt', 'organic-grocery' ),				
				'choices'		  => array(
					'1'      => __( 'On', 'organic-grocery' ),
					'off'    => __( 'Off', 'organic-grocery' ),
				),
				'priority'    => 3,
			)
		)
	);
	$wp_customize->add_setting('organic_grocery_slider_excerpt_color', array(
	    'default' => '#ffffff',
	    'sanitize_callback' => 'sanitize_hex_color',
	    'transport' => 'refresh',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'organic_grocery_slider_excerpt_color', array(
	    'section' => 'organic_farm_slider_section',
	    'label' => esc_html__('Slider Excerpt Color', 'organic-grocery'),
	 	'priority'    => 4,
	)));

	$wp_customize->add_setting('organic_grocery_slider_overlay', array(
	    'default' => '#1a2225',
	    'sanitize_callback' => 'sanitize_hex_color',
	    'transport' => 'refresh',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'organic_grocery_slider_overlay', array(
	    'section' => 'organic_farm_slider_section',
	    'label' => esc_html__('Slider Overlay Color', 'organic-farm'),
	 	'priority'    => 7,
	)));

	//Category Section
	$wp_customize->add_section('organic_grocery_category_section',array(
		'title'	=> __('Category Section','organic-grocery'),
		'priority'=> 5,
		'panel' => 'organic_farm_custompage_panel',
	));
	$wp_customize->add_setting( 'organic_grocery_section_category_heading', array(
			'default'           => '',
			'transport'         => 'refresh',
			'sanitize_callback' => 'sanitize_text_field',
	) );
	$wp_customize->add_control( new Organic_Grocery_Customizer_Customcontrol_Section_Heading( $wp_customize, 'organic_grocery_section_category_heading', array(
		'label'       => esc_html__( 'Category Section', 'organic-grocery' ),
		'section'     => 'organic_grocery_category_section',
		'settings'    => 'organic_grocery_section_category_heading',
	) ) );
	$wp_customize->add_setting(
		'organic_grocery_grocery_cate_show_hide',
		array(
			'type'                 => 'option',
			'capability'           => 'edit_theme_options',
			'theme_supports'       => '',
			'default'              => '1',
			'transport'            => 'refresh',
			'sanitize_callback'    => 'organic_farm_callback_sanitize_switch',
		)
	);
	$wp_customize->add_control(
		new Organic_Grocery_Customizer_Customcontrol_Switch(
			$wp_customize,
			'organic_grocery_grocery_cate_show_hide',
			array(
				'settings'        => 'organic_grocery_grocery_cate_show_hide',
				'section'         => 'organic_grocery_category_section',
				'label'           => __( 'Check To Show Section', 'organic-grocery' ),
				'choices'		  => array(
					'1'      => __( 'On', 'organic-grocery' ),
					'off'    => __( 'Off', 'organic-grocery' ),
				),
				'active_callback' => '',
			)
		)
	);
    $wp_customize->add_setting('organic_grocery_grocery_cate_title',array(
		'default' => '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('organic_grocery_grocery_cate_title',array(
		'label'	=> __('Section Title','organic-grocery'),
		'section' => 'organic_grocery_category_section',
		'type' => 'text',
	));
	$wp_customize->selective_refresh->add_partial( 'organic_grocery_grocery_cate_title', array(
		'selector' => '#home-mission h3',
		'render_callback' => 'organic_farm_customize_partial_organic_grocery_grocery_cate_title',
	) );
	$wp_customize->add_setting('organic_grocery_category_number',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field',
	));
	$wp_customize->add_control('organic_grocery_category_number',array(
		'label'	=> __('Number of posts to show in a category','organic-grocery'),
		'section' => 'organic_grocery_category_section',
		'type'	  => 'number',
	));
	$categories = get_categories();
	$cats = array();
	$i = 0;
	$cat_pst[]= 'select';
	foreach($categories as $category){
		if($i==0){
			$default = $category->slug;
			$i++;
		}
		$cat_pst[$category->slug] = $category->name;
	}
	$wp_customize->add_setting('organic_grocery_category_setting',array(
		'default' => 'select',
		'sanitize_callback' => 'organic_farm_sanitize_choices',
	));
	$wp_customize->add_control('organic_grocery_category_setting',array(
		'type' => 'select',
		'choices' => $cat_pst,
		'label' => __('Select Category to display Post','organic-grocery'),
		'section' => 'organic_grocery_category_section',
	));
	$organic_grocery_category_number = get_theme_mod('organic_grocery_category_number','');
	for ($i=1; $i <= $organic_grocery_category_number; $i++) {
	    $wp_customize->add_setting('organic_grocery_category_icon'.$i,array(
			'default' => 'fas fa-plus',
			'sanitize_callback'	=> 'sanitize_text_field'
		));
		$wp_customize->add_control('organic_grocery_category_icon'.$i,array(
			'label'	=> __('Category Icon ','organic-grocery').$i,
			'input_attrs' => array(
            	'placeholder' => __( '','organic-grocery' ),
        	),
			'section' => 'organic_grocery_category_section',
			'type' => 'text',
		));
	}

	$wp_customize->add_setting('organic_grocery_footer_text',array(
		'default'	=> 'Organic Grocery WordPress Theme',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('organic_grocery_footer_text',array(
		'label'	=> esc_html__('Copyright Text','organic-grocery'),
		'section'	=> 'organic_farm_footer_copyright',
		'type'		=> 'textarea'
	));
	$wp_customize->selective_refresh->add_partial( 'organic_grocery_footer_text', array(
		'selector' => '.site-info a',
		'render_callback' => 'organic_farm_customize_partial_organic_grocery_footer_text',
	) );


	//colors
	$wp_customize->add_setting('organic_grocery_primary_color', array(
	    'default' => '#d30c1f',
	    'sanitize_callback' => 'sanitize_hex_color',
	    'transport' => 'refresh',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'organic_grocery_primary_color', array(
	    'section' => 'colors',
	    'label' => esc_html__('Theme Color', 'organic-grocery'),
	 
	)));

	$wp_customize->add_setting('organic_grocery_heading_color', array(
	    'default' => '#1a2225',
	    'sanitize_callback' => 'sanitize_hex_color',
	    'transport' => 'refresh',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'organic_grocery_heading_color', array(
	    'section' => 'colors',
	    'label' => esc_html__('Theme Heading Color', 'organic-grocery'),
	 
	)));

	$wp_customize->add_setting('organic_grocery_primary_fade', array(
	    'default' => '#fbe2e5',
	    'sanitize_callback' => 'sanitize_hex_color',
	    'transport' => 'refresh',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'organic_grocery_primary_fade', array(
	    'section' => 'colors',
	    'label' => esc_html__('theme Color fade', 'organic-grocery'),
	 
	)));
}
add_action( 'customize_register', 'organic_grocery_customize' );

// comments
function organic_grocery_enqueue_comments_reply() {
  if( is_singular() && comments_open() && ( get_option( 'thread_comments' ) == 1) ) {
    // Load comment-reply.js (into footer)
    wp_enqueue_script( 'comment-reply', '/wp-includes/js/comment-reply.min.js', array(), false, true );
  }
}
add_action(  'wp_enqueue_scripts', 'organic_grocery_enqueue_comments_reply' );

/* Pro control */
if (class_exists('WP_Customize_Control') && !class_exists('Organic_Grocery_Pro_Control')):
    class Organic_Grocery_Pro_Control extends WP_Customize_Control{

    public function render_content(){?>
        <div style="background: linear-gradient(135deg, #2B136B 0%, #A47AE2 100%); padding: 20px; border-radius: 8px; text-align: center; color: #fff;">
			
			<h3 style="margin-top: 0; color: #fff;">
				<?php esc_html_e('Unlock Advanced Features', 'organic-farm'); ?>
			</h3>
	
			<p style="margin: 15px 0;">
				<?php esc_html_e('Upgrade to Pro to get:', 'organic-farm'); ?>
			</p>
	
			<ul style="list-style: none; padding: 0; text-align: left; max-width: 300px; margin: 20px auto;">
				<li>✓ <?php esc_html_e('12+ Premium Header Layouts', 'organic-farm'); ?></li>
				<li>✓ <?php esc_html_e('Advanced Footer Builder', 'organic-farm'); ?></li>
				<li>✓ <?php esc_html_e('Typography Controls', 'organic-farm'); ?></li>
				<li>✓ <?php esc_html_e('WooCommerce Styling Options', 'organic-farm'); ?></li>
				<li>✓ <?php esc_html_e('Priority Support', 'organic-farm'); ?></li>
				<li>✓ <?php esc_html_e('One-Click Demo Import', 'organic-farm'); ?></li>
			</ul>
	
			<a href="<?php echo esc_url(admin_url('themes.php?page=organic-farm-pro')); ?>"
				style="display:inline-block;background:#fff;color:#667eea;padding:12px 30px;text-decoration:none;border-radius:4px;font-weight:600;margin:10px 5px;">
				<?php esc_html_e('View All Features', 'organic-farm'); ?>
			</a>
	
			<a href="<?php echo esc_url( ORGANIC_FARM_BUY_PRO ); ?>" target="_blank"
				style="display:inline-block;background:#ffc107;color:#333;padding:12px 30px;text-decoration:none;border-radius:4px;font-weight:600;margin:10px 5px;">
				<?php esc_html_e('Upgrade Now 🚀', 'organic-farm'); ?>
			</a>

			<a href="<?php echo esc_url( ORGANIC_FARM_BUNDLE_LINK ); ?>" target="_blank"
				style="display: inline-block; background: #28a745; color: #fff; padding: 12px 30px; text-decoration: none; border-radius: 4px; font-weight: 600; margin: 10px 5px;">
				<?php esc_html_e('WordPress Bundle 🎁', 'organic-farm'); ?>
			</a>
	
		</div>
    <?php } }
endif;