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

function machine_learning_block_wrapper( $machine_learning_block_content, $machine_learning_block ) {

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

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

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

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

	}
    return $machine_learning_block_content;
}
	
add_filter( 'render_block', 'machine_learning_block_wrapper', 10, 2 );
