<?php
/**
 * Finix Investment Firm functions and definitions
 *
 * @package Finix Investment Firm
 * @subpackage finix_investment_firm
 */

function finix_investment_firm_setup() {

	load_theme_textdomain( 'finix-investment-firm', get_template_directory() . '/languages' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'woocommerce' );
	add_theme_support( 'title-tag' );
	add_theme_support( "responsive-embeds" );
	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'post-thumbnails' );
	add_image_size( 'finix-investment-firm-featured-image', 2000, 1200, true );
	add_image_size( 'finix-investment-firm-thumbnail-avatar', 100, 100, true );

	// Set the default content width.
	$GLOBALS['content_width'] = 525;

	// This theme uses wp_nav_menu() in two locations.
	register_nav_menus( array(
		'primary-menu'    => __( 'Primary Menu', 'finix-investment-firm' ),
	) );

	// Add theme support for Custom Logo.
	add_theme_support( 'custom-logo', array(
		'width'       => 250,
		'height'      => 250,
		'flex-width'  => true,
    	'flex-height' => true,
	) );

	// Add theme support for selective refresh for widgets.
	add_theme_support( 'customize-selective-refresh-widgets' );

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

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

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

	add_theme_support( 'custom-header', apply_filters( 'finix_investment_firm_custom_header_args', array(
        'default-text-color' => 'fff',
        'header-text'        => false,
        'width'              => 1600,
        'height'             => 400,
        'flex-width'         => true,
        'flex-height'        => true,
        'wp-head-callback'   => 'finix_investment_firm_header_style',
        'default-image'      => get_template_directory_uri() . '/assets/images/sliderimage.png',
    ) ) );

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

}
add_action( 'after_setup_theme', 'finix_investment_firm_setup' );

// Add function after setup:
function finix_investment_firm_conditional_editor_styles() {
	
	add_editor_style( array( 'assets/css/editor-style.css', finix_investment_firm_fonts_url() ) );
}
add_action( 'after_setup_theme', 'finix_investment_firm_conditional_editor_styles', 11 );

function finix_investment_firm_fonts_url() {

	$fonts = array(
		'Plus Jakarta Sans:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;0,800;1,200;1,300;1,400;1,500;1,600;1,700;1,800'
	);

	$query_args = array(
		'family' => rawurlencode( implode( '|', $fonts ) ),
	);

	$font_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css2' );

	// Convert Google fonts to local (GDPR fix)
	if ( function_exists( 'wptt_get_webfont_url' ) ) {
		$font_url = wptt_get_webfont_url( esc_url_raw( $font_url ) );
	}

	return esc_url_raw( $font_url );
}

/**
 * Register widget area.
 */
