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

/**
 * Enqueue files for the TGM PLugin Activation library.
 */
require_once get_template_directory() . '/inc/class-tgm-plugin-activation.php';
require_once get_template_directory() . '/inc/plugins-activation.php';

/**
 * Enqueue scripts for ShoBe Option Page.
 */
require_once trailingslashit( get_template_directory() ) . 'about/about.php';

/**
 * Customizer additions.
 */
require_once trailingslashit( get_template_directory() ) . 'inc/customizer.php';
/**
 * Enqueue WP Bootstrap Navwalker library (responsive menu).
 */
require_once trailingslashit( get_template_directory() ) . 'inc/class-wp-bootstrap-navwalker.php';

/**
 * Register Widgets
 */
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-recent.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-categories.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-tag.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-archives.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-ads.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-ads125.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-popular.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-random.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-views.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-video.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-menu.php';
require_once trailingslashit( get_template_directory() ) . 'inc/widgets/widget-about-footer.php';

function shobe_scripts(){
    // Bootstrap
    wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . '/inc/bootstrap.css', array(), '5.2.3', 'all' );
    wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() . '/inc/bootstrap.js', array( 'jquery' ), '5.2.3', true );
    //Fonts
    wp_enqueue_style( 'bootstrapicons-css', get_template_directory_uri() . '/inc/bootstrap-icons.css', array(), '', 'all' );
    wp_enqueue_style( 'pe-icon-7-css', get_template_directory_uri() . '/inc/pe-icon-7-stroke.css', array(), '', 'all' );
    // Theme's main stylesheet	and JS
	wp_enqueue_style( 'shobe-style', get_stylesheet_uri(), array(), '1.9.3', false);
    wp_enqueue_script( 'shobe-js', get_template_directory_uri() . '/inc/app.js', array(), '1.0', true );
    // Google Fonts
	wp_enqueue_style( 'Poppins-Family', 'https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap' );	
    wp_enqueue_style( 'Saint-Family', 'https://fonts.googleapis.com/css2?family=Mrs+Saint+Delafield&display=swap' );	
    
}

add_action( 'wp_enqueue_scripts', 'shobe_scripts' );

if ( ! function_exists( 'shobe_config' ) ) :
    function shobe_config(){

        register_nav_menus(
			array(
				'shobe_main_menu' 	=> esc_html__( 'ShoBe Main Menu', 'shobe' ),
				'shobe_footer_menu_1' => esc_html__( 'ShoBe Footer Menu 1', 'shobe' ),
				'shobe_footer_menu_2' => esc_html__( 'ShoBe Footer Menu 2', 'shobe' )
			)
		);

		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );

		/*
		 * Add post formats
		 */
		add_theme_support(
			'post-formats',
			array(
				'aside',
				'image',
				'video',
				'quote',
				'link',
				'status',
			)
		);

		/*
		 * Switch default core markup for search form, comment form, and comments
		 * to output valid HTML5.
		 */
		add_theme_support(
			'html5',
			array(
				'search-form',
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
				'style',
				'script',
			)
		);
		
		// This theme is WooCommerce compatible, so we're adding support to WooCommerce
		add_theme_support( 'woocommerce', array(
			'thumbnail_image_width' => 255,
			'single_image_width'    => 255,
	        'product_grid'          => array(
	            'default_rows'    => 10,
	            'min_rows'        => 5,
	            'max_rows'        => 10,
	            'default_columns' => 4,
	            'min_columns'     => 4,
	            'max_columns'     => 4,
	        ),
		) );
		add_theme_support( 'wc-product-gallery-zoom' );
		add_theme_support( 'wc-product-gallery-lightbox' );
		add_theme_support( 'wc-product-gallery-slider' );

        /**
		 * Add support for core custom logo.
		 *
		 * @link https://codex.wordpress.org/Theme_Logo
		 */
        add_theme_support( 'custom-logo', array(
			'height'      => 22,
			'width'       => 118,
			'flex-width'  => true,
			'flex-height' => true,
		) );
        /*
		 * Enable support for Post Thumbnails on posts and pages.
		 *
		 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
		 */		
		add_theme_support( 'post-thumbnails' );
		add_image_size( 'shobe-slider', 1920, 650, array( 'center', 'center' ) );   
		add_image_size( 'shobe-blog', 960, 640, array( 'center', 'center' ) );


        if ( ! isset( $shobe_content_width ) ) {
			$shobe_content_width = 700;
		}
        add_theme_support( 'title-tag' );

		add_action( 'woocommerce_before_main_content', 'shobe_open_container_row', 5 );
		function shobe_open_container_row(){
			?>
				<div class="container">
			<?php
		}
		add_action( 'woocommerce_after_main_content', 'shobe_close_container_row', 5 );
		function shobe_close_container_row(){
			?>
				</div>
			<?php
		}

		remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );

    }

