<?php

/**
 * Block Filters
 *
 * @package Sophia
 * @since 1.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Modify block content with custom elements and icons
 *
 * @param string $block_content The block content
 * @param array  $block The block data
 * @return string Modified block content
 */
function sophia_block_wrapper($block_content, $block)
{


    // Add footer phone icon
    static $phone_icon_injected = false;
    if (
        !$phone_icon_injected &&
        isset($block['blockName']) &&
        $block['blockName'] === 'core/group' &&
        strpos($block_content, 'wp-block-footer-phone') !== false
    ) {
        $block_content = preg_replace(
            '/(<div[^>]*class="[^"]*\bwp-block-footer-phone\b[^"]*"[^>]*>)/',
            '$1' . sophia_get_svg(array('icon' => 'footer-phone')),
            $block_content,
            1
        );

        $phone_icon_injected = true;
    }

    // Add footer location icon
    static $location_icon_injected = false;
    if (
        !$location_icon_injected &&
        isset($block['blockName']) &&
        $block['blockName'] === 'core/group' &&
        strpos($block_content, 'wp-block-footer-location') !== false
    ) {
        $block_content = preg_replace(
            '/(<div[^>]*class="[^"]*\bwp-block-footer-location\b[^"]*"[^>]*>)/',
            '$1' . sophia_get_svg(array('icon' => 'footer-location')),
            $block_content,
            1
        );

        $location_icon_injected = true;
    }


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

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



    // Add navigation icons on single posts
    if (is_single() && 'core/post-navigation-link' === $block['blockName']) {
        if (isset($block['attrs']['type']) && 'previous' === $block['attrs']['type']) {
            $block_content = str_replace(
                '<span class="post-navigation-link__label">',
                '<span class="post-navigation-link__label">' . sophia_get_svg(array('icon' => 'prev')),
                $block_content
            );
        } else {
            $block_content = str_replace(
                '<span class="post-navigation-link__label">Next Post',
                '<span class="post-navigation-link__label">Next Post' . sophia_get_svg(array('icon' => 'next')),
                $block_content
            );
        }
        return $block_content;
    }

    return $block_content;
}

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