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

function cafe_eatery_block_wrapper( $cafe_eatery_block_content, $cafe_eatery_block ) {

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

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

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

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

	}
    return $cafe_eatery_block_content;
}
	
add_filter( 'render_block', 'cafe_eatery_block_wrapper', 10, 2 );