endif;
add_action( 'after_setup_theme', 'shobe_config', 0 );


/**
 * Show cart contents / total Ajax
 */
add_filter( 'woocommerce_add_to_cart_fragments', 'shobe_woocommerce_header_add_to_cart_fragment' );
function shobe_woocommerce_header_add_to_cart_fragment( $fragments ) {
	global $woocommerce;

	ob_start();

	?>
	<span class="items"><?php echo esc_html( WC()->cart->get_cart_contents_count() ); ?></span>
	<?php
	$fragments['span.items'] = ob_get_clean();
	return $fragments;
}

/**
 * Registers a widget area.
 *
 * @link https://developer.wordpress.org/reference/functions/register_sidebar/
 *
 */
add_action( 'widgets_init', 'shobe_sidebars' );
function shobe_sidebars(){
	register_sidebar( array(
		'name'          => esc_html__( 'ShoBe Main Sidebar', 'shobe' ),
		'id'            => 'shobe-sidebar-1',
		'description'   => esc_html__( 'Drag and drop your widgets here.', 'shobe' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s border rounded mb-4">',
		'after_widget'  => '</div>',
		'before_title'  => '<div class="p-3 bg-light rounded"><h5 class="mb-0" >',
		'after_title'   => '</h5></div>',
	) );
	register_sidebar(
		array(
			'name'          => __( 'Footer Column 1', 'shobe' ),
			'id'            => 'footer-1',
			'description'   => __( 'The footer widget area 1st column.', 'shobe' ),
			'before_widget' => '<div id="%1$s" class="col-lg-5 col-md-4 pt-4">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4>',
			'after_title'   => '</h4>',
		)
	);

	register_sidebar(
		array(
			'name'          => __( 'Footer Column 2', 'shobe' ),
			'id'            => 'footer-2',
			'description'   => __( 'The footer widget area 2nd column.', 'shobe' ),
			'before_widget' => '<div id="%1$s" class="col-lg-2 offset-lg-3 col-md-3 pt-4">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4>',
			'after_title'   => '</h4>',
		)
	);

	register_sidebar(
		array(
			'name'          => __( 'Footer Column 3', 'shobe' ),
			'id'            => 'footer-3',
			'description'   => __( 'The footer widget area 3rd column.', 'shobe' ),
			'before_widget' => '<div id="%1$s" class="col-lg-2 col-md-3 pt-4">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4>',
			'after_title'   => '</h4>',
		)
	);
}

/**
 * Adds custom classes to the array of body classes.
 */
function shobe_body_classes( $classes ) {

	// Adds a class of no-sidebar to sites without active sidebar.
	if ( ! is_active_sidebar( 'shobe-sidebar-1' ) ) {
		$classes[] = 'no-sidebar';
	}

	if ( ! is_active_sidebar( 'footer-1' ) && ! is_active_sidebar( 'footer-2' ) && ! is_active_sidebar( 'footer-3' ) ) {
		$classes[] = 'no-sidebar-footer';
	}

	return $classes;
}
add_filter( 'body_class', 'shobe_body_classes' );

/**
 * Adds a shim to wp_body_open backwards compatibility
 */
if ( ! function_exists( 'wp_body_open' ) ) {
	function wp_body_open() {
			do_action( 'wp_body_open' );
	}
}

/**
 * Set our Social Icons URLs.
 * Only needed for our sample customizer preview refresh
 *
 * @return array Multidimensional array containing social media data
 */
if ( ! function_exists( 'shobe_generate_social_urls' ) ) {
	function shobe_generate_social_urls() {
		$plurality = apply_filters( 'shobe_social_icons_plurality', _x( 'me', 'First-person pronoun for Social Icon titles', 'shobe' ) );

		$social_icons = array(
			array( 'url' => 'behance.net', 'icon' => 'bi bi-behance', 'title' => esc_html( sprintf( __( 'Follow %s on Behance', 'shobe' ), $plurality ) ), 'class' => 'behance' ),
			array( 'url' => 'discord.gg', 'icon' => 'bi bi-discord', 'title' => esc_html( sprintf( __( 'Join %s on Discord', 'shobe' ), $plurality ) ), 'class' => 'discord' ),
			array( 'url' => 'dribbble.com', 'icon' => 'bi bi-dribbble', 'title' => esc_html( sprintf( __( 'Follow %s on Dribbble', 'shobe' ), $plurality ) ), 'class' => 'dribbble' ),
			array( 'url' => 'facebook.com', 'icon' => 'bi bi-facebook', 'title' => esc_html( sprintf( __( 'Like %s on Facebook', 'shobe' ), $plurality ) ), 'class' => 'facebook' ),
			array( 'url' => 'github.com', 'icon' => 'bi bi-github', 'title' => esc_html( sprintf( __( 'Fork %s on GitHub', 'shobe' ), $plurality ) ), 'class' => 'github' ),
			array( 'url' => 'instagram.com', 'icon' => 'bi bi-instagram', 'title' => esc_html( sprintf( __( 'Follow %s on Instagram', 'shobe' ), $plurality ) ), 'class' => 'instagram' ),
			array( 'url' => 'linkedin.com', 'icon' => 'bi bi-linkedin', 'title' => esc_html( sprintf( __( 'Connect with %s on LinkedIn', 'shobe' ), $plurality ) ), 'class' => 'linkedin' ),
			array( 'url' => 'mastodon.social', 'icon' => 'bi bi-mastodon', 'title' => esc_html( sprintf( __( 'Follow %s on Mastodon', 'shobe' ), $plurality ) ), 'class' => 'mastodon' ),
			array( 'url' => 'mastodon.art', 'icon' => 'bi bi-mastodon', 'title' => esc_html( sprintf( __( 'Follow %s on Mastodon', 'shobe' ), $plurality ) ), 'class' => 'mastodon' ),
			array( 'url' => 'medium.com', 'icon' => 'bi bi-medium-m', 'title' => esc_html( sprintf( __( 'Follow %s on Medium', 'shobe' ), $plurality ) ), 'class' => 'medium' ),
			array( 'url' => 'pinterest.com', 'icon' => 'bi bi-pinterest', 'title' => esc_html( sprintf( __( 'Follow %s on Pinterest', 'shobe' ), $plurality ) ), 'class' => 'pinterest' ),
			array( 'url' => 'quora.com', 'icon' => 'bi bi-quora', 'title' => esc_html( sprintf( __( 'Follow %s on Quora', 'shobe' ), $plurality ) ), 'class' => 'quora' ),
			array( 'url' => 'reddit.com', 'icon' => 'bi bi-reddit', 'title' => esc_html( sprintf( __( 'Join %s on Reddit', 'shobe' ), $plurality ) ), 'class' => 'reddit' ),
			array( 'url' => 'slack.com', 'icon' => 'bi bi-slack', 'title' => esc_html( sprintf( __( 'Join %s on Slack', 'shobe' ), $plurality ) ), 'class' => 'slack.' ),
			array( 'url' => 'snapchat.com', 'icon' => 'bi bi-snapchat', 'title' => esc_html( sprintf( __( 'Add %s on Snapchat', 'shobe' ), $plurality ) ), 'class' => 'snapchat' ),
			array( 'url' => 'spotify.com', 'icon' => 'bi bi-spotify', 'title' => esc_html( sprintf( __( 'Follow %s on Spotify', 'shobe' ), $plurality ) ), 'class' => 'spotify' ),
			array( 'url' => 'stackoverflow.com', 'icon' => 'bi bi-stack-overflow', 'title' => esc_html( sprintf( __( 'Join %s on Stack Overflow', 'shobe' ), $plurality ) ), 'class' => 'stackoverflow' ),
			array( 'url' => 'steamcommunity.com', 'icon' => 'bi bi-steam', 'title' => esc_html( sprintf( __( 'Follow %s on Steam', 'shobe' ), $plurality ) ), 'class' => 'steam' ),
			array( 'url' => 't.me', 'icon' => 'bi bi-telegram', 'title' => esc_html( sprintf( __( 'Chat with %s on Telegram', 'shobe' ), $plurality ) ), 'class' => 'Telegram' ),
			array( 'url' => 'tiktok.com', 'icon' => 'bi bi-tiktok', 'title' => esc_html( sprintf( __( 'Follow %s on TikTok', 'shobe' ), $plurality ) ), 'class' => 'tiktok' ),
			array( 'url' => 'twitch.tv', 'icon' => 'bi bi-twitch', 'title' => esc_html( sprintf( __( 'Follow %s on Twitch', 'shobe' ), $plurality ) ), 'class' => 'twitch' ),
			array( 'url' => 'x.com', 'icon' => 'bi bi-twitter-x', 'title' => esc_html( sprintf( __( 'Follow %s on Twitter', 'shobe' ), $plurality ) ), 'class' => 'twitter' ),
			array( 'url' => 'assetstore.unity.com', 'icon' => 'bi bi-unity', 'title' => esc_html( sprintf( __( 'Follow %s on Unity Asset Store', 'shobe' ), $plurality ) ), 'class' => 'unity' ),
			array( 'url' => 'vimeo.com', 'icon' => 'bi bi-vimeo', 'title' => esc_html( sprintf( __( 'Follow %s on Vimeo', 'shobe' ), $plurality ) ), 'class' => 'vimeo' ),
			array( 'url' => 'weibo.com', 'icon' => 'bi bi-sina-weibo', 'title' => esc_html( sprintf( __( 'Follow %s on weibo', 'shobe' ), $plurality ) ), 'class' => 'weibo' ),
			array( 'url' => 'wa.me', 'icon' => 'bi bi-whatsapp', 'title' => esc_html( sprintf( __( 'Chat with %s on WhatsApp', 'shobe' ), $plurality ) ), 'class' => 'WhatsApp' ),
			array( 'url' => 'youtube.com', 'icon' => 'bi bi-youtube', 'title' => esc_html( sprintf( __( 'Subscribe to %s on YouTube', 'shobe' ), $plurality ) ), 'class' => 'youtube' ),
		);
		return apply_filters( 'shobe_social_icons', $social_icons );
	}
}

/**
 * Return an unordered list of linked social media icons, based on the urls provided in the Customizer Sortable Repeater
 * This is a sample function to display some social icons on your site.
 * This sample function is also used to show how you can call a PHP function to refresh the customizer preview.
 * Add the following code to header.php if you want to see the sample social icons displayed in the customizer preview and your theme.
 * Before any social icons display, you'll also need to add the relevent URL's to the Header Navigation > Social Icons section in the Customizer.
 * <div class="social">
 *	 <?php echo shobe_get_social_media(); ?>
 * </div>
 *
 * @return string Unordered list of linked social media icons
 */
if ( ! function_exists( 'shobe_get_social_media' ) ) {
	function shobe_get_social_media() {
		$defaults = shobe_generate_defaults();
		$output = array();
		$social_icons = shobe_generate_social_urls();
		$social_urls = explode( ',', get_theme_mod( 'social_urls', $defaults['social_urls'] ) );
		$social_newtab = get_theme_mod( 'social_newtab', $defaults['social_newtab'] );

		foreach( $social_urls as $key => $value ) {
			if ( !empty( $value ) ) {
				$domain = str_ireplace( 'www.', '', parse_url( $value, PHP_URL_HOST ) );
				$index = array_search( strtolower( $domain ), array_column( $social_icons, 'url' ) );
				if( false !== $index ) {
					$output[] = sprintf( '<li class="list-inline-item %1$s"><a class="rounded-circle fs-16 d-inline-block lh-lg" href="%2$s" title="%3$s"%4$s><i class="%5$s"></i></a></li>',
						$social_icons[$index]['class'],
						esc_url( $value ),
						$social_icons[$index]['title'],
						( !$social_newtab ? '' : ' target="_blank"' ),
						$social_icons[$index]['icon']
					);
				}
				else {
					$output[] = sprintf( '<li class="nosocial list-inline-item"><a class="rounded-circle fs-16 d-inline-block lh-lg" href="%2$s"%3$s><i class="%4$s"></i></a></li>',
						$social_icons[$index]['class'],
						esc_url( $value ),
						( !$social_newtab ? '' : ' target="_blank"' ),
						'bi bi-globe'
					);
				}
			}
		}
		if ( !empty( $output ) ) {		
			array_unshift( $output, '<ul class="list-inline social-circle text-center mb-0">' );
			$output[] = '</ul>';
		}

		return implode( '', $output );
	}
}

if( ! function_exists( 'shobe_get_contact_address' ) ) {
	function shobe_get_contact_address(){
		$defaults = shobe_generate_defaults();
		$output = array();
		$contact_address = get_theme_mod( 'contact_address', $defaults['contact_address'] );
		$contact_phone = get_theme_mod( 'contact_phone', $defaults['contact_phone'] );
		$contact_email = get_theme_mod( 'contact_email', $defaults['contact_email'] );
		$contact_about = get_theme_mod( 'contact_about', $defaults['contact_about'] );
		?>
		<a class="navbar-brand logo fw-bolder fs-24 text-dark" href="<?php echo esc_url( home_url('/') );?>">
		<?php
		if( has_custom_logo() ){			
			$output[] =  the_custom_logo();
		}	else {
			$output[] =  bloginfo( 'title' ); 
		}
		?>
		</a>
		<?php
		if( !empty( $contact_address ) ) {
			$output[] = sprintf( '<h4 class="%1$s"><i class="%2$s"></i> %3$s</h4>',
				esc_attr('address'),
				esc_attr('bi bi-geo-alt'),
				esc_html($contact_address)
			);
		}
		if( !empty( $contact_about ) ) {
			$output[] = sprintf( '<p class="%1$s text-muted mb-4"> %2$s</p>',
				esc_attr('about'),
				esc_html($contact_about)
			);
		}
		
		if( !empty( $contact_phone ) ) {
			$output[] = sprintf( '<h5 class="%1$s"><i class="%2$s"></i> %3$s</h5>',
				esc_attr('phone'),
				esc_attr('bi bi-telephone'),
				esc_html($contact_phone)
			);
		}
		if( !empty( $contact_email ) ) {
			$output[] = sprintf( '<h5 class="%1$s"><i class="%2$s"></i> %3$s</h5>',
				esc_attr('email'),
				esc_attr('bi bi-envelope'),
				esc_html($contact_email)
			);
		}

		return implode( '', $output );

	}
}
/**
* Set our default options
*/
if ( ! function_exists( 'shobe_generate_defaults' ) ) {
	function shobe_generate_defaults() {
		$array_defaults = array(
			'social_newtab' => 0,
			'social_urls' => '',
			'social_url_icons' => '',
			'contact_address' => '',
			'contact_phone' => '',
			'contact_email' => '',
			'contact_about' => '',			
		);
		return apply_filters( 'shobe_customizer_defaults', $array_defaults );
	}
}
/**
* Set our Custom Excerpt
*/
function shobe_excerpt_more( $more ) {
    return ' <a href="'.esc_url( get_the_permalink() ).'" rel="nofollow">'.esc_html__( '[Read More...]', 'shobe' ).'</a>';
}
add_filter( 'excerpt_more', 'shobe_excerpt_more' );