Skip to content

Instantly share code, notes, and snippets.

@mrazzari
Last active March 27, 2019 14:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrazzari/ae07391da419e7bc58d2b6e6c3b655b7 to your computer and use it in GitHub Desktop.
Save mrazzari/ae07391da419e7bc58d2b6e6c3b655b7 to your computer and use it in GitHub Desktop.
Update WP and plugins via wp-cli, then create individual commits
#!/usr/bin/env bash
#
# @author: Pablo Sammartino
# http://www.cvam.com.ar
# Created: 2017-05-22
# Last Update: 2019-03-25
#
DATE=`date +%d-%m-%Y`
CURRENT_DIRECTORY=$PWD
#------ Directory prompt-------
read -e -i "`pwd`/" -p "Please enter your WordPress install directory: "
echo
CURRENT_DIRECTORY=$REPLY
eval cd "${CURRENT_DIRECTORY}"
#------ Install/Update WP-CLI -------
# curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# chmod +x wp-cli.phar
# sudo mv wp-cli.phar /usr/local/bin/wp
# sudo wp cli update
#------ Update Wordpress Core -------
CORE_UPDATES=`wp core check-update --format=count`
if [ "${CORE_UPDATES}" ] && [ "${CORE_UPDATES}" -gt "0" ]; then
UPDATES=`wp core check-update --format=csv --field=version`
VERSION=$(echo $UPDATES | cut --delimiter=' ' --fields=2)
read -p "Upgrade WordPress core to version $VERSION? (Y/N)" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
wp core update
echo "Installing new WordPress core update..."
fi
else
echo "WordPress core is already up to date."
echo
fi
#------- Update WordPress Plugins ------
commit_plugin_on_git(){
echo "Updating git repo..."
PLUGIN_PATH=`wp plugin path $1`
PLUGIN_FOLDER_PATH="$(dirname "$PLUGIN_PATH")"
git add $PLUGIN_FOLDER_PATH
git commit --quiet -m "Plugin: $1 updated from version $2 to $3"
echo -e "\n"
}
update_and_activate_plugin(){
read -p "Upgrade '$1'? (Y/N)" -n 1 -r
echo -e "\n"
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Installing updates for $1..."
wp plugin update $1 --format=summary
else
return
fi
PLUGIN_NEW_VERSION=$(echo `wp plugin get $1 --fields=version --format=csv` | cut -f3 -d ',')
if [ "$2" != "${PLUGIN_NEW_VERSION}" ]; then
echo "Updated $1 from version $2 to $PLUGIN_NEW_VERSION"
commit_plugin_on_git $1 $2 $PLUGIN_NEW_VERSION
fi
# read -p "Activate plugin '$1'? (Y/N)" -n 1 -r
# echo
# if [[ $REPLY =~ ^[Yy]$ ]]
# then
# echo "Activating $1..."
# wp plugin activate $1
# else
# return
# fi
}
PLUGINS_LIST=$(wp plugin list --format=csv --fields=name,update,version)
for plugin in $PLUGINS_LIST; do
(
PLUGIN_NAME=$(echo $plugin | cut -f1 -d ',')
if [ ! $PLUGIN_NAME = 'name' ]; then
PLUGIN_PATH=`wp plugin path $PLUGIN_NAME`
if grep -Fq "remove_update" ${PLUGIN_PATH}
then
echo "Note: A customized version of '$PLUGIN_NAME' is being used. Please contact your developer for updates.";
else
PLUGIN_OLD_VERSION=$(echo $plugin | cut -f3 -d ',')
PLUGIN_STATUS=$(echo $plugin | cut -f2 -d ',')
case $PLUGIN_STATUS in
available)
update_and_activate_plugin $PLUGIN_NAME $PLUGIN_OLD_VERSION
;;
none)
echo "Plugin '$PLUGIN_NAME' is already at its latest version."
;;
*)
echo "Error: type mismatch."
;;
esac
fi
fi
)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment