<?php
/**
 * Block Filters
 *
 * @package bio_energy
 * @since 1.0
 */

function bio_energy_block_wrapper( $bio_energy_block_content, $bio_energy_block ) {

	if ( 'core/button' === $bio_energy_block['blockName'] ) {
		
		if( isset( $bio_energy_block['attrs']['className'] ) && strpos( $bio_energy_block['attrs']['className'], 'has-arrow' ) ) {
			$bio_energy_block_content = str_replace( '</a>', bio_energy_get_svg( array( 'icon' => esc_attr( 'caret-circle-right' ) ) ) . '</a>', $bio_energy_block_content );
			return $bio_energy_block_content;
		}
	}

	if( ! is_single() ) {
	
		if ( 'core/post-terms'  === $bio_energy_block['blockName'] ) {
			if( 'post_tag' === $bio_energy_block['attrs']['term'] ) {
				$bio_energy_block_content = str_replace( '<div class="taxonomy-post_tag wp-block-post-terms">', '<div class="taxonomy-post_tag wp-block-post-terms flex">' . bio_energy_get_svg( array( 'icon' => esc_attr( 'tags' ) ) ), $bio_energy_block_content );
			}

			if( 'category' ===  $bio_energy_block['attrs']['term'] ) {
				$bio_energy_block_content = str_replace( '<div class="taxonomy-category wp-block-post-terms">', '<div class="taxonomy-category wp-block-post-terms flex">' . bio_energy_get_svg( array( 'icon' => esc_attr( 'category' ) ) ), $bio_energy_block_content );
			}
			return $bio_energy_block_content;
		}
		if ( 'core/post-date' === $bio_energy_block['blockName'] ) {
			$bio_energy_block_content = str_replace( '<div class="wp-block-post-date">', '<div class="wp-block-post-date flex">' . bio_energy_get_svg( array( 'icon' => esc_attr( 'calendar' ) ) ), $bio_energy_block_content );
			return $bio_energy_block_content;
		}
		if ( 'core/post-author' === $bio_energy_block['blockName'] ) {
			$bio_energy_block_content = str_replace( '<div class="wp-block-post-author">', '<div class="wp-block-post-author flex">' . bio_energy_get_svg( array( 'icon' => esc_attr( 'user' ) ) ), $bio_energy_block_content );
			return $bio_energy_block_content;
		}
	}
	if( is_single() ){

		// Add chevron icon to the navigations
		if ( 'core/post-navigation-link' === $bio_energy_block['blockName'] ) {
			if( isset( $bio_energy_block['attrs']['type'] ) && 'previous' === $bio_energy_block['attrs']['type'] ) {
				$bio_energy_block_content = str_replace( '<span class="post-navigation-link__label">', '<span class="post-navigation-link__label">' . bio_energy_get_svg( array( 'icon' => esc_attr( 'prev' ) ) ), $bio_energy_block_content );
			}
			else {
				$bio_energy_block_content = str_replace( '<span class="post-navigation-link__label">Next Post', '<span class="post-navigation-link__label">Next Post' . bio_energy_get_svg( array( 'icon' => esc_attr( 'next' ) ) ), $bio_energy_block_content );
			}
			return $bio_energy_block_content;
		}
		if ( 'core/post-date' === $bio_energy_block['blockName'] ) {
            $bio_energy_block_content = str_replace( '<div class="wp-block-post-date">', '<div class="wp-block-post-date flex">' . bio_energy_get_svg( array( 'icon' => 'calendar' ) ), $bio_energy_block_content );
            return $bio_energy_block_content;
        }
		if ( 'core/post-author' === $bio_energy_block['blockName'] ) {
            $bio_energy_block_content = str_replace( '<div class="wp-block-post-author">', '<div class="wp-block-post-author flex">' . bio_energy_get_svg( array( 'icon' => 'user' ) ), $bio_energy_block_content );
            return $bio_energy_block_content;
        }

	}
    return $bio_energy_block_content;
}
	
add_filter( 'render_block', 'bio_energy_block_wrapper', 10, 2 );
