<?php
/**
 * Theme Functions.
 */

if ( ! function_exists( 'ultimate_blogger_setup' ) ) :

/* Theme Setup */
function ultimate_blogger_setup() {

	$GLOBALS['content_width'] = apply_filters( 'ultimate_blogger_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'      => 240,
		'width'       => 240,
		'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');
	/*
	 * 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', multipurpose_blog_font_url() ) );

}
endif; 
add_action( 'after_setup_theme', 'ultimate_blogger_setup' );

add_action( 'wp_enqueue_scripts', 'ultimate_blogger_enqueue_styles' );
function ultimate_blogger_enqueue_styles() {
	wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . '/css/bootstrap.css');
	$parent_style = 'multipurpose-blog-basic-style'; // Style handle of parent theme.
	wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
	wp_enqueue_style( 'ultimate-blogger-style', get_stylesheet_uri(), array( $parent_style ) );

	require get_parent_theme_file_path( '/theme-color-option.php' );
	wp_add_inline_style( 'multipurpose-blog-basic-style',$multipurpose_blog_custom_css );
	require get_theme_file_path( '/theme-color-option.php' );
	wp_add_inline_style( 'ultimate-blogger-style',$ultimate_blogger_custom_css );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}


	/*------------------ Skin Option  -------------------*/
	$ultimate_blogger_theme_lay = get_theme_mod( 'multipurpose_blog_background_skin','Without Background');
    if($ultimate_blogger_theme_lay == 'With Background'){
		$ultimate_blogger_custom_css .='.inner-service, .inner-services, #sidebar .widget,.woocommerce ul.products li.product, .woocommerce-page ul.products li.product, .search-cat-box, .login-box a, .front-page-content, .background-img-skin, .imagebox{';
			$ultimate_blogger_custom_css .='background-color: #3d3c3d !important; padding:10px !important;';
		$ultimate_blogger_custom_css .='}';
		$ultimate_blogger_custom_css .='#photography{';
			$ultimate_blogger_custom_css .='background:none;';
		$ultimate_blogger_custom_css .='}';
	}else if($ultimate_blogger_custom_css == 'Without Background'){
		$ultimate_blogger_custom_css .='section, .main-box,#sidebar aside{';
			$ultimate_blogger_custom_css .='background-color: transparent;';
	}

	// wp_add_inline_style( 'ultimate-blogger-style',$ultimate_blogger_custom_css );
}

function ultimate_blogger_customize_register() { 
	global $wp_customize;
	$wp_customize->remove_section( 'ultimate_blogger_global_color' ); 
	$wp_customize->remove_section( 'ultimate_blogger_topbar' );
	$wp_customize->remove_setting( 'ultimate_blogger_category_icon' );
	$wp_customize->remove_control( 'ultimate_blogger_category_icon' );

	$wp_customize->remove_setting( 'multipurpose_blog_phone_icon' );
	$wp_customize->remove_control( 'multipurpose_blog_phone_icon' );

	$wp_customize->remove_setting( 'multipurpose_blog_contact' );
	$wp_customize->remove_control( 'multipurpose_blog_contact' );

	$wp_customize->remove_setting( 'multipurpose_blog_email_icon' );
	$wp_customize->remove_control( 'multipurpose_blog_email_icon' );

	$wp_customize->remove_setting( 'multipurpose_blog_email' );
	$wp_customize->remove_control( 'multipurpose_blog_email' );

} 
add_action( 'customize_register', 'ultimate_blogger_customize_register', 11 );

add_action( 'init', 'ultimate_blogger_remove_parent_function');
function ultimate_blogger_remove_parent_function() {
	remove_action('admin_notices', 'multipurpose_blog_notice');
	remove_action( 'admin_menu', 'multipurpose_blog_gettingstarted' );
}