function finix_investment_firm_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Blog Sidebar', 'finix-investment-firm' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Add widgets here to appear in your sidebar on blog posts and archive pages.', 'finix-investment-firm' ),
		'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'          => __( 'Page Sidebar', 'finix-investment-firm' ),
		'id'            => 'sidebar-2',
		'description'   => __( 'Add widgets here to appear in your sidebar on pages.', 'finix-investment-firm' ),
		'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'          => __( 'Sidebar 3', 'finix-investment-firm' ),
		'id'            => 'sidebar-3',
		'description'   => __( 'Add widgets here to appear in your sidebar on blog posts and archive pages.', 'finix-investment-firm' ),
		'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 1', 'finix-investment-firm' ),
		'id'            => 'footer-1',
		'description'   => __( 'Add widgets here to appear in your footer.', 'finix-investment-firm' ),
		'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', 'finix-investment-firm' ),
		'id'            => 'footer-2',
		'description'   => __( 'Add widgets here to appear in your footer.', 'finix-investment-firm' ),
		'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', 'finix-investment-firm' ),
		'id'            => 'footer-3',
		'description'   => __( 'Add widgets here to appear in your footer.', 'finix-investment-firm' ),
		'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', 'finix-investment-firm' ),
		'id'            => 'footer-4',
		'description'   => __( 'Add widgets here to appear in your footer.', 'finix-investment-firm' ),
		'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', 'finix_investment_firm_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function finix_investment_firm_scripts() {
	// Add custom fonts, used in the main stylesheet.
	wp_enqueue_style( 'finix-investment-firm-fonts', finix_investment_firm_fonts_url(), array(), null );

	// Bootstrap
	wp_enqueue_style( 'bootstrap-css', get_theme_file_uri( '/assets/css/bootstrap.css' ) );

	// Theme stylesheet.
	wp_enqueue_style( 'finix-investment-firm-style', get_stylesheet_uri() );
	require get_parent_theme_file_path( '/tp-theme-color.php' );
	wp_add_inline_style( 'finix-investment-firm-style',$finix_investment_firm_tp_theme_css );
	wp_style_add_data('finix-investment-firm-style', 'rtl', 'replace');
	require get_parent_theme_file_path( '/tp-body-width-layout.php' );
	wp_add_inline_style( 'finix-investment-firm-style',$finix_investment_firm_tp_theme_css );
	wp_style_add_data('finix-investment-firm-style', 'rtl', 'replace');

	// Theme block stylesheet.
	wp_enqueue_style( 'finix-investment-firm-block-style', get_theme_file_uri( '/assets/css/blocks.css' ), array( 'finix-investment-firm-style' ), '1.0' );

	// Fontawesome
	wp_enqueue_style( 'fontawesome-css', get_theme_file_uri( '/assets/css/fontawesome-all.css' ) );
	
	wp_enqueue_script( 'wow-jquery', get_template_directory_uri() . '/assets/js/wow.js', array('jquery'),'' ,true );
	wp_enqueue_style( 'animate-style', get_template_directory_uri().'/assets/css/animate.css' );

	wp_enqueue_script( 'finix-investment-firm-custom-scripts', get_template_directory_uri() . '/assets/js/finix-investment-firm-custom.js', array('jquery'), true );


	wp_enqueue_script( 'bootstrap-js', get_theme_file_uri( '/assets/js/bootstrap.js' ), array( 'jquery' ), true );


	wp_enqueue_script( 'finix-investment-firm-focus-nav', get_template_directory_uri() . '/assets/js/focus-nav.js', array('jquery'), true);

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

	$finix_investment_firm_body_font_family = get_theme_mod('finix_investment_firm_body_font_family', '');

	$finix_investment_firm_heading_font_family = get_theme_mod('finix_investment_firm_heading_font_family', '');

	$finix_investment_firm_tp_theme_css = '
		body, p.simplep, .more-btn a{
		    font-family: '.esc_html($finix_investment_firm_body_font_family).';
		}
		h1,h2, h3, h4, h5, h6, .menubar,.logo h1, .logo p.site-title, p.simplep a, #slider p.slidertop-title, .more-btn a,.wc-block-checkout__actions_row .wc-block-components-checkout-place-order-button,.wc-block-cart__submit-container a,.woocommerce #respond input#submit, .woocommerce a.button, .woocommerce button.button, .woocommerce input.button,.woocommerce #respond input#submit.alt, .woocommerce a.button.alt, .woocommerce button.button.alt, .woocommerce input.button.alt, #theme-sidebar button[type="submit"],
#footer button[type="submit"]{
		    font-family: '.esc_html($finix_investment_firm_heading_font_family).';
		}
	';
	wp_add_inline_style('finix-investment-firm-style', $finix_investment_firm_tp_theme_css);
}
add_action( 'wp_enqueue_scripts', 'finix_investment_firm_scripts' );

