<?php
/**
 *  Amyra functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Amyra
 * @since 1.0.1
 */

// Defining Some Variable
if( !defined( 'AMYRA_LITE_VERSION' ) ) {
	define('AMYRA_LITE_VERSION', '1.0.1'); // Theme Version
}
if( !defined( 'AMYRA_LITE_DIR' ) ) {
	define( 'AMYRA_LITE_DIR', get_template_directory() ); // Theme dir
}
if( !defined( 'AMYRA_LITE_URL' ) ) {
	define( 'AMYRA_LITE_URL', get_template_directory_uri() ); // Theme url
}


// Set up the content width value based on the theme's design and stylesheet.
if ( ! isset( $content_width ) )
	$content_width = 1170;
/**
 * 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 amyra_lite_setup() {
	
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on Amyra, use a find and replace
	 * to change 'amyra-lite' to the name of your theme in all the template files.
	 */
	load_theme_textdomain( 'amyra-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' );
        
        // This theme styles the visual editor with editor-style.css to match the theme style.
	add_editor_style();
        
	/*
	 * 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( 'amyra-lite-soft-featured', 870, 999999, false );
	set_post_thumbnail_size( 870, 500, true );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'menu-1' => esc_html__( 'Header', 'amyra-lite' ),
		'footer' => esc_html__( 'Footer', 'amyra-lite' ),
	) );

	/*
	 * 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',
	) );

	// Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'amyra_lite_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );

	// Add theme support for selective refresh for widgets.
	add_theme_support( 'customize-selective-refresh-widgets' );

	// Add support for custom logo.
	add_theme_support( 'custom-logo' );

	// Post format.
	add_theme_support( 'post-formats', array('video', 'audio', 'quote', 'gallery'));

	// Add theme support for selective refresh for widgets.
	add_theme_support( 'customize-selective-refresh-widgets' );	
}
add_action( 'after_setup_theme', 'amyra_lite_setup' );

/**
 * Admin Welcome Notice
 *
 * @package Amyra
 * @since 1.0
 */
function amyra_lite_admin_welcom_notice() {
	global $pagenow;

	if ( is_admin() && isset( $_GET['activated'] ) && 'themes.php' === $pagenow ) {
		echo '<div class="updated notice notice-success is-dismissible"><p>'.sprintf( __( 'Thank you for choosing Amyra Blog theme. To get started, visit our <a href="%s">welcome page</a>.', 'amyra-lite' ), esc_url( admin_url( 'themes.php?page=amyra_lite' ) ) ).'</p></div>';
	}
}
add_action( 'admin_notices', 'amyra_lite_admin_welcom_notice' );

/**
 * Handles Body Classes
 *
 * @package Amyra
 * @since 1.0
 */
function amyra_lite_render_body_classes( $classes ) {    

    // Blog and category page template
    if( is_home() || is_front_page() ) {    	
    	$classes[] = "amyra-lite-template-".amyra_lite_get_theme_mod( 'blog_template' );
    } elseif( is_category() ) {
    	$classes[] = "amyra-lite-template-".amyra_lite_get_theme_mod( 'cat_template' );
    } elseif( is_single() ) {
    	$classes[] = "amyra-lite-template-".amyra_lite_get_theme_mod( 'single_post_template' );
    }

	// Post single Page
	if( is_single() ) {
		$single_layout = amyra_lite_get_theme_mod('single_post_template');

		$classes[] = 'single-sidebar-'.$single_layout;
	}

	return $classes;
}
add_filter( 'body_class', 'amyra_lite_render_body_classes' );

/**
 * Handles Post Classes
 *
 * @package Amyra
 * @since 1.0
 */
