File: //bigscoots/wpo/cloudflare/bs-cache-audit.sh
#!/bin/bash
# Load common BigScoots functions
source /bigscoots/includes/common.sh
LC_ALL=C
TARGET_DOMAIN=$1
# --- Function to process a single site ---
process_site() {
local d=$1
local domain=$2
# Generate skip list for THIS specific site
SKIP_LIST=$(skip_all_plugins_except bigscoots-cache --path="$d")
# Run the WP-CLI command
csv=$(wp bs_cache status --format=csv --path="$d" --skip-themes --skip-plugins="$SKIP_LIST" --allow-root 2>/dev/null)
if [[ -z "$csv" ]]; then
printf "%-35s | %s\n" "$domain" "PLUGIN NOT ACTIVE / NOT FOUND"
return
fi
# Parse values using awk
cache=$(echo "$csv" | awk -F',' '$2 ~ /Plugin Cache Status/ {gsub(/^"|"$|[[:space:]]+/, "", $3); print $3}')
setup=$(echo "$csv" | awk -F',' '$2 ~ /Plugin Setup Mode/ {gsub(/^"|"$|[[:space:]]+/, "", $3); print $3}')
rule=$(echo "$csv" | awk -F',' '$2 ~ /Cache Rule Status/ {gsub(/^"|"$|[[:space:]]+/, "", $3); print $3}')
hit=$(echo "$csv" | awk -F',' '$2 ~ /Home Page Cache Status/ {gsub(/^"|"$|[[:space:]]+/, "", $3); print $3}')
printf "%-35s | %-20s | %-20s | %-20s | %-20s\n" \
"$domain" "${cache:-N/A}" "${setup:-N/A}" "${rule:-N/A}" "${hit:-N/A}"
}
# --- Header ---
printf "%-35s | %-20s | %-20s | %-20s | %-20s\n" "Site" "Cache" "Setup" "Rules" "Home"
printf "%-35s-+-%-20s-+-%-20s-+-%-20s-+-%-20s\n" \
"$(printf '%.0s-' {1..35})" "$(printf '%.0s-' {1..20})" "$(printf '%.0s-' {1..20})" "$(printf '%.0s-' {1..20})" "$(printf '%.0s-' {1..20})"
# --- Execution Logic ---
if [[ -n "$TARGET_DOMAIN" ]]; then
# Direct Path Mode: Zero scanning, instant execution
SITE_PATH="/home/nginx/domains/$TARGET_DOMAIN/public"
if [[ -f "$SITE_PATH/wp-config.php" ]]; then
process_site "$SITE_PATH" "$TARGET_DOMAIN"
else
echo "Error: WordPress not found at $SITE_PATH"
exit 1
fi
else
# Global Mode: Use find_wp_installs for discovery
find_wp_installs | while read -r d; do
domain=$(basename "$(dirname "$d")")
# Skip staging sites in global scan
[[ "$domain" == *.bigscoots-staging.com ]] && continue
process_site "$d" "$domain"
done
fi