<?php
/**
 * Globinex Blog theme functions.
 *
 * IMPORTANT: This assumes WordPress is installed inside a "blog" subfolder
 * of the main PHP site (e.g. htdocs/globinex/blog). That lets us reuse the
 * main site's css/, js/, and images/ folders directly instead of duplicating
 * them inside the WordPress theme.
 */

// One folder up from /blog = the main PHP site root.
define('GX_MAIN_SITE_URL', dirname(home_url()));

// ---------- Theme setup ----------
function gx_theme_setup() {
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('automatic-feed-links');
    // Matches the blog-image-wrapper / details-hero-img aspect ratios in the design.
    add_image_size('gx-blog-card', 640, 420, true);
    add_image_size('gx-blog-hero', 900, 600, true);
}
add_action('after_setup_theme', 'gx_theme_setup');

// ---------- Enqueue the exact same assets the main PHP site uses ----------
function gx_enqueue_assets() {
    // Bootstrap 5
    wp_enqueue_style('bootstrap', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css', [], '5.3.3');
    // Bootstrap Icons
    wp_enqueue_style('bootstrap-icons', 'https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css', [], '1.11.3');
    // Font Awesome (used by the "Read More" arrows)
    wp_enqueue_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css', [], '6.5.1');
    // Google Fonts (same set as inner pages on the main site)
    wp_enqueue_style('gx-fonts', 'https://fonts.googleapis.com/css2?family=Figtree:wght@300;400;500;600;700;800&family=Rethink+Sans:wght@400;500;600;700&family=Plus+Jakarta+Sans:wght@400;500;600&family=Inter:wght@400;500;600;700&display=swap', [], null);

    // Main site's own stylesheets — reused as-is, not copied.
    wp_enqueue_style('gx-custom', GX_MAIN_SITE_URL . '/css/custom.css', [], '1.0');
    wp_enqueue_style('gx-pages', GX_MAIN_SITE_URL . '/css/pages.css', [], '1.0');

    // Bootstrap bundle JS
    wp_enqueue_script('bootstrap-bundle', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js', [], '5.3.3', true);
    // Main site's nav / scroll-reveal / translator script
    wp_enqueue_script('gx-custom-js', GX_MAIN_SITE_URL . '/js/custom.js', [], '1.0', true);
    // Main site's animated network background script (used on page-hero)
    wp_enqueue_script('gx-pages-js', GX_MAIN_SITE_URL . '/js/pages.js', [], '1.0', true);
}
add_action('wp_enqueue_scripts', 'gx_enqueue_assets');

// ---------- Shorter, cleaner excerpts to match the design's summary style ----------
function gx_excerpt_length($length) {
    return 30;
}
add_filter('excerpt_length', 'gx_excerpt_length');

function gx_excerpt_more($more) {
    return '';
}
add_filter('excerpt_more', 'gx_excerpt_more');

// ---------- Auto-style normal post content to match the design ----------
// Lets writers just type headings/quotes in the WP editor normally —
// this maps them onto the same classes the original static design used.
function gx_style_post_content($content) {
    if (!is_singular('post')) {
        return $content;
    }
    $content = preg_replace('/<h2(\s|>)/i', '<h2 class="details-section-h3"$1', $content);
    $content = preg_replace('/<h3(\s|>)/i', '<h3 class="details-section-h3"$1', $content);
    $content = preg_replace('/<h4(\s|>)/i', '<h4 class="details-subsection-h4"$1', $content);
    $content = preg_replace('/<blockquote(\s|>)/i', '<blockquote class="details-blockquote"$1', $content);
    // Wrap standalone images (not already inside a figure/link) for the inline infographic spacing.
    $content = preg_replace(
        '/<img([^>]+)>/i',
        '<div class="details-content-img-box my-5"><img$1></div>',
        $content
    );
    return $content;
}
add_filter('the_content', 'gx_style_post_content');
