ajax_import_test.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // Simple AJAX endpoint without output buffering
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', 0);
  5. ini_set('max_execution_time', 60);
  6. ini_set('memory_limit', '512M');
  7. // Set header first
  8. header('Content-Type: application/json');
  9. // Simple response function
  10. function sendResponse($success, $message, $data = []) {
  11. $response = array_merge([
  12. 'success' => $success,
  13. 'message' => $message
  14. ], $data);
  15. echo json_encode($response);
  16. exit;
  17. }
  18. try {
  19. require_once '../includes/config.php';
  20. require_once '../includes/database.php';
  21. require_once '../includes/wordpress_import.php';
  22. } catch (Exception $e) {
  23. sendResponse(false, 'Include error: ' . $e->getMessage(), ['step' => 'includes']);
  24. }
  25. $action = $_GET['action'] ?? '';
  26. $step = $action;
  27. try {
  28. switch ($action) {
  29. case 'test_connection':
  30. $wpConfig = [
  31. 'host' => trim($_GET['wp_host'] ?? ''),
  32. 'database' => trim($_GET['wp_database'] ?? ''),
  33. 'username' => trim($_GET['wp_username'] ?? ''),
  34. 'password' => $_GET['wp_password'] ?? ''
  35. ];
  36. $importer = new WordPressImport($wpConfig);
  37. $connectionTest = $importer->testConnection();
  38. if ($connectionTest['success']) {
  39. sendResponse(true, 'Connection successful', [
  40. 'step' => $step,
  41. 'stats' => $connectionTest['stats']
  42. ]);
  43. } else {
  44. sendResponse(false, $connectionTest['error'], ['step' => $step]);
  45. }
  46. break;
  47. case 'import_categories':
  48. $wpConfig = [
  49. 'host' => trim($_GET['wp_host'] ?? ''),
  50. 'database' => trim($_GET['wp_database'] ?? ''),
  51. 'username' => trim($_GET['wp_username'] ?? ''),
  52. 'password' => $_GET['wp_password'] ?? ''
  53. ];
  54. $importer = new WordPressImport($wpConfig);
  55. $result = $importer->importCategories();
  56. sendResponse(true, 'Categories imported', [
  57. 'step' => $step,
  58. 'result' => $result
  59. ]);
  60. break;
  61. case 'import_users':
  62. $wpConfig = [
  63. 'host' => trim($_GET['wp_host'] ?? ''),
  64. 'database' => trim($_GET['wp_database'] ?? ''),
  65. 'username' => trim($_GET['wp_username'] ?? ''),
  66. 'password' => $_GET['wp_password'] ?? ''
  67. ];
  68. $importer = new WordPressImport($wpConfig);
  69. $result = $importer->importUsers();
  70. sendResponse(true, 'Users imported', [
  71. 'step' => $step,
  72. 'result' => $result
  73. ]);
  74. break;
  75. case 'import_posts':
  76. $wpConfig = [
  77. 'host' => trim($_GET['wp_host'] ?? ''),
  78. 'database' => trim($_GET['wp_database'] ?? ''),
  79. 'username' => trim($_GET['wp_username'] ?? ''),
  80. 'password' => $_GET['wp_password'] ?? ''
  81. ];
  82. $importer = new WordPressImport($wpConfig);
  83. $result = $importer->importPosts();
  84. sendResponse(true, 'Posts imported', [
  85. 'step' => $step,
  86. 'result' => $result
  87. ]);
  88. break;
  89. default:
  90. sendResponse(false, 'Unknown action', ['step' => $step]);
  91. }
  92. } catch (Exception $e) {
  93. sendResponse(false, 'Error: ' . $e->getMessage(), [
  94. 'step' => $step,
  95. 'error' => [
  96. 'file' => $e->getFile(),
  97. 'line' => $e->getLine()
  98. ]
  99. ]);
  100. }
  101. ?>
  102. ?>