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

/* Theme Setup */
if ( ! function_exists( 'perfect_blogging_setup' ) ) :
	function perfect_blogging_setup() {
		
		$GLOBALS['content_width'] = apply_filters( 'perfect_blogging_content_width', 640 );

		add_theme_support( 'automatic-feed-links' );
		add_theme_support( 'post-thumbnails' );
		add_theme_support( 'align-wide' );
		add_theme_support( 'wp-block-styles' );
		add_theme_support( 'title-tag' );
		add_theme_support( 'custom-logo', array(
			'height'      => 250,
			'width'       => 250,
			'flex-width'  => true,
			'flex-height' => true,
		) );

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

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

		add_theme_support('responsive-embeds');

		add_editor_style( array( 'css/editor-style.css', perfect_blog_font_url() ) );

		// Theme Activation Notice
		global $pagenow;
		
		if (
		is_admin()
		&&
		('themes.php' == $pagenow)
		// &&
		// isset( $_GET['activated'] )
	) {
		add_action('admin_notices', 'perfect_blogging_activation_notice');
	}
}
endif;
add_action( 'after_setup_theme', 'perfect_blogging_setup' );

// Dashboard Theme Notification  
function perfect_blogging_activation_notice() {
		$perfect_blogging_meta = get_option( 'perfect_blogging_admin_notice' );

	if (!$perfect_blogging_meta) {
	echo '<div id="perfect-blogging-welcome-notice" class="notice is-dismissible perfect-blog-notice">';
		echo '<div class="perfect-blog-banner">';
			echo '<div class="perfect-blog-text">';
				echo '<h2>' . esc_html__( 'Thanks For Installing the "Perfect Blogging" Theme !', 'perfect-blogging' ) . '</h2>';
				echo '<p>' . esc_html__( 'We’re excited to help you get started with your new theme! Set up your website quickly and easily by importing our demo content and customizing it to suit your needs.', 'perfect-blogging' ) . '</p>';
				echo '<div class="perfect-blog-buttons">';
					echo '<a href="' . esc_url( admin_url( 'themes.php?page=perfect_blogging_guide' ) ) . '">' . esc_html__( 'Demo Import', 'perfect-blogging' ) . '</a>';
					echo '<a href="'. esc_url( 'https://preview.themescaliber.com/doc/free-perfect-blog/' ) .'" target=_blank>' . esc_html__( 'Documentation', 'perfect-blogging' ) . '</a>';
					echo '<a href="'. esc_url( 'https://www.themescaliber.com/products/premium-wordpress-blog-theme' ) .'" target=_blank>' . esc_html__( 'Get Premium', 'perfect-blogging' ) . '</a>';
					echo '<a class="bundle_btn" href="'. esc_url( 'https://www.themescaliber.com/products/wordpress-theme-bundle' ) .'" target=_blank>' . esc_html__( 'Bundle of 220+ Themes at $99', 'perfect-blogging' ) . '</a>';
				echo '</div>';
			echo '</div>';
			echo '<div class="perfect-blog-image">';
				echo '<img src="' . esc_url(get_theme_file_uri()) . '/images/demo-preview.png">';
			echo '</div>';
		echo '</div>';
    echo '</div>';
}
}



