Skip to content

Instantly share code, notes, and snippets.

@LostinOrchid
Last active June 1, 2020 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LostinOrchid/bd1fd908c90f1f680948b3e2c7ce216d to your computer and use it in GitHub Desktop.
Save LostinOrchid/bd1fd908c90f1f680948b3e2c7ce216d to your computer and use it in GitHub Desktop.
Update Wordpress Plugin using Local zip file
#!/usr/bin/env bash
script_filename=$0
plugin_name=$1
version=$2
plugins_dir=~/plugins
old_plugins_dir=~/old-plugins
script_name="Wordress Plugin Updater"
script_description="Updates a wordpress plugin given the local plugin directory path."
plugin_filename=
plugin_filepath=
function color()
{
local col=$1
shift
NC='\033[0m' # No Color
case $col in
RED|red|Red|r)
printf "\033[0;31m$*${NC}"
;;
GREEN|green|Green|g)
printf "\033[0;32m$*${NC}"
;;
esac
}
function bold()
{
echo $(tput bold)$*$(tput sgr0)
}
function fail()
{
echo `color RED $*`
exit 1
}
function trailingslashit()
{
local STR=$1
[[ "${STR}" != */ ]] && STR="${STR}/"
echo $STR
}
function display_info()
{
echo "Plugin: $plugin_name"
echo "Version: $version"
echo "Plugins Directory: $plugins_dir"
echo "Plugin file name: $plugin_filename"
echo "Plugin file path: $plugin_filepath"
echo "Old Plugins Directory: $old_plugins_dir"
echo "Backup Folder Name: $backup_dirname"
echo
}
function usage()
{
# echo `bold Name`
# echo
# echo " $script_name"
# echo
# echo `bold Description`
# echo
# echo " $script_description"
# echo
echo Usage:
echo " ./$(basename $script_filename) <name> <version>"
if [[ $1 != "m" ]];
then
echo
echo Arguments:
echo " name The name of the plugin."
echo " version The version to update to."
# echo
# echo Option:
# echo " --plugins-dir Path to plugins repository.(Default: $plugins_dir)"
# echo " --old-plugins-dir Path to where the old plugins will be stored. (Default: $old_plugins_dir)"
# echo " --help|-h Show help."
fi
}
function check_args()
{
# while test $# -gt 0; do
# case "$1" in
# -h|--help)
# usage
# exit
# ;;
# --plugins-dir)
# shift
# echo pugins dir
# plugins_dir=$1
# shift
# ;;
# esac
# done
backup_dirname=$plugin_name-`date +'%F%-H%m%S'`
if [[ -z $plugin_name ]]
then
echo `color r Plugin name is required.`
echo
usage m
exit 1;
fi
if [[ -z $version ]];
then
echo `color r Version is required.`
echo
usage m
exit 1;
fi;
}
function check_wp()
{
if [[ -z `command -v wp` ]];
then
fail "WP-CLI is not installed. Visit https://wp-cli.org/"
fi
# Check if the current directory is a valid wordpress installation.
wp core version 2>&1>/dev/null || fail "This is not a valid wordpress installation."
wp plugin is-installed "$plugin_name" || fail "Plugin $plugin_name is not installed."
}
function do_check()
{
check_wp
check_args
}
function prepare()
{
mkdir -p $old_plugins_dir
plugins_dir=$(trailingslashit $plugins_dir)
old_plugins_dir=$(trailingslashit $old_plugins_dir)
old_plugin_filepath=$old_plugins_dir$backup_dirname
plugin_filename=$plugin_name.$version.zip
plugin_filepath="$plugins_dir$plugin_name/$plugin_filename"
installed_plugin_dir=$(dirname `wp plugin path $plugin_name`)
}
function run()
{
do_check
prepare
display_info
if [[ ! -f "$plugin_filepath" ]]
then
fail "$plugin_filepath does not exists."
fi
# cd into plugin directory
cd $(wp plugin path)
# Only deactivate when it is active.
wp plugin is-active $plugin_name && wp plugin deactivate $plugin_name 1>/dev/null
# Move the current plugin to old plugin dir
mv $plugin_name "$old_plugin_filepath"
# Create the plugin folder.
mkdir $plugin_name
# Cd into the plugin folder and extract the new plugin version there.
cd $plugin_name
echo "Extracting files..."
unzip $plugin_filepath 1>/dev/null
# Activate the plugin.
echo "Activating plugin..."
wp plugin activate $plugin_name 2>&1>/dev/null && {
echo `color g Plugin $plugin_name has been updated to version $version.`
exit
} || {
fail "There was an error activating the plugin."
}
}
# Run now.
run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment