<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][13533] trunk: Introduce basic community visibility feature.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/13533">13533</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/13533","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dcavins</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-07-28 16:15:24 +0000 (Fri, 28 Jul 2023)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="http://buddypress.trac.wordpress.org/ticket/8734">#8734</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcoreadminbpcoreadminsettingsphp">trunk/src/bp-core/admin/bp-core-admin-settings.php</a></li>
<li><a href="#trunksrcbpcorebpcorefunctionsphp">trunk/src/bp-core/bp-core-functions.php</a></li>
<li><a href="#trunksrcbpcorebpcoreoptionsphp">trunk/src/bp-core/bp-core-options.php</a></li>
<li><a href="#trunksrcbpcorebpcoreupdatephp">trunk/src/bp-core/bp-core-update.php</a></li>
<li><a href="#trunksrcbpcoreclassesclassbpadminphp">trunk/src/bp-core/classes/class-bp-admin.php</a></li>
<li><a href="#trunksrcbpcoreclassesclassbpcomponentphp">trunk/src/bp-core/classes/class-bp-component.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li>trunk/src/bp-templates/bp-legacy/buddypress/assets/utils/</li>
<li><a href="#trunksrcbptemplatesbplegacybuddypressassetsutilsrestrictedaccessmessagephp">trunk/src/bp-templates/bp-legacy/buddypress/assets/utils/restricted-access-message.php</a></li>
<li>trunk/src/bp-templates/bp-nouveau/buddypress/assets/utils/</li>
<li><a href="#trunksrcbptemplatesbpnouveaubuddypressassetsutilsrestrictedaccessmessagephp">trunk/src/bp-templates/bp-nouveau/buddypress/assets/utils/restricted-access-message.php</a></li>
<li><a href="#trunktestsphpunittestcasescorecommunityvisibilityphp">trunk/tests/phpunit/testcases/core/community-visibility.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcoreadminbpcoreadminsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/admin/bp-core-admin-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/admin/bp-core-admin-settings.php        2023-07-28 16:15:13 UTC (rev 13532)
+++ trunk/src/bp-core/admin/bp-core-admin-settings.php  2023-07-28 16:15:24 UTC (rev 13533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -48,6 +48,60 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Choose whether the community is visible to anyone or only to members.
+ *
+ * @since 12.0.0
+ */
+function bp_admin_setting_callback_community_visibility() {
+       $visibility = bp_get_community_visibility( 'all' );
+?>
+       <select name="_bp_community_visibility[global]" id="_bp_community_visibility-global" aria-describedby="_bp_community_visibility_description" autocomplete="off">
+               <option value="anyone" <?php echo selected( $visibility['global'], 'anyone' ); ?>><?php esc_html_e( 'Anyone', 'buddypress' ); ?></option>
+               <option value="members" <?php echo selected( $visibility['global'], 'members' ); ?>><?php esc_html_e( 'Members Only', 'buddypress' ); ?></option>
+       </select>
+
+       <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>
+<?php
+}
+
+/**
+ * Sanitize the visibility setting when it is saved.
+ *
+ * @since 12.0.0
+ *
+ * @param mixed $saved_value The value passed to the save function.
+ */
+function bp_admin_sanitize_callback_community_visibility( $saved_value ) {
+       $retval = array();
+
+       // Use the global setting, if it has been passed.
+       $retval['global'] = isset( $saved_value['global'] ) ? $saved_value['global'] : 'anyone';
+       // Ensure the global value is a valid option. Else, assume that the site is open.
+       if ( ! in_array( $retval['global'], array( 'anyone', 'members' ), true ) ) {
+               $retval['global'] = 'anyone';
+       }
+
+       // Keys must be either 'global' or a component ID, but not register or activate.
+       $directory_pages = bp_core_get_directory_pages();
+       foreach ( $directory_pages as $component_id => $component_page ) {
+               if ( in_array( $component_id, array( 'register', 'activate' ), true ) ) {
+                       continue;
+               }
+
+               // Use the global value if a specific value hasn't been set.
+               $component_value = isset( $saved_value[ $component_id ] ) ? $saved_value[ $component_id ] : $retval['global'];
+
+               // Valid values are 'anyone' or 'memebers'.
+               if ( ! in_array( $component_value, array( 'anyone', 'members' ), true ) ) {
+                       $component_value = $retval['global'];
+               }
+               $retval[ $component_id ] = $component_value;
+       }
+
+       return $retval;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Form element to change the active template pack.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function bp_admin_setting_callback_theme_package_id() {
</span></span></pre></div>
<a id="trunksrcbpcorebpcorefunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/bp-core-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-functions.php   2023-07-28 16:15:13 UTC (rev 13532)
+++ trunk/src/bp-core/bp-core-functions.php     2023-07-28 16:15:24 UTC (rev 13533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4971,3 +4971,57 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        return $navigations;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Get the community visibility value calculated from the
+ * saved visibility setting.
+ *
+ * @since 12.0.0
+ *
+ * @param string $component Whether we want the visibility for a single component
+ *                          or for all components.
+ *
+ * @return arrary|string $retval The calculated visbility settings for the site.
+ */
+function bp_get_community_visibility( $component = 'global' ) {
+       $retval = ( 'all' === $component ) ? array( 'global' => 'anyone' ) : 'anyone';
+       if ( 'rewrites' !== bp_core_get_query_parser() ) {
+               return $retval;
+       }
+
+       $saved_value = (array) bp_get_option( '_bp_community_visibility', array() );
+
+       // If the global value has not been set, we assume that the site is open.
+       if ( ! isset( $saved_value['global'] ) ) {
+               $saved_value['global'] = 'anyone';
+       }
+
+       if ( 'all' === $component ) {
+               // Build the component list.
+               $retval = array(
+                       'global' => $saved_value['global']
+               );
+               $directory_pages = bp_core_get_directory_pages();
+               foreach ( $directory_pages as $component_id => $component_page ) {
+                       if ( in_array( $component_id, array( 'register', 'activate' ), true ) ) {
+                               continue;
+                       }
+                       $retval[ $component_id ] = isset( $saved_value[ $component_id ] ) ? $saved_value[ $component_id ] : $saved_value['global'];
+               }
+       } else {
+               // We are checking a particular component.
+               // Fall back to the global value if not set.
+               $retval = isset( $saved_value[ $component ] ) ? $saved_value[ $component ] : $saved_value['global'];
+       }
+
+       /**
+        * Filter the community visibility value calculated from the
+        * saved visibility setting.
+        *
+        * @since 12.0.0
+        *
+        * @param arrary|string $retval    The calculated visbility settings for the site.
+        * @param string        $component The component value to get the visibility for.
+        */
+       return apply_filters( 'bp_get_community_visibility', $retval, $component );
+}
</ins></span></pre></div>
<a id="trunksrcbpcorebpcoreoptionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/bp-core-options.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-options.php     2023-07-28 16:15:13 UTC (rev 13532)
+++ trunk/src/bp-core/bp-core-options.php       2023-07-28 16:15:24 UTC (rev 13533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -74,6 +74,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Email unsubscribe salt.
</span><span class="cx" style="display: block; padding: 0 10px">                'bp-emails-unsubscribe-salt'           => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Community visibility.
+               '_bp_community_visibility'             => array( 'global' => 'anyone' ),
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /* Groups ************************************************************/
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // @todo Move this into the groups component
</span></span></pre></div>
<a id="trunksrcbpcorebpcoreupdatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/bp-core-update.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-update.php      2023-07-28 16:15:13 UTC (rev 13532)
+++ trunk/src/bp-core/bp-core-update.php        2023-07-28 16:15:24 UTC (rev 13533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -860,23 +860,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                /* Widgets **************************************************/
-               $widget_options = array(
-                       'widget_bp_core_login_widget',
-                       'widget_bp_core_members_widget',
-                       'widget_bp_core_whos_online_widget',
-                       'widget_bp_core_recently_active_widget',
-                       'widget_bp_groups_widget',
-                       'widget_bp_messages_sitewide_notices_widget',
-               );
-
-               foreach ( $widget_options as $widget_option ) {
-                       bp_delete_option( $widget_option );
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Finally make sure to rebuilt permalinks at next page load.
</span><span class="cx" style="display: block; padding: 0 10px">                bp_delete_rewrite_rules();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       // Widgets.
+       $widget_options = array(
+               'widget_bp_core_login_widget',
+               'widget_bp_core_members_widget',
+               'widget_bp_core_whos_online_widget',
+               'widget_bp_core_recently_active_widget',
+               'widget_bp_groups_widget',
+               'widget_bp_messages_sitewide_notices_widget',
+       );
+
+       foreach ( $widget_options as $widget_option ) {
+               bp_delete_option( $widget_option );
+       }
+
+       // Community visibility.
+       bp_update_option( '_bp_community_visibility', array( 'global' => 'anyone' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcbpcoreclassesclassbpadminphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/classes/class-bp-admin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-admin.php      2023-07-28 16:15:13 UTC (rev 13532)
+++ trunk/src/bp-core/classes/class-bp-admin.php        2023-07-28 16:15:24 UTC (rev 13533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -476,6 +476,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                add_settings_field( 'bp-disable-account-deletion', __( 'Account Deletion', 'buddypress' ), 'bp_admin_setting_callback_account_deletion', 'buddypress', 'bp_main' );
</span><span class="cx" style="display: block; padding: 0 10px">                register_setting( 'buddypress', 'bp-disable-account-deletion', 'intval' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Community Visibility
+               if ( 'rewrites' === bp_core_get_query_parser() ) {
+                       add_settings_field( '_bp_community_visibility', __( 'Community Visibility', 'buddypress' ), 'bp_admin_setting_callback_community_visibility', 'buddypress', 'bp_main' );
+                       register_setting( 'buddypress', '_bp_community_visibility', 'bp_admin_sanitize_callback_community_visibility' );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Template pack picker.
</span><span class="cx" style="display: block; padding: 0 10px">                add_settings_field( '_bp_theme_package_id', __( 'Template Pack', 'buddypress' ), 'bp_admin_setting_callback_theme_package_id', 'buddypress', 'bp_main', array( 'label_for' => '_bp_theme_package_id' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                register_setting( 'buddypress', '_bp_theme_package_id', 'sanitize_text_field' );
</span></span></pre></div>
<a id="trunksrcbpcoreclassesclassbpcomponentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/classes/class-bp-component.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-component.php  2023-07-28 16:15:13 UTC (rev 13532)
+++ trunk/src/bp-core/classes/class-bp-component.php    2023-07-28 16:15:24 UTC (rev 13533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1257,33 +1257,84 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 12.0.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param  null     $retval A null value to use the regular WP Query.
-        * @param  WP_Query $query  The WP Query object.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param  null     $posts A null value to use the regular WP Query.
+        * @param  WP_Query $query The WP Query object.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return null|array Null if not displaying a BuddyPress page.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *                    An array containing the BuddyPress directory post otherwise.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  *                    An array containing the BuddyPress directory page otherwise.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function pre_query( $retval = null, $query = null ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function pre_query( $posts = null, $query = null ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 remove_filter( 'posts_pre_query', array( $this, 'pre_query' ), 10 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $queried_object = $query->get_queried_object();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $queried_object instanceof WP_Post && 'buddypress' === get_post_type( $queried_object ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Only include the queried directory post into returned posts.
-                       $retval = array( $queried_object );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $component = bp_core_get_component_from_directory_page_id( $queried_object->ID );
+                       if ( bp_current_user_can( 'bp_view', array( 'bp_component' => $component ) ) ) {
+                               // Only include the queried directory post into returned posts.
+                               $posts = array( $queried_object );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Reset some query flags.
-                       $query->is_home       = false;
-                       $query->is_front_page = false;
-                       $query->is_page       = false;
-                       $query->is_archive    = false;
-                       $query->is_tax        = false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         // Reset some query flags.
+                               $query->is_home       = false;
+                               $query->is_front_page = false;
+                               $query->is_page       = false;
+                               $query->is_archive    = false;
+                               $query->is_tax        = false;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( ! is_embed() ) {
-                               $query->is_single = true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( ! is_embed() ) {
+                                       $query->is_single = true;
+                               }
+                       } else {
+                               // The current user may not access the directory page.
+                               $bp                    = buddypress();
+                               $bp->current_component = 'core';
+
+                               // Unset other BuddyPress URI globals.
+                               foreach ( array( 'current_item', 'current_action', 'action_variables', 'displayed_user' ) as $global ) {
+                                       if ( 'action_variables' === $global ) {
+                                               $bp->{$global} = array();
+                                       } elseif ( 'displayed_user' === $global ) {
+                                               $bp->{$global} = new \stdClass();
+                                       } else {
+                                               $bp->{$global} = '';
+                                       }
+                               }
+
+                               // Reset the post.
+                               $post = (object) array(
+                                       'ID'             => 0,
+                                       'post_type'      => 'buddypress',
+                                       'post_name'      => 'restricted',
+                                       'post_title'     => __( 'Members-only area', 'buddypress' ),
+                                       'post_content'   => bp_buffer_template_part( 'assets/utils/restricted-access-message', null, false ),
+                                       'comment_status' => 'closed',
+                                       'comment_count'  => 0,
+                               );
+
+                               // Reset the queried object.
+                               $query->queried_object    = get_post( $post );
+                               $query->queried_object_id = $query->queried_object->ID;
+
+                               // Reset the posts.
+                               $posts = array( $query->queried_object );
+
+                               // Reset some WP Query properties.
+                               $query->found_posts   = 1;
+                               $query->max_num_pages = 1;
+                               $query->posts         = $posts;
+                               $query->post          = $post;
+                               $query->post_count    = 1;
+                               $query->is_home       = false;
+                               $query->is_front_page = false;
+                               $query->is_page       = true;
+                               $query->is_archive    = false;
+                               $query->is_tax        = false;
+
+                               // Make sure no comments are displayed for this page.
+                               add_filter( 'comments_pre_query', 'bp_comments_pre_query', 10, 2 );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       return $posts;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               return $retval;
</del><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunksrcbptemplatesbplegacybuddypressassetsutilsrestrictedaccessmessagephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/bp-templates/bp-legacy/buddypress/assets/utils/restricted-access-message.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-templates/bp-legacy/buddypress/assets/utils/restricted-access-message.php                            (rev 0)
+++ trunk/src/bp-templates/bp-legacy/buddypress/assets/utils/restricted-access-message.php      2023-07-28 16:15:24 UTC (rev 13533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,33 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Show message and log-in form when the currently requested
+ * screen is not accessible by the current user.
+ *
+ * @package BuddyPress
+ * @subpackage bp-legacy
+ *
+ * @since 12.0.0
+ */
+
+// Exit if accessed directly.
+if ( ! defined( 'ABSPATH' ) ) {
+       exit;
+}
+?>
+<!-- wp:paragraph {"align":"center"} -->
+<p class="has-text-align-center"><?php esc_html_e( 'This community area is accessible to logged-in members only.', 'buddypress' ); ?></p>
+<!-- /wp:paragraph -->
+
+<!-- wp:columns -->
+<div class="wp-block-columns"><!-- wp:column {"width":"25%"} -->
+<div class="wp-block-column" style="flex-basis:25%"></div>
+<!-- /wp:column -->
+
+<!-- wp:column {"width":"50%"} -->
+<div class="wp-block-column" style="flex-basis:50%"><!-- wp:bp/login-form {"forgotPwdLink":true} /--></div>
+<!-- /wp:column -->
+
+<!-- wp:column {"width":"25%"} -->
+<div class="wp-block-column" style="flex-basis:25%"></div>
+<!-- /wp:column --></div>
+<!-- /wp:columns -->
</ins></span></pre></div>
<a id="trunksrcbptemplatesbpnouveaubuddypressassetsutilsrestrictedaccessmessagephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/bp-templates/bp-nouveau/buddypress/assets/utils/restricted-access-message.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-templates/bp-nouveau/buddypress/assets/utils/restricted-access-message.php                           (rev 0)
+++ trunk/src/bp-templates/bp-nouveau/buddypress/assets/utils/restricted-access-message.php     2023-07-28 16:15:24 UTC (rev 13533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,33 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Show message and log-in form when the currently requested
+ * screen is not accessible by the current user.
+ *
+ * @package BuddyPress
+ * @subpackage bp-nouveau
+ *
+ * @since 12.0.0
+ */
+
+// Exit if accessed directly.
+if ( ! defined( 'ABSPATH' ) ) {
+       exit;
+}
+?>
+<!-- wp:paragraph {"align":"center"} -->
+<p class="has-text-align-center"><?php esc_html_e( 'This community area is accessible to logged-in members only.', 'buddypress' ); ?></p>
+<!-- /wp:paragraph -->
+
+<!-- wp:columns -->
+<div class="wp-block-columns"><!-- wp:column {"width":"25%"} -->
+<div class="wp-block-column" style="flex-basis:25%"></div>
+<!-- /wp:column -->
+
+<!-- wp:column {"width":"50%"} -->
+<div class="wp-block-column" style="flex-basis:50%"><!-- wp:bp/login-form {"forgotPwdLink":true} /--></div>
+<!-- /wp:column -->
+
+<!-- wp:column {"width":"25%"} -->
+<div class="wp-block-column" style="flex-basis:25%"></div>
+<!-- /wp:column --></div>
+<!-- /wp:columns -->
</ins></span></pre></div>
<a id="trunktestsphpunittestcasescorecommunityvisibilityphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/testcases/core/community-visibility.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/core/community-visibility.php                               (rev 0)
+++ trunk/tests/phpunit/testcases/core/community-visibility.php 2023-07-28 16:15:24 UTC (rev 13533)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,73 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * @group bp_community_visibility
+ */
+class BP_Tests_BP_Community_Visibility_TestCases extends BP_UnitTestCase {
+       protected $old_user;
+       protected $logged_in_user;
+
+       public function set_up() {
+               parent::set_up();
+               $this->old_user = get_current_user_id();
+               $this->logged_in_user = self::factory()->user->create();
+               $this->set_current_user( $this->logged_in_user );
+
+               // Save a typical setting.
+               $setting = array(
+                       'global' => 'members',
+               );
+               update_option( '_bp_community_visibility', $setting );
+       }
+
+       public function tear_down() {
+               parent::tear_down();
+               $this->set_current_user( $this->old_user );
+               // Reset site to totally open.
+               delete_option( '_bp_community_visibility' );
+       }
+
+       // Test that logged-in user has access to component marked anyone and component marked members
+       public function test_bp_community_visibility_allow_visibility_for_logged_in_user() {
+               $this->assertTrue( bp_user_can( $this->logged_in_user, 'bp_view' ) );
+       }
+
+       // Test that anonymous user does not have access
+       public function test_bp_community_visibility_enforce_visibility_for_anon_user() {
+               $this->assertFalse( bp_user_can( 0, 'bp_view' ) );
+       }
+
+       // Bad component ID should use global setting.
+       public function test_bp_community_visibility_bad_component_id() {
+               $this->assertFalse( bp_user_can( 0, 'bp_view', array( 'bp_component' => 'blerg' ) ) );
+               $this->assertTrue( bp_user_can( $this->logged_in_user, 'bp_view', array( 'bp_component' => 'blerg' ) ) );
+       }
+
+       // No saved setting should be open access for anonymous users and logged in users.
+       public function test_bp_community_visibility_no_saved_setting() {
+               delete_option( '_bp_community_visibility' );
+               // No saved setting should result in the site being open to anyone.
+               $this->assertTrue( bp_user_can( 0, 'bp_view' ) );
+               $this->assertTrue( bp_user_can( $this->logged_in_user, 'bp_view' ) );
+       }
+
+       // Ensure that "anyone" setting allows access to everyone.
+       public function test_bp_community_visibility_access_allowed() {
+               $setting = array(
+                       'global'      => 'anyone',
+               );
+               update_option( '_bp_community_visibility', $setting );
+               $this->assertTrue( bp_user_can( 0, 'bp_view' ) );
+               $this->assertTrue( bp_user_can( $this->logged_in_user, 'bp_view' ) );
+       }
+
+       // Make sure fallback logic works for mixed-up setting values.
+       public function test_bp_community_visibility_fallback_setting() {
+               // Save a partial setting.
+               $setting = array(
+                       'global'      => 'members',
+                       'members'     => 'anyone',
+               );
+               update_option( '_bp_community_visibility', $setting );
+               $this->assertTrue( 'members' === bp_get_community_visibility( 'groups' ) );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>