<?php
/**
 * Block Filters
 *
 * @package project-manager
 * @since 1.0
 */

function project_manager_block_wrapper( $project_manager_block_content, $project_manager_block ) {

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

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

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

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

	}
    return $project_manager_block_content;
}
	
add_filter( 'render_block', 'project_manager_block_wrapper', 10, 2 );
