Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/19/2017 04:29:43 AM (9 years ago)
Author:
johnjamesjacoby
Message:

Better 404 handling:

  • Introduce bbp_is_404 query var, and set this in places where the default 404 condition needs to be overridden
  • Introduce bbp_set_200() for cases where a default of 404 needs to be set to a 200
  • Introduce bbp_get_wp_query() helper for getting the $wp_query global
  • Update bbp_set_404() to accept a $query parameter to make passing the query around easier
  • Update child-ids queries to use the last_changed cache to reduce the amount of cache churn when cleaning

Fixes #3047. See #1973.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/forums/functions.php

    r6573 r6583  
    963963    // Query for private forums
    964964    $private_forums = new WP_Query( array(
     965        'fields'           => 'ids',
    965966        'suppress_filters' => true,
    966         'nopaging'         => true,
    967         'no_found_rows'    => true,
    968967        'post_type'        => bbp_get_forum_post_type(),
    969968        'post_status'      => bbp_get_private_status_id(),
    970         'fields'           => 'ids'
     969        'posts_per_page'   => -1,
     970
     971        // Performance
     972        'ignore_sticky_posts'    => true,
     973        'no_found_rows'          => true,
     974        'nopaging'               => true,
     975        'update_post_term_cache' => false,
     976        'update_post_meta_cache' => false
    971977    ) );
    972978
    973979    // Query for hidden forums
    974980    $hidden_forums = new WP_Query( array(
     981        'fields'           => 'ids',
    975982        'suppress_filters' => true,
    976         'nopaging'         => true,
    977         'no_found_rows'    => true,
    978983        'post_type'        => bbp_get_forum_post_type(),
    979984        'post_status'      => bbp_get_hidden_status_id(),
    980         'fields'           => 'ids'
     985        'posts_per_page'   => -1,
     986
     987        // Performance
     988        'ignore_sticky_posts'    => true,
     989        'no_found_rows'          => true,
     990        'nopaging'               => true,
     991        'update_post_term_cache' => false,
     992        'update_post_meta_cache' => false
    981993    ) );
    982994
     
    16501662        if ( empty( $topic_count ) ) {
    16511663            $query = new WP_Query( array(
    1652                 'fields'      => 'ids',
    1653                 'post_parent' => $forum_id,
    1654                 'post_status' => array( bbp_get_trash_status_id(), bbp_get_spam_status_id(), bbp_get_pending_status_id() ),
    1655                 'post_type'   => bbp_get_topic_post_type(),
    1656 
    1657                 // Maybe change these later
    1658                 'posts_per_page'         => -1,
     1664                'fields'           => 'ids',
     1665                'suppress_filters' => true,
     1666                'post_parent'      => $forum_id,
     1667                'post_status'      => array( bbp_get_trash_status_id(), bbp_get_spam_status_id(), bbp_get_pending_status_id() ),
     1668                'post_type'        => bbp_get_topic_post_type(),
     1669                'posts_per_page'   => -1,
     1670
     1671                // Performance
    16591672                'update_post_term_cache' => false,
    16601673                'update_post_meta_cache' => false,
    16611674                'ignore_sticky_posts'    => true,
    1662                 'no_found_rows'          => true
     1675                'no_found_rows'          => true,
     1676                'nopaging'               => true
    16631677            ) );
    16641678            $topic_count = $query->post_count;
     
    17061720    if ( ! empty( $topic_ids ) ) {
    17071721        $query = new WP_Query( array(
    1708             'fields'          => 'ids',
    1709             'post_parent__in' => $topic_ids,
    1710             'post_status'     => bbp_get_public_status_id(),
    1711             'post_type'       => bbp_get_reply_post_type(),
    1712 
    1713             // Maybe change these later
    1714             'posts_per_page'         => -1,
     1722            'fields'           => 'ids',
     1723            'suppress_filters' => true,
     1724            'post_parent__in'  => $topic_ids,
     1725            'post_status'      => bbp_get_public_status_id(),
     1726            'post_type'        => bbp_get_reply_post_type(),
     1727            'posts_per_page'   => -1,
     1728
     1729            // Performance
    17151730            'update_post_term_cache' => false,
    17161731            'update_post_meta_cache' => false,
    17171732            'ignore_sticky_posts'    => true,
    1718             'no_found_rows'          => true
     1733            'no_found_rows'          => true,
     1734            'nopaging'               => true
    17191735        ) );
    17201736        $reply_count = ! empty( $query->posts ) ? count( $query->posts ) : 0;
     
    21062122
    21072123    $query = new WP_Query( array(
    2108         'fields'          => 'ids',
    2109         'post_parent__in' => $topic_ids,
    2110         'post_status'     => bbp_get_public_status_id(),
    2111         'post_type'       => bbp_get_reply_post_type(),
    2112         'orderby'         => array(
     2124        'fields'           => 'ids',
     2125        'suppress_filters' => true,
     2126        'post_parent__in'  => $topic_ids,
     2127        'post_status'      => bbp_get_public_status_id(),
     2128        'post_type'        => bbp_get_reply_post_type(),
     2129        'posts_per_page'   => 1,
     2130        'orderby'          => array(
    21132131            'post_date' => 'DESC',
    21142132            'ID'        => 'DESC'
    21152133        ),
    21162134
    2117         // Maybe change these later
    2118         'posts_per_page'         => 1,
     2135        // Performance
    21192136        'update_post_term_cache' => false,
    21202137        'update_post_meta_cache' => false,
    21212138        'ignore_sticky_posts'    => true,
    2122         'no_found_rows'          => true
     2139        'no_found_rows'          => true,
     2140        'nopaging'               => true
    21232141    ) );
    21242142    $reply_id = array_shift( $query->posts );
     
    21442162    }
    21452163
    2146     global $wp_query;
    2147 
    2148     // Define local variable
     2164    // Define local variables
    21492165    $forum_id = 0;
     2166    $wp_query = bbp_get_wp_query();
    21502167
    21512168    // Check post type
     
    21702187    // If forum is explicitly hidden and user not capable, set 404
    21712188    if ( ! empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
    2172         bbp_set_404();
     2189        bbp_set_404( $wp_query );
    21732190    }
    21742191}
     
    21872204    }
    21882205
    2189     global $wp_query;
    2190 
    2191     // Define local variable
     2206    // Define local variables
    21922207    $forum_id = 0;
     2208    $wp_query = bbp_get_wp_query();
    21932209
    21942210    // Check post type
     
    22142230    // If forum is explicitly hidden and user not capable, set 404
    22152231    if ( ! empty( $forum_id ) && bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) {
    2216         bbp_set_404();
     2232        bbp_set_404( $wp_query );
    22172233    }
    22182234}
     
    22572273    // Note that we get all post statuses here
    22582274    $topics = new WP_Query( array(
     2275        'fields'           => 'id=>parent',
    22592276        'suppress_filters' => true,
     2277
     2278        // What and how
    22602279        'post_type'        => bbp_get_topic_post_type(),
    22612280        'post_parent'      => $forum_id,
    22622281        'post_status'      => array_keys( get_post_stati() ),
    22632282        'posts_per_page'   => -1,
    2264         'nopaging'         => true,
    2265         'no_found_rows'    => true,
    2266         'fields'           => 'id=>parent'
     2283
     2284        // Performance
     2285        'ignore_sticky_posts'    => true,
     2286        'no_found_rows'          => true,
     2287        'nopaging'               => true,
     2288        'update_post_term_cache' => false,
     2289        'update_post_meta_cache' => false
    22672290    ) );
    22682291
     
    23052328    );
    23062329
    2307     // Forum is being trashed, so its topics and replies are trashed too
     2330    // Forum is being trashed, so its topics (and replies) are trashed too
    23082331    $topics = new WP_Query( array(
     2332        'fields'           => 'id=>parent',
    23092333        'suppress_filters' => true,
    23102334        'post_type'        => bbp_get_topic_post_type(),
     
    23122336        'post_status'      => $post_stati,
    23132337        'posts_per_page'   => -1,
    2314         'nopaging'         => true,
    2315         'no_found_rows'    => true,
    2316         'fields'           => 'id=>parent'
     2338
     2339        // Performance
     2340        'ignore_sticky_posts'    => true,
     2341        'no_found_rows'          => true,
     2342        'nopaging'               => true,
     2343        'update_post_term_cache' => false,
     2344        'update_post_meta_cache' => false
    23172345    ) );
    23182346
Note: See TracChangeset for help on using the changeset viewer.