|
| 1 | +#!/bin/env bash |
| 2 | + |
| 3 | +# Set the exact slug of the plugin you want to check |
| 4 | +plugin_slug='classic-editor'; |
| 5 | + |
| 6 | +source 'source/includes.sh'; |
| 7 | + |
| 8 | +# Requires 'bc' for floating-point percentage calculation |
| 9 | +if ! command -v bc &> /dev/null; then |
| 10 | + echo "Error: 'bc' (basic calculator) is not installed." |
| 11 | + echo "Please install 'bc' to run this script." |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +# Initialize counters |
| 16 | +active_count=0 |
| 17 | +failed_sites=0 |
| 18 | + |
| 19 | +# Get all site URLs into a bash array (ignores 'Loading...' line) |
| 20 | +mapfile -t sites < <(wp_skip_all site list --field=url --quiet) |
| 21 | + |
| 22 | +# Get total site count |
| 23 | +total_sites=${#sites[@]} |
| 24 | + |
| 25 | +if [[ $total_sites -eq 0 ]]; then |
| 26 | + echo "No sites found." |
| 27 | + exit 0 |
| 28 | +fi |
| 29 | + |
| 30 | +echo "Auditing '$plugin_slug' plugin status across $total_sites sites..." |
| 31 | +echo "---" |
| 32 | + |
| 33 | +# Loop through each site and check plugin status. |
| 34 | +for s in "${sites[@]}"; do |
| 35 | + |
| 36 | + # Check if the plugin is active. Suppress errors for archived/deleted sites. |
| 37 | + # Exits with 0 if active, 1 if inactive/not-installed. |
| 38 | + wp_skip_all plugin is-active "$plugin_slug" --url="$s" >/dev/null 2>&1 |
| 39 | + plugin_status=$? |
| 40 | + |
| 41 | + if [[ $plugin_status -eq 0 ]]; then |
| 42 | + # Status 0: Plugin is ACTIVE |
| 43 | + ((active_count++)) |
| 44 | + echo "Site: $s | ACTIVE" |
| 45 | + elif [[ $plugin_status -eq 1 ]]; then |
| 46 | + # Status 1: Plugin is INACTIVE or not installed |
| 47 | + echo "Site: $s | INACTIVE" |
| 48 | + else |
| 49 | + # Any other status: WP-CLI command failed |
| 50 | + echo "Site: $s | FAILED to get status (site may be archived or deleted)" |
| 51 | + ((failed_sites++)) |
| 52 | + fi |
| 53 | +done |
| 54 | + |
| 55 | +# Calculate results. Base percentages only on sites we could successfully check. |
| 56 | +total_checked=$((total_sites - failed_sites)) |
| 57 | +inactive_count=$((total_checked - active_count)) |
| 58 | + |
| 59 | +if [[ $total_checked -gt 0 ]]; then |
| 60 | + # Use 'bc' for floating-point percentage calculation |
| 61 | + active_perc=$(echo "scale=2; ($active_count / $total_checked) * 100" | bc) |
| 62 | + inactive_perc=$(echo "scale=2; ($inactive_count / $total_checked) * 100" | bc) |
| 63 | +else |
| 64 | + active_perc="0.00" |
| 65 | + inactive_perc="0.00" |
| 66 | +fi |
| 67 | + |
| 68 | +echo |
| 69 | +echo "##################################################" |
| 70 | +echo " Plugin Usage Report for: $plugin_slug" |
| 71 | +echo "##################################################" |
| 72 | + |
| 73 | +# Print the report |
| 74 | +printf "Active: %-5s sites (%s%%)\n" "$active_count" "$active_perc" |
| 75 | +printf "Inactive: %-5s sites (%s%%)\n" "$inactive_count" "$inactive_perc" |
| 76 | +echo "---" |
| 77 | +printf "Total Sites Checked: %s\n" "$total_checked" |
| 78 | +if [[ $failed_sites -gt 0 ]]; then |
| 79 | + printf "Sites Failed (skipped): %s\n" "$failed_sites" |
| 80 | +fi |
0 commit comments