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

function circus_fair_block_wrapper( $circus_fair_block_content, $circus_fair_block ) {

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

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

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

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

	}
    return $circus_fair_block_content;
}
	
add_filter( 'render_block', 'circus_fair_block_wrapper', 10, 2 );
