add_help_tab([
'id' => 'aa_ls_help_usage',
'title' => __('Usage Guide', 'aa-live-site'),
'content' =>
'
' . __('How to Use', 'aa-live-site') . '
' .
'
- Old URL: e.g., https://staging.example.com
- New URL: e.g., https://www.example.com
- Safe Mode: Previews the number of changes.
- Backup: Creates a full database .sql file.
- Include Custom Tables: Looks inside additional plugin tables.
'
]);
$screen->add_help_tab([
'id' => 'aa_ls_help_troubleshooting',
'title' => __('Troubleshooting', 'aa-live-site'),
'content' =>
'' . __('Troubleshooting', 'aa-live-site') . '
' .
'
- URLs not changing? Make sure the "Old URL" matches exactly.
- Backup not created? Your server may block `exec()` or `mysqldump`.
- Plugin data missing? Try enabling "Include Custom Tables".
- Serialized data? This plugin doesn’t yet support deep serialization.
' .
'' . __('Still stuck? Email us at support@aaplugins.com', 'aa-live-site') . '
'
]);
$screen->set_help_sidebar(
'' . __('Need Support?', 'aa-live-site') . '
' .
'support@aaplugins.com
' .
'' . __('Plugin Docs', 'aa-live-site') . '
'
);
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['aa_ls_submit']) && check_admin_referer('aa_ls_change_urls', 'aa_ls_nonce')) {
$fromurl = esc_url_raw(trim($_POST['fromurl']));
$tourl = esc_url_raw(trim($_POST['tourl']));
$do_backup = !empty($_POST['backup']);
$safe_mode = !empty($_POST['safe_mode']);
$include_custom = !empty($_POST['include_custom']);
$prefix = $wpdb->prefix;
if (empty($fromurl) || empty($tourl)) {
$message .= "" . esc_html__('Both URLs are required.', 'aa-live-site') . "
";
} else {
if ($do_backup) {
$backup_file = plugin_dir_path(__FILE__) . 'backup-' . date("Ymd-His") . '.sql';
$cmd = "mysqldump -h " . escapeshellarg(DB_HOST) . " -u " . escapeshellarg(DB_USER) . " -p" . escapeshellarg(DB_PASSWORD) . " " . escapeshellarg(DB_NAME) . " > " . escapeshellarg($backup_file);
exec($cmd, $output, $result_code);
$message .= $result_code === 0
? "" . esc_html__('Database backup created successfully.', 'aa-live-site') . "
"
: "" . esc_html__('Database backup failed. Please check server configuration.', 'aa-live-site') . "
";
}
$tables = [
"{$prefix}options" => ["option_value"],
"{$prefix}posts" => ["guid", "post_content"],
"{$prefix}postmeta" => ["meta_value"]
];
if ($include_custom) {
$extra_tables = $wpdb->get_results("SHOW TABLES LIKE '%meta%'");
foreach ($extra_tables as $table_obj) {
$table_name = reset($table_obj);
if (!isset($tables[$table_name])) {
$columns = $wpdb->get_col("DESC `$table_name`", 0);
foreach ($columns as $col) {
if (in_array($col, ['meta_value', 'value', 'settings'])) {
$tables[$table_name][] = $col;
}
}
}
}
}
$total_updated = 0;
$simulated = [];
foreach ($tables as $table => $columns) {
foreach ($columns as $column) {
$sql = $wpdb->prepare("SELECT COUNT(*) FROM `$table` WHERE `$column` LIKE %s", '%' . $wpdb->esc_like($fromurl) . '%');
$count = $wpdb->get_var($sql);
if ($safe_mode) {
if ($count > 0) {
$simulated[] = "$table.$column: $count matches";
}
} else {
$query = $wpdb->prepare("UPDATE `$table` SET `$column` = REPLACE(`$column`, %s, %s)", $fromurl, $tourl);
$rows = $wpdb->query($query);
if (!is_wp_error($rows)) {
$total_updated += intval($rows);
}
}
}
}
if ($safe_mode) {
$message .= "Safe Mode: No changes made.
Would update the following:
" . implode('', $simulated) . "
";
} else {
$message .= "" .
esc_html__('URL replacement completed.', 'aa-live-site') . "
" .
esc_html__('From:', 'aa-live-site') . " " . esc_html($fromurl) . "
" .
esc_html__('To:', 'aa-live-site') . " " . esc_html($tourl) . "
" .
sprintf(esc_html__('%d rows updated.', 'aa-live-site'), $total_updated) .
"
";
}
}
}
echo "
" . esc_html__('AA Live Site URL Updater', 'aa-live-site') . "
$message
";
}