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

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Add theme icons to selected core blocks.
 *
 * @since 1.0
 *
 * @param string $block_content The block content.
 * @param array  $block         The parsed block.
 * @return string The filtered block content.
 */
function euphony_blocks_block_wrapper( $block_content, $block ) {

	if ( empty( $block['blockName'] ) ) {
		return $block_content;
	}

	$attrs = isset( $block['attrs'] ) && is_array( $block['attrs'] ) ? $block['attrs'] : array();

	if ( 'core/button' === $block['blockName'] ) {

		// strpos() returns 0 when the class leads the string, so compare against false.
		if ( isset( $attrs['className'] ) && false !== strpos( $attrs['className'], 'has-arrow' ) ) {
			return str_replace(
				'</a>',
				euphony_blocks_get_svg( array( 'icon' => 'caret-circle-right' ) ) . '</a>',
				$block_content
			);
		}

		return $block_content;
	}

	if ( ! is_single() ) {

		if ( 'core/post-terms' === $block['blockName'] ) {
			$term = isset( $attrs['term'] ) ? $attrs['term'] : '';

			if ( 'post_tag' === $term ) {
				$block_content = str_replace(
					'<div class="taxonomy-post_tag wp-block-post-terms">',
					'<div class="taxonomy-post_tag wp-block-post-terms flex">' . euphony_blocks_get_svg( array( 'icon' => 'tags' ) ),
					$block_content
				);
			} elseif ( 'category' === $term ) {
				$block_content = str_replace(
					'<div class="taxonomy-category wp-block-post-terms">',
					'<div class="taxonomy-category wp-block-post-terms flex">' . euphony_blocks_get_svg( array( 'icon' => 'category' ) ),
					$block_content
				);
			}

			return $block_content;
		}

		if ( 'core/post-date' === $block['blockName'] ) {
			return str_replace(
				'<div class="wp-block-post-date">',
				'<div class="wp-block-post-date flex">' . euphony_blocks_get_svg( array( 'icon' => 'calendar' ) ),
				$block_content
			);
		}

		if ( 'core/post-author' === $block['blockName'] ) {
			return str_replace(
				'<div class="wp-block-post-author">',
				'<div class="wp-block-post-author flex">' . euphony_blocks_get_svg( array( 'icon' => 'user' ) ),
				$block_content
			);
		}

		return $block_content;
	}

	// Add chevron icons to the post navigation links.
	if ( 'core/post-navigation-link' === $block['blockName'] ) {

		if ( isset( $attrs['type'] ) && 'previous' === $attrs['type'] ) {
			return str_replace(
				'<span class="post-navigation-link__label">',
				'<span class="post-navigation-link__label">' . euphony_blocks_get_svg( array( 'icon' => 'prev' ) ),
				$block_content
			);
		}

		/*
		 * Append the icon to the end of the label instead of matching the literal string
		 * "Next Post", which never matches on a translated site or a customised label.
		 */
		$icon = euphony_blocks_get_svg( array( 'icon' => 'next' ) );

		return preg_replace_callback(
			'#(<span class="post-navigation-link__label">.*?)(</span>)#s',
			static function ( $matches ) use ( $icon ) {
				return $matches[1] . $icon . $matches[2];
			},
			$block_content,
			1
		);
	}

	return $block_content;
}

add_filter( 'render_block', 'euphony_blocks_block_wrapper', 10, 2 );
