ajax_import_test.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. case 'import_comments':
  90. $wpConfig = [
  91. 'host' => trim($_GET['wp_host'] ?? ''),
  92. 'database' => trim($_GET['wp_database'] ?? ''),
  93. 'username' => trim($_GET['wp_username'] ?? ''),
  94. 'password' => $_GET['wp_password'] ?? ''
  95. ];
  96. $importer = new WordPressImport($wpConfig);
  97. $result = $importer->importComments();
  98. sendResponse(true, 'Comments imported', [
  99. 'step' => $step,
  100. 'result' => $result
  101. ]);
  102. break;
  103. default:
  104. sendResponse(false, 'Unknown action', ['step' => $step]);
  105. }
  106. } catch (Exception $e) {
  107. sendResponse(false, 'Error: ' . $e->getMessage(), [
  108. 'step' => $step,
  109. 'error' => [
  110. 'file' => $e->getFile(),
  111. 'line' => $e->getLine()
  112. ]
  113. ]);
  114. }
  115. ?>
  116. ?>