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

function gemstone_jewelry_block_wrapper( $gemstone_jewelry_block_content, $gemstone_jewelry_block ) {

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

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

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

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

	}
    return $gemstone_jewelry_block_content;
}
	
add_filter( 'render_block', 'gemstone_jewelry_block_wrapper', 10, 2 );
