Skip to content

Instantly share code, notes, and snippets.

@milardovich
Created March 31, 2025 20:55
Show Gist options
  • Save milardovich/0a7a9377b8d6b1e1e928596fccc4ab04 to your computer and use it in GitHub Desktop.
Save milardovich/0a7a9377b8d6b1e1e928596fccc4ab04 to your computer and use it in GitHub Desktop.
Check for diffs before updating wordpress plugins
#!/bin/bash
# WordPress Plugin Update Checker
# =============================
#
# Author: Sergio Milardovich
#
# This script helps maintain WordPress plugins by checking for custom modifications
# before updating them. It's particularly useful in development environments where
# plugins might have been modified for testing or customization purposes.
#
# Features:
# - Checks if a plugin has been modified by comparing it with the WordPress.org version
# - Shows detailed differences between local and WordPress.org versions
# - Can process a single plugin or all installed plugins
# - Logs all operations to output.txt for review
# - Prevents updates of modified plugins to avoid losing custom changes
#
# TODO:
# - Implement proper premium plugin detection using WP-CLI or WordPress.org API
# Currently, the premium check is commented out as it's not reliable
#
# Usage:
# ./check-plugin-updates.sh [--path=/path/to/wordpress] [plugin-slug]
# If no plugin-slug is provided, it will process all installed plugins
#
# Output:
# - Shows results in terminal
# - Saves detailed log to output.txt in the current directory
# - Displays file differences if any are found
#
# Example:
# ./check-plugin-updates.sh contact-form-7
# ./check-plugin-updates.sh --path=/var/www/html contact-form-7
# ./check-plugin-updates.sh # Process all plugins
#
# Note: This script should be run from within the WordPress container
# and requires WP-CLI to be available.
# Parse command line arguments
WP_PATH=""
PLUGIN_SLUG=""
while [[ $# -gt 0 ]]; do
case $1 in
--path=*)
WP_PATH="${1#*=}"
shift
;;
*)
PLUGIN_SLUG="$1"
shift
;;
esac
done
# Set WordPress root directory
if [ -z "$WP_PATH" ]; then
WORDPRESS_ROOT=$(pwd)
else
WORDPRESS_ROOT="$WP_PATH"
fi
# Verify WordPress installation
if [ ! -f "$WORDPRESS_ROOT/wp-config.php" ]; then
echo "Error: WordPress installation not found at $WORDPRESS_ROOT"
exit 1
fi
# Default temp directory if not defined in wp-config.php
DEFAULT_TEMP_DIR="$WORDPRESS_ROOT/wp-content/temp"
# Function to get WordPress temp directory from wp-config.php
get_wp_temp_dir() {
if [ -f "$WORDPRESS_ROOT/wp-config.php" ]; then
TEMP_DIR=$(grep -o "define.*WP_TEMP_DIR.*'[^']*'" "$WORDPRESS_ROOT/wp-config.php" | cut -d"'" -f4)
if [ ! -z "$TEMP_DIR" ]; then
echo "$TEMP_DIR"
return 0
fi
fi
echo "$DEFAULT_TEMP_DIR"
}
# Get the temp directory
TEMP_DIR=$(get_wp_temp_dir)
echo "Using temp directory: $TEMP_DIR"
# Function to check if a plugin is premium
is_premium_plugin() {
local plugin_slug=$1
local version=$2
# Try to download the plugin from wordpress.org
if wget --spider "https://downloads.wordpress.org/plugin/$plugin_slug.$version.zip" 2>/dev/null; then
return 1 # Plugin exists on wordpress.org, so it's not premium
else
return 0 # Plugin doesn't exist on wordpress.org, so it's premium
fi
}
# Function to download and extract plugin
download_plugin() {
local plugin_slug=$1
local version=$2
mkdir -p "$TEMP_DIR"
cd "$TEMP_DIR"
# Download the plugin
wget "https://downloads.wordpress.org/plugin/$plugin_slug.$version.zip"
# Extract the plugin
unzip "$plugin_slug.$version.zip"
# Clean up zip file
rm "$plugin_slug.$version.zip"
}
# Function to compare directories
compare_directories() {
local dir1=$1
local dir2=$2
# Check if directories exist
if [ ! -d "$dir1" ]; then
echo "Error: Local plugin directory '$dir1' does not exist."
return 1
fi
if [ ! -d "$dir2" ]; then
echo "Error: Downloaded plugin directory '$dir2' does not exist."
return 1
fi
echo "Checking for modified files..."
echo "----------------------------------------"
# First, show files that exist in one directory but not in the other
echo "Files only in local version:"
diff -rq "$dir1" "$dir2" | grep "Only in $dir1" | sed "s|Only in $dir1/||" || true
echo "----------------------------------------"
echo "Files only in WordPress.org version:"
diff -rq "$dir1" "$dir2" | grep "Only in $dir2" | sed "s|Only in $dir2/||" || true
echo "----------------------------------------"
# Then show files that have different content
echo "Files with different content:"
diff -rq "$dir1" "$dir2" | grep "Files" | cut -d' ' -f4 | sed "s|$dir1/||" || true
echo "----------------------------------------"
# Finally, show the actual differences in the files
echo "Detailed differences:"
diff -r "$dir1" "$dir2" | grep -v "^Only in" | grep -v "^Common subdirectories" || true
# Return the diff status
diff -r "$dir1" "$dir2" > /dev/null 2>&1
return $?
}
# Function to process a single plugin
process_plugin() {
local plugin_slug=$1
local output_file=$2
echo "Processing plugin: $plugin_slug" | tee -a "$output_file"
# Get current version
CURRENT_VERSION=$(wp plugin list --path="$WORDPRESS_ROOT" --name=$plugin_slug --format=csv --fields=version --allow-root | tail -n 1)
if [ -z "$CURRENT_VERSION" ]; then
echo "Error: Plugin $plugin_slug not found." | tee -a "$output_file"
return 1
fi
echo "Current version: $CURRENT_VERSION" | tee -a "$output_file"
# Create temporary directory for comparison
mkdir -p "$TEMP_DIR"
# Download current version for comparison
echo "Downloading current version for comparison..." | tee -a "$output_file"
download_plugin "$plugin_slug" "$CURRENT_VERSION"
# Compare directories
echo "Comparing directories..." | tee -a "$output_file"
if ! compare_directories "$WORDPRESS_ROOT/wp-content/plugins/$plugin_slug" "$TEMP_DIR/$plugin_slug"; then
echo "Error: Local plugin files differ from WordPress.org version." | tee -a "$output_file"
echo "This might indicate custom modifications. Update aborted." | tee -a "$output_file"
rm -rf "$TEMP_DIR"
return 1
fi
# If we get here, files are identical, proceed with update
echo "Plugin files verified. Proceeding with update..." | tee -a "$output_file"
wp plugin update "$plugin_slug" --path="$WORDPRESS_ROOT" --version="$CURRENT_VERSION" --allow-root
# Cleanup
rm -rf "$TEMP_DIR"
echo "Update process completed for $plugin_slug" | tee -a "$output_file"
echo "----------------------------------------" | tee -a "$output_file"
}
# Main script
OUTPUT_FILE="$WORDPRESS_ROOT/output.txt"
echo "Starting plugin update check at $(date)" > "$OUTPUT_FILE"
if [ -z "$PLUGIN_SLUG" ]; then
echo "No plugin specified. Processing all installed plugins..." | tee -a "$OUTPUT_FILE"
# Get list of all installed plugins
PLUGINS=$(wp plugin list --path="$WORDPRESS_ROOT" --format=csv --fields=name --allow-root | tail -n +2)
for plugin in $PLUGINS; do
process_plugin "$plugin" "$OUTPUT_FILE"
done
else
process_plugin "$PLUGIN_SLUG" "$OUTPUT_FILE"
fi
echo "Plugin update check completed at $(date)" | tee -a "$OUTPUT_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment