Making WordPress.org

Opened 5 months ago

Last modified 9 days ago

#8145 new enhancement

Highlight the Spam and Pending menu items when posts are awaiting approval

Reported by: la-geek's profile La Geek Owned by:
Milestone: Priority: normal
Component: Support Forums Keywords: has-patch
Cc:

Description

It would be very helpful if we could receive visual support during moderation, namely if the items “Spam” and “Pending” were somehow highlighted in the right sidebar (thread view) when posts are awaiting approval (To put this request into context: I am a moderator on the German WordPress forum de.w.org/support).

Email notifications are probably well suited for less frequented forums (as indicated in the ticket). A number indicating how many spam or pending posts are waiting to be approved is unnecessary. It would be sufficient to highlight the “Spam” and “Pending” menu items in some way when there are posts waiting to be approved: text color, border, icon, etc.

It often happens that we respond, but there are already pending posts in this thread and often the responses are superfluous or miss the point. You would have to check before every response whether there are spam or pending posts waiting to be approved.

Attachments (3)

awaiting_approval.jpg (353.0 KB) - added by La Geek 5 months ago.
highlight1.png (104.1 KB) - added by La Geek 5 months ago.
Screenshot 2026-03-23 at 10.46.22 am.png (35.9 KB) - added by dd32 2 months ago.

Download all attachments as: .zip

Change History (13)

#1 @La Geek
5 months ago

Email notifications are probably well suited for less frequented forums (as indicated in the ticket)

This refers to this ticket:
https://meta.trac.wordpress.org/ticket/3570

#2 @Otto42
5 months ago

What coloring or other highlighting would you suggest?

@La Geek
5 months ago

#3 @La Geek
5 months ago

Perhaps something like this, background color of the link?

Last edited 5 months ago by La Geek (previous) (diff)

#4 @La Geek
2 months ago

I have asked ChatGPT; I have no idea if that's feasible. But perhaps it's a good starting point for solving the problem.

Both as a MU-Plugin

Using WP_Query (flexible, precise):

<?php
/**
 * Plugin Name: bbPress Moderation Indicator (Query-based)
 * Description: Adds ⚠ indicators to "Pending" and "Spam" views in bbPress sidebar when items exist.
 * Version: 1.0
 */

if (!defined('ABSPATH')) {
    exit; // جلوگیری direct access
}

/**
 * Check if items exist for a given post status using WP_Query
 */
function bbpmi_has_items_by_status($status) {

    // Only moderators should see indicators
    if (!current_user_can('moderate')) {
        return false;
    }

    // Allow only specific statuses
    $allowed_statuses = array('pending', 'spam');
    if (!in_array($status, $allowed_statuses, true)) {
        return false;
    }

    $transient_key = 'bbpmi_q_' . $status;

    // Return cached result if available
    $cached = get_transient($transient_key);
    if ($cached !== false) {
        return (bool) $cached;
    }

    // Query only 1 post to check existence
    $query = new WP_Query(array(
        'post_type'      => array(
            bbp_get_topic_post_type(),
            bbp_get_reply_post_type()
        ),
        'post_status'    => $status,
        'posts_per_page' => 1,
        'fields'         => 'ids',
        'no_found_rows'  => true,
        'cache_results'  => true,
        'update_post_meta_cache' => false,
        'update_post_term_cache' => false,
    ));

    $has_items = $query->have_posts();

    // Cache result for short time
    set_transient($transient_key, $has_items, 60);

    return $has_items;
}

/**
 * Inject warning icons into bbPress sidebar views
 */
add_filter('bbp_get_views', function ($views) {

    if (!is_array($views)) {
        return $views;
    }

    foreach ($views as $key => $html) {

        if (!is_string($html)) {
            continue;
        }

        // Pending indicator
        if ($key === 'pending' && bbpmi_has_items_by_status('pending')) {
            $views[$key] = str_replace(
                '</a>',
                ' <span class="bbpmi-alert bbpmi-pending" title="' . esc_attr__('Pending items exist', 'bbpress') . '">⚠</span></a>',
                $html
            );
        }

        // Spam indicator
        if ($key === 'spam' && bbpmi_has_items_by_status('spam')) {
            $views[$key] = str_replace(
                '</a>',
                ' <span class="bbpmi-alert bbpmi-spam" title="' . esc_attr__('Spam items exist', 'bbpress') . '">⚠</span></a>',
                $html
            );
        }
    }

    return $views;
});

/**
 * Clear cache when relevant posts change
 */
