0) { $filterParams['id_after'] = $id_after; } } if (!empty($params['date_after']) && is_scalar($params['date_after'])) { $filterParams['date_after'] = sanitize_text_field((string) $params['date_after']); } if (!empty($params['date_before']) && is_scalar($params['date_before'])) { $filterParams['date_before'] = sanitize_text_field((string) $params['date_before']); } if (isset($params['stock_qty_min']) && is_numeric($params['stock_qty_min'])) { $filterParams['stock_qty_min'] = (int) $params['stock_qty_min']; } if (isset($params['stock_qty_max']) && is_numeric($params['stock_qty_max'])) { $filterParams['stock_qty_max'] = (int) $params['stock_qty_max']; } if (isset($params['price_min']) && is_numeric($params['price_min'])) { $filterParams['price_min'] = (float) $params['price_min']; } if (isset($params['price_max']) && is_numeric($params['price_max'])) { $filterParams['price_max'] = (float) $params['price_max']; } return $filterParams; } /** * Filtreleme parametrelerini ProductFilterBuilder'a uygular * * @param ProductFilterBuilder $builder Filter builder instance * @param array $params Filtre parametreleri * @return ProductFilterBuilder Parametreleri uygulanmış builder */ public static function applyToBuilder(ProductFilterBuilder $builder, $params) { if (!empty($params['filter_logic'])) { $builder->filterLogic($params['filter_logic']); } if (!empty($params['catalog_visibility'])) { $builder->catalogVisibility($params['catalog_visibility']); } if (isset($params['search']) && is_scalar($params['search']) && '' !== (string) $params['search']) { $builder->search($params['search']); } if (!empty($params['category'])) { $builder->category($params['category']); } if (!empty($params['tag'])) { $builder->tag($params['tag']); } $builder->pagination( isset($params['page']) ? $params['page'] : 1, isset($params['per_page']) ? $params['per_page'] : 10 ); $orderby = isset($params['orderby']) ? $params['orderby'] : 'date'; $order = isset($params['order']) ? strtoupper($params['order']) : 'DESC'; $builder->orderBy($orderby, $order); if (!empty($params['status'])) { $builder->status($params['status']); } if (!empty($params['stock_status'])) { $builder->stockStatus($params['stock_status']); } if (!empty($params['id_after'])) { $builder->idAfter((int) $params['id_after']); } if (!empty($params['descriptionFilters'])) { $builder->contentFilters($params['descriptionFilters']); } else { $hasDescription = isset($params['hasDescription']) ? $params['hasDescription'] : null; $hasShortDescription = isset($params['hasShortDescription']) ? $params['hasShortDescription'] : null; if ($hasDescription !== null || $hasShortDescription !== null) { $builder->descriptions($hasDescription, $hasShortDescription); } } if (isset($params['hasTags'])) { $builder->tags($params['hasTags']); } if (isset($params['has_image'])) { $builder->image($params['has_image']); } if (isset($params['has_gallery'])) { $builder->gallery($params['has_gallery']); } if (isset($params['has_sku'])) { $builder->sku($params['has_sku']); } if (!empty($params['product_type'])) { $builder->productType($params['product_type']); } if (isset($params['featured'])) { $builder->featured($params['featured']); } if (isset($params['on_sale'])) { $builder->onSale($params['on_sale']); } if (!empty($params['ai_processed'])) { $ai_processed_type = !empty($params['ai_processed_type']) ? $params['ai_processed_type'] : null; $builder->aiProcessed( $ai_processed_type, $params['ai_processed'] === 'processed' ? 'exists' : 'not_exists' ); } if (isset($params['date_after']) || isset($params['date_before'])) { $builder->dateRange( isset($params['date_after']) ? (string) $params['date_after'] : '', isset($params['date_before']) ? (string) $params['date_before'] : '' ); } if (isset($params['stock_qty_min']) || isset($params['stock_qty_max'])) { $builder->stockQuantity( isset($params['stock_qty_min']) ? (int) $params['stock_qty_min'] : null, isset($params['stock_qty_max']) ? (int) $params['stock_qty_max'] : null ); } if (isset($params['price_min']) || isset($params['price_max'])) { $builder->priceRange( isset($params['price_min']) ? (float) $params['price_min'] : null, isset($params['price_max']) ? (float) $params['price_max'] : null ); } return $builder; } private static function isValidDescriptionFilter($descriptionFilter): bool { return in_array( $descriptionFilter, ['with_both', 'with_full', 'with_short', 'without_both', 'without_full', 'without_short', 'with_tags', 'without_tags', 'without_seo_meta', 'without_faq'], true ); } /** * Adapts ProductFilterBuilder response to match ProductListingService format * * This ensures backward compatibility when migrating from ProductListingService * to ProductFilterBuilder across different generators. * * ProductFilterBuilder returns: * ['products' => [], 'total' => int, 'total_pages' => int] * * ProductListingService returns: * ['success' => bool, 'products' => [], 'total' => int, 'totalPages' => int, 'page' => int, 'perPage' => int] * * @param array $result Result from ProductFilterBuilder::getProducts() * @param array $params Original filter params (to extract page/per_page) * @param bool $includeFilters Whether to include the filters in response * @return array Adapted response matching ProductListingService format */ public static function adaptResponse(array $result, array $params = [], bool $includeFilters = false): array { $adapted = [ 'success' => !isset($result['error']), 'products' => $result['products'] ?? [], 'total' => $result['total'] ?? 0, 'totalPages' => $result['total_pages'] ?? 0, 'page' => isset($params['page']) ? absint($params['page']) : 1, 'perPage' => isset($params['per_page']) ? absint($params['per_page']) : 10, ]; if (isset($result['error'])) { $adapted['error'] = $result['error']; } if (isset($result['message'])) { $adapted['message'] = $result['message']; } if ($includeFilters) { $adapted['filters'] = $params; } return $adapted; } /** * Gets all product categories for filter dropdowns * * When Polylang is active, delegates language-aware term retrieval * and deduplication to PolylangTermHandler. * * @return array Array of categories with id, name, slug, count */ public static function getCategories(): array { $cache_key = 'aipt_product_filter_categories'; $cached = wp_cache_get($cache_key, 'aipt_products'); if (false !== $cached && is_array($cached)) { return $cached; } $args = [ 'taxonomy' => 'product_cat', 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', ]; PolylangTermHandler::adjustTermQueryArgs($args); $terms = get_terms($args); if (is_wp_error($terms)) { return []; } if (!is_array($terms)) { return []; } $terms = PolylangTermHandler::deduplicateTerms($terms); $categories = []; foreach ($terms as $term) { if (!is_object($term)) { continue; } $categories[] = [ 'id' => $term->term_id, 'name' => html_entity_decode($term->name, ENT_QUOTES, 'UTF-8'), 'slug' => $term->slug, 'count' => $term->count, ]; } wp_cache_set($cache_key, $categories, 'aipt_products', 300); return $categories; } /** * Gets all product tags for filter dropdowns * * When Polylang is active, delegates language-aware term retrieval * and deduplication to PolylangTermHandler. * * @return array Array of tags with id, name, slug, count */ public static function getTags(): array { $cache_key = 'aipt_product_filter_tags'; $cached = wp_cache_get($cache_key, 'aipt_products'); if (false !== $cached && is_array($cached)) { return $cached; } $args = [ 'taxonomy' => 'product_tag', 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', ]; PolylangTermHandler::adjustTermQueryArgs($args); $terms = get_terms($args); if (is_wp_error($terms)) { return []; } if (!is_array($terms)) { return []; } $terms = PolylangTermHandler::deduplicateTerms($terms); $tags = []; foreach ($terms as $term) { if (!is_object($term)) { continue; } $tags[] = [ 'id' => $term->term_id, 'name' => html_entity_decode($term->name, ENT_QUOTES, 'UTF-8'), 'slug' => $term->slug, 'count' => $term->count, ]; } wp_cache_set($cache_key, $tags, 'aipt_products', 300); return $tags; } /** * Register cache invalidation hooks for term changes. * Should be called during plugin initialization. * * @return void */ public static function registerCacheInvalidationHooks(): void { add_action( 'created_product_cat', array( __CLASS__, 'invalidateCategoriesCache' ) ); add_action( 'edited_product_cat', array( __CLASS__, 'invalidateCategoriesCache' ) ); add_action( 'delete_product_cat', array( __CLASS__, 'invalidateCategoriesCache' ) ); add_action( 'created_product_tag', array( __CLASS__, 'invalidateTagsCache' ) ); add_action( 'edited_product_tag', array( __CLASS__, 'invalidateTagsCache' ) ); add_action( 'delete_product_tag', array( __CLASS__, 'invalidateTagsCache' ) ); add_action( 'aipt_content_marked', array( __CLASS__, 'invalidateProductListCache' ) ); } /** * Invalidate categories cache. * * @return void */ public static function invalidateCategoriesCache(): void { wp_cache_delete( 'aipt_product_filter_categories', 'aipt_products' ); } /** * Invalidate tags cache. * * @return void */ public static function invalidateTagsCache(): void { wp_cache_delete( 'aipt_product_filter_tags', 'aipt_products' ); } /** * Per-request guard so a bulk/automation mark run flushes the product-list * group at most once instead of once per marked product. * * @var bool */ private static $productListCacheFlushed = false; /** * Invalidate the filtered product-list result cache after a content mark. * * ProductFilterBuilder::getProducts() caches results under per-user, * per-filter-hash keys that are not enumerable, so the whole aipt_products * group is flushed, and its last_changed version is bumped for backends * that cannot flush a group. The cat/tag dropdown caches share the group and * simply repopulate on next read. * * The aipt_content_marked hook fires once per product, so a bulk or * automation run would otherwise flush the group hundreds of times in one * request — a real cost on persistent object caches. Nothing repopulates the * group mid-run (marking never reads the filtered list), so a per-request * guard safely collapses those to a single flush. PHP statics reset per * request, keeping this correct across requests. * * @return void */ public static function invalidateProductListCache(): void { if ( self::$productListCacheFlushed ) { return; } self::$productListCacheFlushed = true; if ( function_exists( 'wp_cache_flush_group' ) ) { wp_cache_flush_group( 'aipt_products' ); } ProductFilterBuilder::flush_cache(); } /** * Reset the per-request product-list flush guard. * * PHP statics persist across cases within a single PHPUnit process, so unit * tests that assert the flush reset the guard between cases. Harmless in * production, where each request already begins with a fresh static. * * @return void */ public static function resetProductCacheFlushGuard(): void { self::$productListCacheFlushed = false; } }