146 lines
5.5 KiB
PHP
146 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\User;
|
|
use App\Models\Wallet;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class SyncWordPressUsers extends Command
|
|
{
|
|
protected $signature = 'ifnex:sync-wp-users
|
|
{--wp-db-host=localhost : WordPress database host}
|
|
{--wp-db-port=3306 : WordPress database port}
|
|
{--wp-db-name= : WordPress database name}
|
|
{--wp-db-user=root : WordPress database user}
|
|
{--wp-db-pass= : WordPress database password}
|
|
{--wp-table-prefix=wp_ : WordPress table prefix}';
|
|
|
|
protected $description = 'Sync WordPress users to Laravel';
|
|
|
|
public function handle()
|
|
{
|
|
$this->info('🔄 Syncing WordPress users to Laravel...');
|
|
$this->newLine();
|
|
|
|
// دریافت اطلاعات دیتابیس وردپرس
|
|
$wpHost = $this->option('wp-db-host');
|
|
$wpPort = $this->option('wp-db-port');
|
|
$wpDbName = $this->option('wp-db-name');
|
|
$wpUser = $this->option('wp-db-user');
|
|
$wpPass = $this->option('wp-db-pass');
|
|
$wpPrefix = $this->option('wp-table-prefix');
|
|
|
|
if (empty($wpDbName)) {
|
|
$wpDbName = $this->ask('WordPress database name?');
|
|
}
|
|
|
|
try {
|
|
// اتصال به دیتابیس وردپرس
|
|
$wpDb = new \PDO(
|
|
"mysql:host={$wpHost};port={$wpPort};dbname={$wpDbName};charset=utf8mb4",
|
|
$wpUser,
|
|
$wpPass,
|
|
[\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]
|
|
);
|
|
|
|
$this->info("✅ Connected to WordPress database: {$wpDbName}");
|
|
} catch (\PDOException $e) {
|
|
$this->error("❌ Failed to connect to WordPress database: " . $e->getMessage());
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
// دریافت همه کاربران وردپرس
|
|
$stmt = $wpDb->query("
|
|
SELECT
|
|
u.ID as id,
|
|
u.user_login as login,
|
|
u.user_email as email,
|
|
u.user_registered as registered,
|
|
u.display_name as name,
|
|
um1.meta_value as first_name,
|
|
um2.meta_value as last_name,
|
|
um3.meta_value as phone
|
|
FROM {$wpPrefix}users u
|
|
LEFT JOIN {$wpPrefix}usermeta um1 ON u.ID = um1.user_id AND um1.meta_key = 'first_name'
|
|
LEFT JOIN {$wpPrefix}usermeta um2 ON u.ID = um2.user_id AND um2.meta_key = 'last_name'
|
|
LEFT JOIN {$wpPrefix}usermeta um3 ON u.ID = um3.user_id AND um3.meta_key = 'phone'
|
|
WHERE u.user_email != ''
|
|
ORDER BY u.ID
|
|
");
|
|
|
|
$wpUsers = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
$this->info("📊 Found " . count($wpUsers) . " WordPress users");
|
|
$this->newLine();
|
|
|
|
$created = 0;
|
|
$updated = 0;
|
|
$skipped = 0;
|
|
|
|
foreach ($wpUsers as $wpUser) {
|
|
$email = trim($wpUser['email']);
|
|
|
|
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$skipped++;
|
|
continue;
|
|
}
|
|
|
|
// ساخت نام کامل
|
|
$name = trim(($wpUser['first_name'] ?? '') . ' ' . ($wpUser['last_name'] ?? ''));
|
|
if (empty($name)) {
|
|
$name = $wpUser['name'] ?? $wpUser['login'];
|
|
}
|
|
|
|
// بررسی وجود کاربر در Laravel
|
|
$laravelUser = User::where('email', $email)->first();
|
|
|
|
if ($laravelUser) {
|
|
// بهروزرسانی اطلاعات
|
|
$laravelUser->update([
|
|
'name' => $name,
|
|
'phone' => $wpUser['phone'] ?? $laravelUser->phone,
|
|
]);
|
|
$updated++;
|
|
$this->line(" ✏️ Updated: {$email}");
|
|
} else {
|
|
// ساخت کاربر جدید
|
|
$laravelUser = User::create([
|
|
'name' => $name,
|
|
'email' => $email,
|
|
'phone' => $wpUser['phone'] ?? null,
|
|
'password' => Hash::make('TempPass@12345'), // رمز موقت
|
|
'email_verified_at' => now(),
|
|
'role' => 'customer',
|
|
]);
|
|
|
|
// ساخت کیف پول
|
|
Wallet::create([
|
|
'user_id' => $laravelUser->id,
|
|
'balance' => 0,
|
|
'total_deposited' => 0,
|
|
'total_withdrawn' => 0,
|
|
'is_frozen' => false,
|
|
]);
|
|
|
|
// اختصاص نقش customer
|
|
$laravelUser->assignRole('customer');
|
|
|
|
$created++;
|
|
$this->line(" ✅ Created: {$email} (Password: TempPass@12345)");
|
|
}
|
|
}
|
|
|
|
$this->newLine();
|
|
$this->info('═══════════════════════════════════════');
|
|
$this->info(" 📊 Sync Summary");
|
|
$this->info('═══════════════════════════════════════');
|
|
$this->info(" ✅ Created: {$created}");
|
|
$this->info(" ✏️ Updated: {$updated}");
|
|
$this->info(" ⏭️ Skipped: {$skipped}");
|
|
$this->info('═══════════════════════════════════════');
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
} |