<?php
/**
 * Helper functions used for theme
 * @since 1.0
 * @package evaWp
 */

if ( ! function_exists( 'wp_body_open' ) ) {
	/**
	 * Shim for wp_body_open, ensuring backwards compatibility with versions of WordPress older than 5.2.
	 * @since 1.0
	*/
	function wp_body_open() {
		do_action( 'wp_body_open' );
	}
}

if( !function_exists( 'evawp_schema_for_tags' ) ){
	/**
	 * Returns scama texts for different tags
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_schema_for_tags( $type ) {
		$schema_body = '';
		switch ($type) {
			case 'body' :	
				# Check conditions.
				$is_blog = ( is_home() || is_archive() || is_attachment() || is_tax() || is_single() ) ? true : false;

				# Set up default itemtype.
				$itemtype = 'WebPage';

				# Get itemtype for the blog.
				$itemtype = ( $is_blog ) ? 'Blog' : $itemtype;

				# Get itemtype for search results.
				$itemtype = ( is_search() ) ? 'SearchResultsPage' : $itemtype;
				$schema_body = "itemtype='https://schema.org/{$itemtype}' itemscope='itemscope' ";
			break;
			case 'header' :
				$schema_body = "itemtype='https://schema.org/WPHeader' itemscope='itemscope' role='banner' ";
			break;

			case 'footer' :
				$schema_body = "itemtype='https://schema.org/WPFooter' itemscope='itemscope' role='contentinfo'";
			break;

			case 'article':
				$schema_body = "itemtype='https://schema.org/CreativeWork' itemscope='itemscope'";
			break;

			default :
			break;
		}
		echo $schema_body;
	}
}

if( !function_exists( 'evwap_script_loader' ) ){
	/**
	 * Script loader
	 * @since 1.0
	 * @package evaWp
	 */
	function evwap_script_loader( $files ){
		foreach ( $files as $file ) {
			# Do not try to enqueue anything if handler is not supplied.
			if( ! isset( $file[ 'handle' ] ) ){
			    continue;
			}
			$handle = $file[ 'handle' ];

			$version = false;
			if( isset( $file[ 'version' ] ) ){
			    $version = $file[ 'version' ];
			}

			$dependency = array();
			if( isset( $file[ 'dependency' ] ) ){
			    $dependency = $file[ 'dependency' ];
			}

			if( isset( $file[ 'style' ] ) ){
				$src = get_theme_file_uri( $file[ 'style' ] );
				if( isset( $file[ 'external' ] ) ){
					$src = $file[ 'style' ];
				}
				wp_enqueue_style( $handle, $src, $dependency, $version );
			}elseif( isset( $file[ 'script' ] ) ){
				$src = get_theme_file_uri( $file[ 'script' ] );
				if( isset( $file[ 'external' ] ) ){
					$src = $file[ 'script' ];
				}

				$in_footer = isset( $file[ 'footer' ] ) ? isset( $file[ 'footer' ] ) : false;

				$dependency[] = 'jquery';
				wp_enqueue_script( $handle, $src, $dependency, $version, $in_footer );

				if( isset( $file['localize'] ) && count( $file['localize'] ) > 0 ) {
					wp_localize_script( $file[ 'handle' ] , $file['localize']['key'] , $file['localize']['data'] );
				}
			}
		}
	}
}

if( !function_exists( 'evawp_the_date' ) ){	
	/**
	 * Prints HTML with meta information for the current post-date/time.
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_the_date( $post_id = null ) {		
		$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';

		$time_tag = sprintf(
			$time_string,
			esc_attr( get_the_date( DATE_W3C, $post_id ) ),
			esc_html( get_the_date( get_option('date_format'), $post_id ) ),
			esc_attr( get_the_modified_date( DATE_W3C, $post_id ) ),
			esc_html( get_the_modified_date( DATE_W3C, $post_id ) )
		);

		printf(
			'<span class="posted-on">
				<a href="%1$s" rel="bookmark">
					%2$s
				</a>
			</span>',
			esc_url( get_day_link( get_the_time('Y'), get_the_time('m'), get_the_time('d') ) ),
			$time_tag
		);
	}
}

if( !function_exists( 'evawp_the_category' ) ){	
	/**
	 * Prints the category of the posts
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_the_category( $post_id = false ){
		$cat = get_the_category( $post_id );
		if( !empty( $cat ) ){ 
			$numItems = count( $cat );
			$i = 0; ?>
			<ul class="post-categories">
				<?php foreach ( $cat as $c ) { ?>
					<li>
						<a href="<?php echo esc_url( get_category_link( $c ) ); ?>">
							<?php echo esc_html( $c->name ); ?>
						</a>
					</li>
					<?php  if( ++$i != $numItems ) {
					    echo ",&nbsp;";
					}
				} ?>
			</ul>					
		<?php }
	}
}

if( !function_exists( 'evawp_posted_by' ) ){
	/**
	 * Meta information about theme author.
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_posted_by( $post_id = false ){
		if( $post_id ){
			$author_id = get_post_field( 'post_author', $post_id );
			$author_name = get_the_author_meta( 'display_name', $author_id );
		}
		printf(
			/* translators:1-author link, 2-author image link, 
			 * 3- author text, 4- author name.
			 */
			'<span class="evawp-author-text">
				%2$s
			</span>
			<a class="evawp-author-link" href="%1$s">
				<span class="author">
					%3$s
				</span>
			</a>',
			esc_url( get_author_posts_url( $post_id ? get_the_author_meta( 'ID', $author_id ) : get_the_author_meta( 'ID' ) ) ),
			esc_html__( 'By ', 'evawp' ),
			esc_html( $post_id ? $author_name : get_the_author_meta( 'name' ) )
		);
	}
}

if( !function_exists( 'evawp_customizer_get' ) ){
	function evawp_customizer_get( $id, $default = false ){
		if( !$id ){
			return;
		}
		$id = EVAWP_PREFIX . '_' . $id  . '_setting';
		$val = get_theme_mod( $id, $default );

		return $val;
	}
}

if( !function_exists( 'evawp_add_panel' ) ){
	/**
	 * Adds panel using kriki
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_add_panel( $args = false ){
		if( !class_exists( 'Kirki' ) ){
			return;
		}

		if( !$args && !$args[ 'id' ] ){
			return;
		}

		Kirki::add_panel( $args[ 'id' ], array(
		    'priority'    => $args[ 'priority' ],
		    'title'       => $args[ 'title' ]
		));
	}
}

if( !function_exists( 'evawp_add_sections_and_fields' ) ){
	/**
	 * Adds sections and fields using kriki
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_add_sections_and_fields( $args = false ){
		if( !class_exists( 'Kirki' ) ){
			return;
		}

		if( !$args ){
			return;
		}
		if( isset( $args[ 'section' ] ) ){
			$args_section = array(
			    'title' => $args [ 'section' ][ 'title' ],
			    'priority' => $args [ 'section' ][ 'priority' ],
			);
			if( $args [ 'section' ][ 'panel' ] ){
			    $args_section['panel'] = $args [ 'section' ][ 'panel' ];
			}
			if( isset( $args [ 'section' ][ 'default' ] ) && $args [ 'section' ][ 'default' ] ){
				$section_id = $args[ 'section' ][ 'id' ];
			}else{
				$section_id = EVAWP_PREFIX . '_' . $args[ 'section' ][ 'id' ];
			}
			Kirki::add_section( $section_id , $args_section );

			foreach ( $args[ 'fields' ] as $field_id => $field ) {		
				$field[ 'section' ] = $section_id;
				$field_id = EVAWP_PREFIX . '_' . $field_id;
				$field[ 'settings' ] = $field_id . '_setting';
				Kirki::add_field( $field_id,
					$field
				);
			}
		}
	}
}

if( !function_exists( 'evawp_the_post_per_row_class' ) ){	
	/**
	 * Returns Post per rowe class
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_the_post_per_row_class( $class = '' ){
		$post_per_row = evawp_customizer_get( 'post_per_row', 2 );
		if( '1' == $post_per_row ){
			$class = 'col-md-12';
		}elseif( '2' == $post_per_row ){
			$class = 'col-12 col-sm-6';
		}elseif( '3' == $post_per_row ){
			$class = 'col-12 col-sm-4';
		}elseif( '4' == $post_per_row ){
			$class = 'col-12 col-sm-3';
		}

		echo esc_attr( $class );
	}
}
if( !function_exists( 'evawp_get_comment_number' ) ){
	/**
	 * Returns Number of comment 
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_get_comment_number(){
		if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
			echo '<span class="evawp-comments">';
			comments_popup_link(
				'<i class="far fa-comment"></i> '.esc_html__( 'Leave a comment', 'evawp' ),		
				'<i class="fas fa-comment"></i> '.esc_html__( '1 response', 'evawp' ),
				'<i class="fas fa-comments"></i> % '. esc_html__( 'responses' , 'evawp' )
			);
			echo '</span>';
		}
	}
}

if( !function_exists( 'evawp_get_posts_by_type' ) ){
	/**
	 * Returns post by type
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_get_posts_by_type( $type, $cat_id, $post_to_display = false ){
		$posts = array();
		if( 'post' == $type ){
			$recent_posts = wp_get_recent_posts(array(
			    'numberposts' => $post_to_display ? $post_to_display : -1,
			    'post_status' => 'publish'
			));

			foreach ( $recent_posts as $post) {
				$posts[] = $post[ 'ID' ]; 
			}
		}elseif( 'category' == $type ){			
			$args = array(
				'post_type' => 'post',
				'posts_per_page' => $post_to_display ? $post_to_display : -1,
				'orderby' => 'date',
				'order' => 'DESC',
				'ignore_sticky_posts' => 1,
			);
			if( $cat_id ){
				$args[ 'cat' ] = $cat_id; 
			}

			$query = new WP_Query( $args );
			if ( $query->have_posts() ) {
			    while ( $query->have_posts() ) {
			        $query->the_post();
			        $posts[] = get_the_ID();
			    }
			}
			wp_reset_postdata();					
		}
		if( empty( $posts ) ){
			return false;
		}else{
			return $posts;
		}
	}
}

if( !function_exists( 'evawp_content_meta' ) ){
	/**
	 * Returns Content meta
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_content_meta( $post_id = false, $author = false ){?>
		<div class="evawp-post-meta">
			<?php
				evawp_the_date( $post_id );
				if( $author ){
					evawp_posted_by( $post_id );
				}
				evawp_the_category( $post_id );
				if( !$post_id ){
					evawp_get_comment_number();
				}
			?>
		</div>
	<?php }
}
if( !function_exists( 'evawp_content_navigation' ) ){	
	/**
	 * Pagination for the content seperated by page break.
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_content_navigation(){
		wp_link_pages( array(
			'before'      => '<div class="evawp-page-links">' . esc_html__( 'Pages:', 'evawp' ),
	        'after'       => '</div>',
	        'link_before' => '<span class="evawp-page-number">',
	        'link_after'  => '</span>'
		) );
	}
}

if( !function_exists( 'evawp_the_header_image' ) ){
	/**
	 * Get header image on background
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_the_header_image(){
		if( has_header_image() ){
			$src = get_header_image();
			echo 'style="background-image: url( '. esc_url( $src ) .' )"';
		}
	}
}

if( !function_exists( 'evawp_display_tag_list' ) ){
	/**
	 * Displayes the tag
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_display_tag_list(){
		$tags_list = get_the_tag_list( '', ', ' );
		if ( $tags_list ) {
			printf(
				/* translators: 1: posted in label, only visible to screen readers. 2: list of tags. */
				'<span class="tags-links"><span class="screen-reader-text">%2$s </span>%3$s</span>',
				esc_html__( 'Tags:', 'evawp' ),
				esc_html( $tags_list )
			); // WPCS: XSS OK.
		}
	}
}

if( !function_exists( 'evawp_is_active_plugin' ) ){
	/**
	 * Check plugin installed or not
	 * @since 1.0
	 * @package evaWp
	 */
	function evawp_is_active_plugin( $plugin_name ){
		switch( $plugin_name ){
			case 'woocommerce':
				return class_exists( 'WooCommerce' );
			break;
		}
		return false;
	}
}