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

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

function ts_mobile_app_setup() {

	$GLOBALS['content_width'] = apply_filters( 'ts_mobile_app_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', bb_mobile_application_font_url() ) );

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

// Dashboard Theme Notification  
function ts_mobile_app_activation_notice() {
		$ts_mobile_app_meta = get_option( 'ts_mobile_app_admin_notice' );

	if (!$ts_mobile_app_meta) {
	echo '<div id="ts-mobile-app-welcome-notice" class="notice is-dismissible bb-mobile-application-notice">';
		echo '<div class="bb-mobile-application-banner">';
			echo '<div class="bb-mobile-application-text">';
				echo '<h2>' . esc_html__( 'Thanks For Installing the "TS Mobile App" Theme !', 'ts-mobile-app' ) . '</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.', 'ts-mobile-app' ) . '</p>';
				echo '<div class="bb-mobile-application-buttons">';
					echo '<a href="' . esc_url( admin_url( 'themes.php?page=ts_mobile_app_guide' ) ) . '">' . esc_html__( 'Demo Import', 'ts-mobile-app' ) . '</a>';
					echo '<a href="'. esc_url( 'https://preview.themescaliber.com/doc/free-bb-app/' ) .'" target=_blank>' . esc_html__( 'Documentation', 'ts-mobile-app' ) . '</a>';
					echo '<a href="'. esc_url( 'https://www.themescaliber.com/products/bb-mobile-application-theme' ) .'" target=_blank>' . esc_html__( 'Get Premium', 'ts-mobile-app' ) . '</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', 'ts-mobile-app' ) . '</a>';
				echo '</div>';
			echo '</div>';
			echo '<div class="bb-mobile-application-image">';
				echo '<img src="' . esc_url(get_theme_file_uri()) . '/images/demo-preview.png">';
			echo '</div>';
		echo '</div>';
    echo '</div>';
}
}


/* Theme Widgets Setup */
function ts_mobile_app_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Blog Sidebar', 'ts-mobile-app' ),
		'description'   => __( 'Appears on blog page sidebar', 'ts-mobile-app' ),
		'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', 'ts-mobile-app' ),
		'description'   => __( 'Appears on page sidebar', 'ts-mobile-app' ),
		'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', 'ts-mobile-app' ),
		'description'   => __( 'Appears on page sidebar', 'ts-mobile-app' ),
		'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>',
	) );

	//Footer widget areas
	$bb_mobile_application_widget_areas = get_theme_mod('bb_mobile_application_footer_widget_areas', '4');
	for ($i=1; $i<=$bb_mobile_application_widget_areas; $i++) {
		register_sidebar( array(
			'name'          => __( 'Footer Nav ', 'ts-mobile-app' ) . $i,
			'id'            => 'footer-' . $i,
			'description'   => '',
			'before_widget' => '<aside id="%1$s" class="widget %2$s py-3">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h3 class="widget-title pb-2 mb-3">',
			'after_title'   => '</h3>',
		) );
	}

	register_sidebar( array(
		'name'          => __( 'Shop Page Sidebar', 'ts-mobile-app' ),
		'description'   => __( 'Appears on shop page', 'ts-mobile-app' ),
		'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', 'ts-mobile-app' ),
		'description'   => __( 'Appears on shop page', 'ts-mobile-app' ),
		'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', 'ts_mobile_app_widgets_init' );

add_action( 'wp_enqueue_scripts', 'ts_mobile_app_enqueue_styles' );
function ts_mobile_app_enqueue_styles() {
	$parent_style = 'bb-mobile-application-basic-style'; // Style handle of parent theme.
	wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
	wp_enqueue_style( 'ts-mobile-app-style', get_stylesheet_uri(), array( $parent_style ) );
	wp_enqueue_style( 'ts-mobile-app-block-patterns-style-frontend', get_theme_file_uri('/theme-block-pattern/css/block-pattern-frontend.css') );
	require get_parent_theme_file_path( '/inc/ts-color-pallete.php' );
	wp_add_inline_style( 'ts-mobile-app-style',$bb_mobile_application_custom_css );
	require get_theme_file_path( '/ts-color-pallete.php' );
	wp_add_inline_style( 'ts-mobile-app-style',$bb_mobile_application_custom_css );
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}

