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

if ( ! function_exists( 'influential_lite_setup' ) ) :
/**
 * 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 influential_lite_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on Influential, use a find and replace
	 * to change 'influential-lite' to the name of your theme in all the template files.
	 */
	load_theme_textdomain( 'influential-lite', get_template_directory() . '/languages' );

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

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

	// Indicate widget sidebars can use selective refresh in the Customizer.
	add_theme_support( 'customize-selective-refresh-widgets' );

	// Enable support for custom logo.
	add_theme_support( 'custom-logo', array(
		'height'      => 50,
		'width'       => 250,
		'flex-height' => true,
	) );
	
	/*
	 * 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', influential_lite_fonts_url() ) );	
		
	/*
	 * 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' );
	set_post_thumbnail_size( 1200, 9999 ); // up to 1200 pixels wide and unlimited height
	add_image_size( 'influential-lite-left-featured', 400, 9999 ); // up to 400 pixels wide and unlimited height
	
	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => esc_html__( 'Primary', 'influential-lite' ),
		'social' => esc_html__( 'Social', 'influential-lite' ),
		'footer' => esc_html__( 'Footer', 'influential-lite' ),
	) );

	/*
	 * 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',
	) );
	
	
	// Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'influential_lite_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );
	
}
endif;
add_action( 'after_setup_theme', 'influential_lite_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function influential_lite_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'influential_lite_content_width', 1140 );
}
add_action( 'after_setup_theme', 'influential_lite_content_width', 0 );

/**
 * Register Google fonts.
 * @return string Google fonts URL for the theme.
 */

if ( ! function_exists( 'influential_lite_fonts_url' ) ) :
function influential_lite_fonts_url() {
	$fonts_url = '';
	$fonts     = array();
		if( esc_attr(get_theme_mod( 'load_cyrillic_subset', 0 ) ) ) : 
			$subsets   = 'cyrillic,cyrillic-ext';
		else: 
			$subsets   = 'latin,latin-ext';
		endif;
		
	// Translators: If there are characters in your language that are not supported by Open Sans, translate this to 'off'. Do not translate into your own language. 
	if ( 'off' !== esc_html_x( 'on', 'Open Sans font: on or off', 'influential-lite' ) ) {
		$fonts[] = 'Open Sans:300,400,600,700';
	}	
	
	// check to see if our body font field is empty
	if ( get_theme_mod('second_google_font') !='' ) {	
		// Translators: If there are characters in your language that are not supported by this second Google font, translate this to 'off'. Do not translate into your own language. 
		if ( 'off' !== esc_html_x( 'on', 'Your second Google font: on or off', 'influential-lite' ) ) {
			$fonts[] = esc_attr(get_theme_mod('second_google_font'));
		}	
	}
	
	// check to see if our headings font field is empty
	if ( get_theme_mod('third_google_font') !='' ) {
		// Translators: If there are characters in your language that are not supported by this third Google font, translate this to 'off'. Do not translate into your own language. 
		if ( 'off' !== esc_html_x( 'on', 'Your third Google font: on or off', 'influential-lite' ) ) {
			$fonts[] = esc_attr(get_theme_mod('third_google_font'));
		}	
	}	
	if ( $fonts ) {
		$fonts_url = add_query_arg( array(
			'family' => urlencode( implode( '|', $fonts ) ),
			'subset' => urlencode( $subsets ),
		), 'https://fonts.googleapis.com/css' );
	}

	return $fonts_url;
}
endif;

/**
 * Enqueue scripts and styles.
 */
function influential_lite_scripts() {

	// Add Font Awesome Icons. Unminified version included.
	if( esc_attr(get_theme_mod( 'load_fontawesome', 1 ) ) ) : 
		wp_enqueue_style('fontAwesome', get_template_directory_uri() . '/css/font-awesome.min.css', array(), '4.4.0' );
	endif;

	// Add Animate It. Unminified version included.
	if( esc_attr(get_theme_mod( 'load_animate', 1 ) ) ) : 
		wp_enqueue_style('animate', get_template_directory_uri() . '/css/animate.min.css', array(), '3.5.1' );
	endif;
	
	// Load our custom WooCommerce stylesheet		
	if( esc_attr(get_theme_mod( 'add_woocommerce', 0 ) ) ) : 	
		wp_enqueue_style( 'influential-lite-woocommerce', get_template_directory_uri() . '/css/woocommerce.css', array( ), '1.0.0' );	
	endif;
	
	// Load our responsive stylesheet based on Bootstrap.
	if( esc_attr(get_theme_mod( 'load_bootstrap', 1 ) ) ) :
		wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array( ), '4.0.0' );
	endif;
	
	wp_enqueue_style( 'influential-lite-fonts', influential_lite_fonts_url(), array(), null );
	
	// Load our primary stylesheet
	wp_enqueue_style( 'influential-lite-style', get_stylesheet_uri() );

	wp_enqueue_script( 'influential-lite-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
	
	wp_enqueue_script( 'influential-lite-functions', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20151204', true );

	wp_localize_script( 'influential-lite-functions', 'InfluentialLitescreenReaderText', array(
		'expand'   => __( 'expand child menu', 'influential-lite' ),
		'collapse' => __( 'collapse child menu', 'influential-lite' ),
	) );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'influential_lite_scripts' );

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/extras.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Load Jetpack compatibility file.
 */
require get_template_directory() . '/inc/jetpack.php';

/**
 * Load our inline styles.
 */
require get_template_directory() . '/inc/inline-styles.php';

/**
 * Load our sidebars.
 */
require get_template_directory() . '/inc/sidebars.php';

// display custom admin notice
function influential_custom_admin_notice() {
	$enc_th_info = wp_get_theme(); 
	$currentversion = str_replace('.','',(esc_html( $enc_th_info->get('Version') )));
	$isitdismissed = 'influential_notice_dismissed'.$currentversion;
	if ( !get_user_meta( get_current_user_id() , $isitdismissed ) ) { ?>
	<div class="notice notice-success is-dismissible influential_notice" data-dismissible="disable-done-notice-forever">
		<div>
			<p>	
			<?php _e('Thank you for using the free version of ','influential-lite'); ?>
			<?php echo esc_html( $enc_th_info->get('Name') );?> - 
			<?php echo esc_html( $enc_th_info->get('Version') );
			 ?>
			<?php _e('theme. Want more features? Check out the', 'influential-lite'); ?>
				<script type="text/javascript">alert(<?php echo $isitdismissed?>);</script>
			<a href="<?php echo esc_url( 'https://www.shapedpixels.com/themes/influential/?utm_source=FreeThemes&utm_medium=UpdateMsg&utm_campaign=Influential');?>" target="_blank" aria-label="Dismiss the welcome panel">
				<strong><?php _e('PRO version','influential-lite');?></strong>
			</a>
			<?php _e('for more options and professional support!', 'influential-lite'); ?>
			<a href="?influential-notice-dismissed<?php echo $currentversion;?>">Dismiss this message</a>
			</p>
		</div>
		
	</div>
	
<?php
	}
 }
add_action('admin_notices', 'influential_custom_admin_notice');

function influential_notice_dismissed() {
	$enc_th_info = wp_get_theme(); 
	$currentversion = str_replace('.','',(esc_html( $enc_th_info->get('Version') )));
	$dismissurl = 'influential-notice-dismissed'.$currentversion;
	$isitdismissed = 'influential_notice_dismissed'.$currentversion;
    $user_id = get_current_user_id();
    if ( isset( $_GET[$dismissurl] ) )
        add_user_meta( $user_id, $isitdismissed, 'true', true );
}
add_action( 'admin_init', 'influential_notice_dismissed' );
