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

function tailor_stylist_block_wrapper( $tailor_stylist_block_content, $tailor_stylist_block ) {

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

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

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

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

	}
    return $tailor_stylist_block_content;
}
	
add_filter( 'render_block', 'tailor_stylist_block_wrapper', 10, 2 );