// Customizer Section
function ts_mobile_app_customizer ( $wp_customize ) {

	/*Service Section*/
	$wp_customize->add_section( 'ts_mobile_app_category_post_section' , array(
    	'title'      => __( 'Service Section', 'ts-mobile-app' ),
		'priority'   => 11,
		'panel' => 'bb_mobile_application_panel_id'
	) );

	$wp_customize->add_setting('ts_mobile_app_category_post_premium_info',array(
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('ts_mobile_app_category_post_premium_info',array(
		'type'=> 'hidden',
		'label'	=> __('Premium Features','ts-mobile-app'),
		'description' => "<ul><li>". esc_html('For more settings and features, please explore our premium theme.','ts-mobile-app') ."</li></ul><a target='_blank' href='". esc_url(TS_MOBILE_APP_BUY_NOW) ." '>". esc_html('Upgrade to Pro','ts-mobile-app') ."</a>",
		'section'=> 'ts_mobile_app_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('ts_mobile_app_category_post',array(
		'default'	=> 'select',
		'sanitize_callback' => 'ts_mobile_app_sanitize_choices',
	));
	$wp_customize->add_control('ts_mobile_app_category_post',array(
		'type'    => 'select',
		'choices' => $cat_post,
		'label' => __('Select Category to display post','ts-mobile-app'),
		'section' => 'ts_mobile_app_category_post_section',
	));

	/*About Section*/
	$wp_customize->add_section( 'ts_mobile_app_about_mobile_section' , array(
    	'title'      => __( 'About Section', 'ts-mobile-app' ),
		'priority'   => 10,
		'panel' => 'bb_mobile_application_panel_id'
	) );

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

	$args = array('numberposts' => -1);
	$post_list = get_posts($args);
 	$i = 0;
	$pst[]='Select';  
	foreach($post_list as $post){
	$pst[$post->ID] = $post->post_title;
	}

	$wp_customize->add_setting('ts_mobile_app_post',array(
	   'sanitize_callback' => 'ts_mobile_app_sanitize_choices',
	));
	$wp_customize->add_control('ts_mobile_app_post',array(
	  'type'    => 'select',
	  'choices' => $pst,
	  'label' => __('Select post','ts-mobile-app'),
	  'section' => 'ts_mobile_app_about_mobile_section',
	));

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

/**
	 * Enqueue block editor style
	 */
	function ts_mobile_app_block_editor_styles() {
		wp_enqueue_style( 'ts-mobile-app-font', ts_mobile_app_font_url(), array() );
	    wp_enqueue_style( 'ts-mobile-app-block-patterns-style-editor', get_theme_file_uri( '/theme-block-pattern/css/block-pattern-editor.css' ), false, '1.0', 'all' );
	}
	add_action( 'enqueue_block_editor_assets', 'ts_mobile_app_block_editor_styles' );

/* Theme Font URL */
function ts_mobile_app_font_url(){
	$font_url = '';
	$font_family = array();
	$font_family[] = 'Ubuntu:300,300i,400,400i,500,500i,700,700i';

	$query_args = array(
		'family'	=> rawurlencode(implode('|',$font_family)),
	);
	$font_url = add_query_arg($query_args,'//fonts.googleapis.com/css');
	return $font_url;
}

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

function ts_mobile_app_odd_or_even($counter){
    if($counter % 2 == 0){
        return "even";
    }
    else{
        return "odd";
    }
}

function ts_mobile_app_links_setup() {
	define('TS_MOBILE_APP_BUY_NOW',__('https://www.themescaliber.com/products/bb-mobile-application-theme', 'ts-mobile-app'));
	define('TS_MOBILE_APP_LIVE_DEMO',__('https://preview.themescaliber.com/bb-mobile-application-theme', 'ts-mobile-app'));
	define('TS_MOBILE_APP_CONTACT',__('https://wordpress.org/support/theme/ts-mobile-app/', 'ts-mobile-app'));
	define('TS_MOBILE_APP_PRO_DOC',__('https://preview.themescaliber.com/doc/bb-app/', 'ts-mobile-app'));
	define('TS_MOBILE_APP_FREE_THEME_DOC',__('https://preview.themescaliber.com/doc/free-bb-app/','ts-mobile-app'));
	define('TS_MOBILE_APP_SUPPORT',__('https://wordpress.org/support/theme/ts-mobile-app/','ts-mobile-app'));
	define('TS_MOBILE_APP_REVIEW',__('https://wordpress.org/support/theme/ts-mobile-app/reviews/','ts-mobile-app'));
	add_action( 'init', 'ts_mobile_app_remove_action');
	function ts_mobile_app_remove_action() {
	    remove_action( 'admin_menu','bb_mobile_application_gettingstarted' );
	    remove_action( 'admin_notices','bb_mobile_application_activation_notice' );
	}

	/* 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', 'ts_mobile_app_links_setup');

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

class TS_Mobile_App_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 TS_Mobile_App_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( 'TS_Mobile_App_Customize_Section_Pro' );

	}

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

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

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

// Admin notice code START
function ts_mobile_app_dismissed_notice() {
	update_option( 'ts_mobile_app_admin_notice', true );
}
add_action( 'wp_ajax_ts_mobile_app_dismissed_notice', 'ts_mobile_app_dismissed_notice' );


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

