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

function exercise_workout_studio_block_wrapper( $exercise_workout_studio_block_content, $exercise_workout_studio_block ) {

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

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

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

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

	}
    return $exercise_workout_studio_block_content;
}
	
add_filter( 'render_block', 'exercise_workout_studio_block_wrapper', 10, 2 );