/*radio button sanitization*/
function finix_investment_firm_sanitize_choices( $input, $setting ) {
    global $wp_customize;
    $control = $wp_customize->get_control( $setting->id );
    if ( array_key_exists( $input, $control->choices ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}

// Sanitize Sortable control.
function finix_investment_firm_sanitize_sortable( $val, $setting ) {
	if ( is_string( $val ) || is_numeric( $val ) ) {
		return array(
			esc_attr( $val ),
		);
	}
	$sanitized_value = array();
	foreach ( $val as $item ) {
		if ( isset( $setting->manager->get_control( $setting->id )->choices[ $item ] ) ) {
			$sanitized_value[] = esc_attr( $item );
		}
	}
	return $sanitized_value;
}
/* Excerpt Limit Begin */
function finix_investment_firm_excerpt_function($excerpt_count = 35) {
    $finix_investment_firm_excerpt = get_the_excerpt();

    $FINIX_INVESTMENT_FIRM_TEXT_excerpt = wp_strip_all_tags($finix_investment_firm_excerpt);

    $finix_investment_firm_excerpt_limit = esc_attr(get_theme_mod('finix_investment_firm_excerpt_count', $excerpt_count));

    $finix_investment_firm_theme_excerpt = implode(' ', array_slice(explode(' ', $FINIX_INVESTMENT_FIRM_TEXT_excerpt), 0, $finix_investment_firm_excerpt_limit));

    return $finix_investment_firm_theme_excerpt;
}

// Change number or products per row to 3
add_filter('loop_shop_columns', 'finix_investment_firm_loop_columns');
if (!function_exists('finix_investment_firm_loop_columns')) {
	function finix_investment_firm_loop_columns() {
		$columns = get_theme_mod( 'finix_investment_firm_per_columns', 3 );
		return $columns;
	}
}

// Category count 
function finix_investment_firm_display_post_category_count() {

	$categories = get_the_category();

	if ( ! empty( $categories ) ) {

		$count = count( $categories );

		echo esc_html(
			sprintf(
				_n(
					'%s category',
					'%s categories',
					$count,
					'finix-investment-firm'
				),
				number_format_i18n( $count )
			)
		);
	}
}

//post tag
function finix_investment_firm_custom_tags_filter($finix_investment_firm_tag_list) {
    // Replace the comma (,) with an empty string
    $finix_investment_firm_tag_list = str_replace(', ', '', $finix_investment_firm_tag_list);

    return $finix_investment_firm_tag_list;
}
add_filter('the_tags', 'finix_investment_firm_custom_tags_filter');

function finix_investment_firm_sanitize_dropdown_pages( $page_id, $setting ) {
  // Ensure $input is an absolute integer.
  $page_id = absint( $page_id );
  // If $page_id is an ID of a published page, return it; otherwise, return the default.
  return ( 'publish' == get_post_status( $page_id ) ? $page_id : $setting->default );
}

function finix_investment_firm_custom_output_tags() {
    $finix_investment_firm_tags = get_the_tags();

    if ($finix_investment_firm_tags) {
        $finix_investment_firm_tags_output = '<div class="post_tag">Tags: ';

        $finix_investment_firm_first_tag = reset($finix_investment_firm_tags);

        foreach ($finix_investment_firm_tags as $tag) {
            $finix_investment_firm_tags_output .= '<a href="' . esc_url(get_tag_link($tag)) . '" rel="tag" class="me-2">' . esc_html($tag->name) . '</a>';
            if ($tag !== $finix_investment_firm_first_tag) {
                $finix_investment_firm_tags_output .= ' ';
            }
        }

        $finix_investment_firm_tags_output .= '</div>';

        echo $finix_investment_firm_tags_output;
    }
}
//Change number of products that are displayed per page (shop page)
add_filter( 'loop_shop_per_page', 'finix_investment_firm_per_page', 20 );
function finix_investment_firm_per_page( $finix_investment_firm_cols ) {
  	$finix_investment_firm_cols = get_theme_mod( 'finix_investment_firm_product_per_page', 9 );
	return $finix_investment_firm_cols;
}

function finix_investment_firm_sanitize_number_range( $number, $setting ) {

	// Ensure input is an absolute integer.
	$number = absint( $number );

	// Get the input attributes associated with the setting.
	$atts = $setting->manager->get_control( $setting->id )->input_attrs;

	// Get minimum number in the range.
	$min = ( isset( $atts['min'] ) ? $atts['min'] : $number );

	// Get maximum number in the range.
	$max = ( isset( $atts['max'] ) ? $atts['max'] : $number );

	// Get step.
	$step = ( isset( $atts['step'] ) ? $atts['step'] : 1 );

	// If the number is within the valid range, return it; otherwise, return the default
	return ( $min <= $number && $number <= $max && is_int( $number / $step ) ? $number : $setting->default );
}

function finix_investment_firm_sanitize_checkbox( $input ) {
	// Boolean check
	return ( ( isset( $input ) && true == $input ) ? true : false );
}

function finix_investment_firm_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 );
}

