Cleanup the code and use strict environment variables

This commit is contained in:
Aleksandar Puharic 2022-07-26 21:24:26 +02:00
parent 9ec42b03fa
commit 8b388ae116
Signed by: xZero707
GPG Key ID: 3CC53DCAA9C237BB

View File

@ -1,15 +1,10 @@
#!/usr/bin/with-contenv bash #!/usr/bin/with-contenv bash
DISABLE_WP_UPDATES="${ENFORCE_DISABLE_WP_UPDATES:-true}"
# Designed to replace original, overcomplicated entrypoint script from official wordpress docker repository # Designed to replace original, overcomplicated entrypoint script from official wordpress docker repository
# Why not use already available tools instead?! # Why not use already available tools instead?!
# Removes trailing zero if found # Applies patch for making WordPress updates impossible
# This is required due to inconsistencies between WodPress docker image versioning and wp-cli core download
# If patch version is 0, it is not considered by wp-cli.
WP_VERSION=$(echo "${WP_VERSION}" | sed --expression='s/.0$//g');
function disableUpdatesPatch() { function disableUpdatesPatch() {
DISABLE_WP_UPDATES="${ENFORCE_DISABLE_WP_UPDATES:-true}"
if [ "${DISABLE_WP_UPDATES}" != "false" ]; then if [ "${DISABLE_WP_UPDATES}" != "false" ]; then
echo "> Disabling WordPress updates..." echo "> Disabling WordPress updates..."
patch /var/www/html/wp-admin/update-core.php </etc/wp-mods/wp-admin-update-core.patch patch /var/www/html/wp-admin/update-core.php </etc/wp-mods/wp-admin-update-core.patch
@ -18,6 +13,7 @@ function disableUpdatesPatch() {
fi fi
} }
# Deletes known WordPress files
function deleteWordPress() { function deleteWordPress() {
echo "> Deleting WordPress installation (core files)" echo "> Deleting WordPress installation (core files)"
@ -27,24 +23,38 @@ function deleteWordPress() {
rm -rf "/var/www/${WEB_ROOT}/"{.htaccess,index.php,license.txt,readme.html,wp-activate.php,wp-blog-header.php,wp-comments-post.php,wp-config-sample.php.php,wp-cron.php,wp-links-opml.php,wp-load.php,wp-login.php,wp-mail.php,wp-settings.php,wp-signup.php,wp-trackback.php,xmlrpc.php} rm -rf "/var/www/${WEB_ROOT}/"{.htaccess,index.php,license.txt,readme.html,wp-activate.php,wp-blog-header.php,wp-comments-post.php,wp-config-sample.php.php,wp-cron.php,wp-links-opml.php,wp-load.php,wp-login.php,wp-mail.php,wp-settings.php,wp-signup.php,wp-trackback.php,xmlrpc.php}
} }
echo "> Verifying 'WordPress ${WP_VERSION}' installation..." # Main function
WP_INSTALLED_VERSION="$(wp core version)" function main() {
# Removes trailing zero if found
# This is required due to inconsistencies between WodPress docker image versioning and wp-cli core download
# If patch version is 0, it is not considered by wp-cli.
WP_VERSION=$(echo "${WP_VERSION:?}" | sed --expression='s/.0$//g')
WP_LOCALE="${WP_LOCALE:?}"
if [ -z "${WP_INSTALLED_VERSION}" ]; then echo "> Verifying 'WordPress ${WP_VERSION}' installation..."
echo "> WordPress is not present" WP_INSTALLED_VERSION="$(wp core version)"
echo "> Downloading 'WordPress ${WP_VERSION}'..."
deleteWordPress set -e
wp core download --locale="${WP_LOCALE}" --version="${WP_VERSION}"
disableUpdatesPatch if [ -z "${WP_INSTALLED_VERSION}" ]; then
elif [ "${WP_INSTALLED_VERSION}" != "${WP_VERSION}" ]; then echo "> WordPress is not present"
echo "> WordPress version mismatch" echo "> Downloading 'WordPress ${WP_VERSION}'..."
echo "> Expected version: ${WP_VERSION}" deleteWordPress
echo "> Detected version: ${WP_INSTALLED_VERSION}" wp core download --locale="${WP_LOCALE}" --version="${WP_VERSION}"
echo "> Scraping current files" disableUpdatesPatch
deleteWordPress elif [ "${WP_INSTALLED_VERSION}" != "${WP_VERSION}" ]; then
echo "> Downloading WordPress version '${WP_VERSION}'..." echo "> WordPress version mismatch"
wp core download --locale="${WP_LOCALE}" --version="${WP_VERSION}" echo "> Expected version: ${WP_VERSION}"
disableUpdatesPatch echo "> Detected version: ${WP_INSTALLED_VERSION}"
else echo "> Scraping current files"
echo "> Identified 'WordPress ${WP_VERSION}'" deleteWordPress
fi echo "> Downloading WordPress version '${WP_VERSION}'..."
wp core download --locale="${WP_LOCALE}" --version="${WP_VERSION}"
disableUpdatesPatch
else
echo "> Identified 'WordPress ${WP_VERSION}'"
fi
}
main "${@}"
exit $?