<?php
/**
 * Functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package store-expert
 */

if ( ! function_exists( 'store_expert_setup' ) ) :

/**
* Define Namespaces
*/
define( 'STORE_EXPERT_THEME_VERSION','1.0.0');
define( 'STORE_EXPERT_THEME_DIR', get_template_directory() . '/' );
define( 'STORE_EXPERT_THEME_URI', get_template_directory_uri() . '/' );	
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */
function store_expert_setup() {
	/**
	 * Let WordPress manage the document title.
	 * By adding theme support, we declare that this theme does not use a
	 * hard-coded <title> tag in the document head, and expect WordPress to
	 * provide it for us.
	*/
	add_theme_support( 'title-tag' );

	/**
	* Add default support for add feed on head
	*/
	add_theme_support( 'automatic-feed-links' );

	/**
	* Add default support for thumbnails 
	*/
	add_theme_support( 'post-thumbnails' );

	/*
	* Add image size
	* @link https://developer.wordpress.org/reference/functions/add_image_size/
	*/
	add_image_size( 'store-expert-index', 450, false );
	add_image_size( 'store-expert-slider', 1400, 787, false );

	/*
	* Enable navigational menu 
	* store_expert theme use one navigation menu
	* @link https://codex.wordpress.org/Function_Reference/register_nav_menus
	*/
	register_nav_menus(	array(
		'catnav' => __('Main Navigation Menu', 'store-expert'),
		'topnav' => __('Top Navigation Menu', 'store-expert')
	) );

	/*
	* Enable support for custom logo.
	* @link https://developer.wordpress.org/themes/functionality/custom-logo/
	*/
	add_theme_support( 'custom-logo', array(
		'height'=> 70,
		'width'=> 285,
		'flex-height' => true,
		'flex-width' => true,
		'header-text' => array( 'site-title', 'site-description' ),
	) );

	/*
	* Enable customer background
	* @link https://developer.wordpress.org/themes/functionality/custom-backgrounds/
	*/
	add_theme_support( 'custom-background', array(
		'default-color' => 'fff',
	) );

	/*
	 * Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'html5', array(
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
		'search-form',
	) );

	/*
	 * Declare support for WooCommerce
	 */
	add_theme_support( 'woocommerce' );
	add_theme_support( 'wc-product-gallery-zoom' );
	add_theme_support( 'wc-product-gallery-lightbox' );
	add_theme_support( 'wc-product-gallery-slider' );
	/*
	 * Add support for editor styles.
	 */
	add_theme_support( 'editor-styles' );

	/*
	 * Add support for responsive embedded content.
	 */
	add_theme_support( 'responsive-embeds' );

	/*
	 * Set Content width
	 */
	$GLOBALS['content_width'] = apply_filters( 'store_expert_content_width', 850 );

}
endif;
/*
* Adds the store_expert_setup function to after_setup_theme hook.
* @link https://developer.wordpress.org/reference/functions/add_action/
*/
add_action( 'after_setup_theme', 'store_expert_setup' );



/**
 * Enqueue scripts and styles.
 */
if ( ! function_exists ( 'store_expert_enqueues' ) ) {
	function store_expert_enqueues() {			
		/**
		 * Load CSS files
		 */
		wp_enqueue_style( 'bootstrap', STORE_EXPERT_THEME_URI . 'css/bootstrap.css', '', STORE_EXPERT_THEME_VERSION );
		wp_enqueue_style( 'store-expert-font-awesome', STORE_EXPERT_THEME_URI . 'css/fontawesomeall.min.css', '', STORE_EXPERT_THEME_VERSION );
		wp_enqueue_style( 'nivo-slider', STORE_EXPERT_THEME_URI . 'css/nivo-slider.css', '', STORE_EXPERT_THEME_VERSION );
		wp_enqueue_style( 'simple-line-icons', STORE_EXPERT_THEME_URI . 'css/simple-line-icons.css', '', STORE_EXPERT_THEME_VERSION );
		wp_enqueue_style( 'store-expert-style', get_stylesheet_uri(), array(), STORE_EXPERT_THEME_VERSION );
		/**
		 * Load JS files
		 */
		wp_enqueue_script( 'bootstrap', STORE_EXPERT_THEME_URI . 'js/bootstrap.min.js', array( 'jquery' ), STORE_EXPERT_THEME_VERSION, true );
		wp_enqueue_script( 'nivo-slider', STORE_EXPERT_THEME_URI . 'js/nivo.slider.js', array( 'jquery' ), STORE_EXPERT_THEME_VERSION, true );
		wp_enqueue_script( 'resize-sensor', STORE_EXPERT_THEME_URI . 'js/ResizeSensor.min.js', array( 'jquery' ), STORE_EXPERT_THEME_VERSION, true );
		wp_enqueue_script( 'theia-sticky-sidebar', STORE_EXPERT_THEME_URI . 'js/theia-sticky-sidebar.min.js', array( 'jquery' ), STORE_EXPERT_THEME_VERSION, true );
		wp_enqueue_script( 'navigation', STORE_EXPERT_THEME_URI . 'js/navigation.js', array( 'jquery' ), STORE_EXPERT_THEME_VERSION, true );
		wp_enqueue_script( 'main', STORE_EXPERT_THEME_URI . 'js/main.js', array( 'jquery' ), STORE_EXPERT_THEME_VERSION, true );
		wp_enqueue_script( 'store-expert-custom', STORE_EXPERT_THEME_URI . 'js/custom.js', array( 'jquery' ), STORE_EXPERT_THEME_VERSION, true );
		/**
		 * Load comment-reply script only when it is required
		 */
		if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
			wp_enqueue_script( 'comment-reply' );
		}
	}
}
add_action( 'wp_enqueue_scripts', 'store_expert_enqueues' );


