/*
Theme Name: Hub Child
Theme URI: http://hub.liquid-themes.com/
Author: Liquid Themes
Author URI: https://themeforest.net/user/liquidthemes
Template: hub
Description: Smart, Powerful and Unlimited Customizable WordPress Theme.
Version: 1.0
License: GNU General Public License
License URI: license.txt
Text Domain: hub-child
Tags: responsive, retina, rtl-language-support, blog, portfolio, custom-colors, live-editor
*/

<?php
/**
 * Force clean Open Graph tags for iMessage and social previews
 * Eniw Wyne custom setup
 */

function force_featured_image_or_default_for_og() {
    if (is_single() || is_page()) {
        global $post;

        // Try to use the featured image
        $og_image = get_the_post_thumbnail_url($post->ID, 'full');

        // Fallback image if no featured image found
        if (!$og_image) {
            $og_image = 'https://eniwwyne.com/wp-content/uploads/2025/10/eniw-featured-s1.jpg';
        }

        // Replace .webp with .jpg (iMessage preview fix)
        if (stripos($og_image, '.webp') !== false) {
            $og_image = preg_replace('/\.webp$/i', '.jpg', $og_image);
        }

        // Title and URL
        $og_title = get_the_title($post->ID);
        $og_url   = get_permalink($post->ID);

        // Description: use post excerpt, or fallback to brand default
        $og_desc = get_the_excerpt($post->ID);
        if (empty($og_desc)) {
            $og_desc = "ENIW — We provide private, one-of-a-kind event & farm-stay management services at the most stunning properties in country!";
        }

        // Output tags early in <head>
        echo "\n<!-- Forced OG tags for iMessage / social preview -->\n";
        echo '<meta property="og:title" content="' . esc_attr($og_title) . '">' . "\n";
        echo '<meta property="og:description" content="' . esc_attr($og_desc) . '">' . "\n";
        echo '<meta property="og:image" content="' . esc_url($og_image) . '">' . "\n";
        echo '<meta property="og:url" content="' . esc_url($og_url) . '">' . "\n";
        echo '<meta property="og:type" content="website">' . "\n";
        echo "\n<!-- /Forced OG tags -->\n";
    }
}
add_action('wp_head', 'force_featured_image_or_default_for_og', 1);
