<?php
/**
 * Features that improve the theme by integrating with WordPress
 *
 * @package Pixel Transport
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function pixel_transport_body_classes( $classes ) {
	// Adds a class of theme skin
	if( get_theme_mod( 'skin_select', 'default' ) == 'dark' ){
		$classes[] = 'dark-skin';
	}elseif( get_theme_mod( 'skin_select', 'default' ) == 'blackwhite' ){
		$classes[] = 'black-white-skin';
	}else{
		$classes[] = 'default-skin';
	}

	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

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

/**
 * Page/Post title in frontpage and blog
 */
function pixel_transport_page_title_display() {
	if ( is_singular() || ( !is_home() && is_front_page() ) ): ?>
		<h1 class="page-title entry-title"><?php single_post_title(); ?></h1>
	<?php elseif ( is_archive() ) : 
		the_archive_title( '<h1 class="page-title">', '</h1>' );
	elseif ( is_search() ) : ?>
		<h1 class="page-title entry-title">
			<?php // translators: %s is the search query
		 		printf( esc_html__( 'Search Results for: %s', 'pixel-transport' ), get_search_query() ); 
			?>
		</h1>
	<?php elseif ( is_404() ) :
		echo '<h1 class="page-title entry-title">' . esc_html__( 'Oops! That page can&#39;t be found.', 'pixel-transport' ) . '</h1>';
	endif;
}

/**
 * Adds custom size in images and fallback to a default image if no featured image is available
 */
function pixel_transport_image_size( $pixel_transport_image_size ) {
    // Get the post's featured image ID
    $pixel_transport_image_id = get_post_thumbnail_id();

    // If a featured image is set, use it, otherwise fallback to a default image
    if ( $pixel_transport_image_id ) {
        // Get the alt text for the featured image
        $pixel_transport_alt_text = esc_attr( get_post_meta( $pixel_transport_image_id, '_wp_attachment_image_alt', true ) );
        
        // Display the post thumbnail
        the_post_thumbnail( $pixel_transport_image_size, array( 'alt' => $pixel_transport_alt_text ) );
    } else {
        // Get the default image URL from the theme's 'images' folder
        $pixel_transport_default_image_url = get_template_directory_uri() . '/assets/images/default-posts-image.png'; // Adjust the path as necessary
        
        // Display the default image with proper escaping
        echo '<img src="' . esc_url( $pixel_transport_default_image_url ) . '" alt="' . esc_attr__( 'Default Image', 'pixel-transport' ) . '" />';
    }
}

/* Breadcrumb Begin */
function pixel_transport_breadcrumb_function() {
	if (!is_home()) {
		echo '<a href="';
			echo esc_url( home_url() );
		echo '">';
			bloginfo('name');
		echo "</a> ";
		if (is_category() || is_single()) {
			the_category(',');
			if (is_single()) {
				echo "<span> ";
					the_title();
				echo "</span> ";
			}
		} elseif (is_page()) {
			echo "<span> ";
				the_title();
		}
	}
}

/**
* Adds a submit button in search form
* 
* @since Pixel Transport 1.0.0
* @param string $form
* @return string
*/
function pixel_transport_modify_search_form( $form ){
	return str_replace( '</form>', '<button type="submit" class="search-button"><span class="fas fa-search"></span></button></form>', $form );
}
add_filter( 'get_search_form', 'pixel_transport_modify_search_form' );

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function pixel_transport_pingback_header() {
	if ( is_singular() && pings_open() ) {
		printf( '<link rel="pingback" href="%s">', esc_url( get_bloginfo( 'pingback_url' ) ) );
	}
}
add_action( 'wp_head', 'pixel_transport_pingback_header' );

