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

// enque files
if ( ! function_exists( 'green_farm_enqueue_styles' ) ) :
	/**
	 * Load assets.
	 *
	 * @since 1.0.0
	 */
	function green_farm_enqueue_styles() {
		wp_enqueue_style( 'organic-farm-style-parent', get_template_directory_uri() . '/style.css' );
		wp_enqueue_style( 'green-farm-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( 'green-farm-style',$organic_farm_custom_style );
		require get_theme_file_path( 'inc/extra_customization.php' );
		wp_add_inline_style( 'green-farm-style',$organic_farm_custom_style );
		// blocks css
        wp_enqueue_style( 'green-farm-block-style', get_theme_file_uri( '/assets/css/blocks.css' ), array( 'green-farm-style' ), '1.0' );
	}
endif;
add_action( 'wp_enqueue_scripts', 'green_farm_enqueue_styles', 99 );

//theme setup
function green_farm_setup() {
	load_theme_textdomain( 'green-farm', 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( 'green-farm-featured-image', 2000, 1200, true );
	add_image_size( 'green-farm-thumbnail-avatar', 100, 100, true );

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

	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/green-farm/','green-farm'));
	}
	if ( ! defined( 'ORGANIC_FARM_REVIEW' ) ) {
		define('ORGANIC_FARM_REVIEW',__('https://wordpress.org/support/theme/green-farm/reviews/#new-post','green-farm'));
	}
	if ( ! defined( 'ORGANIC_FARM_FREE_DOC' ) ) {
	define('ORGANIC_FARM_FREE_DOC',__('https://trial.ovationthemes.com/docs/ot-green-farm-free-doc/','green-farm'));
	}
}
add_action( 'after_setup_theme', 'green_farm_setup' );

// custom-header setup
function green_farm_custom_header_setup() {
    add_theme_support( 'custom-header', apply_filters( 'green_farm_custom_header_args', array(
        'default-image'          => get_parent_theme_file_uri( '/assets/images/header-img-2.png' ),
        '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', 'green_farm_custom_header_setup');

// widget
function green_farm_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Sidebar', 'green-farm' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Add widgets here to appear in your sidebar on blog posts and archive pages.', 'green-farm' ),
		'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', 'green-farm' ),
		'id'            => 'sidebar-2',
		'description'   => __( 'Add widgets here to appear in your pages and posts', 'green-farm' ),
		'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', 'green-farm' ),
		'id'            => 'sidebar-3',
		'description'   => __( 'Add widgets here to appear in your sidebar on blog posts and archive pages.', 'green-farm' ),
		'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', 'green-farm' ),
		'id'            => 'footer-1',
		'description'   => __( 'Add widgets here to appear in your footer.', 'green-farm' ),
		'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', 'green-farm' ),
		'id'            => 'footer-2',
		'description'   => __( 'Add widgets here to appear in your footer.', 'green-farm' ),
		'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', 'green-farm' ),
		'id'            => 'footer-3',
		'description'   => __( 'Add widgets here to appear in your footer.', 'green-farm' ),
		'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', 'green-farm' ),
		'id'            => 'footer-4',
		'description'   => __( 'Add widgets here to appear in your footer.', 'green-farm' ),
		'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'          => __( 'Product Category Dropdown', 'green-farm' ),
		'id'            => 'product-cat',
		'description'   => __( 'Add widgets here to appear in your header.', 'green-farm' ),
		'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', 'green_farm_widgets_init' );
// remove sections
function green_farm_customize_register() {
  	global $wp_customize;

  $wp_customize->remove_section( 'organic_farm_pro' );

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

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

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

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

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

	$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_footer_bg' );
	$wp_customize->remove_control( 'organic_farm_footer_bg' );
	$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', 'green_farm_customize_register', 11 );

// customizer
function green_farm_customize( $wp_customize ) {

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

	// pro section
	$wp_customize->add_section('green_farm_pro', array(
		'title'    => __('🔒 Unlock Premium Features', 'green-farm'),
		'priority' => 1,
	));
	$wp_customize->add_setting('green_farm_pro', array(
		'default'           => null,
		'sanitize_callback' => 'sanitize_text_field',
	));
	$wp_customize->add_control(new Green_Farm_Pro_Control($wp_customize, 'green_farm_pro', array(
		'label'    => __('GREEN FARM PREMIUM', 'green-farm'),
		'section'  => 'green_farm_pro',
		'settings' => 'green_farm_pro',
		'priority' => 1,
	)));

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

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

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

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

	$wp_customize->add_setting('green_farm_slider_content_alignment',array(
        'default' => 'CENTER-ALIGN',
        'sanitize_callback' => 'organic_farm_sanitize_choices'
	));
	$wp_customize->add_control('green_farm_slider_content_alignment',array(
		'type' => 'radio',
		'label'     => __('Slider Content Alignment', 'green-farm'),
		'section' => 'organic_farm_slider_section',
		'type' => 'select',
		'choices' => array(
			'LEFT-ALIGN' => __('LEFT','green-farm'),
            'CENTER-ALIGN' => __('CENTER','green-farm'),
            'RIGHT-ALIGN' => __('RIGHT','green-farm'),
		),
		'priority'    => 6,
	) );

	// header section
	$wp_customize->add_setting('green_farm_header_top_text',array(
		'default' => '',
		'sanitize_callback' => 'sanitize_text_field'
	));
	$wp_customize->add_control('green_farm_header_top_text',array(
		'label' => esc_html__('Add Header Text','green-farm'),
		'section' => 'organic_farm_top',
		'setting' => 'green_farm_header_top_text',
		'type'    => 'text'
	));
	$wp_customize->selective_refresh->add_partial( 'green_farm_header_top_text', array(
		'selector' => '.box-center',
		'render_callback' => 'organic_farm_customize_partial_green_farm_header_top_text',
	) );

	//footer
	$wp_customize->add_setting('green_farm_footer_text',array(
		'default'	=> 'Green Farm WordPress Theme',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('green_farm_footer_text',array(
		'label'	=> esc_html__('Copyright Text','green-farm'),
		'section'	=> 'organic_farm_footer_copyright',
		'type'		=> 'textarea'
	));
	$wp_customize->selective_refresh->add_partial( 'green_farm_footer_text', array(
		'selector' => '.site-info a',
		'render_callback' => 'organic_farm_customize_partial_green_farm_footer_text',
	) );

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

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'green_farm_primary_color', array(
	    'section' => 'colors',
	    'label' => esc_html__('Theme Color', 'green-farm'),
	 
	)));

	$wp_customize->add_setting('green_farm_top_bg_color', array(
	    'default' => '#14361f',
	    'sanitize_callback' => 'sanitize_hex_color',
	    'transport' => 'refresh',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'green_farm_top_bg_color', array(
	    'section' => 'colors',
	    'label' => esc_html__('Top Bg Color', 'green-farm'),
	 
	)));

	$wp_customize->add_setting('green_farm_heading_color', array(
	    'default' => '#14361f',
	    'sanitize_callback' => 'sanitize_hex_color',
	    'transport' => 'refresh',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'green_farm_heading_color', array(
	    'section' => 'colors',
	    'label' => esc_html__('Theme Heading Color', 'green-farm'),
	 
	)));

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

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'green_farm_footer_bg', array(
	    'section' => 'colors',
	    'label' => esc_html__('Footer Bg color', 'green-farm'),
	)));
}
add_action( 'customize_register', 'green_farm_customize' );

// comments
function green_farm_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', 'green_farm_enqueue_comments_reply' );

// Footer Text
function green_farm_copyright_link() {
    $green_farm_footer_text = get_theme_mod('green_farm_footer_text', esc_html__('Green Farm WordPress Theme', 'green-farm'));
    $green_farm_credit_link = esc_url('https://www.ovationthemes.com/products/free-green-farm-wordpress-theme');

    echo '<a href="' . $green_farm_credit_link . '" target="_blank">' . esc_html($green_farm_footer_text) . '<span class="footer-copyright">' . esc_html__(' By Ovation Themes', 'green-farm') . '</span></a>';
}

/* Pro control */
if (class_exists('WP_Customize_Control') && !class_exists('Green_Farm_Pro_Control')):
    class Green_Farm_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;
