Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a bp_read cap to handle community visibility #25

Closed
wants to merge 3 commits into from

Conversation

imath
Copy link
Member

@imath imath commented Oct 9, 2022

The main idea is to unset the buddypress()->pages when the user hasn't the WP Cap mapped to a new bp_read one. As a result BP Globals like current_component, current_action and action_variables are not set and we end up into a ! is_buddypress() area for not logged in members when the community visibility option is set to members. Activity RSS feed are then not set. I believe if we use this scenario, we'll need to edit the BP Rest API endpoints to use the bp_current_user_can( 'bp_read' ) into the permissions_check methods.

A benefit of this is the site admin can easily customize the default content displayed to not logged in members using the Block Editor for the corresponding BP Pages.

Trac ticket: https://buddypress.trac.wordpress.org/ticket/8734


This Pull Request is for code review only. Please keep all other discussion in the BuddyPress Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the WordPress Core Handbook for more details.

imath and others added 2 commits October 15, 2022 11:19
1. Use a theme or template pack template to display the community visibility message.
2. In case the theme or the template pack does not include the new template, fallback to @dcavins way of redirecting to the login page.
3. Catch all BP URI globals and send it to the capability check so that plugins can eventually leave not logged in visitors to access their content according to the URL being requested.
4. Improve various `bp_core_no_access...` functions to allow login page error customization for the no community access context.
* Check the `$args` filter's parameter (4th one) to find the requested URI globals.
* eg: `$args['current_component']` for the component.
*/
if ( ! bp_current_user_can( 'bp_read', $bp_uri_globals ) && ! in_array( $bp_uri_globals['current_component'], array( 'register', 'activate' ), true ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an example to let any user access to the members directory:

function leave_members_component_visible( $caps, $cap, $user_id, $args ) {
	$r = reset( $args );

	if ( 'bp_read' === $cap && isset( $r['current_component'] ) && 'members' === $r['current_component'] ) {
		$caps = array( 'exist' );
	}

	return $caps;
}
add_filter( 'bp_map_meta_caps', 'leave_members_component_visible', 10, 4 );

Copy link
Member

@renatonascalves renatonascalves left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few suggestions!


// A template part was found load it.
} else {
add_filter( 'bp_replace_the_content', array( $this, 'gate_content' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add phpdoc?

* Filter the_content with the community gate template part.
*
* @since 11.0.0
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return

src/bp-core/classes/class-bp-core.php Show resolved Hide resolved
Comment on lines +3973 to +3977
array(
'a' => array(
'href' => true,
),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might prove problematic. Imagine if I hook here and pass something like this: <a><span></span></a>...

*
* @param string $value The current community area visibility.
*/
return apply_filters( 'bp_get_community_visibility', bp_get_option( '_bp_community_visibility', $default ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest passing the $default as a second parameter.

* @return string The community gate slug.
*/
function bp_core_get_community_gate_slug() {
$slug = apply_filters( 'bp_core_community_gate_slug', __( 'community-gate', 'buddypress' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing phpdoc?

@imath
Copy link
Member Author

imath commented Apr 23, 2023

A new & improved way of dealing with this visibility feature will be soon worked on.

@imath imath closed this Apr 23, 2023
@imath imath deleted the try/community-visibility branch January 27, 2024 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants