Asgaros / asgaros-forum

Asgaros Forum WordPress plugin repository.
https://asgaros.com
GNU General Public License v2.0
90 stars 34 forks source link

forum shortcode filters don't work #349

Closed mbaierl closed 1 year ago

mbaierl commented 2 years ago

Forum shortcode filters like i.e. [forum forum="11"] don't do anything at all.

While trying to debug the issue I noticed that the function public function handleAttributes() { seems to use a wrong match: $atts = shortcode_parse_atts($matches[3][0]);

[3][0] is always empty, if I use [3][1] it works fine.

And I am wondering why the shortcode attributes are parsed separately and not taken out of the add_shortcode call?

mbaierl commented 2 years ago

Any update on this?

Asgaros commented 1 year ago

Hello @mbaierl

The shortcode filter works fine for me on a clean WordPress installation. I cannot reproduce this issue.

Asgaros commented 1 year ago

I will close this issue because I still cannot reproduce it and also didn't receive and further feedback or similar reports.

A short background note why I parse the shortcode separately: Based on the given parameter, many high-level functionalities of the forum behave differently. So I need to know if and which parameters got passed to the shortcode during an early stage before any rendering happens. If I only do this at the very end when the shortcode gets executed, then many parts of the page already got generated (like header, etc.) which is too late.

daniyalahmadk commented 1 week ago

I was able to reproduce this issue as well. It happens when you already have a shortcode on the top of Asgaros forum shortcode. For exmaple:

When I have

[shortcode x] [forum forum=ID] I have all forums

[forum forum=ID] [shortcode x] I have only 1 forum

To fix the issue, I modified the forum-shortcodes.php (includes/forum-shortcodes.php) to the following one (added another function get_forum_attribute_string to better parse the content)

    public function get_forum_attribute_string($content) {
        // Match the [forum ... ] shortcode and capture its attributes
        if (preg_match('/\[forum\s+([^\]]+)\]/', $content, $matches)) {
            $attributes = $matches[1]; // Contains all attributes inside the shortcode

            // Use regex to find the exact 'forum="6"' attribute
            if (preg_match('/forum="[^"]*"/', $attributes, $attr_match)) {
                return $attr_match[0]; // Returns 'forum="6"'
            }
        }
        return null; // Return null if not found
    }
    public function handleAttributes() {
        $atts    = array();
        $pattern = get_shortcode_regex();

        if ($this->checkForShortcode()) {
            $shortcodetag = $this->get_forum_attribute_string($this->postObject->post_content);
            $atts = shortcode_parse_atts($shortcodetag);

            if (!empty($atts)) {
                // Normalize attribute keys.
                $atts = array_change_key_case((array) $atts, CASE_LOWER);

                if (!empty($atts['post']) && ctype_digit($atts['post'])) {
                    $postID                           = $atts['post'];
                    $this->asgarosforum->current_view = 'post';
                    $this->asgarosforum->setParents($postID, 'post');
                } else if (!empty($atts['topic']) && ctype_digit($atts['topic'])) {
                    $topicID      = $atts['topic'];
                    $allowedViews = array('movetopic', 'addpost', 'editpost', 'topic', 'profile', 'history');

                    // Ensure that we are in the correct element.
                    if ($this->asgarosforum->current_topic != $topicID) {
                        $this->asgarosforum->setParents($topicID, 'topic');
                        $this->asgarosforum->current_view = 'topic';
                    } else if (!in_array($this->asgarosforum->current_view, $allowedViews)) {
                        // Ensure that we are in an allowed view.
                        $this->asgarosforum->current_view = 'topic';
                    }

                    // Configure components.
                    $this->asgarosforum->options['enable_search']       = false;
                    $this->asgarosforum->breadcrumbs->breadcrumbs_level = 1;
                } else if (!empty($atts['forum']) && ctype_digit($atts['forum'])) {
                    $forumID      = $atts['forum'];
                    $allowedViews = array('forum', 'addtopic', 'movetopic', 'addpost', 'editpost', 'topic', 'search', 'subscriptions', 'profile', 'members', 'history');

                    // Ensure that we are in the correct element.
                    if ($this->asgarosforum->current_forum != $forumID && $this->asgarosforum->parent_forum != $forumID && $this->asgarosforum->current_view != 'search' && $this->asgarosforum->current_view != 'subscriptions' && $this->asgarosforum->current_view != 'profile' && $this->asgarosforum->current_view != 'members' && $this->asgarosforum->current_view != 'history') {
                        $this->asgarosforum->setParents($forumID, 'forum');
                        $this->asgarosforum->current_view = 'forum';
                    } else if (!in_array($this->asgarosforum->current_view, $allowedViews)) {
                        // Ensure that we are in an allowed view.
                        $this->asgarosforum->current_view = 'forum';
                    }

                    // Configure components.
                    $this->asgarosforum->breadcrumbs->breadcrumbs_level = ($this->asgarosforum->parent_forum != $forumID) ? 2 : 3;
                    $this->shortcodeSearchFilter                        = 'AND (f.id = '.$forumID.' OR f.parent_forum = '.$forumID.')';
                } else if (!empty($atts['category'])) {
                    $this->includeCategories = explode(',', $atts['category']);

                    $category_doesnt_matter_views = array('search', 'subscriptions', 'profile', 'members', 'history', 'markallread', 'unread', 'activity', 'unapproved', 'reports');

                    // Ensure that we are in the correct category or view, otherwise show overview.
                    if (!in_array($this->asgarosforum->current_category, $this->includeCategories) && !in_array($this->asgarosforum->current_view, $category_doesnt_matter_views)) {
                        $this->asgarosforum->current_category = false;
                        $this->asgarosforum->parent_forum     = false;
                        $this->asgarosforum->current_forum    = false;
                        $this->asgarosforum->current_topic    = false;
                        $this->asgarosforum->current_post     = false;
                        $this->asgarosforum->current_view     = 'overview';
                    }
                }
            }
        }
    }