/**
* Add a class in body
*
* @since Pixel Transport 1.0.0
* @param array $class
* @return array $class
*/
function pixel_transport_body_class_modification( $class ){
	
	// Site Dark Mode
	if( !get_theme_mod( 'disable_dark_mode', true ) ){
		$class[] = 'dark-mode';
	}

	// Site Layouts
	if( get_theme_mod( 'site_layout', 'default' ) == 'default' ){
		$class[] = 'site-layout-default';
	}else if( get_theme_mod( 'site_layout', 'default' ) == 'box' ){
		$class[] = 'site-layout-box';
	}else if( get_theme_mod( 'site_layout', 'default' ) == 'frame' ){
		$class[] = 'site-layout-frame';
	}else if( get_theme_mod( 'site_layout', 'default' ) == 'full' ){
		$class[] = 'site-layout-full';
	}else if( get_theme_mod( 'site_layout', 'default' ) == 'extend' ){
		$class[] = 'site-layout-extend';
	}

	return $class;
}
add_filter( 'body_class', 'pixel_transport_body_class_modification' );

/**
 * 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 pixel_transport_content_width() {
	// This variable is intended to be overruled from themes.
	// Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$GLOBALS['content_width'] = apply_filters( 'pixel_transport_content_width', 720 );
}
add_action( 'after_setup_theme', 'pixel_transport_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 * @since Pixel Transport 1.0.0
 */
