Skip to:
Content

bbPress.org

Opened 17 months ago

Closed 17 months ago

Last modified 3 weeks ago

#3588 closed defect (bug) (duplicate)

Registered meta values for bbPress forum and topic post types not properly defined, causing problems with REST updates

Reported by: terresquall's profile terresquall Owned by:
Milestone: Priority: high
Severity: normal Version:
Component: General - Administration Keywords:
Cc:

Description

I've noticed in the register_meta() method in bbpress.php, there is a section that registers metadata for users and the custom post types that bbPress uses:

// Define "count" meta-type array
$count = array(

        // Counts are always integers
        'type'              => 'integer',

        // Generic count description
        'description'       => esc_html__( 'bbPress Item Count', 'bbpress' ),

        // Counts are single values
        'single'            => true,

        // Counts should be made available in REST
        'show_in_rest'      => true,

        // Never allow counts to go negative
        'sanitize_callback' => 'bbp_number_not_negative',

        // All users may update count meta data
        'auth_callback'     => '__return_true'
);

/** Post **************************************************************/

// Counts
register_meta( 'post', '_bbp_topic_count',           $count );
register_meta( 'post', '_bbp_reply_count',           $count );
register_meta( 'post', '_bbp_total_topic_count',     $count );
register_meta( 'post', '_bbp_total_reply_count',     $count );
register_meta( 'post', '_bbp_voice_count',           $count );
register_meta( 'post', '_bbp_anonymous_reply_count', $count );
register_meta( 'post', '_bbp_topic_count_hidden',    $count );
register_meta( 'post', '_bbp_reply_count_hidden',    $count );
register_meta( 'post', '_bbp_forum_subforum_count',  $count );

This is all fine and dandy, but these meta registrations do not define an object_subtype, i.e. they are not tied to a particular post type, and are registered for all standard and custom posts.

Usually, this doesn't create any issue, since you don't have to register metadata to use them. But if you were to retrieve the metadata of any post in REST, the data will always be included with the post's metadata, regardless of whether the post is actually a bbPress Forum / Topic or not.

// When retrieving post data from WordPress's Gutenberg framework,
// the bbPress meta keys will always be included, even for standard post types.
const meta = wp.data.select('core/editor').getEditedPostAttribute('meta');

Clearly, this is an inconvenience because you will then have to strip the meta values of these unrelated bbPress meta values before you save them.

The fix is simple! Just attach a post type to all registered post meta!

<?php
/** Post **************************************************************/
$forum = array_merge($count, array('object_subtype' => 'forum'));
$topic = array_merge($count, array('object_subtype' => 'topic'));
                
// Counts
register_meta( 'post', '_bbp_topic_count',           $forum );
register_meta( 'post', '_bbp_reply_count',           $forum );
register_meta( 'post', '_bbp_reply_count',           $topic );
register_meta( 'post', '_bbp_total_topic_count',     $forum );
register_meta( 'post', '_bbp_total_reply_count',     $forum );
register_meta( 'post', '_bbp_voice_count',           $topic );
register_meta( 'post', '_bbp_anonymous_reply_count', $topic );
register_meta( 'post', '_bbp_topic_count_hidden',    $forum );
register_meta( 'post', '_bbp_reply_count_hidden',    $forum );
register_meta( 'post', '_bbp_reply_count_hidden',    $topic );
register_meta( 'post', '_bbp_forum_subforum_count',  $forum );

I've deployed the fix in a pull request. I hope this gets implemented soon! It is annoying to deal with.

Change History (1)

#1 @r-a-y
17 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi terresquall, I have an open ticket about this. See #3436.

Feel free to attach a new patch if the ones I have there do not fix your problem.

Note: See TracTickets for help on using tickets.