Files
SODEW/sodew-bash-main/libs/commands/wordpress.sh
T
2026-08-18 09:40:08 +02:00

239 lines
8.5 KiB
Bash

wordpressLabel="[Wordpress]"
# Lists all WordPress users for a site.
# $1 (domain): site domain name.
function cmdWordpressUserList() {
local output error
printRow
if ! run output error wordpressUserList "$@"; then
printDanger "$error"
else
printText "$output"
fi
}
# Sets the password for a WordPress user.
# $1 (domain): site domain name.
# $2 (user): WordPress username or user ID.
# $3 (password): new password.
function cmdWordpressPasswordSet() {
local output error
printRow
if ! run output error wordpressPasswordSet "$@"; then
printDanger "$error"
else
printText "$output"
fi
}
# Executes an arbitrary WP-CLI command inside the site's vhost or all vhosts.
# $1 (all|domain): site domain name or all vhosts.
# $@ (...): WP-CLI sub-command and arguments.
function cmdWordpressExec() {
local target="$1"
shift || true
local vhostList error
printRow
if [[ -z "$target" ]]; then
printDanger "Target not specified";
elif ! run vhostList error openlitespeedConfigVhostList; then
printDanger "Cannot get vhost list: $error";
elif [[ "$target" == "all" ]]; then
local domain
for domain in $vhostList; do
printSection "$domain"
if ! run output error wordpressExec "$domain" "$@"; then
printDanger "$error"
else
printText "$output"
fi
done
elif ! listContains "$target" "$vhostList"; then
printDanger "Unknown domain: $target";
elif ! run output error wordpressExec "$target" "$@"; then
printDanger "$error"
else
printText "$output"
fi
}
function cmdWordpressSalt() {
local target="$1"
local vhostList error
printRow
if [[ -z "$target" ]]; then
printDanger "Target not specified";
elif ! run vhostList error openlitespeedConfigVhostList; then
printDanger "Cannot get vhost list: $error";
elif [[ "$target" == "all" ]]; then
local domain
for domain in $vhostList; do
if ! runError error wordpressSalt "$domain"; then
printDotText "$domain" "$labelFail"
printDanger "$error"
else
printDotText "$domain" "$labelDone"
fi
done
elif ! listContains "$target" "$vhostList"; then
printDanger "Unknown domain: $target";
elif ! runError error wordpressSalt "$target"; then
printDotText "$target" "$labelFail"
printDanger "$error"
else
printDotText "$target" "$labelDone"
fi
}
function cmdWordpressCheck() {
local target="$1"
local vhostList error
printRow
if [[ -z "$target" ]]; then
printDanger "Target not specified";
elif ! run vhostList error openlitespeedConfigVhostList; then
printDanger "Cannot get vhost list: $error";
elif [[ "$target" == "all" ]]; then
local domain
for domain in $vhostList; do
printSection "$domain | core"
wordpressExec "$domain" core verify-checksums
printSection "$domain | plugins"
wordpressExec "$domain" plugin verify-checksums --all
done
elif ! listContains "$target" "$vhostList"; then
printDanger "Unknown domain: $target";
else
printSection "$target | core"
wordpressExec "$target" core verify-checksums
printSection "$target | plugins"
wordpressExec "$target" plugin verify-checksums --all
fi
}
# Updates all WordPress URLs in DB to match the current domain, cleans cache and Redis.
# $1 (domain): site domain name.
# [$2] (abspathOld): old absolute path to replace.
function cmdWordpressDomainUpdate() {
local domain="$1"
[[ -n "$domain" ]] || { printDanger "$wordpressLabel Domain not specified"; return 1; }
local siteNew="https://$domain"
local abspathOld="$2"
local isMultiSite
isMultiSite=$(wordpressExec "$domain" eval 'echo is_multisite() ? 1 : 0;')
if [[ "$isMultiSite" == "1" ]]; then
printDanger "$wordpressLabel This script is for SINGLE SITE only. Detected multisite. Abort."
return 1
fi
local siteConst homeConst siteOption homeOption
siteConst=$(wordpressExec "$domain" eval 'echo defined("WP_SITEURL")?WP_SITEURL:"";' || true)
siteConst=$(strTrimSlash "$siteConst")
homeConst=$(wordpressExec "$domain" eval 'echo defined("WP_HOME")?WP_HOME:"";' || true)
homeConst=$(strTrimSlash "$homeConst")
siteOption=$(wordpressExec "$domain" option get siteurl 2>/dev/null || true)
siteOption=$(strTrimSlash "$siteOption")
homeOption=$(wordpressExec "$domain" option get home 2>/dev/null || true)
homeOption=$(strTrimSlash "$homeOption")
printInfo "$wordpressLabel Wordpress siteurl | Const: $siteConst | Option: $siteOption"
printInfo "$wordpressLabel Wordpress home | Const: $homeConst | Option: $homeOption"
local siteOld="${siteConst:-$siteOption}"
if [[ -z "$siteOld" ]]; then
siteOld="${homeConst:-$homeOption}"
fi
if [[ -z "$siteOld" ]]; then
appError "Could not detect siteOld (neither constants nor DB options present)."
return 2
fi
local schemeOld hostOld rootOld
schemeOld=$(printf '%s' "$siteOld" | awk -F:// '{print $1}')
hostOld=$(printf '%s' "$siteOld" | awk -F[/:] '{print $4}')
rootOld="$schemeOld://$hostOld"
printInfo "$wordpressLabel Wordpress OLD | Site: $siteOld | Scheme: $schemeOld | Host: $hostOld | Root: $rootOld"
local sitePath homePath
sitePath=$(wordpressExec "$domain" eval 'echo parse_url(get_option("siteurl"), PHP_URL_PATH)?:"";' || true)
[[ "$sitePath" == "/" ]] && sitePath=""
homePath=$(wordpressExec "$domain" eval 'echo parse_url(get_option("home"), PHP_URL_PATH)?:"";' || true)
[[ "$homePath" == "/" ]] && homePath=""
local wpType="unknown"
if [[ -z "$homePath" && -z "$sitePath" ]]; then wpType="single_root"
elif [[ -n "$homePath" && -n "$sitePath" && "$homePath" == "$sitePath" ]]; then wpType="single_subdir"
elif [[ -z "$homePath" && -n "$sitePath" ]]; then wpType="single_mixed"
fi
printInfo "$wordpressLabel Wordpress OLD | Type: $wpType"
local siteConstHas homeConstHas
siteConstHas=$(wordpressExec "$domain" eval 'echo defined("WP_SITEURL")?1:0;')
if [[ "$siteConstHas" == "1" ]]; then
wordpressExec "$domain" config delete WP_SITEURL --type=constant || true
fi
homeConstHas=$(wordpressExec "$domain" eval 'echo defined("WP_HOME")?1:0;')
if [[ "$homeConstHas" == "1" ]]; then
wordpressExec "$domain" config delete WP_HOME --type=constant || true
fi
printInfo "$wordpressLabel Update siteurl: $siteNew"
wordpressExec "$domain" option update siteurl "$siteNew"
printInfo "$wordpressLabel Update home: $siteNew"
wordpressExec "$domain" option update home "$siteNew"
printInfo "$wordpressLabel Replace in DB (1): $siteOld --> $siteNew"
wordpressSearchReplace "$domain" "$siteOld" "$siteNew"
if [[ -n "$homeOption" && "$homeOption" != "$siteOld" ]]; then
printInfo "$wordpressLabel Replace in DB (2): $homeOption --> $siteNew"
wordpressSearchReplace "$domain" "$homeOption" "$siteNew"
fi
if [[ -n "$siteOption" && "$siteOption" != "$siteOld" && "$siteOption" != "$homeOption" ]]; then
printInfo "$wordpressLabel Replace in DB (3): $siteOption --> $siteNew"
wordpressSearchReplace "$domain" "$siteOption" "$siteNew"
fi
if [[ -n "$hostOld" ]]; then
printInfo "$wordpressLabel Replace in DB (4): //$hostOld --> $siteNew"
wordpressSearchReplace "$domain" "//$hostOld" "$siteNew"
fi
if [[ "$wpType" == "single_mixed" ]]; then
printInfo "$wordpressLabel Replace in DB (5): $rootOld --> $siteNew"
wordpressSearchReplace "$domain" "$rootOld" "$siteNew"
fi
if [[ -n "$abspathOld" ]]; then
printInfo "$wordpressLabel Replace in DB (6): $abspathOld --> $olsPodVhostsPath/$domain/www"
wordpressSearchReplace "$domain" "$abspathOld" "$olsPodVhostsPath/$domain/www"
fi
printInfo "$wordpressLabel Replace in DB (7): $hostOld --> $domain"
wordpressSearchReplace "$domain" "$hostOld" "$domain"
printInfo "$wordpressLabel Delete options: upload_path/upload_url_path..."
wordpressExec "$domain" option delete upload_path || true
wordpressExec "$domain" option delete upload_url_path || true
printInfo "$wordpressLabel Clean cache..."
wordpressExec "$domain" transient delete --all || true
printInfo "$wordpressLabel Redis clean..."
redisDomainClean "$domain" || true
}