/**
 * Load Google Font
 */
function store_expert_enque_google_font() {
	// Include the file.
	require_once get_theme_file_path( 'inc/wptt-webfont-loader.php' );

	// Load the webfont.
	wp_enqueue_style( 'store-expert-poppins', wptt_get_webfont_url( 'https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900' ), array(), '1.0');
}
add_action( 'wp_enqueue_scripts', 'store_expert_enque_google_font' );

/**
* Adjust excerpt length
* Default WordPress excerpt length doesn't look good with theme
* Hense adjusting upon our need
*/
function store_expert_excerpt_length( $length ) {
	if ( is_admin() ) {
		return $length;
	}
	return 40;
}
add_filter( 'excerpt_length', 'store_expert_excerpt_length', 999 );



/**
* Adjust excerpt 
* Remove read more link
*/
function store_expert_excerpt_more( $more ) {
	if ( is_admin() ) {
		return $more;
	}
	return '';														
}
add_filter('excerpt_more', 'store_expert_excerpt_more');

/**
 * Register sidebar
 *
 * @link http://codex.wordpress.org/Function_Reference/register_sidebar
 */
function store_expert_sidebar() {
register_sidebar (array (
	'name' => __( 'Sidebar widgets', 'store-expert' ),
	'id' => 'general-sidebar',
	'description' => __( 'Place your sidebar widgets here.', 'store-expert' ),
	'before_widget' => '<div id="%1$s" class="widget %2$s">',
	'after_widget' => '</div>',
	'before_title' => '<div class="wi-title clearfix"><h3 class="w-title">',
	'after_title' => '</h3></div>',
));
register_sidebar (array (
	'name' => __( 'Footer Widget Left', 'store-expert' ),
	'id' => 'footleft-sidebar',
	'description' => __( 'Place your first footer widgets here.', 'store-expert' ),
	'before_widget' => '<div id="%1$s" class="widget %2$s">',
	'after_widget' => '</div>',
	'before_title' => '<div class="wi-title clearfix"><h3 class="w-title">',
	'after_title' => '</h3></div>',
));
register_sidebar (array (
	'name' => __( 'Footer Widget Middle', 'store-expert' ),
	'id' => 'footmiddle-sidebar',
	'description' => __( 'Place your second footer widgets here.', 'store-expert' ),
	'before_widget' => '<div id="%1$s" class="widget %2$s">',
	'after_widget' => '</div>',
	'before_title' => '<div class="wi-title clearfix"><h3 class="w-title">',
	'after_title' => '</h3></div>',
));
register_sidebar (array (
	'name' => __( 'Footer Widget Right', 'store-expert' ),
	'id' => 'footright-sidebar',
	'description' => __( 'Place your third footer widgets here.', 'store-expert' ),
	'before_widget' => '<div id="%1$s" class="widget %2$s">',
	'after_widget' => '</div>',
	'before_title' => '<div class="wi-title clearfix"><h3 class="w-title">',
	'after_title' => '</h3></div>',
));
}
add_action( 'widgets_init', 'store_expert_sidebar' );


/**
 * Comment settings
 */
function store_expert_comment($comment, $args, $depth) {	
	
	if (get_comment_type() == 'pingback' || get_comment_type() == 'trackback') : ?>
	
	<?php elseif (get_comment_type() == 'comment') :?>
		<li id="comment-<?php comment_ID();?>">
			<div <?php comment_class('comment-post'); ?>>
				<div class="comment-author">
					<?php echo get_avatar($comment, 70);?>
				</div>
				<div class="comment-content">
					<div class="comment-meta">
						<?php echo get_comment_author_link();?>						
						<p><?php comment_date();?></p>
					</div>
					<div class="comment-text">
						<?php comment_text(); ?>
					</div>
					<span class="bg-color" >
					<?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))); ?>
					</span>
					<hr/>
				</div>				
			</div>				
		</li>
	<?php endif;
}


