wordpress_import_new.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // WordPress Import - Based on working minimal test
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', 0);
  5. ini_set('max_execution_time', 120);
  6. ini_set('memory_limit', '512M');
  7. // Simple auth check - just check if user is logged in
  8. if (session_status() === PHP_SESSION_NONE) {
  9. session_start();
  10. }
  11. $isAdmin = false;
  12. if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true && isset($_SESSION['role']) && $_SESSION['role'] === 'admin') {
  13. $isAdmin = true;
  14. }
  15. // Redirect if not admin
  16. if (!$isAdmin) {
  17. header('Location: login.php');
  18. exit;
  19. }
  20. $message = '';
  21. $stats = [];
  22. // Handle form submission
  23. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  24. try {
  25. require_once '../includes/config.php';
  26. require_once '../includes/database.php';
  27. // Get WordPress database configuration
  28. $wpConfig = [
  29. 'host' => trim($_POST['wp_host'] ?? ''),
  30. 'database' => trim($_POST['wp_database'] ?? ''),
  31. 'username' => trim($_POST['wp_username'] ?? ''),
  32. 'password' => $_POST['wp_password'] ?? ''
  33. ];
  34. // Validate required fields
  35. if (empty($wpConfig['host']) || empty($wpConfig['database']) || empty($wpConfig['username'])) {
  36. throw new Exception("WordPress database configuration is required. Please provide host, database, and username.");
  37. }
  38. // Direct WordPress connection test (exactly same as minimal test)
  39. $options = [
  40. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  41. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  42. PDO::ATTR_TIMEOUT => 10,
  43. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
  44. ];
  45. $dsn = "mysql:host={$wpConfig['host']};dbname={$wpConfig['database']};charset=utf8mb4";
  46. $wpDb = new PDO($dsn, $wpConfig['username'], $wpConfig['password'], $options);
  47. // Test connection
  48. $wpDb->query("SELECT 1");
  49. // Get stats with direct queries (exactly same as minimal test)
  50. try {
  51. $stats['posts'] = $wpDb->query("SELECT COUNT(*) FROM wp_posts WHERE post_type = 'post'")->fetchColumn();
  52. } catch (Exception $e) {
  53. $stats['posts'] = 0;
  54. }
  55. try {
  56. $stats['categories'] = $wpDb->query("SELECT COUNT(*) FROM wp_term_taxonomy WHERE taxonomy = 'category'")->fetchColumn();
  57. } catch (Exception $e) {
  58. $stats['categories'] = 0;
  59. }
  60. try {
  61. $stats['users'] = $wpDb->query("SELECT COUNT(*) FROM wp_users")->fetchColumn();
  62. } catch (Exception $e) {
  63. $stats['users'] = 0;
  64. }
  65. try {
  66. $stats['comments'] = $wpDb->query("SELECT COUNT(*) FROM wp_comments")->fetchColumn();
  67. } catch (Exception $e) {
  68. $stats['comments'] = 0;
  69. }
  70. $message = "WordPress connection successful: Posts: {$stats['posts']}, Categories: {$stats['categories']}, Users: {$stats['users']}, Comments: {$stats['comments']}";
  71. } catch (Exception $e) {
  72. $message = "WordPress connection failed: " . $e->getMessage();
  73. }
  74. }
  75. ?>
  76. <!DOCTYPE html>
  77. <html lang="en">
  78. <head>
  79. <meta charset="UTF-8">
  80. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  81. <title>WordPress Import - <?php echo SITE_TITLE; ?></title>
  82. <link rel="stylesheet" href="../css/style.css">
  83. <style>
  84. .import-form { max-width: 600px; margin: 20px auto; }
  85. .form-group { margin-bottom: 15px; }
  86. label { display: block; margin-bottom: 5px; font-weight: bold; }
  87. input, button { padding: 8px; margin-bottom: 10px; width: 100%; }
  88. button { background: #007cba; color: white; border: none; cursor: pointer; }
  89. button:hover { background: #005a87; }
  90. .message { padding: 10px; margin: 10px 0; border-radius: 4px; }
  91. .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
  92. .error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
  93. .stats { background: #e2e3e5; padding: 10px; border-radius: 4px; margin: 10px 0; }
  94. </style>
  95. </head>
  96. <body>
  97. <div style="max-width: 800px; margin: 20px auto; padding: 20px;">
  98. <h1><a href="index.php"><?php echo SITE_TITLE; ?></a></h1>
  99. <nav>
  100. <a href="index.php">Dashboard</a> |
  101. <a href="publications.php">Publications</a> |
  102. <a href="categories.php">Categories</a> |
  103. <a href="wordpress_import.php" style="font-weight: bold;">WordPress Import</a> |
  104. <a href="logout.php">Logout</a>
  105. </nav>
  106. <h2>WordPress Import</h2>
  107. <p>Test connection to your WordPress database to import posts, categories, users, and comments.</p>
  108. <?php if ($message): ?>
  109. <div class="message <?php echo strpos($message, 'successful') !== false ? 'success' : 'error'; ?>">
  110. <?php echo $message; ?>
  111. </div>
  112. <?php endif; ?>
  113. <?php if (!empty($stats)): ?>
  114. <div class="stats">
  115. <h3>WordPress Database Statistics:</h3>
  116. <ul>
  117. <li>Posts: <?php echo $stats['posts']; ?></li>
  118. <li>Categories: <?php echo $stats['categories']; ?></li>
  119. <li>Users: <?php echo $stats['users']; ?></li>
  120. <li>Comments: <?php echo $stats['comments']; ?></li>
  121. </ul>
  122. </div>
  123. <?php endif; ?>
  124. <form method="post" class="import-form">
  125. <div class="form-group">
  126. <label for="wp_host">Database Host *</label>
  127. <input type="text" id="wp_host" name="wp_host"
  128. value="<?php echo htmlspecialchars($_POST['wp_host'] ?? 'localhost'); ?>"
  129. placeholder="localhost" required>
  130. </div>
  131. <div class="form-group">
  132. <label for="wp_database">Database Name *</label>
  133. <input type="text" id="wp_database" name="wp_database"
  134. value="<?php echo htmlspecialchars($_POST['wp_database'] ?? ''); ?>"
  135. placeholder="wordpress_database" required>
  136. </div>
  137. <div class="form-group">
  138. <label for="wp_username">Database Username *</label>
  139. <input type="text" id="wp_username" name="wp_username"
  140. value="<?php echo htmlspecialchars($_POST['wp_username'] ?? ''); ?>"
  141. placeholder="wp_username" required>
  142. </div>
  143. <div class="form-group">
  144. <label for="wp_password">Database Password</label>
  145. <input type="password" id="wp_password" name="wp_password"
  146. value="<?php echo htmlspecialchars($_POST['wp_password'] ?? ''); ?>"
  147. placeholder="Leave empty if no password">
  148. </div>
  149. <button type="submit" name="test_connection">Test Connection</button>
  150. </form>
  151. <div style="margin-top: 30px; padding: 15px; background: #f8f9fa; border-radius: 4px;">
  152. <h3>Import Options</h3>
  153. <p>For actual importing, use the AJAX import tool which provides step-by-step import functionality:</p>
  154. <a href="ajax_import_test.php" style="display: inline-block; padding: 10px 20px; background: #28a745; color: white; text-decoration: none; border-radius: 4px;">AJAX Import Tool</a>
  155. </div>
  156. </div>
  157. </body>
  158. </html>