lundi 10 août 2020

Nesting if-then statements with wp-cli variables

I am using wp-cli and bash scripting to change WordPress home and siteurl database entries on multiple websites and want to automate it universally with this script. I normally just change it with either wp option set home <newurl> or just a global wp search-replace <oldurl> <newurl> --parameter and have to run an additional wp elementor command to change DB post content with Elementor. I'm using an inital $URL test to determine whether or not the home URL is a temporary domain (ex. http://box1234.temp.domains/~username). To avoid mixed content errors from arising and since the majority of sites are using the Elementor plugin I want to also set up a test that will run and additional search-replace using the native Elementor wp-cli wp elementor replace-urls <oldurl> <newurl> but only need to run the command if Elementor is installed and active. My dilemma is that I'm gathering the oldurl and newurl from a variable set earlier in the script which may or may not be a temp domain name set by the host or just the http:// version of the domain and I need to nest an additional if-then-else statement to get this wp elementor command to run correctly but am not certain how to get it to work.

read -ep "Enter the domain: " DOMAINCOM ; 

URL=$(echo $(wp option get home | grep '~'));

if [ $URL] ; 
  then 
    wp search-replace $url https://$DOMAINCOM; 
    wp elementor replace-urls $URL https://$DOMAINCOM; 
  else 
    wp search-replace http://$DOMAINCOM https://$DOMAINCOM ; 
    wp elementor replace-urls http://$DOMAINCOM https://$DOMAINCOM 
fi 

I need to add the below test to the above script to replace* the wp elementor commands in both the then and else statement to account for whether or not the home URL turns out to be a temp domain or not.

ELEMENTORCHECK=$(wp plugin status elementor | grep -i status | cut -d: -f2); 
if [ $ELEMENTORCHECK= Active ] ; 
  then 
    wp elementor replace-urls $url https://$DOMAINCOM; 
  else 
    echo "Elementor isn't installed" | grep Elementor ; 
fi ; 

I don't think doing a if [ $URL ] && [ $ELEMENTORCHECK ] or if [ $URL ] || [ $ELEMENTORCHECK ] would work and am not sure how to nest the tests using an elif statement without messing up the whole statement and need some guidance. Hope this question makes sense.

Aucun commentaire:

Enregistrer un commentaire