1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Incorrect comparison on array_diff or something else?

Discussion in 'XenForo Development Discussions' started by LPH, Apr 12, 2016.

  1. LPH

    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.

    class Display {

        * comments
        * @var array
    protected $comments;

        public function 
    __construct() {


        public function 
    hooks() {
    add_filter'comments_array', array( $this'BuildCommentsArray' ), 10);

        * 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 ) {

            * 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() ) {

    $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);


                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. 

                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'] );
    $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.
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    Well, your array_diff will never find matching elements to remove, as it compares values. The value of $postModel->getPostIdsInThread is an array keyed by post id, so the values are post information arrays, the postIds you have from comments are pure ints. And an int != the array.

    I would base your code around this line:
               $nonwp_postIds array_diff$postIds$post_ids);
    Which I would change to:
               $nonwp_postIds array_diffarray_keys($postIds), $post_ids);
    And then when you cycle through, access $postIds[$nonwp_postId];
  3. LPH

    LPH Well-Known Member

    Thank you. This is what I've discovered since posting.

    The XenForo post is being written to WordPress comment and commentmeta table but immediately WordPress writes a copy of that comment to XenForo and creates another post with another post_id ... creating a loop because now that post_id is unique and that XenForo post is written to WordPress which writes a post to XenForo with a different post_id ... ....

    Therefore, I'm trying to include a meta value for 'origin' in the commentmeta WordPress table and set a conditional off it before allowing the WordPress comments to be written to XenForo post values.

    I hope that makes sense.

    Once the XenForo post is written then a meta key/value pair is written to the commentmeta table.

    The database shows the value, but the WordPress comment still gets written to XenForo despite the conditional.

    $originget_comment_meta$comment->comment_ID'origin'true );
    if ( 
    $origin != 'XenForo') {
    // Write WP Comment to XF
    Unfortunately this is failing and the new XenForo post is created by the new WP comment .. and the loop continues.

Share This Page