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

function toy_store_fse_block_wrapper( $toy_store_fse_block_content, $toy_store_fse_block ) {

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

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

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

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

	}
    return $toy_store_fse_block_content;
}
	
add_filter( 'render_block', 'toy_store_fse_block_wrapper', 10, 2 );
