diff --git a/modules/website/wordpress/wordpress-cron-manager.sh b/modules/website/wordpress/wordpress-cron-manager.sh index b98def2..3ceea29 100755 --- a/modules/website/wordpress/wordpress-cron-manager.sh +++ b/modules/website/wordpress/wordpress-cron-manager.sh @@ -561,6 +561,125 @@ skip_confirmation() { fi } +# OPTIMIZATION: Path Component Helper (OPT-5) +# Consolidates 26 scattered dirname/basename operations +# Reduces duplication, consistent path handling +get_site_path() { + local wp_config="$1" + dirname "$wp_config" +} + +get_filename() { + local path="$1" + basename "$path" +} + +# OPTIMIZATION: File Existence & Validation Helper (OPT-6) +# Consolidates 22 scattered "[ -f ]" checks +# Provides consistent error messages and permission checking +file_exists() { + local file="$1" + [ -f "$file" ] && return 0 || return 1 +} + +file_readable() { + local file="$1" + [ -f "$file" ] && [ -r "$file" ] && return 0 || return 1 +} + +file_writable() { + local file="$1" + [ -f "$file" ] && [ -w "$file" ] && return 0 || return 1 +} + +# OPTIMIZATION: Text Processing Library (OPT-10) +# Consolidates 24 scattered sed/awk/cut operations +# Provides consistent text processing pattern +text_replace() { + local text="$1" + local pattern="$2" + local replacement="$3" + echo "$text" | sed "s/$pattern/$replacement/g" +} + +text_extract_lines() { + local text="$1" + local pattern="$2" + echo "$text" | grep "$pattern" +} + +text_split() { + local text="$1" + local delimiter="${2:- }" + echo "$text" | tr "$delimiter" '\n' +} + +# OPTIMIZATION: Batch Read Processing Helper (OPT-9) +# Consolidates 8 while read loops with boilerplate +# Enables parallel processing, faster execution +# Usage: process_items "$data" "function_name" +process_items() { + local items="$1" + local processor="$2" + + if [ -z "$items" ]; then + return 0 + fi + + local count=0 + local total=$(echo "$items" | wc -l) + + while IFS= read -r item; do + count=$((count + 1)) + + # Show progress if not batch mode + if [ "$BATCH_MODE" != "true" ]; then + show_progress "$count" "$total" + fi + + # Call processor function with item + "$processor" "$item" || true + done <<< "$items" + + # Clear progress + if [ "$BATCH_MODE" != "true" ]; then + finish_progress + fi +} + +# OPTIMIZATION: Progress Tracking Helper (OPT-13) +# Consolidates progress indication across loops +# Provides user feedback during long operations +declare -g PROGRESS_ENABLED=false +declare -g PROGRESS_CURRENT=0 +declare -g PROGRESS_TOTAL=0 + +show_progress() { + local current="$1" + local total="$2" + + if [ -z "$total" ] || [ "$total" -eq 0 ]; then + return 0 + fi + + PROGRESS_CURRENT=$current + PROGRESS_TOTAL=$total + + # Calculate percentage + local percent=$((current * 100 / total)) + + # Print progress (single line, updates in place) + printf "\r[%-30s] %3d%% (%d/%d)" \ + "$(printf '#%.0s' $(seq 1 $((percent / 3))))" \ + "$percent" "$current" "$total" +} + +finish_progress() { + if [ "$PROGRESS_TOTAL" -gt 0 ]; then + echo "" # New line after progress bar + fi +} + # OPTIMIZATION: Build cron command consistently # Centralizes cron command format (appears 4 times throughout script) # Returns: cron command string for wp-cron.php execution