<?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( 'multipurpose_blog_header_premium_info' );
	$wp_customize->remove_control( 'multipurpose_blog_header_premium_info' );

	$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' );

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

	$wp_customize->remove_section( 'multipurpose_blog_important_links' ); 

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

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

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

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

	$wp_customize->remove_setting( 'multipurpose_blog_metafields_time' );
	$wp_customize->remove_control( 'multipurpose_blog_metafields_time' );
		
	$wp_customize->remove_setting( 'multipurpose_blog_post_time_icon' );
	$wp_customize->remove_control( 'multipurpose_blog_post_time_icon' );

	$wp_customize->remove_setting( 'multipurpose_blog_slider_premium_info' );
	$wp_customize->remove_control( 'multipurpose_blog_slider_premium_info' );
		
	$wp_customize->remove_setting( 'multipurpose_blog_photo_sec_premium_info' );
	$wp_customize->remove_control( 'multipurpose_blog_photo_sec_premium_info' );

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

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

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

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

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

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

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

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

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

} 
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'  => 10,
	    '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',
	));

	//Reset Demo Import
	$wp_customize->add_setting( 'ultimate_blogger_reset_demo_import_settings', array(
		'sanitize_callback' => 'sanitize_text_field',
	) );

	$wp_customize->add_control('ultimate_blogger_reset_demo_import_settings',array(
			'label'   => __( 'Reset Demo Import', 'ultimate-blogger' ),
			'section' => 'multipurpose_blog_reset_section',
			'type'    => 'button',
			'input_attrs' => array(
				'onclick' => 'ResetChildDemoSettings()',
			),
	));

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

	//Important Links
	$wp_customize->add_section( 'ultimate_blogger_important_links' , array(
    	'title' => esc_html__( 'Important Links', 'ultimate-blogger' ),
    	'priority' => 3,
	) );

	$wp_customize->add_setting('ultimate_blogger_doc_links',array(
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('ultimate_blogger_doc_links',array(
		'type'=> 'hidden',
		'section'=> 'ultimate_blogger_important_links',
		'description' => "<a target='_blank' href='". esc_url(ULTIMATE_BLOGGER_DOC) ." '>". esc_html__('Documentation','ultimate-blogger') ."</a>",
	));	

	$wp_customize->add_setting('ultimate_blogger_demo_links',array(
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('ultimate_blogger_demo_links',array(
		'type'=> 'hidden',
		'description' => "<a target='_blank' href='". esc_url(ULTIMATE_BLOGGER_LIVE_DEMO) ." '>". esc_html__('Demo','ultimate-blogger') ."</a>",
		'section'=> 'ultimate_blogger_important_links'
	));

	$wp_customize->add_setting('ultimate_blogger_forum_links',array(
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('ultimate_blogger_forum_links',array(
		'type'=> 'hidden',
		'section'=> 'ultimate_blogger_important_links',
		'description' => "<a target='_blank' href='". esc_url(ULTIMATE_BLOGGER_FREE_SUPPORT) ." '>". esc_html__('Support Forum','ultimate-blogger') ."</a>",
	));

	$wp_customize->add_setting('ultimate_blogger_review_links',array(
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('ultimate_blogger_review_links',array(
		'type'=> 'hidden',
		'section'=> 'ultimate_blogger_important_links',
		'description' => "<a target='_blank' href='". esc_url(ULTIMATE_BLOGGER_REVIEW) ." '>". esc_html__('Review','ultimate-blogger') ."</a>",
	));	

	$wp_customize->add_setting('ultimate_blogger_slider_premium_info',array(
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('ultimate_blogger_slider_premium_info',array(
		'type'=> 'hidden',
		'label'	=> __('Premium Features','ultimate-blogger'),
		'description' => "<ul><li>". esc_html__('You can change how many slides there are.','ultimate-blogger') ."</li><li>". esc_html__('You can change the font family and the colours of headings and subheadings.','ultimate-blogger') ."</li><li>". esc_html__('And so on...','ultimate-blogger') ."</li></ul><a target='_blank' href='". esc_url(MULTIPURPOSE_BLOG_GO_PRO) ." '>". esc_html__('Upgrade to Pro','ultimate-blogger') ."</a>",
		'section'=> 'multipurpose_blog_slidersettings'
	));

	$wp_customize->add_setting('ultimate_blogger_photo_sec_premium_info',array(
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('ultimate_blogger_photo_sec_premium_info',array(
		'type'=> 'hidden',
		'label'	=> __('Premium Features','ultimate-blogger'),
		'description' => "<ul><li>". esc_html__('Includes settings to set section title.','ultimate-blogger') ."</li><li>". esc_html__('Contains settings for the background colour.','ultimate-blogger') ."</li><li>". esc_html__('Contains options for background images.','ultimate-blogger') ."</li><li>". esc_html__('You can change the font family and colours of heading.','ultimate-blogger') ."</li><li>". esc_html__('And so on...','ultimate-blogger') ."</li></ul><a target='_blank' href='". esc_url(MULTIPURPOSE_BLOG_GO_PRO) ." '>". esc_html__('Upgrade to Pro','ultimate-blogger') ."</a>",
		'section'=> 'multipurpose_blog_photo_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'   => 1,
					'title'    => esc_html__( 'Ultimate Blog Pro', 'ultimate-blogger' ),
					'pro_text' => esc_html__( 'Get Pro', 'ultimate-blogger' ),
					'pro_url'  => esc_url('https://www.buywptemplates.com/products/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' );

// Reset all theme settings
function ultimate_blogger_reset_demo_import_settings() {
    if (current_user_can('manage_options')) {

		remove_theme_mod('multipurpose_blog_phone_number');
		remove_theme_mod('multipurpose_blog_option_slider_height');
		remove_theme_mod('multipurpose_blog_slider_button_text');
		remove_theme_mod('multipurpose_blog_slider_small_title');
		remove_theme_mod('multipurpose_blog_topbar_btn_link');
		remove_theme_mod('multipurpose_blog_slider_hide');
		for ( $count = 1; $count <= 4; $count++ ) {
			remove_theme_mod('multipurpose_blog_slider_setting' . $count);
		}
		remove_theme_mod('multipurpose_blog_bestseller_section_title');
		remove_theme_mod('multipurpose_blog_product_setting_');
		remove_theme_mod('multipurpose_blog_product_setting_');
		remove_theme_mod('multipurpose_blog_product_settings');
		remove_theme_mod('ultimate_blogger_dicount_text');
		remove_theme_mod('ultimate_blogger_sale_text');
		remove_theme_mod('ultimate_blogger_facebook_link');
		remove_theme_mod('ultimate_blogger_twitter_link');
		remove_theme_mod('ultimate_blogger_linkedin_link');
		remove_theme_mod('ultimate_blogger_instagram_link');
		remove_theme_mod('multipurpose_blog_photography_section');

        wp_send_json_success(array('message' => __('Demo import settings have been reset.', 'ultimate-blogger')));
    } else {
        wp_send_json_error(array('message' => __('Permission denied.', 'ultimate-blogger')));
    }
}
add_action('wp_ajax_ultimate_blogger_reset_demo_import_settings', 'ultimate_blogger_reset_demo_import_settings'); 

function ultimate_blogger_theme_setup() {
	define('ULTIMATE_BLOGGER_LIVE_DEMO',__('https://demos.buywptemplates.com/ultimate-blogger-pro/', 'ultimate-blogger'));
	define('ULTIMATE_BLOGGER_BUY_PRO',__('https://www.buywptemplates.com/products/premium-ultimate-blogger-wordpress-theme', 'ultimate-blogger'));
	if ( ! defined( 'ULTIMATE_BLOGGER_BUY_BUNDLE' ) ) {
		define('ULTIMATE_BLOGGER_BUY_BUNDLE', __('https://www.buywptemplates.com/products/wp-theme-bundle', 'ultimate-blogger'));
	}
	define('ULTIMATE_BLOGGER_DOC',__('https://demos.buywptemplates.com/demo/docs/ultimate-blogger-pro/', 'ultimate-blogger'));
	define('ULTIMATE_BLOGGER_PRO_DOC',__('https://demos.buywptemplates.com/demo/docs/ultimate-blogger-pro/', 'ultimate-blogger'));
	define('ULTIMATE_BLOGGER_PRO_SUPPORT',__('https://buywptemplates.com/pages/community', 'ultimate-blogger'));
	define('ULTIMATE_BLOGGER_FREE_SUPPORT',__('https://wordpress.org/support/theme/ultimate-blogger/', 'ultimate-blogger'));
	define('ULTIMATE_BLOGGER_REVIEW',__('https://wordpress.org/support/theme/ultimate-blogger/reviews/', 'ultimate-blogger'));
	define('ULTIMATE_BLOGGER_LINK_CREDIT','https://www.buywptemplates.com/products/free-ultimate-blogger-wordpress-theme');	
}
add_action( 'after_setup_theme', 'ultimate_blogger_theme_setup' );

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>";
	}
}

if ( ! defined( 'MULTIPURPOSE_BLOG_GO_PRO' ) ) {
	define( 'MULTIPURPOSE_BLOG_GO_PRO', 'https://www.buywptemplates.com/products/premium-ultimate-blogger-wordpress-theme');
}

/** Load welcome message.*/
require get_theme_file_path() . '/inc/dashboard/get_started_info.php';