/**
 * Help notice 
 **/
function store_expert_general_admin_notice(){

   $msg = sprintf('<div data-dismissible="disable-done-notice-forever" class="notice notice-info" >
          <h3>%1$s</h3>
          <p>%2$s</p><p>
          <a class="avril-btn-get-started button button-primary button-hero avril-button-padding" href="#" data-name="" data-slug="">%3$s</a>				
          <a href="%4$s" target="new" class="button">%5$s</a>
          <a href="%6$s" class="button">%7$s</a>
          <a href="?store_expert_notice_dismissed" style="text-decoration: none; float: right;" >%8$s</a></p></div>',
          esc_html__('Get Most out of the theme','store-expert'),
          esc_html__('Welcome, Thank you for choosing our theme Store Expert. You can start either by importing the ready made demo or get started by customizing it your self','store-expert'),		
          esc_html__('Go to Demo Importer Page', 'store-expert'),
          esc_url(admin_url()."themes.php?page=store_expert_guide"),		
          esc_html__('Go to Theme Guide', 'store-expert'),
          esc_url(admin_url()."customize.php"),	
          esc_html__('Go to Settings page', 'store-expert'),								
          esc_html__('Dismiss Notice', 'store-expert'));
   echo wp_kses_post($msg);
}

if ( isset( $_GET['store_expert_notice_dismissed'] ) ){
          //set notice value false
          update_option('store_expert_help_notice', 'notice_nov24_dismissed');
}

$real_estate_salient_help_notice = get_option('store_expert_help_notice', '');

if (($real_estate_salient_help_notice != 'notice_nov24_dismissed' || $real_estate_salient_help_notice === '') ){
          add_action( 'admin_notices', 'store_expert_general_admin_notice', 20 );
}


/**************************
 *   Plugin Installer
 **************************/
 
 //Admin Enqueue for Admin
function store_expert_admin_enqueue_scripts(){	
	wp_enqueue_script( 'avril-admin-script', get_template_directory_uri() . '/js/admin.js', array( 'jquery' ), '', true );
    wp_localize_script( 'avril-admin-script', 'avril_ajax_object',
        array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )
    );
}
add_action( 'admin_enqueue_scripts', 'store_expert_admin_enqueue_scripts' );

add_action( 'wp_ajax_install_act_plugin', 'store_expert_admin_install_plugin' );

function store_expert_admin_install_plugin() {
    /**
     * Install Plugin.
     */
    include_once ABSPATH . '/wp-admin/includes/file.php';
    include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    include_once ABSPATH . 'wp-admin/includes/plugin-install.php';

    if ( ! file_exists( WP_PLUGIN_DIR . '/ammu-demo-import' ) ) {
        $api = plugins_api( 'plugin_information', array(
            'slug'   => sanitize_key( wp_unslash( 'ammu-demo-import' ) ),
            'fields' => array(
                'sections' => false,
            ),
        ) );

        $skin     = new WP_Ajax_Upgrader_Skin();
        $upgrader = new Plugin_Upgrader( $skin );
        $result   = $upgrader->install( $api->download_link );
    }
	
    if ( ! file_exists( WP_PLUGIN_DIR . '/one-click-demo-import' ) ) {
        $api = plugins_api( 'plugin_information', array(
            'slug'   => sanitize_key( wp_unslash( 'one-click-demo-import' ) ),
            'fields' => array(
                'sections' => false,
            ),
        ) );

        $skin     = new WP_Ajax_Upgrader_Skin();
        $upgrader = new Plugin_Upgrader( $skin );
        $result   = $upgrader->install( $api->download_link );
    }	

    // Activate plugin.
    if ( current_user_can( 'activate_plugin' ) ) {
        $result = activate_plugin( 'one-click-demo-import/one-click-demo-import.php' );
		
        $result = activate_plugin( 'ammu-demo-import/ammu-demo-import.php' );
		
    }
}


/**
* Customizer additions.
*/
require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/template-tags.php';
require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/woocommerce-functions.php';
require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/customize/theme-options.php';
require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/tgm/tgmpa-function.php';
require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/upgrade/class-customize.php';
require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/getstart/getstart.php';



/**
* Get Home page sections
*/
require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/home-sections/home-slider.php';
require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/home-sections/home-category-list.php';
require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/home-sections/home-tabbed-products.php';
require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/home-sections/home-grid-products.php';