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

function magazine_blocks_block_wrapper( $magazine_blocks_block_content, $magazine_blocks_block ) {

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

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

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

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

	}
    return $magazine_blocks_block_content;
}
	
add_filter( 'render_block', 'magazine_blocks_block_wrapper', 10, 2 );
