Skip to content

Instantly share code, notes, and snippets.

@sinanisler
Last active August 20, 2022 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinanisler/39a193ab1e074049026294f3b971a847 to your computer and use it in GitHub Desktop.
Save sinanisler/39a193ab1e074049026294f3b971a847 to your computer and use it in GitHub Desktop.
flarum to wordpress exporter
<?php get_header();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<div class="blog-page-area pd-bottom-80">
<div class="container">
<div class="row">
<div class="col-lg-12 pd-top-50">
<div class="blog-details-page-inner">
<div class="single-blog-inner m-0">
<br><br>
<?php
$DisDb = new wpdb('topluluk', 'w34"12d"12dgw34gw34gw', 'topluluk', 'localhost');
function get_all_db_discussions(){
global $DisDb;
$rows = '';
/*if(!get_transient('last_10_dicussions_to_show_123')){*/
$rows = $DisDb->get_results("SELECT title, id, slug,created_at, first_post_id FROM `discussions` ORDER BY `discussions`.`id` LIMIT 1200");
//set_transient('last_10_dicussions_to_show_123', $rows, 30 * MINUTE_IN_SECONDS);
/*} else {
$rows = get_transient('last_10_dicussions_to_show_1');
}*/
return $rows;
}
function get_content_and_comment_of_discussions($dis_id){
global $DisDb;
if($dis_id) {
$data = $DisDb->get_results("SELECT id,content,user_id,created_at FROM `posts` where `discussion_id` = $dis_id ORDER BY `posts`.`number` ASC LIMIT 1200");
} else {
$data = null;
}
return $data;
}
function get_tags_by_discussion($dis_id){
global $DisDb;
$cat_array = array();
if($dis_id) {
$data = $DisDb->get_results("SELECT * FROM discussion_tag A LEFT JOIN tags B ON A.tag_id = B.id where A.discussion_id = $dis_id LIMIT 1200");
} else {
$data = null;
}
if($data) {
foreach($data as $d){
$catoid = get_category_by_slug( $d->slug );
$cat_array[] = $catoid->term_id;
}
}
return $cat_array;
}
// function flarum_get_all_users(){
// global $DisDb;
// $users = $DisDb->get_results("SELECT * from users where id != 1 LIMIT 1200");
// echo "INSERT INTO `wp_users` (`ID`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_activation_key`, `user_status`, `display_name`) VALUES ";
// foreach($users as $user){
// echo "(".($user->id+2).",'".$user->username."','".$user->password."','".$user->username."','".$user->email."','','".date('Y-m-d H:i:s')."','','0','".$user->username."'),";
// }
// }
// flarum_get_all_users();
if ( ! function_exists( 'post_exists' ) ) {
require_once( ABSPATH . 'wp-admin/includes/post.php' );
}
function insert_post_custom($id,$title,$created_at,$content,$slug,$userid,$cats){
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $content,
'post_date' => $created_at,
'post_name' => $slug,
'post_status' => 'publish',
'post_author' => $userid,
'post_category' => $cats,
'post_type' => 'post'
);
// Insert the post into the database
$npid = post_exists( $title ) or wp_insert_post( $my_post,true );
if ( ! is_wp_error( $npid ) ) {
return $npid;
} else {
echo $npid->get_error_message();
}
}
function ic_wp_insert_comment( $content, $created_at,$postId, $uid ) {
$current_user = get_user_by('id',$uid);
if ( comments_open( $postId ) ) {
$data = array(
'comment_post_ID' => $postId,
'comment_content' => $content,
'comment_date' => $created_at,
'user_id' => $current_user->ID,
'comment_author' => $current_user->user_login,
'comment_author_email' => $current_user->user_email,
'comment_author_url' => $current_user->user_url,
);
$comment_id = wp_insert_comment( $data );
if ( ! is_wp_error( $comment_id ) ) {
return $comment_id;
}
}
return false;
}
//Create simple list from results
// echo "<ul>";
// foreach (get_all_db_discussions() as $obj) :
// // Link creation process is optional. You can change
// echo "<li><a href='https://www.atarikafa.com/topluluk/d/".$obj->id."-".$obj->slug."'>".$obj->title."</a>";
// $cats = get_tags_by_discussion($obj->id);
// $post_id = '';
// if(get_content_and_comment_of_discussions($obj->id)) {
// $data = get_content_and_comment_of_discussions($obj->id);
// echo '<ul>';
// foreach($data as $d){
// if($d->id === $obj->first_post_id) {
// echo "<li>Content: ".$d->content."</li>";
// if($d->user_id != 1){
// $puid = ($d->user_id+2);
// } else {
// $puid = $d->user_id;
// }
// $post_id = insert_post_custom($obj->id,$obj->title,$obj->created_at,$d->content,$obj->id."-".$obj->slug,$puid,$cats);
// echo $post_id;
// } else {
// echo "<li>Comment: ".$d->content." UserID:".$d->user_id."</li><hr />";
// if($post_id != ''){
// if($d->user_id != 1){
// $puid = ($d->user_id+2);
// } else {
// $puid = $d->user_id;
// }
// ic_wp_insert_comment($d->content,$d->created_at,$post_id,$puid);
// }
// }
// }
// echo '</ul>';
// }
// echo "</li>";
// endforeach;
// echo "</ul>";
?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
@serkanalgur
Copy link

foreach($data as $d){
    $catoid = get_category_by_slug( $d->slug );
    $cat_array[] = $catoid->term_id;
}

to

foreach($data as $d){
    $catoid = get_category_by_slug( $d->slug );
    if(empty($catoid)){
    	$catoid = wp_insert_term(
	      $d->name,   // the term 
	      'category', // the taxonomy
	      array(
	          'description' => '',
	          'slug'        => $d->slug,
	      )
    	);
    }
    $cat_array[] = $catoid->term_id;
}

@serkanalgur
Copy link

Work twice for comment!

@serkanalgur
Copy link

user function create a mysql insert for saving current passwords

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment