Funktionen

astra-achild funktions.php:

<?php
/* ===========================================

Nur Admin darf Kategorie 1763 sehen/suchen

Gäste/Nicht-Admins: aus ASP-Suche raus + 404

=========================================== */

if (!defined(‘ABSPATH’)) exit;

define(‘ASP_ADMIN_ONLY_CAT’, 1763); // “adminsuchbar”

/** Helper */
function asp_is_logged_admin() {
return is_user_logged_in() && current_user_can(‘administrator’);
}

/* ————————————————-

1) Ajax Search Pro: Query-Args hart durchsetzen

————————————————- */
add_filter(‘asp_query_args’, ‘asp_admin_only_cat_rules’, 9999, 2);
add_filter(‘asp_modify_wp_query_args’,’asp_admin_only_cat_rules’, 9999, 2);
add_filter(‘wd_asp_query_args’, ‘asp_admin_only_cat_rules’, 9999, 2);

function asp_admin_only_cat_rules($args, $asp_query = null) {
if (asp_is_logged_admin()) {
// Admin: jegliche Ausschlüsse der 1763 entfernen
if (!empty($args[‘category__not_in’])) {
$args[‘category__not_in’] = array_diff((array)$args[‘category__not_in’], array(ASP_ADMIN_ONLY_CAT));
}
if (!empty($args[‘tax_query’]) && is_array($args[‘tax_query’])) {
$args[‘tax_query’] = array_values(array_filter($args[‘tax_query’], function($clause){
return !(is_array($clause)
&& ($clause[‘taxonomy’] ?? ”) === ‘category’
&& ($clause[‘field’] ?? ”) === ‘term_id’
&& in_array(ASP_ADMIN_ONLY_CAT, (array)($clause[‘terms’] ?? []), true)
&& strtoupper($clause[‘operator’] ?? ‘IN’) === ‘NOT IN’
);
}));
if (isset($args[‘tax_query’][‘relation’]) && count($args[‘tax_query’]) <= 1) {
unset($args[‘tax_query’][‘relation’]);
}
}
if (!empty($args[‘post__not_in’])) {
$args[‘post__not_in’] = array_values(array_diff(
(array)$args[‘post__not_in’],
asp_get_all_post_ids_in_cat(ASP_ADMIN_ONLY_CAT)
));
}
return $args;
}

// Gäste/Nicht-Admins: 1763 konsequent ausschließen // A) WP-Style $excluded = isset($args[‘category__not_in’]) && is_array($args[‘category__not_in’]) ? $args[‘category__not_in’] : array(); $excluded[] = ASP_ADMIN_ONLY_CAT; $args[‘category__not_in’] = array_unique(array_map(‘intval’, $excluded)); // B) Tax Query (robust, auch für Index Table) $tax_query = isset($args[‘tax_query’]) && is_array($args[‘tax_query’]) ? $args[‘tax_query’] : array(); $tax_query[] = array( ‘taxonomy’ => ‘category’, ‘field’ => ‘term_id’, ‘terms’ => array(ASP_ADMIN_ONLY_CAT), ‘operator’ => ‘NOT IN’ ); if (count($tax_query) > 1 && empty($tax_query[‘relation’])) { $tax_query[‘relation’] = ‘AND’; } $args[‘tax_query’] = $tax_query; // C) Notbremse: sicherheitshalber Beiträge der 1763 per post__not_in raus if (empty($args[‘post__not_in’])) $args[‘post__not_in’] = array(); $args[‘post__not_in’] = array_unique(array_merge( (array)$args[‘post__not_in’], asp_get_all_post_ids_in_cat(ASP_ADMIN_ONLY_CAT) )); return $args;

}

/* Helper: alle Post-IDs der Kategorie (1h Cache) */
function asp_get_all_post_ids_in_cat($cat_id) {
$key = ‘asp_cat_block_ids_’ . (int)$cat_id;
$ids = get_transient($key);
if ($ids !== false) return $ids;

$ids = get_posts(array( ‘fields’ => ‘ids’, ‘posts_per_page’ => -1, ‘post_status’ => array(‘publish’,’pending’,’draft’,’future’,’private’), ‘tax_query’ => array(array( ‘taxonomy’ => ‘category’, ‘field’ => ‘term_id’, ‘terms’ => array((int)$cat_id), ‘operator’ => ‘IN’ )) )); if (!is_array($ids)) $ids = array(); set_transient($key, $ids, HOUR_IN_SECONDS); return $ids;

}

/* ————————————————-

2) Ajax Search Pro: Ergebnis-Nachfilter (Failsafe)

————————————————- */
add_filter(‘asp_results’, function($results){
if (asp_is_logged_admin()) return $results; foreach ($results as $k => $r) {
// Treffer-Typ “term” (Kategorie selbst)
if (!empty($r->content_type) && $r->content_type === ‘term’) {
if ((int)($r->term_id ?? 0) === (int)ASP_ADMIN_ONLY_CAT) {
unset($results[$k]); continue;
}
}
// Beiträge
$post_id = isset($r->post_id) ? (int)$r->post_id : (int)($r->id ?? 0);
if ($post_id && has_term(ASP_ADMIN_ONLY_CAT, ‘category’, $post_id)) {
unset($results[$k]);
}
}
return array_values($results);
}, 9999);

/* —————————————————————-

3) Frontend: Single-Ansicht blockieren (Nicht-Admin -> 404)

—————————————————————- */
add_action(‘template_redirect’, function () {
if (asp_is_logged_admin()) return; if (is_single()) {
$post_id = get_queried_object_id();
if ($post_id && has_term(ASP_ADMIN_ONLY_CAT, ‘category’, $post_id)) {
global $wp_query;
$wp_query->set_404();
status_header(404);
nocache_headers();
exit;
}
}
});

/* —————————————————————-

4) Klassische WP-Suche zusätzlich absichern (falls genutzt)

—————————————————————- */
add_action(‘pre_get_posts’, function($query){
if (asp_is_logged_admin()) return; if (!is_admin() && $query->is_main_query() && $query->is_search()) {
$excluded = (array)$query->get(‘category__not_in’);
$excluded[] = ASP_ADMIN_ONLY_CAT;
$query->set(‘category__not_in’, array_unique(array_map(‘intval’, $excluded))); $tax = (array)$query->get('tax_query'); $tax[] = array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => array(ASP_ADMIN_ONLY_CAT), 'operator' => 'NOT IN' ); if (count($tax) > 1 && empty($tax['relation'])) $tax['relation'] = 'AND'; $query->set('tax_query', $tax); }
});

// Links in die Admin-Toolbar einfügen
add_action(‘admin_bar_menu’, function($wp_admin_bar) {
if ( current_user_can(‘administrator’) ) {

// Beiträge-Link $wp_admin_bar->add_node(array( ‘id’ => ‘custom_beitraege’, ‘title’ => ‘Beiträge’, ‘href’ => ‘https://medientanz.de/wp-admin/edit.php’, ‘meta’ => array( ‘title’ => ‘Zu den Beiträgen’ ) )); // Format-Link $wp_admin_bar->add_node(array( ‘id’ => ‘custom_format’, ‘title’ => ‘Format’, ‘href’ => ‘https://medientanz.de/formate/’, ‘meta’ => array( ‘title’ => ‘Zu den Formaten’ ) )); // Funktionen-Link $wp_admin_bar->add_node(array( ‘id’ => ‘custom_funktionen’, ‘title’ => ‘Funktionen’, ‘href’ => ‘https://medientanz.de/funktionen/’, ‘meta’ => array( ‘title’ => ‘Zu den Funktionen’ ) )); }

}, 999);