#!/usr/bin/env bash # Download theme (curl) # $1 - theme slug # $2 - theme version (optional) # Returns 0 on success, X on failure download() { THEME_SLUG="${1:?download: THEME_SLUG is required}" THEME_VERSION="${2:-}" if [ -n "${THEME_VERSION}" ]; then THEME_FILENAME="${THEME_SLUG}.${THEME_VERSION}.zip" else THEME_FILENAME="${THEME_SLUG}.zip" fi curl --fail -gsO "https://downloads.wordpress.org/theme/${THEME_FILENAME}" || return $? echo "${THEME_FILENAME}" return 0 } # Unpack theme (unzip) # $1 - theme slug # $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:-}" if [ -n "${THEME_VERSION}" ]; then THEME_FILENAME="${THEME_SLUG}.${THEME_VERSION}.zip" else THEME_FILENAME="${THEME_SLUG}.zip" fi if [ -f "${THEME_FILENAME}" ]; then unzip -qq -d "${TARGET_THEMES_DIR}" "${THEME_FILENAME}" || return $? rm "${THEME_FILENAME}" -f return 0 fi return 1 } # Check if theme is installed # $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}" # Check if theme directory exists if [ ! -d "${THEME_PATH}" ]; then return 1 fi # Check if theme theme.json exists - if yes, theme is probably installed successfully if [ -f "${THEME_PATH}/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 return 0 fi # Check if theme directory is empty - if not, theme is probably installed successfully if [ "$(ls -A "${THEME_PATH}")" ]; then return 0 fi # If we got here, then theme is not installed return 1 } # Delete theme # $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}" # Check if theme directory exists if [ ! -d "${THEME_PATH}" ]; then return 1 fi rm -rf "${THEME_PATH}" return $? } # Main function main() { COMMAND="${1:?COMMAND is required}" TARGET_THEMES_DIR="${WP_THEMES_PATH:?WP_THEMES_PATH is required}" export TARGET_THEMES_DIR # Execute command by calling function with the same name case "${COMMAND}" in download) download "${@:2}" return $? ;; unpack) unpack "${@:2}" return $? ;; check) check "${@:2}" return $? ;; delete) delete "${@:2}" return $? ;; *) echo "Error: Unknown command '${COMMAND}'" return 1 ;; esac } main "${@}" exit $?