<?php
/**
* Plugin Name: W2W Express Migrator
* Plugin URI: https://w2wexpress.com
* Description: turkeycargo.com.tr sitesindeki tüm "Turkey Cargo" ifadelerini "W2W Express" olarak değiştirir. Kullanım: Admin paneli → W2W Migrator
* Version: 1.0.0
* Author: W2W Express
* License: GPL2
*/

if ( ! defined( 'ABSPATH' ) ) exit;

/* ------------------------------------------------------------------ */
/* Admin menüsü */
/* ------------------------------------------------------------------ */
add_action( 'admin_menu', function () {
add_menu_page(
'W2W Express Migrator',
'W2W Migrator',
'manage_options',
'w2w-migrator',
'w2w_migrator_page',
'dashicons-migrate',
30
);
} );

/* ------------------------------------------------------------------ */
/* Değiştirilecek ifadeler */
/* ------------------------------------------------------------------ */
function w2w_get_replacements() {
return [
// Marka adı — büyük/küçük harf varyasyonları
'TURKEY CARGO' => 'W2W EXPRESS',
'Turkey Cargo' => 'W2W Express',
'turkey cargo' => 'w2w express',

// Bileşik ifadeler
'Turkey Cargo Express Courier' => 'W2W Express Courier',
'Turkey Cargo Platform' => 'W2W Express Platform',
'Turkey Cargo Global Forwarding' => 'W2W Express Global Forwarding',
'Turkey Cargo International Cargo and Logistics' => 'W2W Express International Cargo and Logistics',
'Turkey Cargo couriers' => 'W2W Express couriers',
'TURKEY CARGO experts' => 'W2W EXPRESS experts',
'Turkey Cargo experts' => 'W2W Express experts',
'Powered by Turkey Cargo' => 'Powered by W2W Express',

// Alan adı & e-posta
'turkeycargo.com.tr' => 'w2wexpress.com',
'info@turkeycargo.com.tr' => 'info@w2wexpress.com',

// Sosyal medya hesapları
'TurkeyCargoIstanbul' => 'w2wexpress',
'turkeycargoexpress' => 'w2wexpress',
'turkey-cargo' => 'w2w-express',

// Sayfa başlığı / slogan
'Turkey Cargo | Tech-Driven Logistics' => 'W2W Express | Tech-Driven Logistics',
'Turkey Cargo Air & Sea Freight Forwarder' => 'W2W Express Air & Sea Freight Forwarder',
'Air & Sea Cargo - World Wide Express Cargo - Turkey Cargo' => 'Air & Sea Cargo - World Wide Express Cargo - W2W Express',
];
}

/* ------------------------------------------------------------------ */
/* Veritabanı tablolarını listele */
/* ------------------------------------------------------------------ */
function w2w_get_tables() {
global $wpdb;
return [
[ 'table' => $wpdb->posts, 'columns' => [ 'post_title', 'post_content', 'post_excerpt', 'guid' ] ],
[ 'table' => $wpdb->postmeta, 'columns' => [ 'meta_value' ] ],
[ 'table' => $wpdb->options, 'columns' => [ 'option_value' ] ],
[ 'table' => $wpdb->comments, 'columns' => [ 'comment_content', 'comment_author_url' ] ],
[ 'table' => $wpdb->termmeta, 'columns' => [ 'meta_value' ] ],
];
}

/* ------------------------------------------------------------------ */
/* Serialized veri destekli akıllı replace */
/* ------------------------------------------------------------------ */
function w2w_smart_replace( $value, array $replacements ) {
// Önce unserialize dene
$unserialized = @unserialize( $value );
if ( $unserialized !== false || $value === 'b:0;' ) {
$replaced = w2w_recursive_replace( $unserialized, $replacements );
return serialize( $replaced );
}
// JSON dene
$decoded = json_decode( $value, true );
if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
$replaced = w2w_recursive_replace( $decoded, $replacements );
return json_encode( $replaced, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
}
// Düz metin
return str_replace( array_keys( $replacements ), array_values( $replacements ), $value );
}

function w2w_recursive_replace( $data, array $replacements ) {
if ( is_string( $data ) ) {
return str_replace( array_keys( $replacements ), array_values( $replacements ), $data );
}
if ( is_array( $data ) ) {
$result = [];
foreach ( $data as $k => $v ) {
$new_k = w2w_recursive_replace( $k, $replacements );
$result[ $new_k ] = w2w_recursive_replace( $v, $replacements );
}
return $result;
}
if ( is_object( $data ) ) {
foreach ( get_object_vars( $data ) as $k => $v ) {
$data->$k = w2w_recursive_replace( $v, $replacements );
}
return $data;
}
return $data;
}

