/**
* Create bbPress replies with already existing comments
*
* @param int $post_id
* @param int $topic_id
*/
function export_comments_to_bbpress ( $post_id, $topic_id, $topic_forum ) {
/** getting post comments */
$post_comments = get_comments( array('post_id' => $post_id, 'order' => 'ASC'));
if ( $post_comments ) {
foreach ( $post_comments as $post_comment ) {
if (! empty($post_comment->comment_type )) {
continue;
}
// If user is not registered
if ( empty($post_comment->user_id) ) {
//$post_comment->user_id = 107;
// 1. Check if user exists by email
if (! empty($post_comment->comment_author_email)) {
$existing_user = get_user_by('email', $post_comment->comment_author_email);
if ($existing_user)
$post_comment->user_id = $existing_user->ID;
}
}
// Reply data
$reply_data = array(
'post_parent' => $topic_id, // topic ID
'post_status' => bbp_get_public_status_id(),
'post_type' => bbp_get_reply_post_type(),
'post_author' => $post_comment->user_id ,
'post_content' => $post_comment->comment_content ,
'post_date' => $post_comment->comment_date,
'post_date_gmt' => $post_comment->comment_date_gmt,
'post_modified' => $post_comment->comment_date,
'post_modified_gmt' => $post_comment->comment_date_gmt
);
// Reply meta
$reply_meta = array(
'author_ip' => $post_comment->comment_author_IP ,
'forum_id' => $topic_forum,
'topic_id' => $topic_id,
);
// If not registered user, add anonymous user information
if ( empty( $post_comment->user_id )) {
// Parse args
$anonymous = array(
'anonymous_name' => $post_comment->comment_author,
'anonymous_email'=> $post_comment->comment_author_email,
'anonymous_website' => $post_comment->comment_author_url
);
$reply_meta = wp_parse_args( $reply_meta, $anonymous );
}
$reply_id = bbp_insert_reply( $reply_data, $reply_meta );
$topic_permalink = bbp_get_topic_permalink($topic_id);
if ( ! empty($post_comment->comment_author_email) ) {
$post_link = get_permalink( $post_comment->comment_post_ID );
$post_title = get_the_title( $post_comment->comment_post_ID );
$message = "<p>¡Hola, $post_comment->comment_author! Queremos informarte de que estamos mejorando Cuidado Infantil y hemos trasladado el comentario que hiciste en el artículo <a href='$post_link' target='_blank'>$post_title</a> al nuevo foro de la web, en el cual puedes hablar con otros usuarios sobre cualquier duda que tengas en el cuidado de tu bebé o niño.</p><p>Puedes encontrar tu comentario y otros relacionados pulsando sobre este enlace: <a href='$topic_permalink' target='_blank'>$topic_permalink</a></p><p>Además, si te registras, tendrás la posibilidad de conocer otras mamás o papás de niños de la edad del tuyo con los que, seguro, aprenderás consejos nuevos cada día.</p><br /><p>¡Un fuerte abrazo!<br /><b>El equipo de Cuidado Infantil</b></p>";
wp_mail($post_comment->comment_author_email, 'Sobre tu comentario en Cuidado Infantil', $message);
}
}
}
}