function amyra_lite_render_post_classes( $classes ) {
	$wrapper = has_post_thumbnail();	
       
	if ( $wrapper ) {
		$classes[] = 'amyra-lite-has-thumbnail';
	}

	// Add class according to blog layout
    if( is_home() ) { // Blog Page

        $blog_layout	    = amyra_lite_get_theme_mod( 'blog_layout' );
		$blog_layout_grid 	= amyra_lite_get_theme_mod( 'blog_layout_grid' );

		$classes[] = "amyra-lite-{$blog_layout}";
		$classes[] = ( $blog_layout == 'grid' || $blog_layout == 'masonry' ) ? 'amyra-lite-columns amyra-lite-col-'.$blog_layout_grid : 'amyra-lite-columns amyra-lite-col-12';
                
                
                if ( $blog_layout == 'grid' || $blog_layout == 'masonry' ) {
                    $classes[] = "amyra-lite-post-grid";            
                } else if ($blog_layout == 'list') {
                    
                    $classes[]="amyra-lite-post-list";
                    
                } else if ($blog_layout == 'list-alt') {
                    $classes[]="amyra-lite-post-list-alt";
                }
		

	} else if( is_search() || is_author() ) { // Category Page
            
		$classes[] = "amyra-lite-list";
                $classes[] = 'amyra-lite-columns amyra-lite-col-12';
                $classes[] = "amyra-lite-post-list";
                
	} else if( is_category() || is_archive() || is_tag() ) { // Category Page
                
		$cat_layout 		= amyra_lite_get_theme_mod( 'cat_layout' );
		$cat_layout_grid 	= amyra_lite_get_theme_mod( 'cat_layout_grid' );

		$classes[] = "amyra-lite-{$cat_layout}";		
                
                $classes[] = ( $cat_layout == 'grid' || $cat_layout == 'masonry' ) ? 'amyra-lite-columns amyra-lite-col-'.$cat_layout_grid : 'amyra-lite-columns amyra-lite-col-12';
                
                if ( $cat_layout == 'grid' || $cat_layout == 'masonry' ) {
                    $classes[] = "amyra-lite-post-grid";            
                } else if ($cat_layout == 'list') {
                    
                    $classes[]="amyra-lite-post-list";
                    
                } else if ($cat_layout == 'list-alt') {
                    $classes[]="amyra-lite-post-list-alt";
                }               
                
	}

	return $classes;
}
add_filter( 'post_class', 'amyra_lite_render_post_classes' );

/**
	* Register Sidebars
	* 
	* @package Amyra
	* @since 1.0
	*/
	function amyra_lite_register_sidebar() {

		// Main Sidebar Area
		register_sidebar( array(
			'name'          => __( 'Main Sidebar', 'amyra-lite' ),
			'id'            => 'sidebar-1',
			'description'   => __( 'Appears on posts and pages.', 'amyra-lite' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		));
		
		

		// Footer Sidebar Area
		register_sidebar( array(
			'name'          => __( 'Footer', 'amyra-lite' ),
			'id'            => 'footer',
			'description'   => __( 'Footer Widhet Area : Add widgets here.', 'amyra-lite' ),
			'before_widget' => '<section id="%1$s" class="widget amyra-lite-columns '. amyra_lite_footer_widgets_cls( 'footer' ) .' %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		));

		// Footer Instgarm Widget Area
		register_sidebar( array(
			'name'          => esc_html__( 'Footer Instgarm Widget Area', 'amyra-lite' ),
			'id'            => 'amyra-lite-intsgram-feed',
			'description'   => esc_html__( 'Add widgets here.', 'amyra-lite' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		));
	}
	// Action to register sidebar
		
	add_action( 'widgets_init', 'amyra_lite_register_sidebar' );
/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 *
 * @package Amyra
 * @since 1.0
 */
function amyra_lite_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'amyra_lite_pingback_header', 5 );

// Common Functions File
require_once AMYRA_LITE_DIR . '/includes/amyra-functions.php';

// Custom template tags for this theme
require_once AMYRA_LITE_DIR . '/includes/template-tags.php';

// Theme Customizer Settings
require_once AMYRA_LITE_DIR . '/includes/customizer.php';

// Script Class
require_once( AMYRA_LITE_DIR . '/includes/class-amyra-script.php' );

// Theme Dynemic CSS
require_once( AMYRA_LITE_DIR . '/includes/amyra-theme-css.php' );

// Enable shortcodes in text widgets
add_filter('widget_text','do_shortcode');


/**
 * Load tab dashboard
 */
if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
    require get_template_directory() . '/includes/dashboard/amyra-how-it-work.php';
    
}
// Plugin recomendation class
require_once( AMYRA_LITE_DIR . '/includes/plugins/class-amyra-recommendation.php' );