Skip to:
Content

bbPress.org

Changeset 3085


Ignore:
Timestamp:
05/03/2011 08:01:48 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Revert most of r3054. $wp_query needs to be explicitly checked to guarantee we are not checking the $post global from a different internal loop.
Change from post_meta based _bbp_visibility to custom forum post status for private and hidden forums. This allows for easier exclusion without the need for slower meta queries. Add wp_reset_postdata() through-out loop and template files where the post global might have been altered by an external plugin. Add bbp_exclude_forum_ids() function, to explicitly exclude forums based on user capabilities.

Location:
branches/plugin
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-admin/bbp-admin.php

    r3078 r3085  
    446446        }
    447447
    448         // Private?
    449         if ( !empty( $_POST['bbp_forum_visibility'] ) && in_array( $_POST['bbp_forum_visibility'], array( 'public', 'private', 'hidden' ) ) ) {
     448        // Visibility
     449        if ( !empty( $_POST['bbp_forum_visibility'] ) && in_array( $_POST['bbp_forum_visibility'], array( 'publish', 'private', 'hidden' ) ) ) {
    450450
    451451            // Get forums current visibility
     
    468468                        break;
    469469
    470                     // Public (default)
    471                     case 'public'  :
     470                    // Publish (default)
     471                    case 'publish'  :
    472472                    default        :
    473473                        bbp_publicize_forum( $forum_id, $visibility );
     
    21432143
    21442144    $forum['visibility']  = array(
    2145         'public' => __( 'Public',  'bbpress' ),
     2145        'publish' => __( 'Public',  'bbpress' ),
    21462146        'private' => __( 'Private', 'bbpress' ),
    21472147        'hidden'  => __( 'Hidden',  'bbpress' )
  • branches/plugin/bbp-includes/bbp-forum-functions.php

    r3055 r3085  
    174174
    175175    // Only run queries if visibility is changing
    176     if ( 'public' != $current_visibility ) {
     176    if ( 'publish' != $current_visibility ) {
    177177
    178178        // Remove from _bbp_private_forums site option
     
    214214        }
    215215
    216         // Update forums visibility setting
    217         update_post_meta( $forum_id, '_bbp_visibility', 'public' );
     216        // Update forum post_status
     217        global $wpdb;
     218        $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $forum_id ) );
     219        wp_transition_post_status( 'publish', $current_visibility, get_post( $forum_id ) );
    218220    }
    219221
     
    266268
    267269        // Update forums visibility setting
    268         update_post_meta( $forum_id, '_bbp_visibility', 'private' );
     270        global $wpdb;
     271        $wpdb->update( $wpdb->posts, array( 'post_status' => 'private' ), array( 'ID' => $forum_id ) );
     272        wp_transition_post_status( 'private', $current_visibility, get_post( $forum_id ) );
    269273    }
    270274
     
    317321
    318322        // Update forums visibility setting
    319         update_post_meta( $forum_id, '_bbp_visibility', 'hidden' );
     323        global $bbp, $wpdb;
     324        $wpdb->update( $wpdb->posts, array( 'post_status' => 'hidden' ), array( 'ID' => $forum_id ) );
     325        wp_transition_post_status( $bbp->hidden_status_id, $current_visibility, get_post( $forum_id ) );
    320326    }
    321327
     
    762768
    763769/**
     770 * Returns a meta_query that either includes or excludes hidden forum IDs
     771 * from a query.
     772 *
     773 * @uses is_super_admin()
     774 * @uses bbp_is_user_home()
     775 * @uses bbp_get_hidden_forum_ids()
     776 * @uses bbp_get_private_forum_ids()
     777 */
     778function bbp_exclude_forum_ids( $query = array() ) {
     779
     780    // Show hidden topics for super admins
     781    if ( is_super_admin() || bbp_is_user_home() )
     782        return $query;
     783
     784    // Setup arrays
     785    $private = $hidden = array();
     786
     787    // Private forums
     788    if ( !current_user_can( 'read_private_forums' ) )
     789        $private = bbp_get_private_forum_ids();
     790
     791    // Hidden forums
     792    if ( !current_user_can( 'read_hidden_forums' ) )
     793        $hidden  = bbp_get_hidden_forum_ids();
     794
     795    // Merge private and hidden forums together
     796    $forum_ids = array_merge( $private, $hidden );
     797
     798    // Setup a meta_query to remove hidden forums
     799    $value   = implode( ',', $forum_ids );
     800    $compare = ( 1 < count( $forum_ids ) ) ? 'NOT IN' : '!=';
     801
     802    // Add meta_query to $replies_query
     803    $meta_query['meta_query'] = array( array(
     804        'key'     => '_bbp_forum_id',
     805        'value'   => $value,
     806        'compare' => $compare
     807    ) );
     808
     809    // Merge the queries together
     810    if ( !empty( $meta_query ) )
     811        $query = array_merge( $query, $meta_query );
     812
     813    return apply_filters( 'bbp_exclude_forum_ids', $query, $meta_query );
     814}
     815
     816/**
    764817 * Returns the forum's topic ids
    765818 *
     
    857910 */
    858911function bbp_forum_visibility_check() {
     912    global $wp_query;
    859913
    860914    // Bail if not viewing a single item or if user has caps
     
    863917
    864918    // Check post type
    865     switch ( get_post_type() ) {
     919    switch ( $wp_query->get( 'post_type' ) ) {
    866920
    867921        // Forum
    868922        case bbp_get_forum_post_type() :
    869             $forum_id = bbp_get_forum_id( get_the_ID() );
     923            $forum_id = bbp_get_forum_id( $wp_query->post->ID );
    870924            break;
    871925
    872926        // Topic
    873927        case bbp_get_topic_post_type() :
    874             $forum_id = bbp_get_topic_forum_id( get_the_ID() );
     928            $forum_id = bbp_get_topic_forum_id( $wp_query->post->ID );
    875929            break;
    876930
    877931        // Reply
    878932        case bbp_get_reply_post_type() :
    879             $forum_id = bbp_get_reply_forum_id( get_the_ID() );
     933            $forum_id = bbp_get_reply_forum_id( $wp_query->post->ID );
    880934            break;
    881935
  • branches/plugin/bbp-includes/bbp-forum-template.php

    r3066 r3085  
    5858    global $bbp;
    5959
     60    // Make sure we're back where we started
     61    wp_reset_postdata();
     62
     63    // Setup possible post__not_in array
     64    $post_stati[] = 'publish';
     65
     66    // Super admin get whitelisted post statuses
     67    if ( is_super_admin() ) {
     68        $post_stati = array( 'publish', 'private', 'hidden' );
     69
     70    // Not a super admin, so check caps
     71    } else {
     72
     73        // Check if user can read private forums
     74        if ( current_user_can( 'read_private_forums' ) )
     75            $post_stati[] = 'private';
     76
     77        // Check if user can read hidden forums
     78        if ( current_user_can( 'read_hidden_forums' ) )
     79            $post_stati[] = 'hidden';
     80    }
     81
     82    // The default forum query for most circumstances
    6083    $default = array (
    6184        'post_type'      => bbp_get_forum_post_type(),
    6285        'post_parent'    => bbp_get_forum_id(),
     86        'post_status'    => implode( ',', $post_stati ),
    6387        'posts_per_page' => get_option( '_bbp_forums_per_page', 15 ),
    6488        'orderby'        => 'menu_order',
     
    6690    );
    6791
     92    // Parse the default against what is requested
    6893    $bbp_f = wp_parse_args( $args, $default );
    69 
    70     // Allow all forums to be queried if post_parent is set to -1
    71     if ( -1 == $bbp_f['post_parent'] )
    72         unset( $bbp_f['post_parent'] );
    73 
    74     // Don't show private forums to normal users
    75     if ( empty( $bbp_f['meta_key'] ) && empty( $bbp_f['meta_value'] ) ) {
    76 
    77         // Include public and private forums only
    78         if ( current_user_can( 'read_private_forums' ) ) {
    79             $value   = 'public, private';
    80             $compare = 'IN';
    81 
    82         // Include public forums only
    83         } else {
    84             $value   = 'hidden';
    85             $compare = '!=';
    86         }
    87 
    88         // Meta query to determine visibility scope
    89         $bbp_f['meta_query'] = array( array(
    90             'key'     => '_bbp_visibility',
    91             'value'   => $value,
    92             'compare' => $compare
    93         ) );
    94     }
    9594
    9695    // Filter the forums query to allow just-in-time modifications
     
    161160     */
    162161    function bbp_get_forum_id( $forum_id = 0 ) {
    163         global $bbp;
     162        global $bbp, $wp_query;
    164163
    165164        // Easy empty checking
     
    172171
    173172        // Currently viewing a forum
    174         elseif ( bbp_is_forum() && get_the_ID() )
    175             $bbp_forum_id = $bbp->current_forum_id = get_the_ID();
     173        elseif ( bbp_is_forum() && isset( $wp_query->post->ID ) )
     174            $bbp_forum_id = $bbp->current_forum_id = $wp_query->post->ID;
    176175
    177176        // Currently viewing a topic
     
    520519 */
    521520function bbp_forum_get_subforums( $args = '' ) {
     521
     522    // Use passed integer as post_parent
    522523    if ( is_numeric( $args ) )
    523524        $args = array( 'post_parent' => $args );
     525
     526    // Setup possible post__not_in array
     527    $post_stati[] = 'publish';
     528
     529    // Super admin get whitelisted post statuses
     530    if ( is_super_admin() ) {
     531        $post_stati = array( 'publish', 'private', 'hidden' );
     532
     533    // Not a super admin, so check caps
     534    } else {
     535
     536        // Check if user can read private forums
     537        if ( current_user_can( 'read_private_forums' ) )
     538            $post_stati[] = 'private';
     539
     540        // Check if user can read hidden forums
     541        if ( current_user_can( 'read_hidden_forums' ) )
     542            $post_stati[] = 'hidden';
     543    }
    524544
    525545    $default = array(
    526546        'post_parent'    => 0,
    527547        'post_type'      => bbp_get_forum_post_type(),
     548        'post_status'    => implode( ',', $post_stati ),
    528549        'posts_per_page' => get_option( '_bbp_forums_per_page', 15 ),
    529550        'sort_column'    => 'menu_order, post_title'
     
    531552
    532553    $r = wp_parse_args( $args, $default );
    533 
    534554    $r['post_parent'] = bbp_get_forum_id( $r['post_parent'] );
    535 
    536     // Don't show hidden forums to normal users
    537     if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_query'] ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) ) {
    538         $r['meta_query'] = array( array(
    539             'key'     => '_bbp_visibility',
    540             'value'   => 'public, private',
    541             'compare' => 'IN'
    542         ) );
    543     }
    544555
    545556    // No forum passed
     
    12381249        $forum_id = bbp_get_forum_id( $forum_id );
    12391250
    1240         return apply_filters( 'bbp_get_forum_visibility', get_post_meta( $forum_id, '_bbp_visibility', true ), $forum_id );
     1251        return apply_filters( 'bbp_get_forum_visibility', get_post_status( $forum_id ), $forum_id );
    12411252    }
    12421253
     
    13261337
    13271338    // If post status is public, return true
    1328     $retval = ( 'public' == $visibility );
     1339    $retval = ( 'publish' == $visibility );
    13291340
    13301341    // Check ancestors and inherit their privacy setting for display
  • branches/plugin/bbp-includes/bbp-general-template.php

    r3068 r3085  
    4646 */
    4747function bbp_is_forum( $post_id = 0 ) {
    48     global $bbp;
     48    global $bbp, $wp_query;
    4949
    5050    if ( empty( $post_id ) ) {
     
    5353            return true;
    5454
    55         if ( ( $post_type = get_query_var( 'post_type' ) ) && ( bbp_get_forum_post_type() === $post_type ) )
     55        if ( isset( $wp_query->query_vars['post_type'] ) && ( bbp_get_forum_post_type() === $wp_query->query_vars['post_type'] ) )
    5656            return true;
    5757
     
    8181 */
    8282function bbp_is_topic( $post_id = 0 ) {
    83     global $bbp;
     83    global $bbp, $wp_query;
    8484
    8585    // Return false if it's a edit topic page
     
    9292            return true;
    9393
    94         if ( ( $post_type = get_query_var( 'post_type' ) ) && ( bbp_get_topic_post_type() === $post_type ) )
     94        if ( isset( $wp_query->query_vars['post_type'] ) && ( bbp_get_topic_post_type() === $wp_query->query_vars['post_type'] ) )
    9595            return true;
    9696
     
    166166 */
    167167function bbp_is_reply( $post_id = 0 ) {
    168     global $bbp;
     168    global $bbp, $wp_query;
    169169
    170170    // Return false if it's a edit reply page
     
    177177            return true;
    178178
    179         if ( ( $post_type = get_query_var( 'post_type' ) ) && ( bbp_get_reply_post_type() === $post_type ) )
     179        if ( isset( $wp_query->query_vars['post_type'] ) && ( bbp_get_reply_post_type() === $wp_query->query_vars['post_type'] ) )
    180180            return true;
    181181
     
    679679            $r['selected'] = 0;
    680680
    681         // Don't show private forums to normal users
    682         if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_query'] ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) && empty( $r['meta_compare'] ) ) {
    683             $r['meta_query'] = array(
    684                 array(
    685                     'key'     => '_bbp_visibility',
    686                     'value'   => 'public',
    687                     'compare' => '='
    688                 )
    689             );
    690         }
     681        $r = bbp_exclude_forum_ids( $r );
    691682
    692683        extract( $r );
  • branches/plugin/bbp-includes/bbp-reply-template.php

    r3066 r3085  
    6767    global $wp_rewrite, $bbp;
    6868
     69    // Make sure we're back where we started
     70    wp_reset_postdata();
     71
    6972    // Default status
    7073    $default_status = join( ',', array( 'publish', $bbp->closed_status_id ) );
     
    220223     */
    221224    function bbp_get_reply_id( $reply_id = 0 ) {
    222         global $bbp;
     225        global $bbp, $wp_query;
    223226
    224227        // Easy empty checking
     
    227230
    228231        // Currently viewing a reply
    229         elseif ( ( bbp_is_reply() || bbp_is_reply_edit() ) && get_the_ID() )
    230             $bbp_reply_id = $bbp->current_reply_id = get_the_ID();
     232        elseif ( ( bbp_is_reply() || bbp_is_reply_edit() ) && isset( $wp_query->post->ID ) )
     233            $bbp_reply_id = $bbp->current_reply_id = $wp_query->post->ID;
    231234
    232235        // Currently inside a replies loop
  • branches/plugin/bbp-includes/bbp-shortcodes.php

    r3080 r3085  
    274274
    275275        // Remove any topics from hidden forums
    276         $topics_query = bbp_exclude_hidden_forums( $topics_query );
     276        $topics_query = bbp_exclude_forum_ids( $topics_query );
    277277
    278278        // Unset globals
  • branches/plugin/bbp-includes/bbp-topic-template.php

    r3066 r3085  
    6666    global $wp_rewrite, $wp_query, $bbp, $wpdb;
    6767
     68    // Make sure we're back where we started
     69    wp_reset_postdata();
     70
    6871    // Are we in a forum and looking to do a forum only query?
    6972    $in_forum = (bool) ( bbp_is_forum() && !bbp_is_query_name( 'bbp_widget' ) );
    7073
    7174    // What are the default allowed statuses (based on user caps)
    72     if ( !empty( $_GET['view'] ) && ( true == $in_forum ) && ( 'all' == $_GET['view'] && current_user_can( 'edit_others_topics' ) ) )
     75    if ( current_user_can( 'moderate' ) && !bbp_is_query_name( 'bbp_widget' ) && ( !empty( $_GET['view'] ) && ( 'all' == $_GET['view'] ) ) )
    7376        $default_status = join( ',', array( 'publish', $bbp->closed_status_id, $bbp->spam_status_id, 'trash' ) );
    7477    else
     
    321324     */
    322325    function bbp_get_topic_id( $topic_id = 0 ) {
    323         global $bbp;
     326        global $bbp, $wp_query;
    324327
    325328        // Easy empty checking
     
    332335
    333336        // Currently viewing a topic
    334         elseif ( ( bbp_is_topic() || bbp_is_topic_edit() ) && get_the_ID() )
    335             $bbp_topic_id = $bbp->current_topic_id = get_the_ID();
     337        elseif ( ( bbp_is_topic() || bbp_is_topic_edit() ) && isset( $wp_query->post->ID ) )
     338            $bbp_topic_id = $bbp->current_topic_id = $wp_query->post->ID;
    336339
    337340        // Currently viewing a topic
  • branches/plugin/bbp-includes/bbp-user-functions.php

    r3015 r3085  
    173173    // If user has favorites, load them
    174174    if ( $favorites = bbp_get_user_favorites_topic_ids( $user_id ) ) {
    175         $query  = bbp_has_topics( array( 'post__in' => $favorites ) );
    176         return apply_filters( 'bbp_get_user_favorites', $query, $user_id );
     175
     176        // Possibly remove topics from hidden forums
     177        $hidden_query = bbp_exclude_forum_ids();
     178        $favs_query   = array( 'post__in' => $favorites );
     179        $topics_query = array_merge( $hidden_query, $favs_query );
     180        $topics_query = bbp_has_topics( $topics_query );
     181
     182        return apply_filters( 'bbp_get_user_favorites', $topics_query, $user_id );
    177183    }
    178184
     
    783789
    784790    // Query defaults
    785     $topics_query = array(
     791    $default_query = array(
    786792        'post_author'    => $user_id,
    787793        'show_stickies'  => false,
     
    789795    );
    790796
    791     // Setup a meta_query to remove hidden forums
    792     if ( $hidden = bbp_get_hidden_forum_ids() ) {
    793 
    794         // Value and compare for meta_query
    795         $value   = implode( ',', bbp_get_hidden_forum_ids() );
    796         $compare = ( 1 < count( $hidden ) ) ? 'NOT IN' : '!=';
    797 
    798         // Add meta_query to $replies_query
    799         $topics_query['meta_query'] = array( array(
    800             'key'     => '_bbp_forum_id',
    801             'value'   => $value,
    802             'compare' => $compare
    803         ) );
    804         $topics_query['post_parent'] = 'any';
    805     }
    806 
     797    // Assume user cannot read hidden forums
     798    $topics_query = bbp_exclude_forum_ids( $default_query );
     799
     800    // Get the topics
    807801    if ( $query = bbp_has_topics( $topics_query ) )
    808802        return $query;
  • branches/plugin/bbp-includes/bbp-widgets.php

    r3044 r3085  
    303303
    304304        $title        = apply_filters( 'bbp_forum_widget_title', $instance['title'] );
    305         $parent_forum = !empty( $instance['parent_forum'] ) ? $instance['parent_forum'] : 0;
    306 
    307         $default = array(
     305        $parent_forum = !empty( $instance['parent_forum'] ) ? $instance['parent_forum'] : '0';
     306
     307        $forums_query = array(
    308308            'post_parent'    => $parent_forum,
    309309            'posts_per_page' => get_option( '_bbp_forums_per_page', 15 ),
     
    312312        );
    313313
    314         // Don't show private forums to normal users
    315         if ( !current_user_can( 'read_private_forums' ) && empty( $default['meta_key'] ) && empty( $default['meta_value'] ) ) {
    316             $default['meta_key']   = '_bbp_visibility';
    317             $default['meta_value'] = 'public';
    318         }
    319 
    320314        bbp_set_query_name( 'bbp_widget' );
    321315
    322         if ( bbp_has_forums( $default ) ) :
     316        if ( bbp_has_forums( $forums_query ) ) :
    323317
    324318            echo $before_widget;
     
    455449        // Query defaults
    456450        $topics_query = array(
     451            'post_parent'    => $parent_forum,
    457452            'post_author'    => 0,
    458453            'posts_per_page' => $max_shown > $pop_check ? $max_shown : $pop_check,
     454            'posts_per_page' => $max_shown,
    459455            'show_stickies'  => false,
    460             'posts_per_page' => $max_shown,
    461456            'order'          => 'DESC',
    462457        );
    463458
    464         // Setup a meta_query to remove hidden forums
    465         if ( ( empty( $parent_forum ) || ( 'any' == $parent_forum ) ) && ( $hidden = bbp_get_hidden_forum_ids() ) ) {
    466 
    467             // Value and compare for meta_query
    468             $value   = implode( ',', $hidden );
    469             $compare = ( 1 < count( $hidden ) ) ? 'NOT IN' : '!=';
    470 
    471             // Add meta_query to $replies_query
    472             $topics_query['meta_query'] = array( array(
    473                 'key'     => '_bbp_forum_id',
    474                 'value'   => $value,
    475                 'compare' => $compare
    476             ) );
    477             $topics_query['post_parent'] = 'any';
    478         }
    479 
    480459        bbp_set_query_name( 'bbp_widget' );
     460
     461        // Remove any topics from hidden forums
     462        $topics_query = bbp_exclude_forum_ids( $topics_query );
    481463
    482464        if ( $pop_check < $max_shown && bbp_has_topics( $topics_query ) ) :
     
    655637        );
    656638
    657         // Setup a meta_query to remove hidden forums
    658         if ( $hidden = bbp_get_hidden_forum_ids() ) {
    659 
    660             // Value and compare for meta_query
    661             $value   = implode( ',', $hidden );
    662             $compare = ( 1 < count( $hidden ) ) ? 'NOT IN' : '!=';
    663 
    664             // Add meta_query to $replies_query
    665             $replies_query['meta_query'] = array( array(
    666                 'key'     => '_bbp_forum_id',
    667                 'value'   => $value,
    668                 'compare' => $compare
    669             ) );
    670         }
    671 
    672639        // Set the query name
    673640        bbp_set_query_name( 'bbp_widget' );
     641
     642        // Exclude hidden forums
     643        $replies_query = bbp_exclude_forum_ids( $replies_query );
    674644
    675645        // Get replies and display them
  • branches/plugin/bbp-themes/bbp-twentyten/bbpress/form-reply.php

    r3061 r3085  
    77 * @subpackage Theme
    88 */
     9
     10// Make sure we're back where we started
     11wp_reset_postdata();
    912
    1013?>
  • branches/plugin/bbp-themes/bbp-twentyten/bbpress/form-topic.php

    r3061 r3085  
    77 * @subpackage Theme
    88 */
     9
     10// Make sure we're back where we started
     11wp_reset_postdata();
    912
    1013?>
Note: See TracChangeset for help on using the changeset viewer.