do/commands/reset

110 lines
4.8 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.

# ----------------------------------------------------
# Reset Commands
# Handles resetting WordPress components or permissions.
# ----------------------------------------------------
# ----------------------------------------------------
# Resets the WordPress installation to a clean, default state.
# ----------------------------------------------------
function reset_wp() {
# This function now only accepts an optional email flag.
# The admin user is selected interactively.
local admin_email="$1"
# --- 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 ! command -v wget &>/dev/null; then echo "❌ Error: wget not found." >&2; return 1; fi
if ! command -v unzip &>/dev/null; then echo "❌ Error: unzip not found." >&2; return 1; fi
if ! command -v curl &>/dev/null; then echo "❌ Error: curl not found." >&2; return 1; fi
if ! setup_gum; then echo "❌ Error: gum is required for the interactive admin picker." >&2; return 1; fi
echo "🚀 Starting WordPress Site Reset 🚀"
# --- Interactively Select Admin User ---
echo "Fetching list of administrators..."
local admin_users
admin_users=$("$WP_CLI_CMD" user list --role=administrator --field=user_login --format=csv)
if [ -z "$admin_users" ]; then
echo "❌ Error: No administrator users found to assign to the new installation." >&2
return 1
fi
local admin_user
admin_user=$(echo "$admin_users" | "$GUM_CMD" choose --header "Select an administrator for the new installation")
if [ -z "$admin_user" ]; then
echo "No administrator selected. Aborting reset."
return 0
fi
echo "✅ Selected administrator: $admin_user"
# --- End Select Admin User ---
echo "This is a destructive operation."
# A 3-second countdown to allow the user to abort (Ctrl+C)
for i in {3..1}; do echo -n "Continuing in $i... "; sleep 1; done; echo
# --- Gather Info Before Reset ---
local url; url=$( "$WP_CLI_CMD" option get home --skip-plugins --skip-themes )
local title; title=$( "$WP_CLI_CMD" option get blogname --skip-plugins --skip-themes )
# If admin_email flag is not supplied, get it from the selected user.
if [ -z "$admin_email" ]; then
admin_email=$("$WP_CLI_CMD" user get "$admin_user" --field=user_email --format=csv)
echo " Admin email not provided. Using email from selected user '$admin_user': $admin_email"
fi
# --- Perform Reset ---
echo "Step 1/9: Resetting the database..."
"$WP_CLI_CMD" db reset --yes --skip-plugins --skip-themes
echo "Step 2/9: Downloading latest WordPress core..."
"$WP_CLI_CMD" core download --force --skip-plugins --skip-themes
echo "Step 3/9: Installing WordPress core..."
"$WP_CLI_CMD" core install --url="$url" --title="$title" --admin_user="$admin_user" --admin_email="$admin_email" --skip-plugins --skip-themes
echo "Step 4/9: Deleting all other themes..."
"$WP_CLI_CMD" theme delete --all --force --skip-plugins --skip-themes
echo "Step 5/9: Deleting all plugins..."
"$WP_CLI_CMD" plugin delete --all --skip-plugins --skip-themes
echo "Step 6/9: Finding the latest default WordPress theme..."
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 reset."
return 1
fi
echo "✅ Latest default theme is '$latest_default_theme'."
echo "Step 7/9: Installing and activating '$latest_default_theme'..."
"$WP_CLI_CMD" theme install "$latest_default_theme" --force --activate --skip-plugins --skip-themes
echo "Step 8/9: Cleaning up directories (mu-plugins, uploads)..."
rm -rf wp-content/mu-plugins/
mkdir -p wp-content/mu-plugins/
rm -rf wp-content/uploads/
mkdir -p wp-content/uploads/
echo "Step 9/9: Installing helper plugins (Kinsta MU, CaptainCore Helper)..."
# The install_kinsta_mu function will automatically check if it's a Kinsta env.
install_kinsta_mu
install_helper
echo ""
echo "✅ WordPress reset complete!"
echo " URL: $url"
echo " Admin User: $admin_user"
}
# ----------------------------------------------------
# Resets file and folder permissions to common defaults (755 for dirs, 644 for files).
# ----------------------------------------------------
function reset_permissions() {
echo "Resetting file and folder permissions to defaults"
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;
echo "✅ Permissions have been reset."
}