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

function building_renovation_block_wrapper( $building_renovation_block_content, $building_renovation_block ) {

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

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

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

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

	}
    return $building_renovation_block_content;
}
	
add_filter( 'render_block', 'building_renovation_block_wrapper', 10, 2 );
