| 1 |
<?php |
| 2 |
/** |
| 3 |
* Demo Data Importer for Forumax |
| 4 |
* |
| 5 |
* Handles importing sample forum data via AJAX. |
| 6 |
* |
| 7 |
* @package Forumax |
| 8 |
* @since 2.1.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* Handle AJAX demo data import request. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
function forumax_import_demo_data() { |
| 19 |
// Verify nonce. |
| 20 |
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'bbpc-admin-nonce' ) ) { |
| 21 |
wp_send_json_error( array( 'message' => __( 'Security check failed.', 'forumax' ) ) ); |
| 22 |
} |
| 23 |
|
| 24 |
// Check user capabilities. |
| 25 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 26 |
wp_send_json_error( array( 'message' => __( 'You do not have permission to import data.', 'forumax' ) ) ); |
| 27 |
} |
| 28 |
|
| 29 |
// Check if file exists. |
| 30 |
$xml_file = FORUMAX_PATH . 'sample-data/forumax-sample-data.xml'; |
| 31 |
if ( ! file_exists( $xml_file ) ) { |
| 32 |
wp_send_json_error( array( 'message' => __( 'Demo data file not found.', 'forumax' ) ) ); |
| 33 |
} |
| 34 |
|
| 35 |
// Load WordPress Importer if needed. |
| 36 |
if ( ! class_exists( 'WP_Import' ) ) { |
| 37 |
$importer_file = ABSPATH . 'wp-admin/includes/class-wp-importer.php'; |
| 38 |
if ( file_exists( $importer_file ) ) { |
| 39 |
require_once $importer_file; |
| 40 |
} |
| 41 |
|
| 42 |
// Try loading the WordPress Importer plugin. |
| 43 |
$plugin_importer = WP_PLUGIN_DIR . '/wordpress-importer/wordpress-importer.php'; |
| 44 |
if ( file_exists( $plugin_importer ) ) { |
| 45 |
require_once $plugin_importer; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// If WP_Import still not available, use custom parser. |
| 50 |
if ( ! class_exists( 'WP_Import' ) ) { |
| 51 |
$result = forumax_custom_import_demo_data( $xml_file ); |
| 52 |
if ( is_wp_error( $result ) ) { |
| 53 |
wp_send_json_error( array( 'message' => $result->get_error_message() ) ); |
| 54 |
} |
| 55 |
|
| 56 |
forumax_recalculate_counts_after_import(); |
| 57 |
|
| 58 |
wp_send_json_success( array( 'message' => $result ) ); |
| 59 |
} |
| 60 |
|
| 61 |
// Use WP_Import if available. |
| 62 |
$importer = new WP_Import(); |
| 63 |
$importer->fetch_attachments = false; |
| 64 |
|
| 65 |
ob_start(); |
| 66 |
$importer->import( $xml_file ); |
| 67 |
$output = ob_get_clean(); |
| 68 |
|
| 69 |
forumax_recalculate_counts_after_import(); |
| 70 |
|
| 71 |
wp_send_json_success( |
| 72 |
array( |
| 73 |
'message' => __( 'Demo data imported successfully! Refreshing page...', 'forumax' ), |
| 74 |
) |
| 75 |
); |
| 76 |
} |
| 77 |
add_action( 'wp_ajax_forumax_import_demo_data', 'forumax_import_demo_data' ); |
| 78 |
|
| 79 |
/** |
| 80 |
* Custom XML parser for importing demo data when WP_Import is not available. |
| 81 |
* |
| 82 |
* @param string $xml_file Path to XML file. |
| 83 |
* @return string|WP_Error Success message or error. |
| 84 |
*/ |
| 85 |
/** |
| 86 |
* Custom XML parser for importing demo data when WP_Import is not available. |
| 87 |
* |
| 88 |
* @param string $xml_file Path to XML file. |
| 89 |
* @return string|WP_Error Success message or error. |
| 90 |
*/ |
| 91 |
function forumax_custom_import_demo_data( $xml_file ) { |
| 92 |
// Load XML. |
| 93 |
libxml_use_internal_errors( true ); |
| 94 |
$xml = simplexml_load_file( $xml_file ); |
| 95 |
|
| 96 |
if ( false === $xml ) { |
| 97 |
$errors = libxml_get_errors(); |
| 98 |
libxml_clear_errors(); |
| 99 |
return new WP_Error( 'xml_parse_error', __( 'Failed to parse XML file.', 'forumax' ) ); |
| 100 |
} |
| 101 |
|
| 102 |
// Register namespaces. |
| 103 |
$namespaces = $xml->getNamespaces( true ); |
| 104 |
$wp_ns = isset( $namespaces['wp'] ) ? $namespaces['wp'] : 'http://wordpress.org/export/1.2/'; |
| 105 |
$dc_ns = isset( $namespaces['dc'] ) ? $namespaces['dc'] : 'http://purl.org/dc/elements/1.1/'; |
| 106 |
$content_ns = isset( $namespaces['content'] ) ? $namespaces['content'] : 'http://purl.org/rss/1.0/modules/content/'; |
| 107 |
|
| 108 |
$imported = array( |
| 109 |
'forums' => 0, |
| 110 |
'topics' => 0, |
| 111 |
'replies' => 0, |
| 112 |
); |
| 113 |
|
| 114 |
$post_id_map = array(); |
| 115 |
$author_map = array(); |
| 116 |
$current_user_id = get_current_user_id(); |
| 117 |
|
| 118 |
// Import/Map Authors. |
| 119 |
if ( isset( $xml->channel->children( $wp_ns )->author ) ) { |
| 120 |
foreach ( $xml->channel->children( $wp_ns )->author as $author ) { |
| 121 |
$login = (string) $author->author_login; |
| 122 |
$email = (string) $author->author_email; |
| 123 |
$display_name = (string) $author->author_display_name; |
| 124 |
|
| 125 |
// Check if user exists by login. |
| 126 |
$user = get_user_by( 'login', $login ); |
| 127 |
if ( ! $user ) { |
| 128 |
// Check if user exists by email. |
| 129 |
$user = get_user_by( 'email', $email ); |
| 130 |
} |
| 131 |
|
| 132 |
if ( ! $user ) { |
| 133 |
// Create new user. |
| 134 |
$user_id = wp_create_user( $login, wp_generate_password(), $email ); |
| 135 |
if ( ! is_wp_error( $user_id ) ) { |
| 136 |
wp_update_user( |
| 137 |
array( |
| 138 |
'ID' => $user_id, |
| 139 |
'display_name' => $display_name, |
| 140 |
'role' => 'subscriber', // Default role. |
| 141 |
) |
| 142 |
); |
| 143 |
$author_map[ $login ] = $user_id; |
| 144 |
} else { |
| 145 |
// Fallback to current user if creation fails. |
| 146 |
$author_map[ $login ] = $current_user_id; |
| 147 |
} |
| 148 |
} else { |
| 149 |
$author_map[ $login ] = $user->ID; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Process items in order (forums first, then topics, then replies). |
| 155 |
$items_by_type = array( |
| 156 |
'forum' => array(), |
| 157 |
'topic' => array(), |
| 158 |
'reply' => array(), |
| 159 |
); |
| 160 |
|
| 161 |
foreach ( $xml->channel->item as $item ) { |
| 162 |
$wp = $item->children( $wp_ns ); |
| 163 |
$post_type = (string) $wp->post_type; |
| 164 |
if ( isset( $items_by_type[ $post_type ] ) ) { |
| 165 |
$items_by_type[ $post_type ][] = $item; |
| 166 |
} else { |
| 167 |
// Handle other post types (like attachments) if needed, or put them in a generic bucket. |
| 168 |
// For now, we mainly care about forums, topics, and replies, but attachments are often 'attachment' post_type. |
| 169 |
if ( 'attachment' === $post_type ) { |
| 170 |
$items_by_type['attachment'][] = $item; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// Helper to run import loop. |
| 176 |
$run_import = function ( $items ) use ( $wp_ns, $dc_ns, $content_ns, &$post_id_map, $current_user_id, $author_map ) { |
| 177 |
$count = 0; |
| 178 |
if ( ! empty( $items ) ) { |
| 179 |
foreach ( $items as $item ) { |
| 180 |
$result = forumax_import_single_item( $item, $wp_ns, $dc_ns, $content_ns, $post_id_map, $current_user_id, $author_map ); |
| 181 |
if ( ! is_wp_error( $result ) && $result > 0 ) { |
| 182 |
$count++; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
return $count; |
| 187 |
}; |
| 188 |
|
| 189 |
// Import order: Attachments -> Forums -> Topics -> Replies. |
| 190 |
if ( isset( $items_by_type['attachment'] ) ) { |
| 191 |
$run_import( $items_by_type['attachment'] ); |
| 192 |
} |
| 193 |
|
| 194 |
$imported['forums'] = $run_import( $items_by_type['forum'] ); |
| 195 |
$imported['topics'] = $run_import( $items_by_type['topic'] ); |
| 196 |
$imported['replies'] = $run_import( $items_by_type['reply'] ); |
| 197 |
|
| 198 |
// Flush rewrite rules. |
| 199 |
flush_rewrite_rules(); |
| 200 |
|
| 201 |
return sprintf( |
| 202 |
/* translators: %1$d: forums count, %2$d: topics count, %3$d: replies count */ |
| 203 |
__( 'Successfully imported %1$d forums, %2$d topics, and %3$d replies. Refreshing page...', 'forumax' ), |
| 204 |
$imported['forums'], |
| 205 |
$imported['topics'], |
| 206 |
$imported['replies'] |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Import a single item from XML. |
| 212 |
* |
| 213 |
* @param SimpleXMLElement $item The item to import. |
| 214 |
* @param string $wp_ns WordPress namespace. |
| 215 |
* @param string $dc_ns Dublin Core namespace. |
| 216 |
* @param string $content_ns Content namespace. |
| 217 |
* @param array $post_id_map Reference to post ID mapping array. |
| 218 |
* @param int $current_user_id Current user ID. |
| 219 |
* @param array $author_map Map of XML login to WP user ID. |
| 220 |
* @return int|WP_Error New post ID or error. |
| 221 |
*/ |
| 222 |
function forumax_import_single_item( $item, $wp_ns, $dc_ns, $content_ns, &$post_id_map, $current_user_id, $author_map = array() ) { |
| 223 |
$wp = $item->children( $wp_ns ); |
| 224 |
$dc = $item->children( $dc_ns ); |
| 225 |
$content = $item->children( $content_ns ); |
| 226 |
|
| 227 |
$post_type = (string) $wp->post_type; |
| 228 |
$old_post_id = (int) $wp->post_id; |
| 229 |
$old_parent = (int) $wp->post_parent; |
| 230 |
$title = (string) $item->title; |
| 231 |
$post_name = (string) $wp->post_name; |
| 232 |
$post_content = (string) $content->encoded; |
| 233 |
$post_status = (string) $wp->status; |
| 234 |
$menu_order = (int) $wp->menu_order; |
| 235 |
$creator = (string) $dc->creator; |
| 236 |
$attachment_url = (string) $wp->attachment_url; |
| 237 |
|
| 238 |
// Determine author. |
| 239 |
$post_author = $current_user_id; |
| 240 |
if ( ! empty( $creator ) && isset( $author_map[ $creator ] ) ) { |
| 241 |
$post_author = $author_map[ $creator ]; |
| 242 |
} |
| 243 |
|
| 244 |
// Map old parent ID to new ID. |
| 245 |
$new_parent = 0; |
| 246 |
if ( $old_parent > 0 && isset( $post_id_map[ $old_parent ] ) ) { |
| 247 |
$new_parent = $post_id_map[ $old_parent ]; |
| 248 |
} |
| 249 |
|
| 250 |
// Check if post already exists. |
| 251 |
$existing = get_page_by_path( $post_name, OBJECT, $post_type ); |
| 252 |
if ( $existing && 'attachment' !== $post_type ) { |
| 253 |
$post_id_map[ $old_post_id ] = $existing->ID; |
| 254 |
return $existing->ID; |
| 255 |
} |
| 256 |
|
| 257 |
// Prepare post data. |
| 258 |
$post_data = array( |
| 259 |
'post_title' => $title, |
| 260 |
'post_name' => $post_name, |
| 261 |
'post_content' => $post_content, |
| 262 |
'post_status' => $post_status, |
| 263 |
'post_type' => $post_type, |
| 264 |
'post_parent' => $new_parent, |
| 265 |
'menu_order' => $menu_order, |
| 266 |
'post_author' => $post_author, |
| 267 |
); |
| 268 |
|
| 269 |
// Handle attachment-specific logic (sideloading). |
| 270 |
if ( 'attachment' === $post_type && ! empty( $attachment_url ) ) { |
| 271 |
// Simple check to avoid re-uploading if exists is tricky without hash, |
| 272 |
// but let's just try to create it. |
| 273 |
// Note: For a robust importer, we'd use media_sideload_image or verify existence. |
| 274 |
// However, since this is a simple "custom" importer when WP_Import fails, |
| 275 |
// we might just create the post entry. But actually, attachments need files. |
| 276 |
// If we can't download the file, we can't really make a valid attachment. |
| 277 |
// For this demo, let's assume we just create the post object for reference, |
| 278 |
// or skipping real file download to keep it simple unless requested. |
| 279 |
// Given the constraints, let's just insert the post object so ID mapping works. |
| 280 |
// Real file sideloading might timeout or require filesystem access. |
| 281 |
} |
| 282 |
|
| 283 |
// Insert the post. |
| 284 |
$new_post_id = wp_insert_post( $post_data, true ); |
| 285 |
|
| 286 |
if ( is_wp_error( $new_post_id ) ) { |
| 287 |
return $new_post_id; |
| 288 |
} |
| 289 |
|
| 290 |
// Map old ID to new ID. |
| 291 |
$post_id_map[ $old_post_id ] = $new_post_id; |
| 292 |
|
| 293 |
// Import post meta. |
| 294 |
foreach ( $wp->postmeta as $meta ) { |
| 295 |
$meta_key = (string) $meta->meta_key; |
| 296 |
$meta_value = (string) $meta->meta_value; |
| 297 |
|
| 298 |
// Update parent IDs in meta. |
| 299 |
if ( in_array( $meta_key, array( '_bbp_forum_id', '_bbp_topic_id', '_bbp_reply_to', '_thumbnail_id' ), true ) ) { |
| 300 |
$old_meta_id = (int) $meta_value; |
| 301 |
if ( isset( $post_id_map[ $old_meta_id ] ) ) { |
| 302 |
$meta_value = $post_id_map[ $old_meta_id ]; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
update_post_meta( $new_post_id, $meta_key, $meta_value ); |
| 307 |
} |
| 308 |
|
| 309 |
// Handle topic tags. |
| 310 |
foreach ( $item->category as $category ) { |
| 311 |
$domain = (string) $category['domain']; |
| 312 |
if ( 'topic-tag' === $domain ) { |
| 313 |
$tag_name = (string) $category; |
| 314 |
$tag_slug = (string) $category['nicename']; |
| 315 |
wp_set_object_terms( $new_post_id, $tag_name, 'topic-tag', true ); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
// Handle sticky. |
| 320 |
if ( 'topic' === $post_type && '1' === (string) $wp->is_sticky ) { |
| 321 |
update_post_meta( $new_post_id, '_bbp_topic_sticky', 'stick' ); |
| 322 |
} |
| 323 |
|
| 324 |
// Update reply to mapping for threaded replies if missed in standard meta loop (usually captured above). |
| 325 |
// The meta loop above handles _bbp_reply_to if it's in postmeta. |
| 326 |
|
| 327 |
return $new_post_id; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Recalculate counts after import. |
| 332 |
*/ |
| 333 |
function forumax_recalculate_counts_after_import() |
| 334 |
{ |
| 335 |
if ( ! function_exists( 'bbp_admin_repair_forum_topic_count' ) ) { |
| 336 |
if ( defined( 'BBP_PLUGIN_DIR' ) ) { |
| 337 |
require_once BBP_PLUGIN_DIR . '/includes/admin/tools/repair.php'; |
| 338 |
} elseif ( file_exists( FORUMAX_DIR . 'lib/bbpress/includes/admin/tools/repair.php' ) ) { |
| 339 |
require_once FORUMAX_DIR . 'lib/bbpress/includes/admin/tools/repair.php'; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
if ( function_exists( 'bbp_admin_repair_forum_topic_count' ) ) { |
| 344 |
bbp_admin_repair_forum_topic_count(); |
| 345 |
bbp_admin_repair_forum_reply_count(); |
| 346 |
bbp_admin_repair_topic_reply_count(); |
| 347 |
bbp_admin_repair_freshness(); |
| 348 |
|
| 349 |
// Also repair forum meta (parent/child relationships) |
| 350 |
bbp_admin_repair_forum_meta(); |
| 351 |
} |
| 352 |
} |
| 353 |
|