Skip to:
Content

bbPress.org

Changeset 6414


Ignore:
Timestamp:
05/19/2017 02:44:11 AM (8 years ago)
Author:
johnjamesjacoby
Message:

Forums: Audit private/hidden forum ID exclusion:

  • improve performance of bbp_exclude_forum_ids()
  • Use bbp_exclude_forum_ids() where duplicate code existed
  • Make sure $forum_ids are passed into bbp_exclude_forum_ids filter
  • Prefer arrays over imploded strings (also with post statuses)
Location:
trunk/src/includes
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/core/cache.php

    r6058 r6414  
    181181    // Loop through query types and clean caches
    182182    foreach ( $post_types as $post_type ) {
    183         wp_cache_delete( 'bbp_parent_all_' . $post->ID . '_type_' . $post_type . '_child_ids', 'bbpress_posts' );
    184     }
    185 
    186     /**
    187      * Fires immediately after the given post's cache is cleaned.
     183        $key = 'bbp_parent_all_' . $post->ID . '_type_' . $post_type . '_child_ids';
     184        wp_cache_delete( $key, 'bbpress_posts' );
     185    }
     186
     187    /**
     188     * Fires immediately after the given post cache is cleaned.
    188189     *
    189190     * @since 2.1.0
  • trunk/src/includes/forums/functions.php

    r6400 r6414  
    20902090
    20912091    // Setup arrays
    2092     $private = $hidden = $meta_query = $forum_ids = array();
     2092    $forum_ids = array();
    20932093
    20942094    // Capability performance optimization
    20952095    if ( ! empty( $types[ $type ] ) ) {
    2096         $retval = $types[ $type ];
    2097 
    2098     // Populate forum exclude type
     2096        $retval    = $types[ $type ];
     2097        $forum_ids = $types['array'];
     2098
     2099    // Populate forum types
    20992100    } else {
    21002101
    2101         // Default return value
    2102         switch ( $type ) {
    2103             case 'string' :
    2104                 $retval = '';
    2105                 break;
    2106 
    2107             case 'array'  :
    2108                 $retval = array();
    2109                 break;
    2110 
    2111             case 'meta_query' :
    2112                 $retval = array( array() ) ;
    2113                 break;
    2114         }
     2102        // Types
     2103        $types = array(
     2104            'array'      => array(),
     2105            'string'     => '',
     2106            'meta_query' => array()
     2107        );
    21152108
    21162109        // Exclude for everyone but keymasters
     
    21182111
    21192112            // Private forums
    2120             if ( ! current_user_can( 'read_private_forums' ) ) {
    2121                 $private = bbp_get_private_forum_ids();
     2113            $private = ! current_user_can( 'read_private_forums' )
     2114                ? bbp_get_private_forum_ids()
     2115                : array();
     2116
     2117            // Hidden forums
     2118            $hidden = ! current_user_can( 'read_hidden_forums' )
     2119                ? bbp_get_hidden_forum_ids()
     2120                : array();
     2121
     2122            // Merge private and hidden forums together and remove any empties
     2123            $forum_ids = ( ! empty( $private ) || ! empty( $hidden ) )
     2124                ? array_filter( wp_parse_id_list( array_merge( $private, $hidden ) ) )
     2125                : array();
     2126
     2127            // Comparison
     2128            $compare = ( 1 < count( $forum_ids ) )
     2129                ? 'NOT IN'
     2130                : '!=';
     2131
     2132            // Store return values in static types array
     2133            if ( ! empty( $forum_ids ) ) {
     2134                $types['array']      = $forum_ids;
     2135                $types['string']     = implode( ',', $forum_ids );
     2136                $types['meta_query'] = array(
     2137                    'key'     => '_bbp_forum_id',
     2138                    'value'   => $forum_ids,
     2139                    'type'    => 'NUMERIC',
     2140                    'compare' => $compare
     2141                );
    21222142            }
    2123 
    2124             // Hidden forums
    2125             if ( ! current_user_can( 'read_hidden_forums' ) ) {
    2126                 $hidden  = bbp_get_hidden_forum_ids();
    2127             }
    2128 
    2129             // Merge private and hidden forums together
    2130             $forum_ids = (array) array_filter( wp_parse_id_list( array_merge( $private, $hidden ) ) );
    2131 
    2132             // There are forums that need to be excluded
    2133             if ( ! empty( $forum_ids ) ) {
    2134 
    2135                 switch ( $type ) {
    2136 
    2137                     // Separate forum ID's into a comma separated string
    2138                     case 'string' :
    2139                         $retval = implode( ',', $forum_ids );
    2140                         break;
    2141 
    2142                     // Use forum_ids array
    2143                     case 'array'  :
    2144                         $retval = $forum_ids;
    2145                         break;
    2146 
    2147                     // Build a meta_query
    2148                     case 'meta_query' :
    2149                         $retval = array(
    2150                             'key'     => '_bbp_forum_id',
    2151                             'value'   => implode( ',', $forum_ids ),
    2152                             'type'    => 'NUMERIC',
    2153                             'compare' => ( 1 < count( $forum_ids ) ) ? 'NOT IN' : '!='
    2154                         );
    2155                         break;
    2156                 }
    2157             }
    2158         }
    2159 
    2160         // Store return value in static types array
    2161         $types[ $type ] = $retval;
     2143        }
     2144
     2145        // There are forums that need to be excluded
     2146        $retval = $types[ $type ];
    21622147    }
    21632148
     
    22602245
    22612246        // Bail if no forums to exclude
    2262         if ( ! array_filter( $forum_ids ) ) {
     2247        if ( empty( $forum_ids ) ) {
    22632248            return;
    22642249        }
     
    25702555
    25712556    // Allowed post statuses to pre-trash
    2572     $post_stati = implode( ',', array(
     2557    $post_stati = array(
    25732558        bbp_get_public_status_id(),
    25742559        bbp_get_closed_status_id(),
    25752560        bbp_get_pending_status_id()
    2576     ) );
     2561    );
    25772562
    25782563    // Forum is being trashed, so its topics and replies are trashed too
  • trunk/src/includes/forums/template.php

    r6384 r6414  
    722722        'post_parent'         => 0,
    723723        'post_type'           => bbp_get_forum_post_type(),
    724         'post_status'         => implode( ',', $post_stati ),
     724        'post_status'         => $post_stati,
    725725        'posts_per_page'      => get_option( '_bbp_forums_per_page', 50 ),
    726726        'orderby'             => 'menu_order title',
  • trunk/src/includes/replies/template.php

    r6396 r6414  
    168168
    169169        // Join post statuses together
    170         $default['post_status'] = implode( ',', $post_statuses );
     170        $default['post_status'] = $post_statuses;
    171171
    172172    // Lean on the 'perm' query var value of 'readable' to provide statuses
  • trunk/src/includes/search/template.php

    r6300 r6414  
    8080
    8181        // Join post statuses together
    82         $default['post_status'] = implode( ',', $post_statuses );
     82        $default['post_status'] = $post_statuses;
    8383
    8484    // Lean on the 'perm' query var value of 'readable' to provide statuses
  • trunk/src/includes/topics/template.php

    r6402 r6414  
    184184
    185185        // Join post statuses together
    186         $default['post_status'] = implode( ',', $post_statuses );
     186        $default['post_status'] = $post_statuses;
    187187
    188188    // Lean on the 'perm' query var value of 'readable' to provide statuses
  • trunk/src/includes/users/template.php

    r6384 r6414  
    22872287function bbp_get_forums_for_current_user( $args = array() ) {
    22882288
    2289     // Setup arrays
    2290     $private = $hidden = $exclude = array();
    2291 
    2292     // Private forums
    2293     if ( ! current_user_can( 'read_private_forums' ) ) {
    2294         $private = bbp_get_private_forum_ids();
    2295     }
    2296 
    2297     // Hidden forums
    2298     if ( ! current_user_can( 'read_hidden_forums' ) ) {
    2299         $hidden  = bbp_get_hidden_forum_ids();
    2300     }
    2301 
    2302     // Merge private and hidden forums together and remove any empties
    2303     $forum_ids = (array) array_filter( wp_parse_id_list( array_merge( $private, $hidden ) ) );
    2304 
    2305     // There are forums that need to be excluded
    2306     if ( ! empty( $forum_ids ) ) {
    2307         $exclude = implode( ',', $forum_ids );
    2308     }
    2309 
    23102289    // Parse arguments against default values
    23112290    $r = bbp_parse_args( $args, array(
    2312         'post_type'   => bbp_get_forum_post_type(),
    2313         'post_status' => bbp_get_public_status_id(),
    2314         'numberposts' => -1,
    2315         'exclude'     => $exclude
     2291        'post_type'    => bbp_get_forum_post_type(),
     2292        'post_status'  => bbp_get_public_status_id(),
     2293        'post__not_in' => bbp_exclude_forum_ids( 'array' ),
     2294        'numberposts'  => -1
    23162295    ), 'get_forums_for_current_user' );
    23172296
     
    23242303    }
    23252304
    2326     return apply_filters( 'bbp_get_forums_for_current_user', $forums );
     2305    return apply_filters( 'bbp_get_forums_for_current_user', $forums, $r, $args );
    23272306}
    23282307
Note: See TracChangeset for help on using the changeset viewer.