<?php
/**
 * Theme Functions.
 */

/* Theme Setup */
if (!function_exists('blogger_base_setup')):

function blogger_base_setup() {

	$GLOBALS['content_width'] = apply_filters('blogger_base_content_width', 640);

	add_theme_support('automatic-feed-links');
	add_theme_support('post-thumbnails');
	add_theme_support('title-tag');
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'html5', array( 'comment-list', 'search-form', 'comment-form', ) );
	add_theme_support( 'align-wide' );
	add_theme_support( 'wp-block-styles' );
	add_theme_support('custom-logo', array(
		'height'      => 240,
		'width'       => 240,
		'flex-height' => true,
	));

	add_theme_support('custom-background', array(
		'default-color' => 'f1f1f1',
	));
	/*
	 * This theme styles the visual editor to resemble the theme style,
	 * specifically font, colors, icons, and column width.
	 */
	add_editor_style(array('css/editor-style.css', blogger_hub_font_url()));
	// activation notice
	global $pagenow;
if (
		is_admin()
		&&
		('themes.php' == $pagenow)
		// &&
		// isset( $_GET['activated'] )
	) {
		add_action('admin_notices', 'blogger_base_activation_notice');
	}
}
endif;
add_action('after_setup_theme', 'blogger_base_setup');

function blogger_base_activation_notice() {
	$blogger_base_meta = get_option( 'blogger_base_admin_notice' );

	if (!$blogger_base_meta) {
	echo '<div id="blogger-base-welcome-notice" class="notice notice-success is-dismissible activation-notice">';
		echo '<div class="content">';
			echo '<h2>' . esc_html__( 'Thank You for Choosing Blogger Base by Themesglance!', 'blogger-base' ) . '</h2>';
			echo '<p class="notice-para">' . esc_html__( 'We’re thrilled to have you on board. To help you get started quickly and make the most out of your theme, check out the resources below:', 'blogger-base' ) . '</p>';
			echo '<p>';
			echo '<a href="' . esc_url( admin_url( 'themes.php?page=blogger_base_guide' ) ) . '" class="button">' . esc_html__( 'Get Started', 'blogger-base' ) . '</a>';
				echo '<a href="' . esc_url( 'https://preview.themesglance.com/blogger-hub-pro/' ) . '" class="button" target="_blank">' . esc_html__( 'Demo', 'blogger-base' ) . '</a>';
				echo '<a href="' . esc_url( 'https://www.themesglance.com/products/premium-blog-wordpress-theme' ) . '" class="button" target="_blank">' . esc_html__( 'Get Premium', 'blogger-base' ) . '</a>';
				echo '<a href="' . esc_url( 'https://preview.themesglance.com/demo/docs/free-blogger-hub/' ) . '" class="button" target="_blank">' . esc_html__( 'Documentation', 'blogger-base' ) . '</a>';
			echo '</p>';
		echo '</div>';
		echo '<div class="image-preview">';
		echo '<img src="' . esc_url(get_theme_file_uri()) . '/images/notice-img.png">';
		echo '</div>';
	echo '</div>';
}
}

// Admin notice code START
function blogger_base_dismissed_notice() {
	update_option( 'blogger_base_admin_notice', true );
}
add_action( 'wp_ajax_blogger_base_dismissed_notice', 'blogger_base_dismissed_notice' );


//After Switch theme function
add_action('after_switch_theme', 'blogger_base_getstart_setup_options');
function blogger_base_getstart_setup_options () {
    update_option('blogger_base_admin_notice', false );
}

add_action( 'wp_enqueue_scripts', 'blogger_base_enqueue_styles' );
function blogger_base_enqueue_styles() {
	$parent_style = 'blogger-hub-style'; // Style handle of parent theme.
	wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
	wp_enqueue_style( 'blogger-base-style', get_stylesheet_uri(), array( $parent_style ) );
	if (is_singular() && comments_open() && get_option('thread_comments')) {
		wp_enqueue_script('comment-reply');
	}
	wp_enqueue_style( 'blogger-base-block-patterns-style-frontend', get_theme_file_uri('/block-patterns/css/block-frontend.css') );

	require get_theme_file_path( '/inc/color-option.php' );
	wp_add_inline_style( 'blogger-base-style',$blogger_base_custom_css);
}

// Enqueue Customizer JS
function blogger_base_enqueue_customizer_js() {
	wp_enqueue_script('blogger_base-customizer-control', get_stylesheet_directory_uri() . '/js/customize-controls.js', array( 'jquery', 'customize-controls' ), false, true
	);
}
add_action( 'customize_controls_enqueue_scripts', 'blogger_base_enqueue_customizer_js' );

function blogger_base_enqueue_customizer_css() {
	wp_enqueue_style( 'blogger_base-customizer-css', get_stylesheet_directory_uri() . '/block-patterns/css/customize-controls.css' );
	wp_enqueue_style( 'blogger_base-customizer-main-css', get_stylesheet_directory_uri() . '/css/customize-controls.css' );
	}
add_action( 'customize_controls_print_styles', 'blogger_base_enqueue_customizer_css' );


function blogger_base_customize_register() {     
	global $wp_customize;  
	$wp_customize->remove_control( 'blogger_hub_metafields_date' );
	$wp_customize->remove_setting( 'blogger_hub_metafields_date' ); 
	$wp_customize->remove_control( 'blogger_hub_metafields_author' );
	$wp_customize->remove_setting( 'blogger_hub_metafields_author' );
	$wp_customize->remove_control( 'blogger_hub_metafields_comment' );
	$wp_customize->remove_setting( 'blogger_hub_metafields_comment' );
	$wp_customize->remove_control( 'blogger_hub_metafields_time' );
	$wp_customize->remove_setting( 'blogger_hub_metafields_time' );
	$wp_customize->remove_control( 'blogger_hub_postdate_icon' );
	$wp_customize->remove_setting( 'blogger_hub_postdate_icon' );
	$wp_customize->remove_control( 'blogger_hub_postauthor_icon' );
	$wp_customize->remove_setting( 'blogger_hub_postauthor_icon' );
	$wp_customize->remove_control( 'blogger_hub_postcomment_icon' );
	$wp_customize->remove_setting( 'blogger_hub_postcomment_icon' );
	$wp_customize->remove_control( 'blogger_hub_posttime_icon' );
	$wp_customize->remove_setting( 'blogger_hub_posttime_icon' );
	$wp_customize->remove_control( 'blogger_hub_first_theme_color' );
	$wp_customize->remove_setting( 'blogger_hub_first_theme_color' ); 
	$wp_customize->remove_control( 'blogger_hub_second_theme_color' );
	$wp_customize->remove_setting( 'blogger_hub_second_theme_color' ); 
	$wp_customize->remove_setting( 'blogger_hub_reset_color' );
	$wp_customize->remove_control( 'blogger_hub_reset_color' );
	$wp_customize->remove_setting( 'blogger_hub_reset_demo_import_settings' );
	$wp_customize->remove_control( 'blogger_hub_reset_demo_import_settings' );

	//Reset Global Color
	$wp_customize->add_setting('blogger_base_reset_color', array(
		'default'           => '',
		'sanitize_callback' => 'sanitize_text_field',
	));
	
	$wp_customize->add_control('blogger_base_reset_color', array(
		'type'    => 'button',
		'label'   => __('Reset Global Color', 'blogger-base'),
		'section' => 'blogger_hub_reset_section',
		'input_attrs' => array(
			'onclick' => 'ResetChildGlobalColor()',
		),
	));
} 

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

function blogger_base_remove_parent_function() {
	remove_action( 'admin_notices', 'blogger_hub_activation_notice' );
	remove_action( 'admin_menu', 'blogger_hub_gettingstarted' );
}
add_action( 'init', 'blogger_base_remove_parent_function');

