XF 2.2 A Bit Of Help Please

Ozzy47

Well-known member
I've been tinkering with an addon that will automatically moderate a thread/post if it contains censored words. I'm using a listener on entity_post_save that seems to be working okay.

PHP:
    // Moderate Posts If Necessary
    public static function moderatePostsPostSave(\XF\Mvc\Entity\Entity $entity)
    {
        // Get Visitor
        $visitor = \XF::visitor();
 
        // Get options
        $options = \XF::options();
 
        // Get Variables
        $opt1 = $options->ozzmodz_moderate_censored_words_posts;
      
        //Set Counter
        $counter = 0;
      
        if($entity->isInsert() )
        {
            if ($opt1 && !$visitor->hasPermission('ozzmodzMCWPosts', 'bypass'))
            {
                foreach (\XF::options()->censorWords as $word)
                {
                    preg_match($word["regex"], $entity->message, $matches, PREG_OFFSET_CAPTURE);

                    if ($matches && !$entity->getRelationOrDefault('ApprovalQueue')->isDeleted())
                    {
                        $counter++;
                    }
                }
            }
          
            if ($counter)
            {
                $entity->fastUpdate('message_state', "moderated");
                $approvalQueue = $entity->getRelationOrDefault('ApprovalQueue', false);
                $approvalQueue->save();
            } else {
                $entity->fastUpdate('message_state', "visible");
                $approvalQueue = $entity->getRelation('ApprovalQueue');
              
                if($approvalQueue)
                {
                    $approvalQueue->delete();
                }
            }
          }
    }

The above works great when posting a new post. But If I edit a post, it doesn't auto moderate the post. So I changed this, if($entity->isInsert() ) to this, if($entity->isInsert() || $entity->isUpdate() ) and now if you edit a post and add censored words, it's auto moderated. Perfect, or so I thought.

Problem now is if I go to to the approval queue and delete the post, it actually makes the post live, it don't delete it. I'm not sure what to do to overcome that behavior.
 
Last edited:
I would probably recommend extending the thread creator/replier services instead. As described, your code will run every time the entity is updated (an entity is always being either inserted or updated at pre/post-save time).
 
Top Bottom