#!/bin/bash

# Debug WordPress filters affecting queries

# Get absolute path to this script's directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Navigate to wordpress-dev directory
cd "$(dirname "$SCRIPT_DIR")" || exit 1

# Load environment variables
ENV_FILE=".env"
if [ ! -f "$ENV_FILE" ]; then
    echo "Error: .env file not found at: $ENV_FILE"
    exit 1
fi

source "$ENV_FILE"

echo "=== Debugging Query Filters ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "==============================="

# Test query filters
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" <<'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

# Create PHP test script
cat > debug-filters.php << 'PHP'
<?php
require_once 'wp-load.php';

// Remove all query filters temporarily
remove_all_filters('pre_get_posts');
remove_all_filters('posts_clauses');
remove_all_filters('posts_where');
remove_all_filters('posts_join');

// Try simple query
echo "Test 1: Simple query after removing filters\n";
$args = array(
    'post_type' => 'tribe_events',
    'author' => 17,
    'post_status' => 'any',
    'posts_per_page' => 5,
    'suppress_filters' => false
);
$query = new WP_Query($args);
echo "Found posts: " . $query->found_posts . "\n";
echo "SQL: " . $query->request . "\n\n";

// Try with suppress_filters
echo "Test 2: With suppress_filters = true\n";
$args['suppress_filters'] = true;
$query2 = new WP_Query($args);
echo "Found posts: " . $query2->found_posts . "\n";
echo "SQL: " . $query2->request . "\n\n";

// Check if Community Events is filtering queries
echo "Test 3: Check active plugins\n";
$active_plugins = get_option('active_plugins');
foreach ($active_plugins as $plugin) {
    if (strpos($plugin, 'community') !== false || strpos($plugin, 'events') !== false) {
        echo "Active: " . $plugin . "\n";
    }
}

// Check specific event
echo "\nTest 4: Get specific event\n";
$event = get_post(5482);
if ($event) {
    echo "Event 5482 exists\n";
    echo "Author: " . $event->post_author . "\n";
    echo "Type: " . $event->post_type . "\n";
    echo "Status: " . $event->post_status . "\n";
}

// Check capabilities
echo "\nTest 5: User capabilities\n";
$user = get_user_by('ID', 17);
if ($user) {
    echo "User login: " . $user->user_login . "\n";
    echo "Roles: " . implode(', ', $user->roles) . "\n";
    echo "Can read private posts: " . ($user->has_cap('read_private_posts') ? 'Yes' : 'No') . "\n";
    echo "Can edit posts: " . ($user->has_cap('edit_posts') ? 'Yes' : 'No') . "\n";
}
PHP

php debug-filters.php
rm debug-filters.php
EOF

echo "Debug completed!"