diff --git a/modules/website/wordpress/wordpress-cron-manager.sh b/modules/website/wordpress/wordpress-cron-manager.sh index 37f45f8..0764c2a 100755 --- a/modules/website/wordpress/wordpress-cron-manager.sh +++ b/modules/website/wordpress/wordpress-cron-manager.sh @@ -120,6 +120,26 @@ get_wp_sites_cached() { echo "$WP_SITES_CACHE" } +# OPTIMIZATION: User extraction caching (memoization) +# extract_user_from_path() called 10 times, often for same path +# Cache results to avoid redundant extraction operations +declare -gA USER_EXTRACTION_CACHE + +get_user_from_path_cached() { + local site_path="$1" + + # Check if already in cache + if [ -n "${USER_EXTRACTION_CACHE[$site_path]}" ]; then + echo "${USER_EXTRACTION_CACHE[$site_path]}" + return 0 + fi + + # Not in cache, extract and cache result + local user=$(extract_user_from_path "$site_path") + USER_EXTRACTION_CACHE[$site_path]="$user" + echo "$user" +} + # Function to safely add cron job to user's crontab # Returns 0 on success, 1 on failure safe_add_cron_job() { @@ -338,6 +358,58 @@ is_valid_username_format() { return 1 } +# OPTIMIZATION: Logging wrapper for consistent output formatting (39 occurrences) +# Provides foundation for future logging to file capability +# Usage: log_info "Processing site", log_success "Done", log_error "Failed" +declare -g LOG_ENABLED=false +declare -g LOG_FILE="" + +# Log message with level +log_message() { + local level="$1" + local message="$2" + + case "$level" in + INFO) + echo -e "${CYAN}[INFO]${NC} $message" + ;; + SUCCESS) + echo -e "${GREEN}[✓]${NC} $message" + ;; + WARNING) + echo -e "${YELLOW}[⚠]${NC} $message" + ;; + ERROR) + echo -e "${RED}[✗]${NC} $message" + ;; + *) + echo "$message" + ;; + esac + + # Write to log file if enabled + if [ "$LOG_ENABLED" = "true" ] && [ -n "$LOG_FILE" ]; then + echo "[$level] $message" >> "$LOG_FILE" 2>/dev/null + fi +} + +# Convenience wrappers for common log levels +log_info() { + log_message "INFO" "$1" +} + +log_success() { + log_message "SUCCESS" "$1" +} + +log_warning() { + log_message "WARNING" "$1" +} + +log_error() { + log_message "ERROR" "$1" +} + # OPTIMIZATION: Build cron command consistently # Centralizes cron command format (appears 4 times throughout script) # Returns: cron command string for wp-cron.php execution