/* Theme Widgets Setup */
function perfect_blogging_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Blog Sidebar', 'perfect-blogging' ),
		'description'   => __( 'Appears on blog page sidebar', 'perfect-blogging' ),
		'id'            => 'sidebar-1',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Page Sidebar', 'perfect-blogging' ),
		'description'   => __( 'Appears on page sidebar', 'perfect-blogging' ),
		'id'            => 'sidebar-2',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Third Column Sidebar', 'perfect-blogging' ),
		'description'   => __( 'Appears on page sidebar', 'perfect-blogging' ),
		'id'            => 'sidebar-3',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Home Page Sidebar', 'perfect-blogging' ),
		'description'   => __( 'Appears on Home Page', 'perfect-blogging' ),
		'id'            => 'home-page',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	//Footer widget areas
	$perfect_blog_widget_areas = get_theme_mod('perfect_blog_footer_widget_areas', '4');
	for ($i=1; $i<=$perfect_blog_widget_areas; $i++) {
		register_sidebar( array(
			'name'          => __( 'Footer Nav ', 'perfect-blogging' ) . $i,
			'id'            => 'footer-' . $i,
			'description'   => '',
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
		) );
	}

	register_sidebar( array(
		'name'          => __( 'Shop Page Sidebar', 'perfect-blogging' ),
		'description'   => __( 'Appears on shop page', 'perfect-blogging' ),
		'id'            => 'woocommerce_sidebar',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Single Product Page Sidebar', 'perfect-blogging' ),
		'description'   => __( 'Appears on shop page', 'perfect-blogging' ),
		'id'            => 'woocommerce-single-sidebar',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
}

add_action( 'widgets_init', 'perfect_blogging_widgets_init' );

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

	$perfect_blog_custom_css = '';

	$perfect_blog_show_header = get_theme_mod( 'perfect_blogging_button_border', false);
	if($perfect_blog_show_header == true){
		$perfect_blog_custom_css .='.read-comp a,#slider .inner_carousel .more-btn a{';
			$perfect_blog_custom_css .='border:2px solid; margin:10px 0;';
		$perfect_blog_custom_css .='}';
	}

	$perfect_blog_button_padding_top_bottom = get_theme_mod('perfect_blog_button_padding_top_bottom');
	$perfect_blog_button_padding_left_right = get_theme_mod('perfect_blog_button_padding_left_right');
	$perfect_blog_custom_css .='.read-comp a,#slider .inner_carousel .more-btn a{';
		$perfect_blog_custom_css .='padding-top: '.esc_attr($perfect_blog_button_padding_top_bottom).'px; padding-bottom: '.esc_attr($perfect_blog_button_padding_top_bottom).'px; padding-left: '.esc_attr($perfect_blog_button_padding_left_right).'px; padding-right: '.esc_attr($perfect_blog_button_padding_left_right).'px; display:inline-block;';
	$perfect_blog_custom_css .='}';

	$perfect_blog_button_border_radius = get_theme_mod('perfect_blog_button_border_radius');
	$perfect_blog_custom_css .='.read-comp a,#slider .inner_carousel .more-btn a, #comments .form-submit input[type="submit"]{';
		$perfect_blog_custom_css .='border-radius: '.esc_attr($perfect_blog_button_border_radius).'px;';
	$perfect_blog_custom_css .='}';

	// site title and tagline font size option
	$perfect_blog_site_title_size_option = get_theme_mod('perfect_blog_site_title_size_option', 35);{
		$perfect_blog_custom_css .='#header .perfect-blogging-logo a{';
		$perfect_blog_custom_css .='font-size: '.esc_attr($perfect_blog_site_title_size_option).'px;';
		$perfect_blog_custom_css .='}';
	}

	$perfect_blog_site_tagline_size_option = get_theme_mod('perfect_blog_site_tagline_size_option', 13);{
		$perfect_blog_custom_css .='#header .perfect-blogging-logo p{';
		$perfect_blog_custom_css .='font-size: '.esc_attr($perfect_blog_site_tagline_size_option).'px;';
		$perfect_blog_custom_css .='}';
	}

	// button font size
	$perfect_blog_button_font_size = get_theme_mod('perfect_blog_button_font_size');
	$perfect_blog_custom_css .='.main-cat-box .overlay .read-comp a{';
		$perfect_blog_custom_css .='font-size: '.esc_attr($perfect_blog_button_font_size).'px;';
	$perfect_blog_custom_css .='}';

	// Button Text Transform
	$perfect_blog_theme_lay = get_theme_mod( 'perfect_blog_button_text_transform','Uppercase');
    if($perfect_blog_theme_lay == 'Uppercase'){
		$perfect_blog_custom_css .='.main-cat-box .overlay .read-comp a{';
			$perfect_blog_custom_css .='text-transform: uppercase;';
		$perfect_blog_custom_css .='}';
	}else if($perfect_blog_theme_lay == 'Lowercase'){
		$perfect_blog_custom_css .='.main-cat-box .overlay .read-comp a{';
			$perfect_blog_custom_css .='text-transform: lowercase;';
		$perfect_blog_custom_css .='}';
	}
	else if($perfect_blog_theme_lay == 'Capitalize'){
		$perfect_blog_custom_css .='.main-cat-box .overlay .read-comp a{';
			$perfect_blog_custom_css .='text-transform: capitalize;';
		$perfect_blog_custom_css .='}';
	}

	// Blog Post ALignment
	$perfect_blog_blog_post_alignment = get_theme_mod( 'perfect_blog_blog_post_alignment','Left');
    if($perfect_blog_blog_post_alignment == 'Left'){
		$perfect_blog_custom_css .='.main-cat-box{';
		$perfect_blog_custom_css .='text-align: left;';
		$perfect_blog_custom_css .='}';
	}else if($perfect_blog_blog_post_alignment == 'Center'){
		$perfect_blog_custom_css .='.main-cat-box{';
			$perfect_blog_custom_css .='text-align: center;';
		$perfect_blog_custom_css .='}';
	}else if($perfect_blog_blog_post_alignment == 'Right'){
		$perfect_blog_custom_css .='.main-cat-box{';
			$perfect_blog_custom_css .='text-align: right;';
		$perfect_blog_custom_css .='}';
	}

	wp_add_inline_style( 'perfect-blogging-basic-style',$perfect_blog_custom_css );
}

