do/commands/clean
2025-07-10 11:14:49 -04:00

117 lines
No EOL
4.7 KiB
Bash
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ----------------------------------------------------
# Cleans up inactive themes.
# ----------------------------------------------------
function clean_themes() {
# --- Pre-flight checks ---
if ! setup_wp_cli; then echo "❌ Error: WP-CLI not found." >&2; return 1; fi
if ! "$WP_CLI_CMD" core is-installed --quiet; then echo "❌ Error: This does not appear to be a WordPress installation." >&2; return 1; fi
echo "🔎 Finding the latest default WordPress theme to preserve..."
latest_default_theme=$("$WP_CLI_CMD" theme search twenty --field=slug --per-page=1 --quiet --skip-plugins --skip-themes)
if [ $? -ne 0 ] || [ -z "$latest_default_theme" ]; then
echo "❌ Error: Could not determine the latest default theme. Aborting." >&2
return 1
fi
echo "✅ The latest default theme is '$latest_default_theme'. This will be preserved."
inactive_themes=($("$WP_CLI_CMD" theme list --status=inactive --field=name --skip-plugins --skip-themes))
if [ ${#inactive_themes[@]} -eq 0 ]; then
echo "👍 No inactive themes found to process. All done!"
return 0
fi
echo "🚀 Processing ${#inactive_themes[@]} inactive themes..."
for theme in "${inactive_themes[@]}"; do
# Check if the current inactive theme is the one we want to keep
if [[ "$theme" == "$latest_default_theme" ]]; then
echo "⚪️ Keeping inactive default theme: $theme"
else
echo "❌ Deleting inactive theme: $theme"
"$WP_CLI_CMD" theme delete "$theme"
fi
done
echo "✨ Cleanup complete."
}
# ----------------------------------------------------
# Deletes inactive plugins.
# On multisite, only deletes plugins not active on any site.
# ----------------------------------------------------
function clean_plugins() {
# --- Pre-flight checks ---
if ! setup_wp_cli; then echo "❌ Error: WP-CLI not found." >&2; return 1; fi
if ! "$WP_CLI_CMD" core is-installed --quiet; then echo "❌ Error: This does not appear to be a WordPress installation." >&2; return 1; fi
if ! setup_gum; then return 1; fi
echo "🚀 Cleaning inactive plugins..."
# --- Multisite vs. Single Site Logic ---
if "$WP_CLI_CMD" core is-installed --network --quiet; then
echo " Multisite installation detected. Finding plugins that are not active on any site..."
# Get all installed plugins (slugs)
local all_installed_plugins
all_installed_plugins=$("$WP_CLI_CMD" plugin list --field=name)
# Get all network-activated plugins
local network_active_plugins
network_active_plugins=$("$WP_CLI_CMD" plugin list --network --status=active --field=name)
# Get all plugins active on individual sites
local site_active_plugins
site_active_plugins=$("$WP_CLI_CMD" site list --field=url --format=ids | xargs -I % "$WP_CLI_CMD" plugin list --url=% --status=active --field=name)
# Combine all active plugins (network + site-specific) and get a unique, sorted list
local all_active_plugins
all_active_plugins=$(echo -e "${network_active_plugins}\n${site_active_plugins}" | sort -u | grep -v '^$')
# Find plugins that are in the installed list but not in the combined active list
local plugins_to_delete
plugins_to_delete=$(comm -23 <(echo "$all_installed_plugins" | sort) <(echo "$all_active_plugins" | sort))
else
echo " Single site installation detected. Finding inactive plugins..."
# On a single site, 'inactive' is sufficient
local plugins_to_delete
plugins_to_delete=$("$WP_CLI_CMD" plugin list --status=inactive --field=name)
fi
if [ -z "$plugins_to_delete" ]; then
echo "✅ No inactive plugins found to delete."
return 0
fi
local plugins_to_delete_count
plugins_to_delete_count=$(echo "$plugins_to_delete" | wc -l | xargs)
echo "🔎 Found ${plugins_to_delete_count} plugin(s) to delete:"
echo "$plugins_to_delete"
echo
if ! "$GUM_CMD" confirm "Proceed with deletion?"; then
echo "Operation cancelled by user."
return 0
fi
while IFS= read -r plugin; do
if [ -n "$plugin" ]; then
echo " - Deleting '$plugin'..."
"$WP_CLI_CMD" plugin delete "$plugin"
fi
done <<< "$plugins_to_delete"
echo "✨ Plugin cleanup complete."
}
# ----------------------------------------------------
# Analyzes disk usage using rclone.
# ----------------------------------------------------
function clean_disk() {
echo "🚀 Launching interactive disk usage analysis..."
if ! setup_rclone; then
echo "Aborting analysis: rclone setup failed." >&2
return 1
fi
"$RCLONE_CMD" ncdu .
}