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

function commercial_cleaning_block_wrapper( $commercial_cleaning_block_content, $commercial_cleaning_block ) {

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

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

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

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

	}
    return $commercial_cleaning_block_content;
}
	
add_filter( 'render_block', 'commercial_cleaning_block_wrapper', 10, 2 );