function perfect_blogging_customize_register() {     
		global $wp_customize; 
		$wp_customize->remove_section( 'perfect_blog_theme_color_option' );  //Modify this line as needed 

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

		$wp_customize->remove_setting( 'perfect_blog_slider_premium_info' );
		$wp_customize->remove_control( 'perfect_blog_slider_premium_info' );
		
	} 
add_action( 'customize_register', 'perfect_blogging_customize_register', 11 );

// Customizer Section
function perfect_blogging_customizer ( $wp_customize ) {

	$wp_customize->add_section( 'perfect_blogging_category_post_section' , array(
    	'title'      => __( 'Single Category Post', 'perfect-blogging' ),
		'priority'   => null,
		'panel' => 'perfect_blog_panel_id'
	) );

	$wp_customize->add_setting('perfect_blogging_category_sec_premium_info',array(
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('perfect_blogging_category_sec_premium_info',array(
		'type'=> 'hidden',
		'label'	=> __('Premium Features','perfect-blogging'),
		'description' => "<ul><li>". esc_html('Includes settings to set section title.','perfect-blogging') ."</li><li>". esc_html('Contains settings for the background colour.','perfect-blogging') ."</li><li>". esc_html('Contains options for background images.','perfect-blogging') ."</li><li>". esc_html('You can change the font family and colours of heading.','perfect-blogging') ."</li><li>". esc_html('And so on...','perfect-blogging') ."</li></ul><a target='_blank' href='". esc_url(PERFECT_BLOGGING_BUY_NOW) ." '>". esc_html('Upgrade to Pro','perfect-blogging') ."</a>",
		'section'=> 'perfect_blogging_category_post_section'
	));

	$categories = get_categories();
	$cats = array();
	$i = 0;
	$cat_post[]= 'select';
	foreach($categories as $category){
		if($i==0){
			$default = $category->slug;
			$i++;
		}
		$cat_post[$category->slug] = $category->name;
	}

	$wp_customize->add_setting('perfect_blogging_category_post',array(
		'default'	=> 'select',
		'sanitize_callback' => 'perfect_blogging_sanitize_choices',
	));
	$wp_customize->add_control('perfect_blogging_category_post',array(
		'type'    => 'select',
		'choices' => $cat_post,
		'label' => __('Select Category to display post','perfect-blogging'),
		'section' => 'perfect_blogging_category_post_section',
	));

    // Single Category Post
    $wp_customize->add_setting( 'perfect_blogging_category_button_text', array(
		'default'   => __('Read Complete Post','perfect-blogging' ),
		'sanitize_callback'	=> 'sanitize_text_field'
	) );
	$wp_customize->add_control( 'perfect_blogging_category_button_text', array(
		'label'    => __( 'Category Post Button text','perfect-blogging' ),
		'section'  => 'perfect_blogging_category_post_section',
		'type'     => 'text',
		'settings' => 'perfect_blogging_category_button_text'
	) );

	$wp_customize->add_setting('perfect_blogging_slider_premium_info',array(
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('perfect_blogging_slider_premium_info',array(
		'type'=> 'hidden',
		'label'	=> __('Premium Features','perfect-blogging'),
		'description' => "<ul><li>". esc_html('You can change how many slides there are.','perfect-blogging') ."</li><li>". esc_html('You can change the font family and the colours of headings and subheadings.','perfect-blogging') ."</li><li>". esc_html('And so on...','perfect-blogging') ."</li></ul><a target='_blank' href='". esc_url(PERFECT_BLOGGING_BUY_NOW) ." '>". esc_html('Upgrade to Pro','perfect-blogging') ."</a>",
		'section'=> 'perfect_blog_slidersettings'
	));
	
	//Show /Hide border
	$wp_customize->add_setting( 'perfect_blogging_button_border',array(
		'default' => '',
      	'sanitize_callback'	=> 'perfect_blog_sanitize_checkbox'
    ) );
    $wp_customize->add_control('perfect_blogging_button_border',array(
    	'type' => 'checkbox',
        'label' => __( 'Show / Hide Button Border','perfect-blogging' ),
        'section' => 'perfect_blog_theme_button'
    ));

}
add_action( 'customize_register', 'perfect_blogging_customizer' );

/**
	 * Enqueue block editor style
	 */
	function perfect_blogging_block_editor_styles() {
		wp_enqueue_style( 'perfect_blogging-font', perfect_blog_font_url(), array() );
	    wp_enqueue_style( 'perfect_blogging-block-patterns-style-editor', get_theme_file_uri( '/theme-block-pattern/css/block-pattern-editor.css' ), false, '1.0', 'all' );
	    wp_enqueue_style('font-awesome-style', get_template_directory_uri().'/css/fontawesome-all.css');
	}
	add_action( 'enqueue_block_editor_assets', 'perfect_blogging_block_editor_styles' );


/*radio button sanitization*/
function perfect_blogging_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;
    }
}

add_action( 'init', 'perfect_blogging_remove_action');
function perfect_blogging_remove_action() {
   remove_action( 'admin_menu','perfect_blog_gettingstarted' );
    remove_action( 'admin_notices','perfect_blog_activation_notice' );
}

/*------------------------------section-pro.php part----------------------------------------*/
require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );

class Perfect_Blogging_Customize_Section_Pro extends WP_Customize_Section {

	/**
	 * Outputs the Underscore.js template.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return void
	 */
	protected function render_template() { ?>

		<li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }} cannot-expand">
			<h3 class="accordion-section-title">
				{{ data.title }}

				<# if ( data.pro_text && data.pro_url ) { #>
					<a href="{{ data.pro_url }}" class="button button-secondary alignright" target="_blank">{{ data.pro_text }}</a>
				<# } #>
			</h3>
		</li>
	<?php }
}

