Files
shell_scripts/wp-cli/install_wp.sh

182 lines
5.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# WordPress installer
# ATTENTION: si wp-cli.local.yml est présent, wp l'utilise !
if [ "$1" == "--help" ]; then
echo -e "\033[93minstall_wp.sh\033[0m"
echo
echo "Installateur WordPress"
echo
echo "Requiert:"
echo " - wp-cli (https://wp-cli.org/)"
echo
echo "Attention:"
echo " - si wp-cli.local.yml est présent dans le répertoire d'installation (\$wordpress_path), wp-cli l'utilise !"
echo
echo "Configuration:"
echo -e " - \033[4m\$wordpress_path\033[0m est le chemin où sera installé WordPress"
echo -e " - \033[4m\$path\033[0m est le nom du répertoire d'installation (wordpress par défaut)"
echo -e " - \033[4m\$url\033[0m est l'adresse du site:"
echo " - \$url=http://localhost - WP sera installé à la racine du site."
echo " - \$url=http://localhost/wordpress - WP sera installé dans le dossier \$path"
echo
echo "USAGE: install_wp_yml"
echo
echo " --help display this help"
echo
exit 0
fi
# Paramètres:
#bdd
dbhost=localhost
dbuser=xxx
dbpass='xxx'
#
dbname='wordpress4'
dbprefix='wp4_'
#wp-cli
path=wordpress
wordpress_path='/home/bruno/Sites'
url=http://localhost/wordpress
# Administrateur
title="My WordPress Blog"
admin_user=xxx
admin_password='xxx'
admin_email='xxx'
# Thèmes activé, plugins installés
default_theme=twentyfourteen
defaults_plugins='admin-post-navigation meow-lightbox wp-retina-2x-pro sola-newsletters'
# Version de WordPress
locale=fr_FR
version=4.8
# extra_php is an array: array=("value1" "value2")
extra_php=("define( 'WP_DEBUG', true );" "define( 'WP_DEBUG_LOG', true );")
# Packages wp-cli installés
defaults_packages="wp-cli/admin-command trepmal/wp-revisions-cli"
# Ouvrir la page d'aministration
admin=true
source pass.cfg
# /Paramètres
cd $wordpress_path
# Il y a déjà une installation de WordPress ?
exist=$(wp find . | sed -n '1!p' | awk '{print$1}')
# Si oui, on la supprime
if [ -n "$exist" ]; then
# /home/bruno/Sites/wordpress/wp-includes/version.php
wp_exist="$(dirname $(dirname $exist))"
wp_folder_name=$(echo "$exist" | awk -F "/" '{for (i=1;i<=NF;i++) if ($i=="wp-includes") print $(i-1)}')
read -p $'\n\033[1;31mWordPress is already installed. Do you really want to erase it ? \033[39m(y/n)\033[0m' choice
case "$choice" in
y|Y|o ) cd "$wp_folder_name/" && wp db drop --yes && cd "$wordpress_path" && rm -rf "$wp_folder_name/" && echo "Deleting directory '$wordpress_path/$wp_folder_name'.";;
* ) echo "Ok, let's exit" && exit 0;;
# * ) echo "invalid";;
esac
fi
# On récupère la liste des packages installés
echo "Get WP-CLI packages already installed list..."
packages_list=$(wp package list | sed -n '1!p' | awk '{print $1'})
#if [ $? != 0 ]; then echo "$packages_list"; fi
if [ -n "$packages_list" ]; then echo "Info: Installed WP-CLI packages: $packages_list" | tr '\n' ' ' | sed '$s/ $/\n/'
else echo "Info: No package installed !"
fi
# Télécharger WordPress:
echo "path: $path"
dl=$(wp core download --path=$path --locale=$locale --version=$version)
#if [ $? != 0 ]; then echo "$dl"; fi
# Créer le fichier de config (wp-config.php):
#$ wp config create --dbname=testdb --dbuser=wp --dbpass=yourpassword
# Avec Des options supplémentaires:
wp core config --path=$path --dbname=$dbname --dbuser=$dbuser --dbpass=$dbpass --dbhost=$dbhost --dbprefix=$dbprefix --extra-php <<PHP
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
PHP
# Créer la bdd 'wordpress4':
db=$(wp db create --path=$path)
#if [ $? != 0 ]; then echo "$db"; fi
# Pour connaitre les encodages utilisés:
#$ wp db cli
#MariaDB [wordpress5]> SELECT @@character_set_database, @@collation_database;
# Installer WordPress:
# Si sh: 1: /usr/sbin/sendmail: not found => ajouter --skip-email
#OK
#co=$(wp core install --path=$path --url=$url --title="echo $title" --admin_user=$admin_user --admin_password=$admin_password --admin_email=$admin_email --skip-email)
#Error: Too many positional arguments: WordPress Blog
#co=$(wp core install --path=$path --url=$url --title=$title --admin_user=$admin_user --admin_password=$admin_password --admin_email=$admin_email --skip-email)
#
co=$(wp core install --path=$path --url=$url --title="$title" --admin_user=$admin_user --admin_password=$admin_password --admin_email=$admin_email --skip-email)
#if [ $? != 0 ]; then echo "$co"; fi
# Changement de la structure des liens pour ne conserver que le nom de l'article:
rw=$(wp rewrite structure --path=$path '/%postname%')
#if [ $? != 0 ]; then echo "$rw"; fi
# Installer un thème:
#Warning: twentyseventeen: Theme already installed.
th=$(wp theme install --path=$path $default_theme)
#if [ $? != 0 ]; then echo "$th"; fi
# Activer le thème:
#Warning: The 'Twenty Seventeen' theme is already active.
ac=$(wp theme activate --path=$path $default_theme)
#if [ $? != 0 ]; then echo "$ac"; fi
# Installer et activer les plugins:
#Warning: Couldn't find 'wp-retina-2x-pro' in the WordPress.org plugin directory.
#Warning: The 'wp-retina-2x-pro' plugin could not be found.
pl=$(wp plugin install --path=$path $defaults_plugins --activate)
#if [ $? != 0 ]; then echo -e "\033[31mErrors log:\033[0m" && echo "$pl\n"; fi
wp plugin status --path=$path
wp plugin status --path=$path
# Installer les packages:
for i in $defaults_packages
do
if [[ "$packages_list" != *"$i"* ]]; then
pa=$(wp package install "$i")
#if [ $? != 0 ]; then echo "$pa"; fi
else
echo "Info: $i already installed !"
fi
done
# Ouvrir le tableau de bord dans un navigateur:
# Si le package /wp-cli/admin-command est installé, on ouvre la page d'administration
if [ "$admin" = true ]; then
echo -e "\n\033[1mOpen wp-admin/ page in browser...\033[0m"
wp admin --path=$path
fi