function pixel_transport_widgets_init() {

	register_sidebar( array(
		'name'          => esc_html__( 'Left Sidebar', 'pixel-transport' ),
		'id'            => 'sidebar-1',
		'description'   => esc_html__( 'Add widgets here.', 'pixel-transport' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<div class="widget-title-wrap"><h2 class="widget-title">',
		'after_title'   => '</h2></div>',
	) );

	for( $i = 1; $i <= 4; $i++ ){
		register_sidebar( array(
			'name'          => esc_html__( 'Footer Sidebar', 'pixel-transport' ) . ' ' . $i,
			'id'            => 'footer-sidebar-' . $i,
			'description'   => esc_html__( 'Add widgets here.', 'pixel-transport' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="footer-item">',
			'after_widget'  => '</div></div>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );
	}
}
add_action( 'widgets_init', 'pixel_transport_widgets_init' );


if( ! function_exists( 'pixel_transport_sort_category' ) ):
/**
 * Helper function for pixel_transport_get_the_category()
 *
 * @since Pixel Transport 1.0.0
 */
function pixel_transport_sort_category( $a, $b ){
    return $a->term_id < $b->term_id;
}
endif;

/**
 * Validation functions
 *
 * @package Pixel Transport
 */

if ( ! function_exists( 'pixel_transport_validate_excerpt_count' ) ) :
	/**
	 * Check if the input value is valid integer.
	 *
	 * @param WP_Customize_Control $control WP_Customize_Control instance.
	 * @return string Whether the value is valid to the current preview.
	 */
	function pixel_transport_validate_excerpt_count( $validity, $value ){
		$value = intval( $value );
		if ( empty( $value ) || ! is_numeric( $value ) ) {
			$validity->add( 'required', esc_html__( 'You must supply a valid number.', 'pixel-transport' ) );
		} elseif ( $value < 1 ) {
			$validity->add( 'min_slider', esc_html__( 'Minimum no of Excerpt Lenght is 1', 'pixel-transport' ) );
		} elseif ( $value > 50 ) {
			$validity->add( 'max_slider', esc_html__( 'Maximum no of Excerpt Lenght is 50', 'pixel-transport' ) );
		}
		return $validity;
	}
endif;

/**
 * To disable archive prefix title.
 * @since Pixel Transport 1.0.0
 */

function pixel_transport_modify_archive_title( $title ) {
	if( is_category() ) {
		$title = single_cat_title( '', false );
	} elseif ( is_tag() ) {
		$title = single_tag_title( '', false );
    } elseif ( is_author() ) {
		$title = '<span class="vcard">' . get_the_author() . '</span>' ;
    } elseif ( is_year() ) {
        $title = get_the_date( _x( 'Y', 'yearly archives date format', 'pixel-transport' ) );
    } elseif ( is_month() ) {
        $title = get_the_date( _x( 'F Y', 'monthly archives date format', 'pixel-transport' ) );
    } elseif ( is_day() ) {
        $title = get_the_date( _x( 'F j, Y', 'daily archives date format', 'pixel-transport' ) );
     } elseif ( is_post_type_archive() ) {
        $title = post_type_archive_title( '', false );
    } elseif ( is_tax() ) {
        $title = single_term_title( '', false );
    }

	return $title;
}

add_filter( 'get_the_archive_title', 'pixel_transport_modify_archive_title' );

if( ! function_exists( 'pixel_transport_get_the_category' ) ):
/**
* Returns categories after sorting by term id descending
* 
* @since Pixel Transport 1.0.0
* @uses pixel_transport_sort_category()
* @return array
*/
function pixel_transport_get_the_category( $id = false ){
    $failed = true;

    if( !$id ){
        $id = get_the_id();
    }
    
    # Check if Yoast Plugin is installed 
    # If yes then, get Primary category, set by Plugin

    if ( class_exists( 'WPSEO_Primary_Term' ) ){

        # Show the post's 'Primary' category, if this Yoast feature is available, & one is set
        $wpseo_primary_term = new WPSEO_Primary_Term( 'category', $id );
        $wpseo_primary_term = $wpseo_primary_term->get_primary_term();

        $pixel_transport_cat[0] = get_term( $wpseo_primary_term );

        if ( !is_wp_error( $pixel_transport_cat[0] ) ) { 
           $failed = false;
        }
    }

    if( $failed ){

      $pixel_transport_cat = get_the_category( $id );
      usort( $pixel_transport_cat, 'pixel_transport_sort_category' );  
    }
    
    return $pixel_transport_cat;
}

endif;

/**
* Get post categoriesby by term id
* 
* @since Pixel Transport 1.0.0
* @uses pixel_transport_get_post_categories()
* @return array
*/
function pixel_transport_get_post_categories(){

	$terms = get_terms( array(
	    'taxonomy' => 'category',
	    'hide_empty' => true,
	) );

	if( empty($terms) || !is_array( $terms ) ){
		return array();
	}

	$data = array();
	foreach ( $terms as $key => $value) {
		$term_id = absint( $value->term_id );
		$data[$term_id] =  esc_html( $value->name );
	}
	return $data;

}

/**
* Get Custom Logo URL
* 
* @since Pixel Transport 1.0.0
*/
function pixel_transport_get_custom_logo_url(){
    $custom_logo_id = get_theme_mod( 'custom_logo' );
    $image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
    if ( is_array($image) ){
	    return $image[0];
	}else{
		return '';
	}
}

/**
* Add a footer image
* @since Pixel Transport 1.0.0
*/
function pixel_transport_footer_image(){
	$render_bottom_footer_image_size 	= get_theme_mod( 'render_bottom_footer_image_size', 'full' );
	$bottom_footer_image_id 			= get_theme_mod( 'bottom_footer_image', '' );
	$get_bottom_footer_image_array 		= wp_get_attachment_image_src( $bottom_footer_image_id, $render_bottom_footer_image_size );
	if( is_array( $get_bottom_footer_image_array ) ){
		$bottom_footer_image = $get_bottom_footer_image_array[0];
	}else{
		$bottom_footer_image = '';
	}
	$alt = get_post_meta( get_theme_mod( 'bottom_footer_image', '' ), '_wp_attachment_image_alt', true );
	if ( $bottom_footer_image ){
		$bottom_footer_image_target = get_theme_mod( 'bottom_footer_image_target', true );
		$link_target = '';
		if( $bottom_footer_image_target ){
			$link_target = '_blank';
		}
 	?>
	 	<div class="bottom-footer-image-wrap">
	 		<a href="<?php echo esc_url( get_theme_mod( 'bottom_footer_image_link', '' ) ); ?>" alt="<?php echo esc_attr($alt); ?>" target="<?php echo esc_attr( $link_target ); ?>">
	 			<img src="<?php echo esc_url( $bottom_footer_image ); ?>">
	 		</a>
	 	</div>
	<?php
	}
}

if( !function_exists( 'pixel_transport_has_header_media' ) ){
	/**
	* Check if header media slider item is empty.
	* 
	* @since Pixel Transport 1.2.4
	* @return bool
	*/
	function pixel_transport_has_header_media(){
		$header_slider_defaults = array(
			array(
				'slider_item' 	=> '',
			)			
		);
		$header_image_slider = get_theme_mod( 'header_image_slider', $header_slider_defaults );
		$has_header_media = false;
		if ( is_array( $header_image_slider ) ){
			foreach( $header_image_slider as $value ){
				if( !empty( $value['slider_item'] ) ){
					$has_header_media = true;
					break;
				}
			}
		}
		return $has_header_media;
	}
}

if( !function_exists( 'pixel_transport_header_media' ) ){
	/**
	* Add header banner/slider.
	* 
	* @since Pixel Transport 1.2.4
	*/
	function pixel_transport_header_media(){
		$header_slider_defaults = array(
			array(
				'slider_item' 	=> '',
			)			
		);
		$header_image_slider = get_theme_mod( 'header_image_slider', $header_slider_defaults ); ?>
		<div class="header-image-slider">
		    <?php
		    $render_header_image_size = get_theme_mod( 'render_header_image_size', 'full' ); 
		    foreach( $header_image_slider as $slider_item ) :
		    	if( wp_attachment_is_image( $slider_item['slider_item'] ) ){
	    			$get_header_image_array = wp_get_attachment_image_src( $slider_item['slider_item'], $render_header_image_size );
	    			if( is_array( $get_header_image_array ) ){
	    				$header_image_url = $get_header_image_array[0];
	    			}else{
	    				$header_image_url = '';
	    			}
		    	}else{
		    		if( empty( $slider_item['slider_item'] ) ){
	    				$header_image_url = '';
	    			}else{
	    				$header_image_url = $slider_item['slider_item'];
	    			}
		    	} ?>
		    	<div class="header-slide-item" style="background-image: url( <?php echo esc_url( $header_image_url ); ?> )">
		    		<div class="slider-inner"></div>
		      </div>
		    <?php endforeach; ?>
		</div>
		<?php if( !get_theme_mod( 'disable_header_slider_arrows', false ) ) { ?>
			<ul class="slick-control">
		        <li class="header-slider-prev">
		        	<span></span>
		        </li>
		        <li class="header-slider-next">
		        	<span></span>
		        </li>
		    </ul>
		<?php }
		if ( !get_theme_mod( 'disable_header_slider_dots', false ) ){ ?>
			<div class="header-slider-dots"></div>
		<?php }
	}
}

if( !function_exists( 'pixel_transport_get_intermediate_image_sizes' ) ){
	/**
	* Array of image sizes.
	* 
	* @since Pixel Transport 1.3.6
	* @return array
	*/
	function pixel_transport_get_intermediate_image_sizes(){

		$data 	= array(
			'full'			=> esc_html__( 'Full Size', 'pixel-transport' ),
			'large'			=> esc_html__( 'Large Size', 'pixel-transport' ),
			'medium'		=> esc_html__( 'Medium Size', 'pixel-transport' ),
			'medium_large'	=> esc_html__( 'Medium Large Size', 'pixel-transport' ),
			'thumbnail'		=> esc_html__( 'Thumbnail Size', 'pixel-transport' ),
			'1536x1536'		=> esc_html__( '1536x1536 Size', 'pixel-transport' ),
			'2048x2048'		=> esc_html__( '2048x2048 Size', 'pixel-transport' ),
			'pixel-transport-1920-550' => esc_html__( '1920x550 Size', 'pixel-transport' ),
			'pixel-transport-1370-550'	=> esc_html__( '1370x550 Size', 'pixel-transport' ),
			'pixel-transport-590-310'	=> esc_html__( '590x310 Size', 'pixel-transport' ),
			'pixel-transport-420-380'	=> esc_html__( '420x380 Size', 'pixel-transport' ),
			'pixel-transport-420-300'	=> esc_html__( '420x300 Size', 'pixel-transport' ),
			'pixel-transport-420-200'	=> esc_html__( '420x200 Size', 'pixel-transport' ),
			'pixel-transport-290-150'	=> esc_html__( '290x150 Size', 'pixel-transport' ),
			'pixel-transport-80-60'	=> esc_html__( '80x60 Size', 'pixel-transport' ),
		);
		
		return $data;

	}
}