Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/28/2023 04:15:24 PM (18 months ago)
Author:
dcavins
Message:

Introduce basic community visibility feature.

Site admins now have the option to restrict access to the
community portion of a BuddyPress site. If the site admin
chooses "members only," then, when a non-logged-in
user attempts to visit a BuddyPress screen, she will
be shown an access error message and a log-in form.

Props imath, sbrajesh, jjj, shawfactor.

Fixes #8734.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-component.php

    r13518 r13533  
    12581258     * @since 12.0.0
    12591259     *
    1260      * @param  null     $retval A null value to use the regular WP Query.
    1261      * @param  WP_Query $query  The WP Query object.
     1260     * @param  null     $posts A null value to use the regular WP Query.
     1261     * @param  WP_Query $query The WP Query object.
    12621262     * @return null|array Null if not displaying a BuddyPress page.
    1263      *                    An array containing the BuddyPress directory post otherwise.
    1264      */
    1265     public function pre_query( $retval = null, $query = null ) {
     1263     *                    An array containing the BuddyPress directory page otherwise.
     1264     */
     1265    public function pre_query( $posts = null, $query = null ) {
    12661266        remove_filter( 'posts_pre_query', array( $this, 'pre_query' ), 10 );
    12671267
     
    12691269
    12701270        if ( $queried_object instanceof WP_Post && 'buddypress' === get_post_type( $queried_object ) ) {
    1271             // Only include the queried directory post into returned posts.
    1272             $retval = array( $queried_object );
    1273 
    1274             // Reset some query flags.
    1275             $query->is_home       = false;
    1276             $query->is_front_page = false;
    1277             $query->is_page       = false;
    1278             $query->is_archive    = false;
    1279             $query->is_tax        = false;
    1280 
    1281             if ( ! is_embed() ) {
    1282                 $query->is_single = true;
    1283             }
    1284         }
    1285 
    1286         return $retval;
     1271            $component = bp_core_get_component_from_directory_page_id( $queried_object->ID );
     1272            if ( bp_current_user_can( 'bp_view', array( 'bp_component' => $component ) ) ) {
     1273                // Only include the queried directory post into returned posts.
     1274                $posts = array( $queried_object );
     1275
     1276                // Reset some query flags.
     1277                $query->is_home       = false;
     1278                $query->is_front_page = false;
     1279                $query->is_page       = false;
     1280                $query->is_archive    = false;
     1281                $query->is_tax        = false;
     1282
     1283                if ( ! is_embed() ) {
     1284                    $query->is_single = true;
     1285                }
     1286            } else {
     1287                // The current user may not access the directory page.
     1288                $bp                    = buddypress();
     1289                $bp->current_component = 'core';
     1290
     1291                // Unset other BuddyPress URI globals.
     1292                foreach ( array( 'current_item', 'current_action', 'action_variables', 'displayed_user' ) as $global ) {
     1293                    if ( 'action_variables' === $global ) {
     1294                        $bp->{$global} = array();
     1295                    } elseif ( 'displayed_user' === $global ) {
     1296                        $bp->{$global} = new \stdClass();
     1297                    } else {
     1298                        $bp->{$global} = '';
     1299                    }
     1300                }
     1301
     1302                // Reset the post.
     1303                $post = (object) array(
     1304                    'ID'             => 0,
     1305                    'post_type'      => 'buddypress',
     1306                    'post_name'      => 'restricted',
     1307                    'post_title'     => __( 'Members-only area', 'buddypress' ),
     1308                    'post_content'   => bp_buffer_template_part( 'assets/utils/restricted-access-message', null, false ),
     1309                    'comment_status' => 'closed',
     1310                    'comment_count'  => 0,
     1311                );
     1312
     1313                // Reset the queried object.
     1314                $query->queried_object    = get_post( $post );
     1315                $query->queried_object_id = $query->queried_object->ID;
     1316
     1317                // Reset the posts.
     1318                $posts = array( $query->queried_object );
     1319
     1320                // Reset some WP Query properties.
     1321                $query->found_posts   = 1;
     1322                $query->max_num_pages = 1;
     1323                $query->posts         = $posts;
     1324                $query->post          = $post;
     1325                $query->post_count    = 1;
     1326                $query->is_home       = false;
     1327                $query->is_front_page = false;
     1328                $query->is_page       = true;
     1329                $query->is_archive    = false;
     1330                $query->is_tax        = false;
     1331
     1332                // Make sure no comments are displayed for this page.
     1333                add_filter( 'comments_pre_query', 'bp_comments_pre_query', 10, 2 );
     1334            }
     1335
     1336            return $posts;
     1337        }
    12871338    }
    12881339
Note: See TracChangeset for help on using the changeset viewer.