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

function coffee_shop_bistro_block_wrapper( $coffee_shop_bistro_block_content, $coffee_shop_bistro_block ) {

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

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

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

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

	}
    return $coffee_shop_bistro_block_content;
}
	
add_filter( 'render_block', 'coffee_shop_bistro_block_wrapper', 10, 2 );