/*---------------customizer.php part--------------------------*/
/**
 * Singleton class for handling the theme's customizer integration.
 *
 * @since  1.0.0
 * @access public
 */
final class Perfect_Blogging_Customize {

	/**
	 * Returns the instance.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return object
	 */
	public static function get_instance() {

		static $instance = null;

		if ( is_null( $instance ) ) {
			$instance = new self;
			$instance->setup_actions();
		}

		return $instance;
	}

	/**
	 * Constructor method.
	 *
	 * @since  1.0.0
	 * @access private
	 * @return void
	 */
	private function __construct() {}

	/**
	 * Sets up initial actions.
	 *
	 * @since  1.0.0
	 * @access private
	 * @return void
	 */
	private function setup_actions() {

		// Register panels, sections, settings, controls, and partials.
		add_action( 'customize_register', array( $this, 'sections' ) );

		// Register scripts and styles for the controls.
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ), 0 );
	}

	/**
	 * Sets up the customizer sections.
	 *
	 * @since  1.0.0
	 * @access public
	 * @param  object  $manager
	 * @return void
	 */
	public function sections( $manager ) {

		// Register custom section types.
		$manager->register_section_type( 'Perfect_Blogging_Customize_Section_Pro' );

	}

	/**
	 * Loads theme customizer CSS.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return void
	 */

	public function enqueue_control_scripts() {
		wp_enqueue_style( 'perfect_blogging-customize-controls', get_stylesheet_directory_uri() . '/css/customize-controls-child.css' );
	}
}

// Doing this customizer thang!
Perfect_Blogging_Customize::get_instance();

function perfect_blogging_links_setup() {
	define('PERFECT_BLOGGING_BUY_NOW',__('https://www.themescaliber.com/products/premium-wordpress-blog-theme', 'perfect-blogging'));
	define('PERFECT_BLOGGING_LIVE_DEMO',__('https://preview.themescaliber.com/perfect-blog/', 'perfect-blogging'));
	define('PERFECT_BLOGGING_CONTACT',__('https://wordpress.org/support/theme/perfect-blogging/', 'perfect-blogging'));
	define('PERFECT_BLOGGING_PRO_DOC',__('https://preview.themescaliber.com/doc/perfect-blog-pro/', 'perfect-blogging'));
	define('PERFECT_BLOGGING_FREE_THEME_DOC',__('https://preview.themescaliber.com/doc/free-perfect-blog/','perfect-blogging'));
	define('PERFECT_BLOGGING_SUPPORT',__('https://wordpress.org/support/theme/perfect-blogging/','perfect-blogging'));
	define('PERFECT_BLOGGING_REVIEW',__('https://wordpress.org/support/theme/perfect-blogging/reviews/','perfect-blogging'));
	/* Admin about theme */
	require get_theme_file_path('/inc/admin/admin.php');

	/* Block Pattern */
	require get_theme_file_path('/theme-block-pattern/theme-block-pattern.php');
}
add_action('after_setup_theme', 'perfect_blogging_links_setup');

// Admin notice code START
function perfect_blogging_dismissed_notice() {
	update_option( 'perfect_blogging_admin_notice', true );
}
add_action( 'wp_ajax_perfect_blogging_dismissed_notice', 'perfect_blogging_dismissed_notice' );


//After Switch theme function
add_action('after_switch_theme', 'perfect_blogging_getstart_setup_options');
function perfect_blogging_getstart_setup_options () {
    update_option('perfect_blogging_admin_notice', false );
}
// Admin notice code END