function bbpmi_flush_cache($post_id = 0) {

    if ($post_id) {
        $type = get_post_type($post_id);

        if (!in_array($type, array(
            bbp_get_topic_post_type(),
            bbp_get_reply_post_type()
        ), true)) {
            return;
        }
    }

    delete_transient('bbpmi_q_pending');
    delete_transient('bbpmi_q_spam');
}

add_action('save_post', 'bbpmi_flush_cache');
add_action('transition_post_status', 'bbpmi_flush_cache');

/**
 * Add inline CSS only on bbPress pages
 */
add_action('wp_enqueue_scripts', function () {

    if (!function_exists('is_bbpress') || !is_bbpress()) {
        return;
    }

    wp_register_style('bbpmi-style', false);
    wp_enqueue_style('bbpmi-style');

    wp_add_inline_style('bbpmi-style', "
        #bbpress-forums .bbpmi-alert {
            margin-left: 4px;
            font-weight: bold;
        }
        #bbpress-forums .bbpmi-pending {
            color: #dba617;
        }
        #bbpress-forums .bbpmi-spam {
            color: #d63638;
        }
    ");
});

#5 @La Geek
2 months ago

Or

Using wp_count_posts (high-performance):

<?php
/**
 * Plugin Name: bbPress Moderation Indicator (Count-based)
 * Description: Adds ⚠ indicators using wp_count_posts (fast, no queries).
 * Version: 1.0
 */

if (!defined('ABSPATH')) {
    exit; // Prevent direct access
}

/**
 * Get count of items for a given status (topics + replies)
 */
function bbpmi_count_items_by_status($status) {

    if (!current_user_can('moderate')) {
        return 0;
    }

    $allowed_statuses = array('pending', 'spam');
    if (!in_array($status, $allowed_statuses, true)) {
        return 0;
    }

    // Get counts for topics and replies separately
    $topic_counts = wp_count_posts(bbp_get_topic_post_type());
    $reply_counts = wp_count_posts(bbp_get_reply_post_type());

    $topic_count = isset($topic_counts->$status) ? (int) $topic_counts->$status : 0;
    $reply_count = isset($reply_counts->$status) ? (int) $reply_counts->$status : 0;

    return $topic_count + $reply_count;
}

/**
 * Add indicators to bbPress views
 */
add_filter('bbp_get_views', function ($views) {

    if (!is_array($views)) {
        return $views;
    }

    foreach ($views as $key => $html) {

        if (!is_string($html)) {
            continue;
        }

        // Pending indicator
        if ($key === 'pending' && bbpmi_count_items_by_status('pending') > 0) {
            $views[$key] = str_replace(
                '</a>',
                ' <span class="bbpmi-alert bbpmi-pending" title="' . esc_attr__('Pending items exist', 'bbpress') . '">⚠</span></a>',
                $html
            );
        }

        // Spam indicator
        if ($key === 'spam' && bbpmi_count_items_by_status('spam') > 0) {
            $views[$key] = str_replace(
                '</a>',
                ' <span class="bbpmi-alert bbpmi-spam" title="' . esc_attr__('Spam items exist', 'bbpress') . '">⚠</span></a>',
                $html
            );
        }
    }

    return $views;
});

/**
 * Add CSS only on bbPress pages
 */
add_action('wp_enqueue_scripts', function () {

    if (!function_exists('is_bbpress') || !is_bbpress()) {
        return;
    }

    wp_register_style('bbpmi-style', false);
    wp_enqueue_style('bbpmi-style');

    wp_add_inline_style('bbpmi-style', "
        #bbpress-forums .bbpmi-alert {
            margin-left: 4px;
            font-weight: bold;
        }
        #bbpress-forums .bbpmi-pending {
            color: #dba617;
        }
        #bbpress-forums .bbpmi-spam {
            color: #d63638;
        }
    ");
});

This ticket was mentioned in Slack in #forums by la-geek. View the logs.


2 months ago

#7 @dd32
2 months ago

visual support during moderation, namely if the items “Spam” and “Pending” were somehow highlighted in the right sidebar (thread view)

My thinking is a right-aligned bubble with the count in it. That way if you've seen something you can mentally recall "those 2 posts in there are waiting for someone else, oh it's 3 now, okay I'll go check", such as the attachment.

Using wp_count_posts (high-performance):

This is probably a non-starter, as wp_count_posts() isn't very performant on large datasets :)

Using WP_Query (flexible, precise):

This is probably a viable approach.

#8 @La Geek
8 weeks ago

  • Type changed from feature request to enhancement

#9 @La Geek
7 weeks ago

  • Keywords needs-testing has-patch added

#10 @La Geek
9 days ago

  • Keywords needs-testing removed

Is there anything else I can do? Are there any issues with the code? Why isn't this being incorporated into the bbPress code? Thanks.

Note: See TracTickets for help on using tickets.