<?php
/*-----------------------------------------------*/
/*  EXTHEM.ES
/*  PREMIUM WORDRESS THEMES
/*
/*  STOP DON'T TRY EDIT
/*  IF YOU DON'T KNOW PHP 
/*
/*  As Errors In Your Themes
/*  Are Not The Responsibility
/*  Of The DEVELOPERS
/*  @EXTHEM.ES
/*  
/*  Follow Social Media Exthem.es
/*  Youtube : https://www.youtube.com/channel/UCpcZNXk6ySLtwRSBN6fVyLA
/*  Facebook : https://www.facebook.com/groups/exthem.es
/*  Twitter : https://x.com/bangreyblog
/*  Instagram : https://www.instagram.com/exthem.es/
/*  Telegram : https://t.me/exthemes_helps
/*  TikTok : https://www.tiktok.com/@exthemes
/*	More Premium Themes Visit Now On https://exthem.es/
/*-----------------------------------------------*/
if ( ! defined( 'ABSPATH' ) ) exit; 
// Silence is golden.

if ( ! function_exists( 'cari_app' ) ) 
{
	function cari_app( $clauses, $wp_query ) 
	{
		global $wpdb;

		// Jalankan hanya pada search utama
		if (
			empty( $wp_query->get( 's' ) ) ||
			! $wp_query->is_main_query() ||
			$wp_query->get( 'post_type' ) !== 'post' // ganti CPT jika perlu
		) {
			return $clauses;
		}

		$keyword = $wp_query->get( 's' );
		$like    = '%' . $wpdb->esc_like( $keyword ) . '%';

		// JOIN hanya meta yang diperlukan
		$clauses['join'] .= "
			LEFT JOIN {$wpdb->postmeta} pm_pkg
				ON {$wpdb->posts}.ID = pm_pkg.post_id
				AND pm_pkg.meta_key IN ('wp_GP_ID','_package_name')

			LEFT JOIN {$wpdb->postmeta} pm_ver
				ON {$wpdb->posts}.ID = pm_ver.post_id
				AND pm_ver.meta_key IN ('available_version','wp_version_GP')
		";

		// Bungkus SEMUA kondisi search dalam satu AND (...)
		$clauses['where'] .= $wpdb->prepare(
			"
			AND (
				{$wpdb->posts}.post_title LIKE %s
				OR {$wpdb->posts}.post_content LIKE %s
				OR pm_pkg.meta_value LIKE %s
				OR pm_ver.meta_value LIKE %s
			)
			",
			$like, $like, $like, $like
		);

		// Hindari duplikasi hasil
		$clauses['distinct'] = 'DISTINCT';

		return $clauses;
	}
	// Aktifkan filter
	add_filter( 'posts_clauses', 'cari_app', 10, 2 );
}


