#!/bin/bash # Deploy WordPress plugin to staging server with safety measures # Enhanced version with path validation and dry-run option # Load configuration file if [ -z "$1" ]; then echo "Usage: $0 --config [--dry-run]" exit 1 fi DRY_RUN=false while [ "$1" != "" ]; do case $1 in --config ) shift if [ -z "$1" ]; then echo "Error: --config requires a value" exit 1 fi CONFIG_FILE="$1" shift ;; --dry-run ) DRY_RUN=true shift ;; * ) echo "Error: Invalid argument: $1" exit 1 esac done if [ ! -f "$CONFIG_FILE" ]; then echo "Error: Configuration file not found: $CONFIG_FILE" exit 1 fi source "$CONFIG_FILE" # Check required variables if [ -z "$REMOTE_HOST" ] || [ -z "$REMOTE_USER" ] || [ -z "$REMOTE_PATH_BASE" ] || [ -z "$PLUGIN_SLUG" ] || [ -z "$REMOTE_PLUGIN_PATH" ] || [ -z "$LOCAL_PLUGIN_PATH" ]; then echo "Error: Missing required variables in configuration file." exit 1 fi # Validate paths to ensure we're only modifying plugin directory if [[ "$REMOTE_PLUGIN_PATH" != *"/wp-content/plugins/$PLUGIN_SLUG"* ]]; then echo "Error: Remote plugin path does not appear to be within the WordPress plugins directory." echo "Expected path pattern: */wp-content/plugins/$PLUGIN_SLUG*" echo "Actual path: $REMOTE_PLUGIN_PATH" exit 1 fi if [[ "$LOCAL_PLUGIN_PATH" != *"/wp-content/plugins/$PLUGIN_SLUG"* ]]; then echo "Error: Local plugin path does not appear to be within the WordPress plugins directory." echo "Expected path pattern: */wp-content/plugins/$PLUGIN_SLUG*" echo "Actual path: $LOCAL_PLUGIN_PATH" exit 1 fi # Create backup of plugin directory on staging server echo "Creating backup of plugin directory on staging server..." if [ "$DRY_RUN" = false ]; then ssh "$REMOTE_USER@$REMOTE_HOST" "if [ -d \"$REMOTE_PLUGIN_PATH\" ]; then cp -r \"$REMOTE_PLUGIN_PATH\" \"${REMOTE_PLUGIN_PATH}_backup_$(date +%Y%m%d%H%M%S)\"; fi" if [ $? -ne 0 ]; then echo "Warning: Failed to create backup on staging server." fi else echo "[DRY RUN] Would execute: ssh \"$REMOTE_USER@$REMOTE_HOST\" \"if [ -d \\\"$REMOTE_PLUGIN_PATH\\\" ]; then cp -r \\\"$REMOTE_PLUGIN_PATH\\\" \\\"${REMOTE_PLUGIN_PATH}_backup_$(date +%Y%m%d%H%M%S)\\\"; fi\"" fi # Rsync the plugin files echo "Deploying plugin $PLUGIN_SLUG to staging server..." # Change to project root before rsync # === Custom: Copy required test config files into plugin directory before rsync === cp -f "$PROJECT_ROOT/wordpress-dev/tests/bootstrap-staging.php" "$LOCAL_PLUGIN_PATH/bootstrap-staging.php" cp -f "$PROJECT_ROOT/wordpress-dev/tests/wp-tests-config-staging.php" "$LOCAL_PLUGIN_PATH/wp-tests-config-staging.php" # Only copy phpunit-staging.xml if it exists and is explicitly needed for test execution if [ -f "$PROJECT_ROOT/wordpress-dev/tests/phpunit-staging.xml" ]; then cp -f "$PROJECT_ROOT/wordpress-dev/tests/phpunit-staging.xml" "$LOCAL_PLUGIN_PATH/phpunit-staging.xml" fi cd ../.. RSYNC_CMD="rsync -avz --delete \ --exclude '.git' \ --exclude 'node_modules' \ --include 'tests/' \ --include 'tests/unit/' \ --include 'tests/unit/*.php' \ --include 'tests/test-doubles.php' \ --include 'tests/bootstrap.php' \ --include 'composer.json' \ --include 'composer.lock' \ --include 'hvac-community-events.php' \ --include 'phpunit.xml.dist' \ --include 'wp-tests-config.php' \ \"$LOCAL_PLUGIN_PATH/\" \ \"$REMOTE_USER@$REMOTE_HOST:$REMOTE_PLUGIN_PATH\"" if [ "$DRY_RUN" = true ]; then echo "[DRY RUN] Would execute: $RSYNC_CMD" else eval $RSYNC_CMD if [ $? -ne 0 ]; then echo "Error: rsync failed." exit 1 fi fi # Change back to original directory (optional, but good practice) # cd - echo "Plugin deployment completed successfully." # Optional: Install Composer dependencies on staging echo "Installing Composer dependencies on staging server..." if [ "$DRY_RUN" = true ]; then echo "[DRY RUN] Would execute: ssh \"$REMOTE_USER@$REMOTE_HOST\" \"cd $REMOTE_PLUGIN_PATH && composer install\"" else ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PLUGIN_PATH && composer install" if [ $? -ne 0 ]; then echo "Warning: Composer installation failed." fi fi # Activate plugin to ensure hooks fire properly echo "Activating plugin..." if [ "$DRY_RUN" = true ]; then echo "[DRY RUN] Would execute: ssh \"$REMOTE_USER@$REMOTE_HOST\" \"cd $REMOTE_PATH_BASE && wp plugin deactivate $PLUGIN_SLUG --allow-root && wp plugin activate $PLUGIN_SLUG --allow-root\"" else ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH_BASE && wp plugin deactivate $PLUGIN_SLUG --allow-root 2>/dev/null || true" ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH_BASE && wp plugin activate $PLUGIN_SLUG --allow-root" if [ $? -eq 0 ]; then echo "Plugin activated successfully." else echo "Warning: Plugin activation failed." fi fi # Clear Breeze cache after plugin activation echo "Clearing Breeze cache..." if [ "$DRY_RUN" = true ]; then echo "[DRY RUN] Would clear Breeze cache" else # Use existing cache clearing script if available if [ -f "$(dirname "$0")/clear-breeze-cache.sh" ]; then echo "Using existing cache clearing script..." "$(dirname "$0")/clear-breeze-cache.sh" else echo "Performing manual Breeze cache clearing..." # Clear Breeze cache files ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH_BASE && find wp-content/cache/breeze -type f -name '*.php' -delete 2>/dev/null || true" # Clear Breeze minified files ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH_BASE && rm -rf wp-content/uploads/breeze/js/* wp-content/uploads/breeze/css/* 2>/dev/null || true" # Clear WordPress caches ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH_BASE && wp cache flush --allow-root 2>/dev/null || true" ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH_BASE && wp transient delete --all --allow-root 2>/dev/null || true" # Clear Breeze-specific transients ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH_BASE && wp transient delete '_breeze_minification' --allow-root 2>/dev/null || true" fi echo "Cache clearing completed." fi # Flush rewrite rules echo "Flushing rewrite rules..." if [ "$DRY_RUN" = true ]; then echo "[DRY RUN] Would flush rewrite rules" else ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH_BASE && wp rewrite flush --allow-root 2>/dev/null || true" echo "Rewrite rules flushed." fi # Verify deployment echo "Verifying deployment..." if [ "$DRY_RUN" = true ]; then echo "[DRY RUN] Would execute: ssh \"$REMOTE_USER@$REMOTE_HOST\" \"ls -la $REMOTE_PLUGIN_PATH\"" else ssh "$REMOTE_USER@$REMOTE_HOST" "ls -la $REMOTE_PLUGIN_PATH" if [ $? -ne 0 ]; then echo "Warning: Verification failed." fi fi exit 0