Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/28/2023 04:15:24 PM (3 years 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/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
Note: See TracChangeset for help on using the changeset viewer.