queryArgs($view_config); if (!count($query_args)) return; $ids = get_posts($query_args); foreach ($ids as $id) { try { $offering = new OfferingEntity((int) $id, Config::general()); Adapter::get($offering)->upcoming(clone $target, 1); } catch (Exception $e) { continue; } } } /** * @param mixed[] $view_config * @return mixed[] */ private function queryArgs(array $view_config): array { $filters = isset($view_config['filters']) && is_array($view_config['filters']) ? $view_config['filters'] : []; $meta_query = [ [ 'key' => 'daytidy_freq', 'value' => RepeatTypes::CUSTOM, 'compare' => '!=', ], [ 'relation' => 'OR', [ 'key' => OfferingVisibility::META, 'compare' => 'NOT EXISTS', ], [ 'key' => OfferingVisibility::META, 'value' => '0', 'compare' => '!=', ], ], ]; if (!$this->repeatMetaQuery($meta_query, $filters['repeat_types'] ?? [])) return []; $args = [ 'post_type' => Offering::PT, 'post_status' => 'publish', 'fields' => 'ids', 'posts_per_page' => -1, 'no_found_rows' => true, 'meta_query' => $meta_query, ]; $this->taxonomyQuery($args, $filters['taxonomies'] ?? []); $this->authorQuery($args, $filters['users'] ?? []); $this->customFieldMetaQuery($args, $filters['custom_fields'] ?? []); return $args; } /** * @param mixed[] $meta_query * @param mixed $filter */ private function repeatMetaQuery(array &$meta_query, $filter): bool { if (!is_array($filter)) return true; $raw_include = isset($filter['include']) && is_array($filter['include']) ? $filter['include'] : []; $include = array_diff($this->strings($raw_include), [RepeatTypes::CUSTOM]); $exclude = array_diff($this->strings($filter['exclude'] ?? []), [RepeatTypes::CUSTOM]); if (count($raw_include) && !count($include)) return false; if (count($include)) { $meta_query[] = [ 'key' => 'daytidy_freq', 'value' => array_values($include), 'compare' => 'IN', ]; } if (count($exclude)) { $meta_query[] = [ 'key' => 'daytidy_freq', 'value' => array_values($exclude), 'compare' => 'NOT IN', ]; } return true; } /** * @param mixed[] $args * @param mixed $filters */ private function taxonomyQuery(array &$args, $filters): void { if (!is_array($filters)) return; $tax_query = []; foreach ($filters as $taxonomy => $filter) { if (!taxonomy_exists((string) $taxonomy) || !is_array($filter)) continue; $include = $this->ids($filter['include'] ?? []); $exclude = $this->ids($filter['exclude'] ?? []); if (count($include)) { $tax_query[] = [ 'taxonomy' => (string) $taxonomy, 'field' => 'term_id', 'terms' => $include, 'operator' => 'IN', ]; } if (count($exclude)) { $tax_query[] = [ 'taxonomy' => (string) $taxonomy, 'field' => 'term_id', 'terms' => $exclude, 'operator' => 'NOT IN', ]; } } if (count($tax_query)) { $tax_query['relation'] = 'AND'; $args['tax_query'] = $tax_query; } } /** * @param mixed[] $args * @param mixed $filter */ private function authorQuery(array &$args, $filter): void { if (!is_array($filter)) return; $include = $this->ids($filter['include'] ?? []); $exclude = $this->ids($filter['exclude'] ?? []); if (count($include)) $args['author__in'] = $include; if (count($exclude)) $args['author__not_in'] = $exclude; } /** * @param mixed[] $args * @param mixed $filters */ private function customFieldMetaQuery(array &$args, $filters): void { if (!is_array($filters)) return; foreach ($filters as $field_id => $filter) { if (!is_array($filter)) continue; $type = sanitize_key((string) ($filter['type'] ?? 'text')); $operator = sanitize_key((string) ($filter['operator'] ?? '')); $key = OfferingDataIndexer::key((string) $field_id, $type === 'number'); if ($type === 'number') { $this->numberMetaQuery($args['meta_query'], $key, $filter); continue; } $values = isset($filter['values']) && is_array($filter['values']) ? $this->strings($filter['values']) : []; if (in_array($operator, ['in', 'not_in'], true) && count($values)) { $args['meta_query'][] = [ 'key' => $key, 'value' => $values, 'compare' => $operator === 'not_in' ? 'NOT IN' : 'IN', ]; continue; } $value = trim((string) ($filter['value'] ?? '')); if ($value === '') continue; $args['meta_query'][] = [ 'key' => $key, 'value' => $value, 'compare' => $operator === 'eq' ? '=' : 'LIKE', ]; } } /** * @param mixed[] $meta_query * @param mixed[] $filter */ private function numberMetaQuery(array &$meta_query, string $key, array $filter): void { $min = trim((string) ($filter['min'] ?? '')); $max = trim((string) ($filter['max'] ?? '')); if ($min === '' && $max === '') return; if ($min !== '' && $max !== '' && is_numeric($min) && is_numeric($max)) { $meta_query[] = [ 'key' => $key, 'value' => [(float) $min, (float) $max], 'compare' => 'BETWEEN', 'type' => 'NUMERIC', ]; return; } if ($min !== '' && is_numeric($min)) { $meta_query[] = [ 'key' => $key, 'value' => (float) $min, 'compare' => '>=', 'type' => 'NUMERIC', ]; } if ($max !== '' && is_numeric($max)) { $meta_query[] = [ 'key' => $key, 'value' => (float) $max, 'compare' => '<=', 'type' => 'NUMERIC', ]; } } /** * @param mixed $value * @return int[] */ private function ids($value): array { if (!is_array($value)) return []; return array_values(array_unique(array_filter(array_map('absint', $value)))); } /** * @param mixed $value * @return string[] */ private function strings($value): array { if (!is_array($value)) return []; $strings = array_map('sanitize_text_field', $value); return array_values(array_unique(array_filter($strings, static function (string $string): bool { return $string !== ''; }))); } }