add_action( 'after_setup_theme', 'blogger_hub_override' );
function blogger_hub_override() {
    unregister_sidebar('home-page');   
    register_sidebar(array(
        'name' => 'Home Page Sidebar',
        'description'   => __('Appears on home page', 'blogger-base'),
		'id'            => 'home-page',
		'before_widget' => '<aside id="%1$s" class="widget %2$s mb-4">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title py-3 mb-3 text-center">',
		'after_title'   => '</h3>',
    )); 
}

/* Excerpt Limit Begin */
function blogger_base_string_limit_words($string, $word_limit) {
	$words = explode(' ', $string, ($word_limit + 1));
	if(count($words) > $word_limit)
	array_pop($words);
	return implode(' ', $words);
}
function blogger_base_mysetup() {
	define('BLOGGER_BASE_THEMESGLANCE_PRO_THEME_URL',__('https://www.themesglance.com/products/premium-blog-wordpress-theme', 'blogger-base'));
	define('BLOGGER_BASE_THEMESGLANCE_THEME_DOC',__('https://preview.themesglance.com/demo/docs/blogger-hub-pro/', 'blogger-base'));
	define('BLOGGER_BASE_THEMESGLANCE_LIVE_DEMO',__('https://preview.themesglance.com/blogger-hub-pro/', 'blogger-base'));
	define('BLOGGER_BASE_THEMESGLANCE_FREE_THEME_DOC',__('https://preview.themesglance.com/demo/docs/free-blogger-hub/', 'blogger-base'));
	define('BLOGGER_BASE_THEMESGLANCE_SUPPORT',__('https://wordpress.org/support/theme/blogger-base/', 'blogger-base'));
	define('BLOGGER_BASE_THEMESGLANCE_REVIEW',__('https://wordpress.org/support/theme/blogger-base/reviews/', 'blogger-base'));
	define('BLOGGER_BASE_SITE_URL',__('https://www.themesglance.com/products/free-blogger-wordpress-theme/','blogger-base'));
	define('BLOGGER_BASE_BUNDLE_URL',__('https://www.themesglance.com/products/wp-theme-bundle','blogger-base'));

	if ( ! defined( 'BLOGGER_HUB_PRO_NAME' ) ) {
		define( 'BLOGGER_HUB_PRO_NAME', __( 'Blogger Base Pro', 'blogger-base' ));
	}
	if ( ! defined( 'BLOGGER_HUB_DOC' ) ) {
		define( 'BLOGGER_HUB_DOC', __( 'Blogger Base Doc', 'blogger-base' ));
	}
	if ( ! defined( 'BLOGGER_HUB_DEMO' ) ) {
		define( 'BLOGGER_HUB_DEMO', __( 'Blogger Base Demo', 'blogger-base' ));
	}
	/* Block Pattern */
	require get_theme_file_path('/block-patterns/block-patterns.php');
}
add_action( 'after_setup_theme', 'blogger_base_mysetup' );

/**
* Enqueue block editor style
*/
function blogger_base_block_editor_styles() {
	wp_enqueue_style( 'blogger_base-font', blogger_hub_font_url(), array() );
    wp_enqueue_style( 'blogger_base-block-patterns-style-editor', get_theme_file_uri( '/block-patterns/css/block-editor.css' ), false, '1.0', 'all' );
    wp_enqueue_style( 'font-awesome-css', get_template_directory_uri().'/css/fontawesome-all.css' );
}
add_action( 'enqueue_block_editor_assets', 'blogger_base_block_editor_styles' );

function blogger_base_customizer ( $wp_customize ) {
	
	$wp_customize->add_setting( 'blogger_base_first_theme_color', array(
	    'default' => '',
	    'sanitize_callback' => 'sanitize_hex_color'
  	));
  	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'blogger_base_first_theme_color', array(
  		'label' => 'Color Option',
	    'description' => __('One can change complete theme global color settings on just one click.', 'blogger-base'),
	    'section' => 'blogger_hub_theme_color_option',
	    'settings' => 'blogger_base_first_theme_color',
  	)));
}

add_action( 'customize_register', 'blogger_base_customizer' );

/* Implement the get started page */
require get_theme_file_path() . '/inc/getting-started/getting-started.php';

