super powered (@super-powered)

Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)

  • super powered
    Participant

    @super-powered

    Relevant tag validation code for above since I had to write it for my project anyways:

    
    
    function new_reply_pre_set_terms_tag_test($terms, $topic_id, $reply_id)
    {
        //Bail Early if not set
        if(!$terms)
            return $terms;
    
        //Bail early if we are allowed to modify tags
        if(current_user_can( 'modify_topic_tags' ))
            return $terms;
    
        if(!is_array($terms))
            $terms = explode(', ', $terms);
    
        //For each to check if array term already exists. Remove if doesnt
        foreach ($terms as $tag => $tag_value):
            if(!term_exists($tag_value, 'topic-tag')):
                unset($terms[$tag]);
            endif;
        endforeach;
    
        return implode(', ', $terms);
    }
    
    function new_topic_pre_insert_tag_test($data)
    {
        //Bail Early
        if(!is_array($data) || !isset($data['tax_input']) || !isset($data['tax_input']['topic-tag']))
            return $data;
    
        //Bail early if we are allowed to modify tags
        if(current_user_can( 'modify_topic_tags' ))
            return $data;
    
        //Make sure we're an array
        if(!is_array($data['tax_input']['topic-tag']))
            $data['tax_input']['topic-tag'] = array($data['tax_input']['topic-tag']);
    
        //For each to check if array term already exists. Remove if doesnt
        foreach ($data['tax_input']['topic-tag'] as $tag => $tag_value):
            if(!term_exists($tag_value, 'topic-tag')):
                unset($data['tax_input']['topic-tag'][$tag]);
            endif;
        endforeach;
    
        return $data;
    }
    
    add_filter('bbp_new_reply_pre_set_terms','new_reply_pre_set_terms_tag_test', 22, 3);
    add_filter('bbp_edit_reply_pre_set_terms','new_reply_pre_set_terms_tag_test', 22, 3);
    add_filter('bbp_new_topic_pre_insert','new_topic_pre_insert_tag_test');
    
    

    super powered
    Participant

    @super-powered

    The reason the function doesn’t work is because BBPress throws a fatal error when it tries to handle the array of items when it expects it as a string. We probably just need to get a filter in the core that allows us to manipulate that value before it tries to use the split() function.

    Until then, kind of a hacky workaround is to use JS to append the values to a hidden field:

    
    function display_tag_elements($selectedTags, $userRole, $task)
    {
        $html = array();
        $tags = get_categories(array('hide_empty' => 0, 'taxonomy' => 'topic-tag'));
        $selectedTagsArray = explode(', ', $selectedTags);
        if ($userRole != 'bbp_participant' || $task != 'edit')
        {
            $html[] = "";
            foreach ($tags as $tag)
            {
                $selected = '';
                if (in_array($tag->name, $selectedTagsArray))
                {
                    $selected = 'checked';
                }
                $html[] = "<input type='checkbox' class='bbp_topic_tags_checkbox' name='bbp_topic_tags_checkbox[]'" . $selected . " value='" . $tag->name . "'>" . $tag->name . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
    
            $html[] = "<input type='hidden' class='bbp_topic_tags_hidden' name='bbp_topic_tags' value='".$selectedTags."'>";
    
            $html[] = "
                <script>
                    var checkboxes = document.getElementsByClassName('bbp_topic_tags_checkbox');
                    var hidden = document.getElementsByClassName('bbp_topic_tags_hidden');
                    var hiddenVal = hidden[0].value.split(', ');
                    
                    for(var x = 0; x < checkboxes.length; x++)
                    {
                        checkboxes[x].addEventListener( 'change', function() 
                        {
                            var index =  hiddenVal.indexOf(this.value);
                            var checked = this.checked;
                            if(checked && !index > -1) 
                                 hiddenVal.push(this.value);
                            else if (!checked && index > -1)
                                 hiddenVal.splice(index, 1);
                            
                             hidden[0].value = hiddenVal.join();
                        });
                    }
                </script>
            ";
        } else
        {
            $html[] = $selectedTags;
        }
    
        return implode('', $html);
    }
    
    

    However this has a pretty notable downside in that someone could just inspect the code and add whatever they want to the hidden field. So you’ll want to make sure your also using one of the spam filters like bbp_new_reply_pre_set_terms to make sure the new values are terms you’ve predefined.

    Side note: there also seems to be a pretty annoying bug in that participants can edit the topics tags on reply, so I would just disable it on reply for non-moderators.

    EDIT: kind of like I just did. I don’t think I should be allowed to add tags to existing topics. this feels like a bug.

Viewing 2 replies - 1 through 2 (of 2 total)