| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\WooCommerceMigrator; |
| 4 |
|
| 5 |
class WooCommerceMigratorHelper |
| 6 |
{ |
| 7 |
public static function doBulkInsert($table, $data) |
| 8 |
{ |
| 9 |
if (empty($data)) { |
| 10 |
return false; |
| 11 |
} |
| 12 |
|
| 13 |
global $wpdb; |
| 14 |
$firstRow = reset($data); |
| 15 |
$columns = array_keys($firstRow); |
| 16 |
$values = []; |
| 17 |
$placeHolders = []; |
| 18 |
|
| 19 |
foreach ($data as $row) { |
| 20 |
$rowPlaceholders = []; |
| 21 |
foreach ($columns as $column) { |
| 22 |
$values[] = $row[$column]; |
| 23 |
$rowPlaceholders[] = is_numeric($row[$column]) ? '%d' : '%s'; |
| 24 |
} |
| 25 |
$placeHolders[] = '(' . implode(',', $rowPlaceholders) . ')'; |
| 26 |
} |
| 27 |
|
| 28 |
$query = "INSERT INTO {$wpdb->prefix}{$table} (`" . implode('`,`', $columns) . "`) VALUES " . implode(',', $placeHolders); |
| 29 |
|
| 30 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared on this line with values |
| 31 |
return $wpdb->query($wpdb->prepare($query, $values)); |
| 32 |
} |
| 33 |
|
| 34 |
public static function logMigrationError($productId, $error) |
| 35 |
{ |
| 36 |
$failedLogs = get_option('_fluent_wc_failed_migration_logs', []); |
| 37 |
$failedLogs[$productId] = is_wp_error($error) ? $error->get_error_message() : $error; |
| 38 |
update_option('_fluent_wc_failed_migration_logs', $failedLogs); |
| 39 |
} |
| 40 |
|
| 41 |
public static function updateMigrationStatus($step, $status) |
| 42 |
{ |
| 43 |
$migrationSteps = get_option('__fluent_cart_wc_migration_steps', []); |
| 44 |
$migrationSteps[$step] = $status; |
| 45 |
update_option('__fluent_cart_wc_migration_steps', $migrationSteps); |
| 46 |
} |
| 47 |
|
| 48 |
public static function checkRequiredTables() |
| 49 |
{ |
| 50 |
global $wpdb; |
| 51 |
$requiredTables = [ |
| 52 |
'fct_product_details', |
| 53 |
'fct_product_variations', |
| 54 |
'fct_product_downloads' |
| 55 |
]; |
| 56 |
|
| 57 |
foreach ($requiredTables as $table) { |
| 58 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 59 |
if ($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}{$table}'") != $wpdb->prefix . $table) { |
| 60 |
return new \WP_Error('wc_migrator_error', "Required table {$table} does not exist."); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
} |
| 69 |
|