/* ------------------------------------------------------------------ */
/* DRY-RUN: kaç kayıt etkilenecek? */
/* ------------------------------------------------------------------ */
function w2w_dry_run() {
global $wpdb;
$replacements = w2w_get_replacements();
$tables = w2w_get_tables();
$total = 0;
$details = [];

foreach ( $tables as $t ) {
foreach ( $t['columns'] as $col ) {
$like_parts = [];
$params = [];
foreach ( array_keys( $replacements ) as $search ) {
$like_parts[] = "`$col` LIKE %s";
$params[] = '%' . $wpdb->esc_like( $search ) . '%';
}
$where = implode( ' OR ', $like_parts );
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$count = (int) $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM `{$t['table']}` WHERE $where",
$params
) );
if ( $count > 0 ) {
$details[] = "<tr><td><code>{$t['table']}</code></td><td><code>$col</code></td><td><strong>$count</strong></td></tr>";
$total += $count;
}
}
}
return [ 'total' => $total, 'details' => $details ];
}

/* ------------------------------------------------------------------ */
/* GERÇEK DEĞİŞİKLİK */
/* ------------------------------------------------------------------ */
function w2w_run_migration() {
global $wpdb;
$replacements = w2w_get_replacements();
$tables = w2w_get_tables();
$log = [];
$grand_total = 0;

foreach ( $tables as $t ) {
$table = $t['table'];
// Birincil anahtar
$pk = $wpdb->get_var( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = '$table'
AND COLUMN_KEY = 'PRI' LIMIT 1" );
if ( ! $pk ) continue;

foreach ( $t['columns'] as $col ) {
// İlgili satırları çek
$like_parts = [];
$params = [];
foreach ( array_keys( $replacements ) as $search ) {
$like_parts[] = "`$col` LIKE %s";
$params[] = '%' . $wpdb->esc_like( $search ) . '%';
}
$where = implode( ' OR ', $like_parts );
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$rows = $wpdb->get_results( $wpdb->prepare(
"SELECT `$pk`, `$col` FROM `$table` WHERE $where",
$params
), ARRAY_A );

$updated = 0;
foreach ( $rows as $row ) {
$old_val = $row[ $col ];
$new_val = w2w_smart_replace( $old_val, $replacements );
if ( $new_val !== $old_val ) {
$wpdb->update( $table, [ $col => $new_val ], [ $pk => $row[ $pk ] ] );
$updated++;
}
}
if ( $updated > 0 ) {
$log[] = "<tr><td><code>$table</code></td><td><code>$col</code></td><td><strong>$updated</strong> satır güncellendi</td></tr>";
$grand_total += $updated;
}
}
}

// Site URL güncelle
update_option( 'siteurl', str_replace( 'turkeycargo.com.tr', 'w2wexpress.com', get_option('siteurl') ) );
update_option( 'home', str_replace( 'turkeycargo.com.tr', 'w2wexpress.com', get_option('home') ) );

// WordPress cache temizle
if ( function_exists( 'wp_cache_flush' ) ) wp_cache_flush();
if ( function_exists( 'rocket_clean_domain' ) ) rocket_clean_domain(); // WP Rocket

return [ 'total' => $grand_total, 'log' => $log ];
}

/* ------------------------------------------------------------------ */
/* Admin sayfası HTML */
/* ------------------------------------------------------------------ */
function w2w_migrator_page() {
if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Yetkiniz yok.' );

$action = isset( $_POST['w2w_action'] ) ? sanitize_text_field( $_POST['w2w_action'] ) : '';
$nonce = isset( $_POST['w2w_nonce'] ) ? $_POST['w2w_nonce'] : '';
$message = '';
$result = null;

if ( $action && wp_verify_nonce( $nonce, 'w2w_migrator' ) ) {
if ( $action === 'dryrun' ) {
$result = w2w_dry_run();
} elseif ( $action === 'migrate' ) {
$result = w2w_run_migration();
}
}

?>
<style>
#w2w-wrap { max-width: 860px; font-family: -apple-system, sans-serif; }
.w2w-card { background: #fff; border: 1px solid #ddd; border-radius: 8px; padding: 24px 28px; margin-bottom: 24px; }
.w2w-card h2 { margin-top: 0; color: #1a3c6e; }
.w2w-badge { display:inline-block; background:#1a3c6e; color:#fff; border-radius:4px; padding:2px 10px; font-size:12px; margin-left:8px; }
.w2w-table { width:100%; border-collapse:collapse; margin-top:12px; }
.w2w-table th { background:#f0f4fa; color:#1a3c6e; padding:8px 12px; text-align:left; border:1px solid #ddd; }
.w2w-table td { padding:7px 12px; border:1px solid #eee; font-size:13px; }
.w2w-table tr:nth-child(even) td { background:#f9f9f9; }
.w2w-btn { padding:10px 22px; border:none; border-radius:5px; cursor:pointer; font-size:14px; font-weight:600; }
.w2w-btn-blue { background:#2563a8; color:#fff; }
.w2w-btn-red { background:#c0392b; color:#fff; }
.w2w-btn-blue:hover { background:#1a4a80; }
.w2w-btn-red:hover { background:#a93226; }
.w2w-notice-ok { background:#eafaf1; border-left:4px solid #27ae60; padding:12px 16px; border-radius:4px; margin:12px 0; }
.w2w-notice-warn{ background:#fef9e7; border-left:4px solid #f39c12; padding:12px 16px; border-radius:4px; margin:12px 0; }
.w2w-notice-err { background:#fdedec; border-left:4px solid #c0392b; padding:12px 16px; border-radius:4px; margin:12px 0; }
.w2w-steps { counter-reset: step; }
.w2w-steps li { counter-increment: step; list-style:none; padding:6px 0 6px 36px; position:relative; }
.w2w-steps li::before { content: counter(step); position:absolute; left:0; top:5px; background:#2563a8; color:#fff; border-radius:50%; width:22px; height:22px; line-height:22px; text-align:center; font-size:12px; font-weight:700; }
</style>

<div id="w2w-wrap" class="wrap">
<h1>🚀 W2W Express Migrator <span class="w2w-badge">v1.0</span></h1>
<p style="color:#555">Bu eklenti <strong>turkeycargo.com.tr</strong> sitesindeki tüm <em>Turkey Cargo</em> ifadelerini <strong>W2W Express</strong> olarak değiştirir.</p>

<?php if ( $result && $action === 'dryrun' ) : ?>
<div class="w2w-card">
<h2>📊 Ön İzleme Sonucu</h2>
<?php if ( $result['total'] === 0 ) : ?>
<div class="w2w-notice-ok">✅ Değiştirilecek kayıt bulunamadı. Geçiş zaten tamamlanmış olabilir.</div>
<?php else : ?>
<div class="w2w-notice-warn">⚠️ Toplam <strong><?php echo $result['total']; ?> satır</strong> etkilenecek. Aşağıyı inceleyin ve gerçek geçişi başlatın.</div>
<table class="w2w-table">
<tr><th>Tablo</th><th>Kolon</th><th>Etkilenen Satır</th></tr>
<?php echo implode('', $result['details']); ?>
</table>
<?php endif; ?>
</div>
<?php endif; ?>

<?php if ( $result && $action === 'migrate' ) : ?>
<div class="w2w-card">
<h2>✅ Geçiş Tamamlandı</h2>
<?php if ( $result['total'] === 0 ) : ?>
<div class="w2w-notice-ok">Değiştirilecek kayıt bulunamadı. Zaten güncel olabilir.</div>
<?php else : ?>
<div class="w2w-notice-ok">🎉 Toplam <strong><?php echo $result['total']; ?> satır</strong> başarıyla güncellendi. Cache temizlendi.</div>
<table class="w2w-table">
<tr><th>Tablo</th><th>Kolon</th><th>Sonuç</th></tr>
<?php echo implode('', $result['log']); ?>
</table>
<div class="w2w-notice-warn" style="margin-top:16px">
⚠️ <strong>Sonraki adımlar:</strong> Sitenizi tarayıcıda kontrol edin. Herhangi bir sayfa oluşturucu cache'i varsa (Elementor, WP Rocket vb.) temizleyin.
</div>
<?php endif; ?>
</div>
<?php endif; ?>

<!-- DEĞİŞİKLİK LİSTESİ -->
<div class="w2w-card">
<h2>🔄 Yapılacak Değişiklikler</h2>
<table class="w2w-table">
<tr><th>Eski İfade</th><th>Yeni İfade</th></tr>
<?php foreach ( w2w_get_replacements() as $old => $new ) : ?>
<tr><td style="color:#c0392b"><?php echo esc_html($old); ?></td><td style="color:#27ae60;font-weight:600"><?php echo esc_html($new); ?></td></tr>
<?php endforeach; ?>
</table>
</div>

<!-- ADIMLAR -->
<div class="w2w-card">
<h2>📋 Kullanım</h2>
<ol class="w2w-steps">
<li><strong>Önce yedek alın</strong> — phpMyAdmin'den veritabanınızı export edin.</li>
<li><strong>Ön İzleme</strong> — Kaç kayıt etkileneceğini görmek için "Ön İzleme" butonuna tıklayın.</li>
<li><strong>Geçişi Başlat</strong> — Sonuçları inceledikten sonra "Geçişi Başlat" butonuna tıklayın.</li>
<li><strong>Cache temizle</strong> — Kullandığınız cache eklentisinin cache'ini temizleyin.</li>
<li><strong>Kontrol edin</strong> — Sitenizi açıp tüm sayfaları gözden geçirin.</li>
</ol>
</div>

<!-- BUTONLAR -->
<div class="w2w-card">
<h2>⚙️ İşlemler</h2>
<div class="w2w-notice-warn">⚠️ <strong>Önemli:</strong> "Geçişi Başlat" geri alınamaz. Önce mutlaka yedek alın ve "Ön İzleme" yapın.</div>
<form method="post" style="margin-top:16px; display:flex; gap:12px; flex-wrap:wrap;">
<?php wp_nonce_field( 'w2w_migrator', 'w2w_nonce' ); ?>
<button type="submit" name="w2w_action" value="dryrun" class="w2w-btn w2w-btn-blue">
🔍 Ön İzleme (Dry Run)
</button>
<button type="submit" name="w2w_action" value="migrate" class="w2w-btn w2w-btn-red"
onclick="return confirm('Tüm değişiklikler veritabanına yazılacak. Emin misiniz?\n\nÖNEMLİ: Bu işlem geri alınamaz!');">
🚀 Geçişi Başlat
</button>
</form>
</div>
</div>
<?php
}

Leave a comment