Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/19/2017 04:29:43 AM (7 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/common/functions.php

    r6573 r6583  
    9393 */
    9494function bbp_get_paged() {
    95     global $wp_query;
     95    $wp_query = bbp_get_wp_query();
    9696
    9797    // Check the query var
     
    15671567
    15681568    $query = new WP_Query( array(
    1569         'fields'      => 'ids',
    1570         'post_parent' => $parent_id,
    1571         'post_status' => $post_status,
    1572         'post_type'   => $post_type,
     1569        'fields'           => 'ids',
     1570        'suppress_filters' => true,
     1571        'post_parent'      => $parent_id,
     1572        'post_status'      => $post_status,
     1573        'post_type'        => $post_type,
     1574        'posts_per_page'   => -1,
    15731575
    15741576        // Maybe change these later
    1575         'posts_per_page'         => -1,
    15761577        'update_post_term_cache' => false,
    15771578        'update_post_meta_cache' => false,
    15781579        'ignore_sticky_posts'    => true,
    1579         'no_found_rows'          => true
     1580        'no_found_rows'          => true,
     1581        'nopaging'               => true
    15801582    ) );
    15811583    $child_ids = ! empty( $query->posts ) ? $query->posts : array();
     
    16031605    }
    16041606
    1605     // The ID of the cached query
    1606     $cache_id  = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';
     1607    // Check cache key
     1608    $key          = md5( serialize( array( 'parent_id' => $parent_id, 'post_type' => $post_type ) ) );
     1609    $last_changed = wp_cache_get_last_changed( 'bbpress_posts' );
     1610    $cache_key    = "bbp_child_ids:{$key}:{$last_changed}";
    16071611
    16081612    // Check for cache and set if needed
    1609     $child_ids = wp_cache_get( $cache_id, 'bbpress_posts' );
     1613    $child_ids = wp_cache_get( $cache_key, 'bbpress_posts' );
    16101614    if ( false === $child_ids ) {
    16111615        $post_status = array( bbp_get_public_status_id() );
     
    16451649
    16461650        // Always cache the results
    1647         wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );
     1651        wp_cache_set( $cache_key, $child_ids, 'bbpress_posts' );
    16481652    }
    16491653
     
    20162020 *
    20172021 * @since 2.0.0 bbPress (r3051)
    2018  *
    2019  * @global WP_Query $wp_query
    2020  */
    2021 function bbp_set_404() {
    2022     global $wp_query;
    2023 
    2024     if ( ! isset( $wp_query ) ) {
    2025         _doing_it_wrong( __FUNCTION__, esc_html__( 'Conditional query tags do not work before the query is run. Before then, they always return false.', 'bbpress' ), '3.1' );
    2026         return false;
    2027     }
    2028 
    2029     $wp_query->set_404();
    2030 }
    2031 
    2032 /**
    2033  * Maybe avoid default 404 handling for some bbPress pages
     2022 * @since 2.6.0 bbPress (r6583) Use status_header() & nocache_headers()
     2023 *
     2024 * @param WP_Query $query  The query being checked
     2025 *
     2026 * @return bool Always returns true
     2027 */
     2028function bbp_set_404( $query = null ) {
     2029
     2030    // Global fallback
     2031    if ( empty( $query ) ) {
     2032        $query = bbp_get_wp_query();
     2033    }
     2034
     2035    // Setup environment
     2036    $query->set_404();
     2037
     2038    // Setup request
     2039    status_header( 404 );
     2040    nocache_headers();
     2041}
     2042
     2043/**
     2044 * Sets the 200 status header.
     2045 *
     2046 * @since 2.6.0 bbPress (r6583)
     2047 */
     2048function bbp_set_200() {
     2049    status_header( 200 );
     2050}
     2051
     2052/**
     2053 * Maybe handle the default 404 handling for some bbPress conditions
     2054 *
     2055 * Some conditions (like private/hidden forums and edits) have their own checks
     2056 * on `bbp_template_redirect` and are not currently 404s.
    20342057 *
    20352058 * @since 2.6.0 bbPress (r6555)
     2059 *
     2060 * @param bool $override Whether to override the default handler
     2061 * @param WP_Query $wp_query The posts query being referenced
     2062 *
     2063 * @return bool False to leave alone, true to override
    20362064 */
    20372065function bbp_pre_handle_404( $override = false, $wp_query = false ) {
     2066
     2067    // Handle a bbPress 404 condition
     2068    if ( isset( $wp_query->bbp_is_404 ) ) {
     2069
     2070        // Either force a 404 when 200, or a 200 when 404
     2071        $override = ( true === $wp_query->bbp_is_404 )
     2072            ? bbp_set_404( $wp_query )
     2073            : bbp_set_200();
     2074    }
     2075
    20382076    return $override;
    20392077}
     2078
     2079/**
     2080 * Maybe pre-assign the posts that are returned from a WP_Query.
     2081 *
     2082 * This effectively short-circuits the default query for posts, which is
     2083 * currently only used to avoid calling the main query when it's not necessary.
     2084 *
     2085 * @since 2.6.0 bbPress (r6580)
     2086 *
     2087 * @param mixed $posts Default null. Array of posts (possibly empty)
     2088 * @param WP_Query $wp_query
     2089 *
     2090 * @return mixed Null if no override. Array if overridden.
     2091 */
     2092function bbp_posts_pre_query( $posts = null, $wp_query = false ) {
     2093
     2094    // Custom 404 handler is set, so set posts to empty array to avoid 2 queries
     2095    if ( isset( $wp_query->bbp_is_404 ) ) {
     2096        $posts = array();
     2097    }
     2098
     2099    // Return, maybe overridden
     2100    return $posts;
     2101}
Note: See TracChangeset for help on using the changeset viewer.