captaincore/lib/remote-scripts/update
2024-11-21 11:03:11 -05:00

171 lines
6.1 KiB
Bash

#!/usr/bin/env bash
#
# Update themes and plugins
#
# `update`
#
# [--<field>=<value>]
# Extra arguments to pass to `[wp theme update](https://developer.wordpress.org/cli/commands/theme/update/)` and `[wp plugin update]( https://developer.wordpress.org/cli/commands/plugin/update/)`.
#
# Loop through arguments and separate regular arguments from flags
for arg in "$@"; do
# Add to arguments array. (Does not starts with "--")
if [[ $arg != --* ]]; then
count=1+${#arguments[*]}
arguments[$count]=$arg
continue
fi
# Remove leading "--"
flag_name=$( echo $arg | cut -c 3- )
# Add to flags array
count=1+${#flags[*]}
if [[ "$arg" != "--script"* ]] && [[ "$arg" != *"--exclude_plugins"* ]] && [[ "$arg" != "--exclude_themes"* ]] && [[ "$arg" != "--site"* ]] && [[ "$arg" != "--provider"* ]] && [[ "$arg" != "--captain_id"* ]] && [[ "$arg" != "--process_id"* ]]; then
# Remove first and last quote if found
arg_for_flag="${arg%\"}"
arg_for_flag="${arg_for_flag/=\"/=}"
flags[$count]=$arg_for_flag
fi
# Process flags without data (Assign to variable)
if [[ $arg != *"="* ]]; then
flag_name=${flag_name//-/_}
declare "$flag_name"=true
fi
# Process flags with data (Assign to variable)
if [[ $arg == *"="* ]]; then
flag_value=$( echo $flag_name | perl -n -e '/.+?=(.+)/&& print $1' ) # extract value
flag_name=$( echo $flag_name | perl -n -e '/(.+?)=.+/&& print $1' ) # extract name
flag_name=${flag_name/-/_}
# Remove first and last quote if found
flag_value="${flag_value%\"}"
flag_value="${flag_value#\"}"
declare "$flag_name"="$flag_value"
continue
fi
done
# Store current path
home_directory=$(pwd)
run_command() {
if $( wp theme is-active Newspaper ); then
required_theme_plugins=()
active_plugins=( $( wp plugin list --field=name ) )
for slug in ${active_plugins[@]}; do
if [[ "$slug" == "td-"* ]]; then
required_theme_plugins+=( $slug )
fi
done
read -r -d '' php_code << heredoc
<?php
\$theme_info = tagdiv_check_theme_version()[0];
\$theme_version = wp_get_theme()->display( 'Version' );
\$theme_version_to_check = array_keys( \$theme_info )[0];
\$theme_url = \$theme_info[ \$theme_version_to_check ];
if ( version_compare( \$theme_version, \$theme_version_to_check, "<" ) ) {
echo "theme install \$theme_url --force --skip-plugins=wps-hide-login";
}
heredoc
# Run Newspaper theme update if found
command=$( echo -n "$php_code" | wp eval-file - )
if [[ "$command" != "" ]]; then
silent=$( wp $command 2>&1 )
fi
fi
if [[ $exclude_themes == "" ]]; then
wp theme update ${flags[@]} --exec="define( 'WP_ADMIN', true );" --skip-plugins=wps-hide-login
else
wp theme update ${flags[@]} --exec="define( 'WP_ADMIN', true );" --exclude=$exclude_themes --skip-plugins=wps-hide-login
fi
echo " "
if [[ $exclude_plugins == "" ]]; then
wp plugin update ${flags[@]} --exec="define( 'WP_ADMIN', true );" --skip-plugins=wps-hide-login
silent=$( wp cache flush )
silent=$( wp plugin list )
silent=$( wp plugin update ${flags[@]} --exec="define( 'WP_ADMIN', true );" --skip-plugins=wps-hide-login )
else
wp plugin update ${flags[@]} --exec="define( 'WP_ADMIN', true );" --exclude=$exclude_plugins --skip-plugins=wps-hide-login
silent=$( wp cache flush )
silent=$( wp plugin list )
silent=$( wp plugin update ${flags[@]} --exec="define( 'WP_ADMIN', true );" --exclude=$exclude_plugins --skip-plugins=wps-hide-login )
fi
# Handle WooCommerce database updates if installed
if $( wp plugin is-installed woocommerce --skip-plugins --skip-themes ); then
silent=$( wp wc update 2>&1 )
# Handle WooCommerce database updates on multisite if installed
if $( wp core is-installed --network ); then
for site_id in $( wp site list --field=blog_id ); do
site_url=$( wp site list --field=url --blog_id=${site_id} )
if $( wp plugin is-active woocommerce --url=$site_url ); then
silent=$( wp wc update --url=${site_url} 2>&1 )
fi
done
fi
fi
if $( wp theme is-active Newspaper ); then
for slug in ${required_theme_plugins[@]}; do
command="\$key = array_search( \"$slug\", array_column( tagdiv_global::\$theme_plugins_list, \"slug\" ) ); echo tagdiv_global::\$theme_plugins_list[\$key][\"source\"];";
silent=$( wp plugin install $( wp eval "$command" --skip-plugins ) --force --activate 2>&1 )
done
fi
# Handle AdRotate Professional database updates if installed
if $( wp plugin is-installed adrotate-pro --skip-plugins --skip-themes ); then
silent=$( wp eval "adrotate_check_upgrade();" 2>&1 )
fi
# Purge Autoptimize cache if installed
if $( wp plugin is-installed autoptimize --skip-plugins --skip-themes ); then
silent=$( wp autoptimize clear 2>&1 )
fi
# Handle Elementor database updates if installed
if $( wp plugin is-installed elementor --skip-plugins --skip-themes ); then
silent=$( wp elementor update db 2>&1 )
silent=$( wp elementor flush_css 2>&1 )
# Handle Elementor database updates on multisite if installed
if $( wp core is-installed --network ); then
silent=$( wp elementor update db --network 2>&1 )
fi
fi
# Handle Elementor Pro database updates if installed
if $( wp plugin is-installed elementor-pro --skip-plugins --skip-themes ); then
silent=$( wp elementor-pro update db 2>&1 )
# Handle Elementor Pro database updates on multisite if installed
if $( wp core is-installed --network ); then
silent=$( wp elementor-pro update db --network 2>&1 )
fi
fi
# Handle redirection database updates if installed
if $( wp plugin is-installed redirection --skip-plugins --skip-themes ); then
silent=$( wp redirection database upgrade 2>&1 )
fi
silent=$( wp cache flush )
if [[ $provider == "kinsta" ]]; then
silent=$( wp kinsta cache purge --all )
fi
# Update language packs
silent=$( wp language core update )
silent=$( wp language plugin update --all )
silent=$( wp language theme update --all )
silent=$( wp maintenance-mode deactivate --skip-plugins --skip-themes )
}
run_command