/**
 * Use front-page.php when Front page displays is set to a static page.
 */
function finix_investment_firm_front_page_template( $template ) {
	return is_home() ? '' : $template;
}
add_filter( 'frontpage_template','finix_investment_firm_front_page_template' );

// logo
function finix_investment_firm_logo_width(){

	$finix_investment_firm_logo_width   = get_theme_mod( 'finix_investment_firm_logo_width', 80 );

	echo "<style type='text/css' media='all'>"; ?>
		img.custom-logo{
		    width: <?php echo absint( $finix_investment_firm_logo_width ); ?>px;
		    max-width: 100%;
		}
	<?php echo "</style>";
}

add_action( 'wp_head', 'finix_investment_firm_logo_width' );

function finix_investment_firm_theme_setup() {

	define('FINIX_INVESTMENT_FIRM_CREDIT',__('https://www.themespride.com/products/finix-investment-firm', 'finix-investment-firm') );
	if ( ! function_exists( 'finix_investment_firm_credit' ) ) {
		function finix_investment_firm_credit(){
			echo "<a href=".esc_url(FINIX_INVESTMENT_FIRM_CREDIT)." target='_blank'>".esc_html__(get_theme_mod('finix_investment_firm_footer_text',__('Finix Investment Firm WordPress Theme','finix-investment-firm')))."</a>";
		}
	}

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

	/**
	 * Additional features to allow styling of the templates.
	 */
	require get_parent_theme_file_path( '/inc/template-functions.php' );

	/**
	 * Customizer additions.
	 */
	require get_parent_theme_file_path( '/inc/customizer.php' );

	/**
	 * Load Theme Web File
	 */
	require get_parent_theme_file_path('/inc/wptt-webfont-loader.php' );
	/**
	 * Load Theme Web File
	 */
	require get_parent_theme_file_path( '/inc/controls/customize-control-toggle.php' );
	/**
	 * load sortable file
	 */
	require get_parent_theme_file_path( '/inc/controls/sortable-control.php' );

	/**
	 * TGM Recommendation
	 */
	require get_parent_theme_file_path( '/inc/TGM/tgm.php' );

	/**
	 * About Theme Page
	 */
	require get_parent_theme_file_path( '/inc/about-theme.php' );

}
add_action( 'after_setup_theme', 'finix_investment_firm_theme_setup' );


//Admin Enqueue for Admin
function finix_investment_firm_admin_enqueue_scripts(){
	wp_enqueue_style('finix-investment-firm-admin-style', get_template_directory_uri() . '/assets/css/admin.css');
	wp_register_script( 'finix-investment-firm-admin-script', get_template_directory_uri() . '/assets/js/finix-investment-firm-admin.js', array( 'jquery' ), '', true );

	wp_localize_script(
		'finix-investment-firm-admin-script',
		'finix_investment_firm',
		array(
			'admin_ajax'	=>	admin_url('admin-ajax.php'),
			'wpnonce'			=>	wp_create_nonce('finix_investment_firm_dismissed_notice_nonce')
		)
	);
	wp_enqueue_script('finix-investment-firm-admin-script');

    wp_localize_script( 'finix-investment-firm-admin-script', 'finix_investment_firm_ajax_object',
        array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )
    );
}
add_action( 'admin_enqueue_scripts', 'finix_investment_firm_admin_enqueue_scripts' );