// Customizer Section
function ultimate_blogger_customizer ( $wp_customize ) {

	//Our Blog Category section
  	$wp_customize->add_section('ultimate_blogger_our_blog_cat_section',array(
	    'title' => __('Our Blog Category Section','ultimate-blogger'),
	    'description' => '',
	    'priority'  => null,
	    'panel' => 'ultimate_blogger_panel_id',
	)); 

	$categories = get_categories();
	$cats = array();
	$i = 0;
  	foreach($categories as $category){
  	if($i==0){
	$default = $category->slug;
	$i++;
	}
	$cats[$category->slug] = $category->name;
	}

	$wp_customize->add_setting('ultimate_blogger_our_category',array(
	    'default' => 'select',
	    'sanitize_callback' => 'ultimate_blogger_sanitize_choices',
  	));
  	$wp_customize->add_control('ultimate_blogger_our_category',array(
	    'type'    => 'select',
	    'choices' => $cats,
	    'label' => __('Select Category to display Latest Post','ultimate-blogger'),
	    'section' => 'ultimate_blogger_our_blog_cat_section',
	));

}
add_action( 'customize_register', 'ultimate_blogger_customizer' );

/*------------------------------section-pro.php part----------------------------------------*/
require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );

class Ultimate_Blogger_Customize_Section_Pro extends WP_Customize_Section {

	/**
	 * The type of customize section being rendered.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $type = 'ultimate-blogger';

	/**
	 * Custom button text to output.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $pro_text = '';

	/**
	 * Custom pro button URL.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $pro_url = '';

	/**
	 * Add custom parameters to pass to the JS via JSON.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return void
	 */
	public function json() {
		$json = parent::json();

		$json['pro_text'] = $this->pro_text;
		$json['pro_url']  = esc_url( $this->pro_url );

		return $json;
	}

	/**
	 * 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 Ultimate_Blogger_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( 'Ultimate_Blogger_Customize_Section_Pro' );

		// Register sections.
		$manager->add_section(
			new Ultimate_Blogger_Customize_Section_Pro(
				$manager,
				'ultimate_blogger',
				array(
					'priority'   => 9,
					'title'    => esc_html__( 'Ultimate Blog Pro', 'ultimate-blogger' ),
					'pro_text' => esc_html__( 'Go Pro', 'ultimate-blogger' ),
					'pro_url'  => esc_url('https://www.buywptemplates.com/themes/premium-ultimate-blogger-wordpress-theme/'),
				)
			)
		);
	}

	/**
	 * Loads theme customizer CSS.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return void
	 */

	public function enqueue_control_scripts() {

		wp_enqueue_script( 'ultimate-blogger-customize-controls', get_stylesheet_directory_uri() . '/js/customize-controls-child.js', array( 'customize-controls' ) );

		wp_enqueue_style( 'ultimate-blogger-customize-controls', get_stylesheet_directory_uri() . '/css/customize-controls-child.css' );
	}
}

// Doing this customizer thang!
Ultimate_Blogger_Customize::get_instance();


/* Theme Widgets Setup */
function ultimate_blogger_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Home Sidebar', 'ultimate-blogger' ),
		'description'   => __( 'Appears on Home Page', 'ultimate-blogger' ),
		'id'            => 'home',
		'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', 'ultimate_blogger_widgets_init' );

define('ULTIMATE_BLOGGER_LIVE_DEMO',__('https://www.buywptemplates.com/ultimate-blogger-pro/', 'ultimate-blogger'));
define('ULTIMATE_BLOGGER_BUY_PRO',__('https://www.buywptemplates.com/themes/premium-ultimate-blogger-wordpress-theme/', 'ultimate-blogger'));
define('ULTIMATE_BLOGGER_PRO_DOC',__('https://buywptemplates.com/demo/docs/ultimate-blogger-pro/', 'ultimate-blogger'));
define('ULTIMATE_BLOGGER_PRO_SUPPORT',__('https://www.buywptemplates.com/support/', 'ultimate-blogger'));
define('ULTIMATE_BLOGGER_FREE_SUPPORT',__('https://wordpress.org/support/theme/ultimate-blogger/', 'ultimate-blogger'));

define('ULTIMATE_BLOGGER_LINK_CREDIT','https://www.buywptemplates.com/themes/free-ultimate-blogger-wordpress-theme/');
if ( ! function_exists( 'ultimate_blogger_link_credit' ) ) {
	function ultimate_blogger_link_credit(){
		echo "<a href=".esc_url(ULTIMATE_BLOGGER_LINK_CREDIT)." target='_blank'>".esc_html__('Blogger WordPress Theme','ultimate-blogger')."</a>";
	}
}

/** Load welcome message.*/
require get_theme_file_path() . '/inc/dashboard/get_started_info.php';