Files
wordpress/src/wp-utils/wp-plugin
2024-05-19 21:36:57 +02:00

121 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Download plugin (curl)
# $1 - plugin slug
# $2 - plugin version (optional)
# Returns 0 on success, X on failure
download() {
local pluginSlug="${1:?download: PLUGIN_SLUG is required}"
local pluginVersion="${2:-}"
local pluginFilename="${pluginSlug}.zip"
if [ -n "${pluginVersion}" ]; then
pluginFilename="${pluginSlug}.${pluginVersion}.zip"
fi
curl --fail -gsO "https://downloads.wordpress.org/plugin/${pluginFilename}" || return $?
echo "${pluginFilename}"
return 0
}
# Unpack plugin (unzip)
# $1 - plugin slug
# $2 - plugin version (optional)
# $3 - target directory (optional)
# Returns 0 on success, 1 on failure
unpack() {
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 "${pluginVersion}" ]; then
pluginFilename="${pluginSlug}.${pluginVersion}.zip"
fi
if [ -f "${pluginFilename}" ]; then
unzip -qq -d "${pluginsDir}" "${pluginFilename}" || return $?
rm "${pluginFilename}" -f
return 0
fi
return 1
}
# Check if plugin is installed
# $1 - plugin slug
# Returns 0 if plugin is installed, 1 otherwise
check() {
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 "${pluginPath}" ]; then
return 1
fi
# Check if plugin file exists - if yes, plugin is probably installed successfully
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 "${pluginPath}")" ]; then
return 0
fi
# If we got here, then plugin is not installed
return 1
}
# Delete plugin
# $1 - plugin slug
# Returns 0 on success, X on failure
delete() {
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 "${pluginPath}" ]; then
return 1
fi
rm -rf "${pluginPath}"
return $?
}
# Main function
main() {
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 "${action}" in
download)
download "${@:2}"
return $?
;;
unpack)
unpack "${@:2}"
return $?
;;
check)
check "${@:2}"
return $?
;;
delete)
delete "${@:2}"
return $?
;;
*)
echo "Error: Unknown action '${action}'"
return 1
;;
esac
}
main "${@}"