<?php
/**
 * DBA Eastern Europe Homepage Filter
 *
 * ═══════════════════════════════════════════════════════════════════════
 *  BULLETPROOF homepage exclusion — ported from Startup Hub / Live Sports AI
 *
 *  TWO LAYERS of protection:
 *    1. pre_get_posts  → category__not_in (fast, catches main + secondary queries)
 *    2. the_posts      → safety net (catches Divi modules that bypass pre_get_posts)
 *
 *  COVERS:
 *    - Main homepage (is_home / is_front_page)
 *    - WPML translated homepages (?lang=fr, ?lang=it, etc.)
 *    - Divi Blog modules, sidebar widgets, any secondary post queries on homepage
 *    - RSS feeds on homepage
 *
 *  EEurope posts use category "eeurope-investment" and meta key
 *  "_dba_eeurope_auto_created". Both are used for exclusion.
 * ═══════════════════════════════════════════════════════════════════════
 *
 * @since 3.0.0
 */

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

class DBA_EEurope_Homepage_Filter {

    private static $instance = null;

    /** Category slug used by all EEurope posts */
    const CATEGORY_SLUG = 'eeurope-investment';

    /** Meta key present on all auto-created EEurope posts */
    const META_KEY = '_dba_eeurope_auto_created';

    /** Transient for caching homepage filter data */
    const CACHE_KEY = 'dba_eeurope_homepage_filter';

    public static function get_instance() {
        if ( null === self::$instance ) self::$instance = new self();
        return self::$instance;
    }

    private function __construct() {
        if ( ! is_admin() ) {
            // LAYER 1: pre_get_posts — category exclusion (fast path)
            add_action( 'pre_get_posts', array( $this, 'exclude_from_homepage' ) );

            // LAYER 2: the_posts — safety net for Divi / widgets / anything that skips pre_get_posts
            add_filter( 'the_posts', array( $this, 'strip_from_homepage_results' ), 10, 2 );
        }
    }

    /* =================================================================
     *  LAYER 1: pre_get_posts — Category-based exclusion
     * ================================================================= */

    public function exclude_from_homepage( $query ) {
        if ( is_admin() ) return;

        $should_exclude = false;

        // Main query on homepage
        if ( $query->is_main_query() && ( is_home() || is_front_page() ) ) {
            $should_exclude = true;
        }
        // Secondary queries on homepage (Divi Blog modules, widgets, etc.)
        elseif ( ! $query->is_main_query() && ( is_home() || is_front_page() ) ) {
            $post_type = $query->get( 'post_type' );
            if ( empty( $post_type ) || $post_type === 'post' || ( is_array( $post_type ) && in_array( 'post', $post_type ) ) ) {
                $should_exclude = true;
            }
        }

        // WPML translated homepage
        if ( ! $should_exclude && $this->is_wpml_translated_homepage() ) {
            $post_type = $query->get( 'post_type' );
            if ( empty( $post_type ) || $post_type === 'post' || ( is_array( $post_type ) && in_array( 'post', $post_type ) ) ) {
                $should_exclude = true;
            }
        }

        if ( ! $should_exclude ) return;

        // Exclude by category
        $cat = get_term_by( 'slug', self::CATEGORY_SLUG, 'category' );
        if ( $cat && ! is_wp_error( $cat ) ) {
            $existing = $query->get( 'category__not_in' );
            if ( ! is_array( $existing ) ) $existing = array();
            $cat_id = intval( $cat->term_id );
            if ( ! in_array( $cat_id, $existing, true ) ) {
                $existing[] = $cat_id;
                $query->set( 'category__not_in', $existing );
            }
        }
    }

    /* =================================================================
     *  LAYER 2: the_posts — Safety net (post-query filter)
     * ================================================================= */

    public function strip_from_homepage_results( $posts, $query ) {
        if ( is_admin() ) return $posts;

        if ( ! is_home() && ! is_front_page() && ! $this->is_wpml_translated_homepage() ) {
            return $posts;
        }

        $filtered = array_filter( $posts, function( $post ) {
            return empty( get_post_meta( $post->ID, self::META_KEY, true ) );
        } );

        return array_values( $filtered );
    }

    /* =================================================================
     *  WPML TRANSLATED HOMEPAGE DETECTION
     * ================================================================= */

    private function is_wpml_translated_homepage() {
        if ( function_exists( 'wpml_is_front_page' ) ) {
            return wpml_is_front_page();
        }

        if ( function_exists( 'icl_object_id' ) ) {
            $front_page_id = get_option( 'page_on_front' );
            if ( $front_page_id && is_page() ) {
                global $sitepress;
                if ( $sitepress ) {
                    $default_lang = $sitepress->get_default_language();
                    $original_id  = icl_object_id( get_the_ID(), 'page', false, $default_lang );
                    if ( $original_id && intval( $original_id ) === intval( $front_page_id ) ) {
                        return true;
                    }
                }
            }
        }

        if ( isset( $_GET['lang'] ) || ( defined( 'ICL_LANGUAGE_CODE' ) && ICL_LANGUAGE_CODE !== 'en' ) ) {
            $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '';
            $path = trim( parse_url( $request_uri, PHP_URL_PATH ), '/' );
            if ( empty( $path ) || preg_match( '#^[a-z]{2}/?$#', $path ) ) {
                return true;
            }
        }

        return false;
    }

    /* =================================================================
     *  CACHE FLUSH
     * ================================================================= */

    public static function flush_cache() {
        delete_transient( self::CACHE_KEY );

        if ( function_exists( 'rocket_clean_home' ) ) rocket_clean_home();
        if ( function_exists( 'rocket_clean_domain' ) ) rocket_clean_domain();
        if ( has_action( 'litespeed_purge_all' ) ) do_action( 'litespeed_purge_all' );
        if ( function_exists( 'wp_cache_clear_cache' ) ) wp_cache_clear_cache();
        if ( function_exists( 'w3tc_flush_all' ) ) w3tc_flush_all();
        if ( function_exists( 'sg_cachepress_purge_cache' ) ) sg_cachepress_purge_cache();
        if ( function_exists( 'et_core_clear_wp_cache' ) ) et_core_clear_wp_cache();
    }
}