// get started
add_action( 'wp_ajax_finix_investment_firm_dismissed_notice_handler', 'finix_investment_firm_ajax_notice_handler' );

function finix_investment_firm_ajax_notice_handler() {
	if (!wp_verify_nonce($_POST['wpnonce'], 'finix_investment_firm_dismissed_notice_nonce')) {
		exit;
	}
    if ( isset( $_POST['type'] ) ) {
        $type = sanitize_text_field( wp_unslash( $_POST['type'] ) );
        update_option( 'dismissed-' . $type, TRUE );
    }
}

function finix_investment_firm_activation_notice() { 

	if ( ! get_option('dismissed-get_started', FALSE ) ) { ?>

    <div class="finix-investment-firm-notice-wrapper updated notice notice-get-started-class is-dismissible" data-notice="get_started">
        <div class="finix-investment-firm-getting-started-notice clearfix">
        	<div class="row-top">
	            <div class="finix-investment-firm-theme-notice-content">
	                <h2 class="finix-investment-firm-notice-h2">
	                    <?php
	                printf(
	                /* translators: 1: welcome page link starting html tag, 2: welcome page link ending html tag. */
	                    esc_html__( 'Install the Demo Import Plugin now to instantly set up your site like the live preview.', 'finix-investment-firm' ), '<strong>'. wp_get_theme()->get('Name'). '</strong>' );
	                ?>
	                </h2>
	                <?php 
					$finix_investment_firm_theme = wp_get_theme();
					?>
					<a class="finix-investment-firm-btn-get-started button button-primary button-hero finix-investment-firm-button-padding" href="<?php echo esc_url( admin_url( 'themes.php?page=finix-investment-firm-about' ) ); ?>">
					    <?php 
					    echo sprintf(
					        esc_html__( 'Get Started with %s Theme', 'finix-investment-firm' ),
					        esc_html( $finix_investment_firm_theme->get( 'Name' ) )
					    ); 
					    ?>
					</a>
	            </div>
	            <div class="image-box">
			    	<img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/theme-notice.png' ); ?>" alt="<?php echo esc_attr__( 'Finix Investment Firm', 'finix-investment-firm' ); ?>" />
				</div>
	        </div>
        </div>
    </div>
<?php }

}
add_action( 'admin_notices', 'finix_investment_firm_activation_notice' );

add_action('after_switch_theme', 'finix_investment_firm_setup_options');
function finix_investment_firm_setup_options () {
    update_option('dismissed-get_started', FALSE );
}

// Get Started Detail Notice - Dismiss permanently
function finix_investment_firm_dismissed_get_started_detail_notice() {
    update_option( 'dismissed-get_started-detail', true );
    wp_send_json_success();
}
add_action( 'wp_ajax_finix_investment_firm_dismissed_get_started_detail_notice', 'finix_investment_firm_dismissed_get_started_detail_notice' );
add_action( 'wp_ajax_nopriv_finix_investment_firm_dismissed_get_started_detail_notice', 'finix_investment_firm_dismissed_get_started_detail_notice' );

// Reset on theme switch
add_action('after_switch_theme', 'finix_investment_firm_setup_settings');
function finix_investment_firm_setup_settings() {
    update_option('dismissed-get_started', false );
    update_option('dismissed-get_started-detail', false );
}

add_action( 'wp_ajax_finix_investment_firm_popup_done', 'finix_investment_firm_popup_done' );
function finix_investment_firm_popup_done() {
	$theme_slug = get_stylesheet();
	update_option( $theme_slug . '_demo_popup_shown', true );
	wp_die();
}