['post_format'], 'meta' => ['footnotes', 'wp_dummy_content_generator_post'], 'acf' => [], 'terms' => [ ['taxonomy' => 'category', 'slug' => 'uncategorized'], ['taxonomy' => 'category', 'slug' => 'uncategorised'], ], ]; } /** Preserve case-sensitive metadata identifiers and existing percent-encoded slugs. */ function exact_identifier($value) { if (!is_string($value) && !is_numeric($value)) return ''; $value = trim(wp_check_invalid_utf8((string) $value)); return $value !== '' && !preg_match('/[\x00-\x1F\x7F<>]/', $value) ? $value : ''; } function sanitize_settings($settings) { $settings = array_merge(defaults(), is_array($settings) ? $settings : []); $result = ['taxonomy' => [], 'meta' => [], 'acf' => [], 'terms' => []]; foreach (['taxonomy', 'meta', 'acf'] as $type) { foreach (is_array($settings[$type]) ? $settings[$type] : [] as $source) { $source = exact_identifier($source); if ($type === 'taxonomy') $source = sanitize_key($source); if ($source !== '' && !in_array($source, $result[$type], true)) $result[$type][] = $source; } } $seen = []; foreach (is_array($settings['terms']) ? $settings['terms'] : [] as $term) { if (!is_array($term)) continue; $taxonomy = sanitize_key(exact_identifier($term['taxonomy'] ?? '')); $slug = exact_identifier($term['slug'] ?? ''); $key = $taxonomy . "\0" . $slug; if ($taxonomy === '' || $slug === '' || isset($seen[$key])) continue; $seen[$key] = true; $result['terms'][] = ['taxonomy' => $taxonomy, 'slug' => $slug]; } return $result; } function get_exclusions_settings() { return sanitize_settings(get_option(OPTION, defaults())); } /** Caller owns authorization/nonces; explicit empty lists restore all choices. */ function save_settings($settings) { $previous = get_exclusions_settings(); $settings = sanitize_settings($settings); update_option(OPTION, $settings, false); if ($settings !== $previous) { // Current serializers project policy after raw caches, so cached discovery // remains reusable. Retire only obsolete, already-filtered v3 dictionaries. global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Invalidation must discover current obsolete transient names; caching this cleanup would leave stale dictionaries. $names = $wpdb->get_col($wpdb->prepare('SELECT option_name FROM %i WHERE option_name LIKE %s', $wpdb->options, $wpdb->esc_like('_transient_fqbp_dict_') . '%')); foreach ($names as $name) delete_transient(substr($name, strlen('_transient_'))); if (function_exists('wp_cache_supports') && wp_cache_supports('flush_group')) wp_cache_flush_group('fqbp_dictionary'); delete_option('fqbp_dictionary_cache_version'); do_action('fqbp_catalog_exclusions_updated', $settings, $previous); } return $settings; } function is_source_excluded($type, $source) { $type = is_string($type) ? strtolower(trim($type)) : ''; if (!in_array($type, ['taxonomy', 'meta', 'acf'], true)) return false; $source = exact_identifier($source); if ($type === 'taxonomy') $source = sanitize_key($source); return in_array($source, get_exclusions_settings()[$type], true); } function is_term_excluded($taxonomy, $slug) { $taxonomy = sanitize_key(exact_identifier($taxonomy)); $slug = exact_identifier($slug); foreach (get_exclusions_settings()['terms'] as $term) { if ($term['taxonomy'] === $taxonomy && $term['slug'] === $slug) return true; } return false; } /** Project a canonical runtime source map without changing fixed-query inputs. */ function filter_sources($sources) { $result = []; foreach ((array) $sources as $id => $source) { if (!is_array($source) || is_source_excluded($source['type'] ?? '', $source['source'] ?? '')) continue; if (($source['type'] ?? '') === 'taxonomy') $source['terms'] = array_values(array_filter((array) ($source['terms'] ?? []), static fn($term) => is_array($term) && !is_term_excluded($source['source'] ?? '', $term['slug'] ?? ''))); $result[$id] = $source; } return $result; } /** Response-only projection. The caller may attach edition/capability metadata. */ function filter_catalog($catalog) { if (!is_array($catalog)) return $catalog; foreach ($catalog['post_types'] ?? [] as $post_type => $data) { if (!is_array($data)) continue; foreach (['taxonomies' => ['taxonomy', 'slug'], 'meta_fields' => ['meta', 'key'], 'acf_fields' => ['acf', 'name']] as $collection => [$type, $key]) { if (!isset($data[$collection]) || !is_array($data[$collection])) continue; $data[$collection] = array_values(array_filter($data[$collection], static fn($source) => is_array($source) && !is_source_excluded($type, $source[$key] ?? ''))); } foreach ($data['taxonomies'] ?? [] as $index => $taxonomy) { if (isset($taxonomy['terms']) && is_array($taxonomy['terms'])) $data['taxonomies'][$index]['terms'] = array_values(array_filter($taxonomy['terms'], static fn($term) => is_array($term) && !is_term_excluded($taxonomy['slug'] ?? '', $term['slug'] ?? ''))); } $catalog['post_types'][$post_type] = $data; } return $catalog; }