svalavuo 4 giorni fa
parent
commit
414d0cbecf
1 ha cambiato i file con 183 aggiunte e 0 eliminazioni
  1. 183 0
      admin/wordpress_import_new.php

+ 183 - 0
admin/wordpress_import_new.php

@@ -0,0 +1,183 @@
+<?php
+// WordPress Import - Based on working minimal test
+error_reporting(E_ALL);
+ini_set('display_errors', 0);
+ini_set('max_execution_time', 120);
+ini_set('memory_limit', '512M');
+
+// Simple auth check - just check if user is logged in
+if (session_status() === PHP_SESSION_NONE) {
+    session_start();
+}
+
+$isAdmin = false;
+if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true && isset($_SESSION['role']) && $_SESSION['role'] === 'admin') {
+    $isAdmin = true;
+}
+
+// Redirect if not admin
+if (!$isAdmin) {
+    header('Location: login.php');
+    exit;
+}
+
+$message = '';
+$stats = [];
+
+// Handle form submission
+if ($_SERVER['REQUEST_METHOD'] === 'POST') {
+    try {
+        require_once '../includes/config.php';
+        require_once '../includes/database.php';
+        
+        // Get WordPress database configuration
+        $wpConfig = [
+            'host' => trim($_POST['wp_host'] ?? ''),
+            'database' => trim($_POST['wp_database'] ?? ''),
+            'username' => trim($_POST['wp_username'] ?? ''),
+            'password' => $_POST['wp_password'] ?? ''
+        ];
+        
+        // Validate required fields
+        if (empty($wpConfig['host']) || empty($wpConfig['database']) || empty($wpConfig['username'])) {
+            throw new Exception("WordPress database configuration is required. Please provide host, database, and username.");
+        }
+        
+        // Direct WordPress connection test (exactly same as minimal test)
+        $options = [
+            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
+            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
+            PDO::ATTR_TIMEOUT => 10,
+            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
+        ];
+        
+        $dsn = "mysql:host={$wpConfig['host']};dbname={$wpConfig['database']};charset=utf8mb4";
+        $wpDb = new PDO($dsn, $wpConfig['username'], $wpConfig['password'], $options);
+        
+        // Test connection
+        $wpDb->query("SELECT 1");
+        
+        // Get stats with direct queries (exactly same as minimal test)
+        try {
+            $stats['posts'] = $wpDb->query("SELECT COUNT(*) FROM wp_posts WHERE post_type = 'post'")->fetchColumn();
+        } catch (Exception $e) {
+            $stats['posts'] = 0;
+        }
+        
+        try {
+            $stats['categories'] = $wpDb->query("SELECT COUNT(*) FROM wp_term_taxonomy WHERE taxonomy = 'category'")->fetchColumn();
+        } catch (Exception $e) {
+            $stats['categories'] = 0;
+        }
+        
+        try {
+            $stats['users'] = $wpDb->query("SELECT COUNT(*) FROM wp_users")->fetchColumn();
+        } catch (Exception $e) {
+            $stats['users'] = 0;
+        }
+        
+        try {
+            $stats['comments'] = $wpDb->query("SELECT COUNT(*) FROM wp_comments")->fetchColumn();
+        } catch (Exception $e) {
+            $stats['comments'] = 0;
+        }
+        
+        $message = "WordPress connection successful: Posts: {$stats['posts']}, Categories: {$stats['categories']}, Users: {$stats['users']}, Comments: {$stats['comments']}";
+        
+    } catch (Exception $e) {
+        $message = "WordPress connection failed: " . $e->getMessage();
+    }
+}
+?>
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>WordPress Import - <?php echo SITE_TITLE; ?></title>
+    <link rel="stylesheet" href="../css/style.css">
+    <style>
+        .import-form { max-width: 600px; margin: 20px auto; }
+        .form-group { margin-bottom: 15px; }
+        label { display: block; margin-bottom: 5px; font-weight: bold; }
+        input, button { padding: 8px; margin-bottom: 10px; width: 100%; }
+        button { background: #007cba; color: white; border: none; cursor: pointer; }
+        button:hover { background: #005a87; }
+        .message { padding: 10px; margin: 10px 0; border-radius: 4px; }
+        .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
+        .error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
+        .stats { background: #e2e3e5; padding: 10px; border-radius: 4px; margin: 10px 0; }
+    </style>
+</head>
+<body>
+    <div style="max-width: 800px; margin: 20px auto; padding: 20px;">
+        <h1><a href="index.php"><?php echo SITE_TITLE; ?></a></h1>
+        <nav>
+            <a href="index.php">Dashboard</a> | 
+            <a href="publications.php">Publications</a> | 
+            <a href="categories.php">Categories</a> | 
+            <a href="wordpress_import.php" style="font-weight: bold;">WordPress Import</a> | 
+            <a href="logout.php">Logout</a>
+        </nav>
+        
+        <h2>WordPress Import</h2>
+        <p>Test connection to your WordPress database to import posts, categories, users, and comments.</p>
+        
+        <?php if ($message): ?>
+            <div class="message <?php echo strpos($message, 'successful') !== false ? 'success' : 'error'; ?>">
+                <?php echo $message; ?>
+            </div>
+        <?php endif; ?>
+        
+        <?php if (!empty($stats)): ?>
+            <div class="stats">
+                <h3>WordPress Database Statistics:</h3>
+                <ul>
+                    <li>Posts: <?php echo $stats['posts']; ?></li>
+                    <li>Categories: <?php echo $stats['categories']; ?></li>
+                    <li>Users: <?php echo $stats['users']; ?></li>
+                    <li>Comments: <?php echo $stats['comments']; ?></li>
+                </ul>
+            </div>
+        <?php endif; ?>
+        
+        <form method="post" class="import-form">
+            <div class="form-group">
+                <label for="wp_host">Database Host *</label>
+                <input type="text" id="wp_host" name="wp_host" 
+                       value="<?php echo htmlspecialchars($_POST['wp_host'] ?? 'localhost'); ?>" 
+                       placeholder="localhost" required>
+            </div>
+            
+            <div class="form-group">
+                <label for="wp_database">Database Name *</label>
+                <input type="text" id="wp_database" name="wp_database" 
+                       value="<?php echo htmlspecialchars($_POST['wp_database'] ?? ''); ?>" 
+                       placeholder="wordpress_database" required>
+            </div>
+            
+            <div class="form-group">
+                <label for="wp_username">Database Username *</label>
+                <input type="text" id="wp_username" name="wp_username" 
+                       value="<?php echo htmlspecialchars($_POST['wp_username'] ?? ''); ?>" 
+                       placeholder="wp_username" required>
+            </div>
+            
+            <div class="form-group">
+                <label for="wp_password">Database Password</label>
+                <input type="password" id="wp_password" name="wp_password" 
+                       value="<?php echo htmlspecialchars($_POST['wp_password'] ?? ''); ?>" 
+                       placeholder="Leave empty if no password">
+            </div>
+            
+            <button type="submit" name="test_connection">Test Connection</button>
+        </form>
+        
+        <div style="margin-top: 30px; padding: 15px; background: #f8f9fa; border-radius: 4px;">
+            <h3>Import Options</h3>
+            <p>For actual importing, use the AJAX import tool which provides step-by-step import functionality:</p>
+            <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>
+        </div>
+    </div>
+</body>
+</html>