Use local variables

This commit is contained in:
Aleksandar Puharic 2024-05-19 21:36:57 +02:00
parent 40dd1e2987
commit aa5e378f30
Signed by: xZero707
GPG Key ID: 3CC53DCAA9C237BB
4 changed files with 80 additions and 91 deletions

View File

@ -1,15 +1,15 @@
#!/usr/bin/env bash
function main() {
FULL_CHECK="${1:-false}"
local fullCheck="${1:-false}"
if [ -f "/tmp/.wp-unhealthy" ]; then
echo "Error: WordPress health is compromised: $(cat "/tmp/.wp-unhealthy")"
return 1
fi
# If FULL_CHECK is not requested, site functionality check with curl is skipped
if [ "${FULL_CHECK}" = "false" ]; then
# If fullCheck is not requested, site functionality check with curl is skipped
if [ "${fullCheck}" = "false" ]; then
return 0
fi

View File

@ -3,29 +3,29 @@
set -e
# Applies patch for preventing WordPress updates
function main() {
PATCH_FILE="${1:?PATCH_FILE is required}"
TARGET_FILE="${2:?TARGET_FILE is required}"
main() {
local patchFile="${1:?PATCH_FILE is required}"
local targetFile="${2:?TARGET_FILE is required}"
if [ ! -f "${PATCH_FILE}" ]; then
echo "> No such file [PATCH]: ${PATCH_FILE}"
if [ ! -f "${patchFile}" ]; then
echo "> No such file [PATCH]: ${patchFile}"
return 1
fi
if [ ! -f "${TARGET_FILE}" ]; then
echo "> No such file [TARGET]: ${TARGET_FILE}"
if [ ! -f "${targetFile}" ]; then
echo "> No such file [TARGET]: ${targetFile}"
return 1
fi
echo "> Loading patch ${PATCH_FILE}"
echo " Patching '${TARGET_FILE}'..."
patch --verbose "${TARGET_FILE}" <"${PATCH_FILE}"
echo "> Loading patch ${patchFile}"
echo " Patching '${targetFile}'..."
patch --verbose "${targetFile}" <"${patchFile}"
MARK_READ_ONLY="${3:-true}"
if [ "${MARK_READ_ONLY}" = "true" ]; then
local markReadOnly="${3:-true}"
if [ "${markReadOnly}" = "true" ]; then
# This is done in order to prevent WordPress overwriting the file
echo " Marking the patched file read-only..."
chmod 0440 "${TARGET_FILE}"
chmod 0440 "${targetFile}"
fi
return 0

View File

@ -5,17 +5,16 @@
# $2 - plugin version (optional)
# Returns 0 on success, X on failure
download() {
PLUGIN_SLUG="${1:?download: PLUGIN_SLUG is required}"
PLUGIN_VERSION="${2:-}"
local pluginSlug="${1:?download: PLUGIN_SLUG is required}"
local pluginVersion="${2:-}"
local pluginFilename="${pluginSlug}.zip"
if [ -n "${PLUGIN_VERSION}" ]; then
PLUGIN_FILENAME="${PLUGIN_SLUG}.${PLUGIN_VERSION}.zip"
else
PLUGIN_FILENAME="${PLUGIN_SLUG}.zip"
if [ -n "${pluginVersion}" ]; then
pluginFilename="${pluginSlug}.${pluginVersion}.zip"
fi
curl --fail -gsO "https://downloads.wordpress.org/plugin/${PLUGIN_FILENAME}" || return $?
echo "${PLUGIN_FILENAME}"
curl --fail -gsO "https://downloads.wordpress.org/plugin/${pluginFilename}" || return $?
echo "${pluginFilename}"
return 0
}
@ -25,19 +24,18 @@ download() {
# $3 - target directory (optional)
# Returns 0 on success, 1 on failure
unpack() {
TARGET_PLUGINS_DIR="${TARGET_PLUGINS_DIR:?check: TARGET_PLUGINS_DIR is required}"
PLUGIN_SLUG="${1:?unpack: PLUGIN_SLUG is required}"
PLUGIN_VERSION="${2:-}"
local pluginsDir="${TARGET_PLUGINS_DIR:?check: TARGET_PLUGINS_DIR is required}"
local pluginSlug="${1:?unpack: PLUGIN_SLUG is required}"
local pluginVersion="${2:-}"
local pluginFilename="${pluginSlug}.zip"
if [ -n "${PLUGIN_VERSION}" ]; then
PLUGIN_FILENAME="${PLUGIN_SLUG}.${PLUGIN_VERSION}.zip"
else
PLUGIN_FILENAME="${PLUGIN_SLUG}.zip"
if [ -n "${pluginVersion}" ]; then
pluginFilename="${pluginSlug}.${pluginVersion}.zip"
fi
if [ -f "${PLUGIN_FILENAME}" ]; then
unzip -qq -d "${TARGET_PLUGINS_DIR}" "${PLUGIN_FILENAME}" || return $?
rm "${PLUGIN_FILENAME}" -f
if [ -f "${pluginFilename}" ]; then
unzip -qq -d "${pluginsDir}" "${pluginFilename}" || return $?
rm "${pluginFilename}" -f
return 0
fi
return 1
@ -47,23 +45,22 @@ unpack() {
# $1 - plugin slug
# Returns 0 if plugin is installed, 1 otherwise
check() {
TARGET_PLUGINS_DIR="${TARGET_PLUGINS_DIR:?check: TARGET_PLUGINS_DIR is required}"
PLUGIN_SLUG="${1:?check: PLUGIN_SLUG is required}"
PLUGIN_PATH="${TARGET_PLUGINS_DIR}/${PLUGIN_SLUG}"
local pluginsDir="${TARGET_PLUGINS_DIR:?check: TARGET_PLUGINS_DIR is required}"
local pluginSlug="${1:?check: PLUGIN_SLUG is required}"
local pluginPath="${pluginsDir}/${pluginSlug}"
# Check if plugin directory exists
if [ ! -d "${PLUGIN_PATH}" ]; then
if [ ! -d "${pluginPath}" ]; then
return 1
fi
# Check if plugin file exists - if yes, plugin is probably installed successfully
if [ -f "${PLUGIN_PATH}/${PLUGIN_SLUG}.php" ]; then
if [ -f "${pluginPath}/${pluginSlug}.php" ]; then
return 0
fi
# Check if plugin directory is empty - if not, plugin is probably installed successfully
if [ "$(ls -A "${PLUGIN_PATH}")" ]; then
if [ "$(ls -A "${pluginPath}")" ]; then
return 0
fi
@ -75,29 +72,28 @@ check() {
# $1 - plugin slug
# Returns 0 on success, X on failure
delete() {
TARGET_PLUGINS_DIR="${TARGET_PLUGINS_DIR:?delete: TARGET_PLUGINS_DIR is required}"
PLUGIN_SLUG="${1:?delete: PLUGIN_SLUG is required}"
PLUGIN_PATH="${TARGET_PLUGINS_DIR}/${PLUGIN_SLUG}"
local pluginsDir="${TARGET_PLUGINS_DIR:?delete: TARGET_PLUGINS_DIR is required}"
local pluginSlug="${1:?delete: PLUGIN_SLUG is required}"
local pluginPath="${pluginsDir}/${pluginSlug}"
# Check if plugin directory exists
if [ ! -d "${PLUGIN_PATH}" ]; then
if [ ! -d "${pluginPath}" ]; then
return 1
fi
rm -rf "${PLUGIN_PATH}"
rm -rf "${pluginPath}"
return $?
}
# Main function
main() {
COMMAND="${1:?COMMAND is required}"
TARGET_PLUGINS_DIR="${WP_PLUGINS_PATH:?WP_PLUGINS_PATH is required}"
export TARGET_PLUGINS_DIR
local action="${1:?ACTION is required}"
# Execute command by calling function with the same name
case "${COMMAND}" in
case "${action}" in
download)
download "${@:2}"
return $?
@ -115,11 +111,10 @@ main() {
return $?
;;
*)
echo "Error: Unknown command '${COMMAND}'"
echo "Error: Unknown action '${action}'"
return 1
;;
esac
}
main "${@}"
exit $?

View File

@ -5,17 +5,16 @@
# $2 - theme version (optional)
# Returns 0 on success, X on failure
download() {
THEME_SLUG="${1:?download: THEME_SLUG is required}"
THEME_VERSION="${2:-}"
local themeSlug="${1:?download: THEME_SLUG is required}"
local themeVersion="${2:-}"
local themeFilename="${themeSlug}.zip"
if [ -n "${THEME_VERSION}" ]; then
THEME_FILENAME="${THEME_SLUG}.${THEME_VERSION}.zip"
else
THEME_FILENAME="${THEME_SLUG}.zip"
if [ -n "${themeVersion}" ]; then
themeFilename="${themeSlug}.${themeVersion}.zip"
fi
curl --fail -gsO "https://downloads.wordpress.org/theme/${THEME_FILENAME}" || return $?
echo "${THEME_FILENAME}"
curl --fail -gsO "https://downloads.wordpress.org/theme/${themeFilename}" || return $?
echo "${themeFilename}"
return 0
}
@ -24,19 +23,18 @@ download() {
# $2 - theme version (optional)
# Returns 0 on success, 1 on failure
unpack() {
TARGET_THEMES_DIR="${TARGET_THEMES_DIR:?check: TARGET_THEMES_DIR is required}"
THEME_SLUG="${1:?unpack: THEME_SLUG is required}"
THEME_VERSION="${2:-}"
local themesDir="${TARGET_THEMES_DIR:?check: TARGET_THEMES_DIR is required}"
local themeSlug="${1:?unpack: THEME_SLUG is required}"
local themeVersion="${2:-}"
local themeFilename="${themeSlug}.zip"
if [ -n "${THEME_VERSION}" ]; then
THEME_FILENAME="${THEME_SLUG}.${THEME_VERSION}.zip"
else
THEME_FILENAME="${THEME_SLUG}.zip"
if [ -n "${themeVersion}" ]; then
themeFilename="${themeSlug}.${themeVersion}.zip"
fi
if [ -f "${THEME_FILENAME}" ]; then
unzip -qq -d "${TARGET_THEMES_DIR}" "${THEME_FILENAME}" || return $?
rm "${THEME_FILENAME}" -f
if [ -f "${themeFilename}" ]; then
unzip -qq -d "${themesDir}" "${themeFilename}" || return $?
rm "${themeFilename}" -f
return 0
fi
return 1
@ -46,28 +44,27 @@ unpack() {
# $1 - theme slug
# Returns 0 if theme is installed, 1 otherwise
check() {
TARGET_THEMES_DIR="${TARGET_THEMES_DIR:?check: TARGET_THEMES_DIR is required}"
THEME_SLUG="${1:?check: THEME_SLUG is required}"
THEME_PATH="${TARGET_THEMES_DIR}/${THEME_SLUG}"
local themesDir="${TARGET_THEMES_DIR:?check: TARGET_THEMES_DIR is required}"
local themeSlug="${1:?check: THEME_SLUG is required}"
local themePath="${themesDir}/${themeSlug}"
# Check if theme directory exists
if [ ! -d "${THEME_PATH}" ]; then
if [ ! -d "${themePath}" ]; then
return 1
fi
# Check if theme theme.json exists - if yes, theme is probably installed successfully
if [ -f "${THEME_PATH}/theme.json" ]; then
if [ -f "${themePath}/theme.json" ]; then
return 0
fi
# Check if theme index.php exists - if yes, theme is probably installed successfully
if [ -f "${THEME_PATH}/index.php" ]; then
if [ -f "${themePath}/index.php" ]; then
return 0
fi
# Check if theme directory is empty - if not, theme is probably installed successfully
if [ "$(ls -A "${THEME_PATH}")" ]; then
if [ "$(ls -A "${themePath}")" ]; then
return 0
fi
@ -79,30 +76,28 @@ check() {
# $1 - theme slug
# Returns 0 on success, X on failure
delete() {
TARGET_THEMES_DIR="${TARGET_THEMES_DIR:?delete: TARGET_THEMES_DIR is required}"
THEME_SLUG="${1:?delete: THEME_SLUG is required}"
THEME_PATH="${TARGET_THEMES_DIR}/${THEME_SLUG}"
local themesDir="${TARGET_THEMES_DIR:?delete: TARGET_THEMES_DIR is required}"
local themeSlug="${1:?delete: THEME_SLUG is required}"
local themePath="${themesDir}/${themeSlug}"
# Check if theme directory exists
if [ ! -d "${THEME_PATH}" ]; then
if [ ! -d "${themePath}" ]; then
return 1
fi
rm -rf "${THEME_PATH}"
rm -rf "${themePath}"
return $?
}
# Main function
main() {
COMMAND="${1:?COMMAND is required}"
TARGET_THEMES_DIR="${WP_THEMES_PATH:?WP_THEMES_PATH is required}"
export TARGET_THEMES_DIR
local action="${1:?ACTION is required}"
# Execute command by calling function with the same name
case "${COMMAND}" in
case "${action}" in
download)
download "${@:2}"
return $?
@ -120,11 +115,10 @@ main() {
return $?
;;
*)
echo "Error: Unknown command '${COMMAND}'"
echo "Error: Unknown action '${action}'"
return 1
;;
esac
}
main "${@}"
exit $?