Skip to:
Content

BuddyPress.org

Changeset 13332


Ignore:
Timestamp:
09/30/2022 08:04:47 PM (3 years ago)
Author:
imath
Message:

Avoid querying for comments when viewing a BuddyPress page in TT3

The Twenty Twenty-Three block theme is using the core/comments block to
display comments for WP Pages or Posts. After checking if comments are
open, this block is doing an additional comment count query which wrongly
returns the site's all comment count as BuddyPress pages ID is reset to
0.

In this case forcing comments to be closed is not enough, we also need to
force the comment count to be 0.

See #8744 (trunk)

--Cette ligne, et les suivantes
ci-dessous, seront ignorées--

M src/bp-core/bp-core-filters.php
M src/bp-core/bp-core-theme-compatibility.php

Location:
trunk/src/bp-core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-filters.php

    r13322 r13332  
    7878// Turn comments off for BuddyPress pages.
    7979add_filter( 'comments_open', 'bp_comments_open', 10, 2 );
     80
     81// Force comments count to be 0 or comments list to be an empty array.
     82add_filter( 'comments_pre_query', 'bp_comments_pre_query', 10, 2 );
    8083
    8184// Prevent DB query for WP's main loop.
  • trunk/src/bp-core/bp-core-theme-compatibility.php

    r13108 r13332  
    937937
    938938/**
     939 * Avoid potential extra comment query on BuddyPress pages.
     940 *
     941 * @since 10.5.0
     942 *
     943 * @param array|int|null   $comment_data     The comments list, the comment count or null.
     944 * @param WP_Comment_Query $wp_comment_query The WP_Comment_Query instance.
     945 * @return array|int|null Null to leave WordPress deal with the comment query, an empty array or 0 to shortcircuit it.
     946 */
     947function bp_comments_pre_query( $comment_data, $wp_comment_query ) {
     948    $is_post_null = isset( $wp_comment_query->query_vars['post_id'] ) && 0 === (int) $wp_comment_query->query_vars['post_id'];
     949
     950    if ( ! is_buddypress() || ! $is_post_null ) {
     951        return $comment_data;
     952    }
     953
     954    if ( isset( $wp_comment_query->query_vars['count'] ) && $wp_comment_query->query_vars['count'] ) {
     955        $comment_data = 0;
     956    } else {
     957        $comment_data = array();
     958    }
     959
     960    return $comment_data;
     961}
     962
     963/**
    939964 * Do not allow {@link comments_template()} to render during theme compatibility.
    940965 *
Note: See TracChangeset for help on using the changeset viewer.