LPH
Well-known member
I'm trying to collect the posts from a thread and compare them to the comments written in WordPress. Take the difference and write the post information to the WordPress table.
It kinda works, until I refresh the page and then the script tries to write the same posts into WordPress. It becomes a big circle.
Here is the class.
My question is ... do you see anything in the logic / reasoning to cause a refresh of the page to not change the array_diff? The $commentdata is being written to the WordPress database.
var_dump is showing the array_diff returns the values correctly the first time but the refresh leads to the array_diff giving the same values.
The post_id should always be unique in the commentmeta table, but in looking there are duplicates being written. So something in the conditionals isn't catching duplicates and making sure they aren't written.
I hope this makes sense.
It kinda works, until I refresh the page and then the script tries to write the same posts into WordPress. It becomes a big circle.
Here is the class.
PHP:
class Display {
/**
* comments
* @var array
*/
protected $comments;
public function __construct() {
$this->hooks();
}
public function hooks() {
add_filter( 'comments_array', array( $this, 'BuildCommentsArray' ), 10, 2 );
}
/**
* Create an array of replies that have not been written to the comments table
* then write these comments to the comment and commentmeta table.
*
* Separate out comments that have been written to the comment meta table
*
* @param $comment
* @param $post_id
*
* @return array|string
* @throws Zend_Exception
*/
public function BuildCommentsArray( $comment, $post_id ) {
global $XF, $wp_query;
/**
* Loads the XenWord options saved in the XenWord panel.
* @var $xenword_options
*/
$xenword_options = get_option( 'xenword_options' );
$wp_query->set( 'comments_per_page', $xenword_options['comments_per_page'] );
if ( XenWord_Comments::useXenForoCommentHandler() ) {
global $post;
$threadId = get_post_meta( $post->ID, 'thread_id', true);
/** @var $postModel XenForo_Model_Post */
$postModel = XenForo_Model::create('XenForo_Model_Post');
$postIds = $postModel->getPostIdsInThread($threadId, $ordered = true);
array_shift($postIds);
foreach ( $comment as $mycomment ) {
$post_id = (int)( get_comment_meta( $mycomment->comment_ID, 'post_id', true ) );
$post_ids[] = $post_id;
}
$nonwp_postIds = array_diff( $postIds, $post_ids);
// This is returning the difference in arrays but on refresh keeps showing the same ones.
var_dump($nonwp_postIds);
foreach ( $nonwp_postIds as $nonwp_postId ) {
$xf_post = $postModel->getPostById($nonwp_postId);
// var_dump($xf_post); // shows array of post information !
$commentdata = array(
'comment_post_ID' => $post->ID,
'comment_author' => $xf_post['username'],
'comment_author_email' => '',
'comment_author_url' => '',
'comment_content' => $xf_post['message'],
'comment_type' => '',
'comment_parent' => 0,
'user_id' => $xf_post['user_id']
);
// Insert new comment and get the comment ID.
$comment_id = wp_new_comment( $commentdata );
if ( isset( $xf_post['post_id'] ) ) {
update_comment_meta( $comment_id, 'post_id', $xf_post['post_id'] );
}
if ( isset ( $xf_post['thread_id'] ) ) {
update_comment_meta( $comment_id, 'thread_id', $xf_post['thread_id'] );
}
}
}
return $comment; // Return comments from WordPress
}
}
$display = new Display();
My question is ... do you see anything in the logic / reasoning to cause a refresh of the page to not change the array_diff? The $commentdata is being written to the WordPress database.
var_dump is showing the array_diff returns the values correctly the first time but the refresh leads to the array_diff giving the same values.
The post_id should always be unique in the commentmeta table, but in looking there are duplicates being written. So something in the conditionals isn't catching duplicates and making sure they aren't written.
I hope this makes sense.