Skip to content

Instantly share code, notes, and snippets.

@raptor235
Last active December 8, 2017 14:09
Show Gist options
  • Save raptor235/a66e84b8b66a5cbe1498e78c1ae0df8e to your computer and use it in GitHub Desktop.
Save raptor235/a66e84b8b66a5cbe1498e78c1ae0df8e to your computer and use it in GitHub Desktop.
RED="\033[0;31m"
REDBOLD="\033[1;31m"
GREEN="\033[0;32m"
GREENBOLD="\033[1;32m"
YELLOW="\033[0;33m"
YELLOWBOLD="\033[1;33m"
BLUE="\033[0;34m"
BLUEBOLD="\033[1;34m"
PURPLE="\033[0;35m"
PURPLEBOLD="\033[1;35m"
CYAN="\033[0;36m"
CYANBOLD="\033[1;36m"
WHITE="\033[0;37m"
WHITEBOLD="\033[1;37m"
NC='\033[0m' # No Color
log(){
if [[ $2 ]]; then
printf "\n${CYANBOLD}$1${NC} ${YELLOW} $2 ${NC}"
else
printf "\n${CYANBOLD}$1${NC}"
fi
}
log_error(){
if [[ $2 ]]; then
printf "\n${RED}$1${NC} ${CYAN} $2 ${NC}"
else
printf "\n${RED}$1${NC}"
fi
}
log_success(){
if [[ $2 ]]; then
printf "\n${GREEN}$1${NC} ${CYAN} $2 ${NC}"
else
printf "\n${GREEN}$1${NC}"
fi
}
log_warn(){
if [[ $2 ]]; then
printf "\n${YELLOW}$1${NC} ${CYAN} $2 ${NC}"
else
printf "\n${YELLOW}$1${NC}"
fi
}
log "Checking for plugins to update"
count_inactive=0
count_updated=0
inactive_plugins=''
not_in_repo_plugins=''
up_to_date_plugins=''
updated_plugins=''
git reset --hard HEAD
function commit_plugin_update {
plugin_slug=$1
old_version=$2
new_version=$3
git add -- ./wp-content/plugins/$plugin_slug
git commit -q -m "Plugin Update: $plugin_slug $old_version -> $new_version" -- ./wp-content/plugins/$plugin_slug
}
while read plugin ; do
old_version="$(echo $plugin | jq -r '.version')"
update="$(echo $plugin | jq -r '.update')"
plugin_status="$(echo $plugin | jq -r '.status')"
plugin_slug="$(echo $plugin | jq -r '.name')"
if [ $update = 'available' ]; then
update_result="$(wp plugin update $plugin_slug --format=json)"
update_status="$(echo $update_result | jq -c -r '.[].status')"
new_version="$(echo $update_result | jq -c -r '.[].new_version')"
if [[ $update_status = 'Updated' ]]; then
# log "Updated $plugin_slug to $new_version"
up_to_date_plugins="Updated $plugin_slug to $new_version \n"
commit_plugin_update $plugin_slug $old_version $new_version
else
log_error "Update Error: Couldn't update $plugin_slug"
echo $update_result
fi
else
if [[ $(curl --write-out %{http_code} --silent --output /dev/null "https://wordpress.org/plugins/$plugin_slug/") = 200 ]]
then
log_success "Plugin: $plugin_slug is up to date."
up_to_date_plugins="$up_to_date_plugins \n $plugin_slug"
else
log_warn "Plugin: $plugin_slug is not in WordPress repo."
fi
fi
if [[ $plugin_status = 'inactive' ]]; then
count_inactive=$(($count_inactive+1))
inactive_plugins="${inactive_plugins}"$'\n'"${plugin_slug}"
fi
# if [[ count_inactive > 1 ]]; then
# break
# fi
done <<< "$(wp plugin list --format=json | jq -r -c '.[]')"
if [[ $count_inactive > 0 ]]; then
log_warn "\nThere are $count_inactive inactive plugins."
echo "${inactive_plugins}"
fi
if [[ $count_updated > 0 ]]; then
log_success "\nThere are $count_updated updated plugins."
log_success $updated_plugins
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment