Skip to:
Content

BuddyPress.org

Changeset 13533


Ignore:
Timestamp:
07/28/2023 04:15:24 PM (12 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.

Location:
trunk
Files:
5 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/admin/bp-core-admin-settings.php

    r13173 r13533  
    4646
    4747<?php
     48}
     49
     50/**
     51 * Choose whether the community is visible to anyone or only to members.
     52 *
     53 * @since 12.0.0
     54 */
     55function bp_admin_setting_callback_community_visibility() {
     56    $visibility = bp_get_community_visibility( 'all' );
     57?>
     58    <select name="_bp_community_visibility[global]" id="_bp_community_visibility-global" aria-describedby="_bp_community_visibility_description" autocomplete="off">
     59        <option value="anyone" <?php echo selected( $visibility['global'], 'anyone' ); ?>><?php esc_html_e( 'Anyone', 'buddypress' ); ?></option>
     60        <option value="members" <?php echo selected( $visibility['global'], 'members' ); ?>><?php esc_html_e( 'Members Only', 'buddypress' ); ?></option>
     61    </select>
     62
     63    <p id="_bp_community_visibility_description" class="description"><?php esc_html_e( 'Choose "Anyone" to allow any visitor access to your community area. Choose "Members Only" to restrict access to your community area to logged-in members only.', 'buddypress' ); ?></p>
     64<?php
     65}
     66
     67/**
     68 * Sanitize the visibility setting when it is saved.
     69 *
     70 * @since 12.0.0
     71 *
     72 * @param mixed $saved_value The value passed to the save function.
     73 */
     74function bp_admin_sanitize_callback_community_visibility( $saved_value ) {
     75    $retval = array();
     76
     77    // Use the global setting, if it has been passed.
     78    $retval['global'] = isset( $saved_value['global'] ) ? $saved_value['global'] : 'anyone';
     79    // Ensure the global value is a valid option. Else, assume that the site is open.
     80    if ( ! in_array( $retval['global'], array( 'anyone', 'members' ), true ) ) {
     81        $retval['global'] = 'anyone';
     82    }
     83
     84    // Keys must be either 'global' or a component ID, but not register or activate.
     85    $directory_pages = bp_core_get_directory_pages();
     86    foreach ( $directory_pages as $component_id => $component_page ) {
     87        if ( in_array( $component_id, array( 'register', 'activate' ), true ) ) {
     88            continue;
     89        }
     90
     91        // Use the global value if a specific value hasn't been set.
     92        $component_value = isset( $saved_value[ $component_id ] ) ? $saved_value[ $component_id ] : $retval['global'];
     93
     94        // Valid values are 'anyone' or 'memebers'.
     95        if ( ! in_array( $component_value, array( 'anyone', 'members' ), true ) ) {
     96            $component_value = $retval['global'];
     97        }
     98        $retval[ $component_id ] = $component_value;
     99    }
     100
     101    return $retval;
    48102}
    49103
  • trunk/src/bp-core/bp-core-functions.php

    r13532 r13533  
    49724972    return $navigations;
    49734973}
     4974
     4975/**
     4976 * Get the community visibility value calculated from the
     4977 * saved visibility setting.
     4978 *
     4979 * @since 12.0.0
     4980 *
     4981 * @param string $component Whether we want the visibility for a single component
     4982 *                          or for all components.
     4983 *
     4984 * @return arrary|string $retval The calculated visbility settings for the site.
     4985 */
     4986function bp_get_community_visibility( $component = 'global' ) {
     4987    $retval = ( 'all' === $component ) ? array( 'global' => 'anyone' ) : 'anyone';
     4988    if ( 'rewrites' !== bp_core_get_query_parser() ) {
     4989        return $retval;
     4990    }
     4991
     4992    $saved_value = (array) bp_get_option( '_bp_community_visibility', array() );
     4993
     4994    // If the global value has not been set, we assume that the site is open.
     4995    if ( ! isset( $saved_value['global'] ) ) {
     4996        $saved_value['global'] = 'anyone';
     4997    }
     4998
     4999    if ( 'all' === $component ) {
     5000        // Build the component list.
     5001        $retval = array(
     5002            'global' => $saved_value['global']
     5003        );
     5004        $directory_pages = bp_core_get_directory_pages();
     5005        foreach ( $directory_pages as $component_id => $component_page ) {
     5006            if ( in_array( $component_id, array( 'register', 'activate' ), true ) ) {
     5007                continue;
     5008            }
     5009            $retval[ $component_id ] = isset( $saved_value[ $component_id ] ) ? $saved_value[ $component_id ] : $saved_value['global'];
     5010        }
     5011    } else {
     5012        // We are checking a particular component.
     5013        // Fall back to the global value if not set.
     5014        $retval = isset( $saved_value[ $component ] ) ? $saved_value[ $component ] : $saved_value['global'];
     5015    }
     5016
     5017    /**
     5018     * Filter the community visibility value calculated from the
     5019     * saved visibility setting.
     5020     *
     5021     * @since 12.0.0
     5022     *
     5023     * @param arrary|string $retval    The calculated visbility settings for the site.
     5024     * @param string        $component The component value to get the visibility for.
     5025     */
     5026    return apply_filters( 'bp_get_community_visibility', $retval, $component );
     5027}
  • trunk/src/bp-core/bp-core-options.php

    r13490 r13533  
    7474        // Email unsubscribe salt.
    7575        'bp-emails-unsubscribe-salt'           => '',
     76
     77        // Community visibility.
     78        '_bp_community_visibility'             => array( 'global' => 'anyone' ),
    7679
    7780        /* Groups ************************************************************/
  • trunk/src/bp-core/bp-core-update.php

    r13490 r13533  
    861861        }
    862862
    863         /* Widgets **************************************************/
    864         $widget_options = array(
    865             'widget_bp_core_login_widget',
    866             'widget_bp_core_members_widget',
    867             'widget_bp_core_whos_online_widget',
    868             'widget_bp_core_recently_active_widget',
    869             'widget_bp_groups_widget',
    870             'widget_bp_messages_sitewide_notices_widget',
    871         );
    872 
    873         foreach ( $widget_options as $widget_option ) {
    874             bp_delete_option( $widget_option );
    875         }
    876 
    877863        // Finally make sure to rebuilt permalinks at next page load.
    878864        bp_delete_rewrite_rules();
    879865    }
     866
     867    // Widgets.
     868    $widget_options = array(
     869        'widget_bp_core_login_widget',
     870        'widget_bp_core_members_widget',
     871        'widget_bp_core_whos_online_widget',
     872        'widget_bp_core_recently_active_widget',
     873        'widget_bp_groups_widget',
     874        'widget_bp_messages_sitewide_notices_widget',
     875    );
     876
     877    foreach ( $widget_options as $widget_option ) {
     878        bp_delete_option( $widget_option );
     879    }
     880
     881    // Community visibility.
     882    bp_update_option( '_bp_community_visibility', array( 'global' => 'anyone' ) );
    880883}
    881884
  • trunk/src/bp-core/classes/class-bp-admin.php

    r13468 r13533  
    476476        add_settings_field( 'bp-disable-account-deletion', __( 'Account Deletion', 'buddypress' ), 'bp_admin_setting_callback_account_deletion', 'buddypress', 'bp_main' );
    477477        register_setting( 'buddypress', 'bp-disable-account-deletion', 'intval' );
     478
     479        // Community Visibility
     480        if ( 'rewrites' === bp_core_get_query_parser() ) {
     481            add_settings_field( '_bp_community_visibility', __( 'Community Visibility', 'buddypress' ), 'bp_admin_setting_callback_community_visibility', 'buddypress', 'bp_main' );
     482            register_setting( 'buddypress', '_bp_community_visibility', 'bp_admin_sanitize_callback_community_visibility' );
     483        }
    478484
    479485        // Template pack picker.
  • 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.