<?php
/**
 * Theme functions and definitions
 * evaWp only works in WordPress 4.7 or later.
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 * @package evaWp
 */

define( 'EVAWP_PREFIX', 'evawp' );
define( 'EVAWP_VERSION', '1.3' );

if( !function_exists( 'evawp_file_loader' ) ){
	/**
	 * Load the files needed for theme
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_file_loader( $files, $base = 'inc' ){
		foreach ( $files as $file ) {
			$path = $base . '/' . $file . '.php';
			require_once get_theme_file_path( $path );
		}
	}
}

/**
 * Needed Files for theme theme
 * @since 1.0
 * @package evaWp
 */
evawp_file_loader( array(
	'framework/kirki/kirki',
	'framework/tgm/class-tgm-plugin-activation',
	'theme-hooks/after-setup-theme',
	'helper/theme-helper',
	'internal-css/dynamic-css',
	'breadcrumb/init',
	'theme-options/config'
));

/**
 * Enqueue Scripts fot theme
 * @since 1.0
 * @package evaWp
 */
function evawp_enqueue_scripts(){
	global $wp_query;
	evwap_script_loader( array(
		array(
			'handle' => 'main-style',
			'style' => 'style.css'
		),
		array(
			'handle' => 'evawp-style',
			'style' => 'assets/css/evawp-style.css'
		),
		array(
			'handle' => 'google-font',
			'style' => 'https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,300;0,400;0,700;1,400&family=Montserrat:ital,wght@0,300;0,400;0,500;1,300;1,500&display=swap',
			'external' => true
		),
		array(
			'handle' => 'bootstrap',
			'style' => 'assets/vendor/bootstrap/bootstrap.min.css',
			'version' => '4.3.1'
		),
		array(
			'handle' => 'slick',
			'style' => 'assets/vendor/slick/slick.css',
			'version' => '1.8.1'
		),
		array(
		    'handle' => 'block-style',
		    'style'   => 'assets/css/blocks.css',
		),	
		array(
			'handle' => 'fontawesome',
			'style' => 'assets/vendor/fontawesome/css/all.min.css',
			'version' => '5.15.3'
		),
		array(
			'handle' => 'slick',
			'script' => 'assets/vendor/slick/slick.js',
			'version' => '1.8.1'
		),	
		array(
			'handle' => 'jquery-sticky',
			'script' => 'assets/js/jquery-sticky.js',
			'version' => '1.0.4'
		),			
		array(
			'handle' => 'evawp-script',
			'script' => 'assets/js/evawp-script.js',
			'localize'	=> array(
				'key'	=> 'EVAWPLOADMORE',
				'data'	=> array(
					'admin_url'	=> admin_url( 'admin-ajax.php' ),
					'posts' => json_encode( $wp_query->query_vars ),
					'nonce' => wp_create_nonce('evawp-loadmore-nonce'),
					'max_page' => $wp_query->max_num_pages,
					'pagination_text' => evawp_customizer_get( 'loadmore_post_text', esc_html__( 'Load More', 'evawp' ) )
				),
			)
		)
	));

	if( is_rtl() ){
		evwap_script_loader( array(
			array(
				'handle' => 'evawp-rtl',
				'style' => 'rtl.css'
			),
		));
	}

	# enqueue comment-reply.js in single page only
	if( ( is_single() || is_page() ) && comments_open() && get_option( 'thread_comments' ) ){
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'evawp_enqueue_scripts' );

/**
 * Enqueue customizer style
 * @since 1.2
 * @package evaWp
 */
function evawp_customizer_style(){
	evwap_script_loader( array(
		array(
			'handle' => 'customizer-style',
			'style' => 'assets/css/customizer.css',
		)
	));
}
add_action( 'customize_controls_enqueue_scripts', 'evawp_customizer_style', 99 );

function evawp_editor_assets(){
	evwap_script_loader( array(
		array(
			'handle' => 'evawp-block-editor',
			'style' => 'assets/css/editor-blocks.css',
		),
		array(
			'handle' => 'evawp-editor-styles',
			'style' => 'assets/css/editor-style.css',
		),
	));
}
add_action( 'enqueue_block_editor_assets', 'evawp_editor_assets' );

/**
 * Register sidebar for theme
 * @since 1.0
 * @package evaWp
 */
function evawp_sidebar_register(){
	register_sidebar( array(
        'name' 			=> esc_html__( 'Sidebar', 'evawp' ),
        'id' 			=> 'evawp-sidebar',
        'description' 	=> esc_html__( 'Widgets in this area will be shown on side of the page.', 'evawp' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
	));

	for( $i = 1; $i <= evawp_customizer_get( 'footer_widget_area', '4' ) ; $i++ ){
		register_sidebar( array(
			/* translators: %d: number of unexpected outputed characters */
	        'name' 			=> sprintf( esc_html__( 'Footer Widget Area %d ', 'evawp' ), $i ),
	        'id' 			=>  'evawp_footer_' . $i,
	        'description' 	=> esc_html__( 'Widgets in this area will be displayed the footer. If empty then column will not be displayed.', 'evawp' ),
	        'before_widget' => '<section id="%1$s" class="widget %2$s">',
	        'after_widget'  => '</section>',
	        'before_title'  => '<h2 class="widget-title">',
	        'after_title'   => '</h2>',
		));
	}
}
add_action( 'widgets_init', 'evawp_sidebar_register' );


/**
 * Modified excerpt length
 * @since 1.0
 * @package evaWp
 */
function evawp_custom_excerpt_length( $length ){
	if( is_admin() && !wp_doing_ajax() ){
		return esc_html( $length );
	}
	$length = evawp_customizer_get( 'excerpt_length', 30 );
	return $length;
}
add_filter( 'excerpt_length', 'evawp_custom_excerpt_length' );

/**
 * Modified excerpt readmore
 * @since 1.0
 * @package evaWp
 */
function evawp_excerpt_more( $text ){
	if( is_admin() && !wp_doing_ajax() ){
		return esc_html( $text );
	}	
			
	$more = sprintf( '...<a href="%1$s" class="evawp-readmore">%2$s</a>',
        esc_url( get_the_permalink() ),
        evawp_customizer_get( 'read_more_text', esc_html__( 'Read more', 'evawp' ) )
    );

	return $more;
}
add_filter( 'excerpt_more', 'evawp_excerpt_more' );

/**
 * Check theme options for sidebar
 * @since 1.0
 * @package evaWp
 */
function evawp_is_sidebar_active(){
	$sidebar = evawp_customizer_get( 'enable_sidebar', 'right-sidebar' );
	return 'no-sidebar' == $sidebar ? false : true;
}

/**
 * Adds slider on front paage
 * @since 1.0
 * @package evaWp
 */
function evawp_front_page_slider(){
	if( !evawp_customizer_get( 'enable_slider', true )){
		return;
	}
	if( is_home() || is_front_page() ){
		get_template_part( 'templates/front-page/slider', '' );
	}
}
add_action( 'evawp-after-header', 'evawp_front_page_slider');

if( !function_exists( 'evawp_footer_logo' ) ){
	/**
	 * Print site identity for header
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_footer_logo(){
		if(has_custom_logo()) {
		    the_custom_logo();
		} else { ?>
		    <p class="site-title">
		    	<a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
		    		<?php bloginfo( 'name' ); ?>									
		    	</a>
		    </p>
		<?php }
	}
}

function evawp_scroll_top_top(){
	/**
	 * Scroll to top for theme
	 * @since 1.0
	 * @package evaWp
	 */
	if( !evawp_customizer_get( 'enable_stt', true ) ){
		return;
	}
	echo '<a href="#" class="evawp-scroll-top"><i class="fa fa-angle-up"></i></a>';
}
add_action( 'evawp_after_footer', 'evawp_scroll_top_top' );

function evawp_add_preloader(){
	/**
	 * Pre Loader for theme
	 * @since 1.0
	 * @package evaWp
	 */
	if( evawp_customizer_get( 'enable_pre_loader', false ) ){ ?>		
		<div class="evawp-loader-wrapper">
			<div class="loader"></div>
		</div>
	<?php }
}
add_action( 'evawp-before-header', 'evawp_add_preloader' );

function evawp_customize_register( $wp_customize ){
	/**
	 * modify customizer options
	 * @since 1.0
	 * @package evaWp
	 */
	$wp_customize->get_control( 'header_textcolor' )->section = 'title_tagline';
	$wp_customize->get_control( 'header_textcolor' )->label = esc_html__( 'Site Identity Color', 'evawp' );

	$wp_customize->get_section( 'header_image' )->title = esc_html__( 'Banner Options', 'evawp' );
	$wp_customize->get_section( 'header_image' )->panel = 'general_options';

	# change background color section
	$wp_customize->get_control( 'background_color' )->section = 'evawp_color_options';

	# change background image panel
	$wp_customize->get_section( 'background_image' )->panel = 'general_options';
}
add_action( 'customize_register', 'evawp_customize_register' );


function evawp_add_recent_post_single(){
	/**
	 * Related Post for single page
	 * @since 1.0
	 * @package evaWp
	 */
	$related = get_posts( 
	    array( 
	        'category__in' => wp_get_post_categories( get_the_ID() ), 
	        'numberposts'  => 3, 
	        'post__not_in' => array( get_the_ID() ) 
	    ) 
	);
	if( $related ){		
		set_query_var('single_related_posts', $related );
		get_template_part( 'templates/content/single', 'related');
	}
}
add_action( 'evawp_single_before_comment', 'evawp_add_recent_post_single' );

/**
 * Single page haeder with banner and breadcrumb
 * @since 1.0
 * @package evaWp
 */
function evawp_site_banner(){
	if( !is_single() && !is_page() ){
		return;
	}
	if( is_front_page() ){
		return;
	}?>
	<div class="evawp-single-page-header-wrapper" <?php evawp_the_header_image(); ?>>
		<div class="container">
			<div class="evawp-single-page-header">
				<div class="evawp-nav-previous">
					<?php previous_post_link( '<i class="fas fa-chevron-left"></i> %link', '%title', true ); ?>
				</div>
				<div class="evawp-single-title-meta">
					<?php if( is_single() ): ?>
						<div class="evawp-meta-wrapper">				
							<?php evawp_content_meta( get_the_ID(), true ); ?>
						</div>
					<?php endif; ?>
					<h1 class="evawp-page-title">
						<?php the_title(); ?>					
					</h1>
					<div class="evawp-breadcrumb-wrapper">
						<?php evawp_the_breadcrumb(); ?>			
					</div>	
				</div>
				<div class="evawp-nav-next">
					<?php next_post_link( '%link <i class="fas fa-chevron-right"></i>', '%title', true ); ?>
				</div>
			</div>	
		</div>

	</div>
<?php }
add_action( 'evawp-after-header', 'evawp_site_banner' );

/**
 * Return Body class
 * @since 1.0
 * @package evaWp
 */
function evawp_add_body_classes( $classes ){
	if( 'boxed' == evawp_customizer_get( 'site_layout', 'default' ) ){
		$classes[] = 'layout-box';
	}

	if( 'right-sidebar' == evawp_customizer_get( 'enable_sidebar', 'right-sidebar' ) ){
		$classes[] = 'right-sidebar';
	}

	return $classes;
}
add_action( 'body_class', 'evawp_add_body_classes' );

/**
 * Generates mark up for more posts
 * @since 1.0
 * @package evaWp
 */	
function evawp_view_more_posts(){
	$response = array(
		'data' => null,
		'status' => 400
	);
	if ( wp_verify_nonce( $_POST['nonce'], 'evawp-loadmore-nonce' ) ){

		// prepare our arguments for the query
		$args = json_decode( stripslashes( $_POST['query'] ), true );
		$args[ 'paged' ] = $_POST['page'];
		$args[ 'post_status' ] = 'publish';
		
		// it is always better to use WP_Query but not here
		query_posts( $args );

		ob_start();
		if( have_posts() ){
			while( have_posts() ){
				the_post();?>
				<div class="<?php evawp_the_post_per_row_class(); ?>">
					<?php get_template_part( 'templates/content/content', '' ); ?>
				</div>
			<?php }
		}
		wp_reset_postdata();

		$response[ 'data' ] = ob_get_clean(); ob_flush();
		$response[ 'status' ] = 200;

	}
	
	wp_send_json( $response[ 'data' ], $response[ 'status' ] );
}
add_action( 'wp_ajax_view_more_posts', 'evawp_view_more_posts' );
add_action( 'wp_ajax_nopriv_view_more_posts', 'evawp_view_more_posts' );

/**
 * Plugin recommendation
 * @since 1.3
 * @package evaWp
 */	
function evawp_register_required_plugins(){
	$plugins = array(
		array(
			'name'     => esc_html__( 'Rise Blocks - A Complete Gutenberg Page builder', 'evawp' ),
			'slug'     => 'rise-blocks',
			'required' => false,
		),
		array(
			'name'     => esc_html__( 'RT Easy Builder – Advanced addons for Elementor', 'evawp' ),
			'slug'     => 'rt-easy-builder-advanced-addons-for-elementor',
			'required' => false,
		),
	);

	tgmpa( $plugins );
}
add_action( 'tgmpa_register', 'evawp_register_required_plugins' );