LLMS_Blocks_Visibility::maybe_filter_block( string $content, array $block )
Filter block output.
Parameters Parameters
- $content
-
(string) (Required) Block inner content.
- $block
-
(array) (Required) Block data array.
Return Return
(string)
Source Source
File: libraries/lifterlms-blocks/includes/class-llms-blocks-visibility.php
public function maybe_filter_block( $content, $block ) {
// Allow conditionally filtering the block based on external context.
if ( ! $this->should_filter_block( $block ) ) {
return $content;
}
// Set the `user_login` field block's visibility to its default 'logged_out' if not set.
// The WordPress serializer `getCommentAttributes()` function removes the attribute before being
// serialized into `post_content` if the attribute can have only one value and it's the default.
if ( 'llms/form-field-user-login' === $block['blockName'] && empty( $block['attrs']['llms_visibility'] ) ) {
$block['attrs']['llms_visibility'] = 'logged_out';
}
// No attributes or no llms visibility settings (visible to "all").
if ( empty( $block['attrs'] ) || empty( $block['attrs']['llms_visibility'] ) ) {
return $content;
}
$uid = get_current_user_id();
// Show only to logged in users.
if ( 'logged_in' === $block['attrs']['llms_visibility'] && ! $uid ) {
$content = '';
// Show only to logged out users.
} elseif ( 'logged_out' === $block['attrs']['llms_visibility'] && $uid ) {
$content = '';
// Enrolled checks.
} elseif ( 'enrolled' === $block['attrs']['llms_visibility'] && ! empty( $block['attrs']['llms_visibility_in'] ) ) {
// Don't have to run any further checks if we don't have a user.
if ( ! $uid ) {
$content = '';
// Checks for the "any" conditions.
} elseif ( in_array( $block['attrs']['llms_visibility_in'], array( 'any', 'any_course', 'any_membership' ), true ) ) {
$found = $this->get_enrollment_count_by_type( $uid, $block['attrs']['llms_visibility_in'] );
if ( ! $found ) {
$content = '';
}
// Checks for specifics.
} elseif ( in_array( $block['attrs']['llms_visibility_in'], array( 'this', 'list_all', 'list_any' ), true ) ) {
$relation = 'list_any' === $block['attrs']['llms_visibility_in'] ? 'any' : 'all'; // "this" becomes an "all" relationship
if ( ! llms_is_user_enrolled( $uid, $this->get_post_ids_from_block_attributes( $block['attrs'] ), $relation ) ) {
$content = '';
}
}
// Not-Enrolled checks.
} elseif ( 'not_enrolled' === $block['attrs']['llms_visibility'] && ! empty( $block['attrs']['llms_visibility_in'] ) ) {
// Only need to check logged in users.
if ( $uid ) {
// Checks for the "any" conditions.
if ( in_array( $block['attrs']['llms_visibility_in'], array( 'any', 'any_course', 'any_membership' ), true ) ) {
$found = $this->get_enrollment_count_by_type( $uid, $block['attrs']['llms_visibility_in'] );
if ( $found ) {
$content = '';
}
// Checks for specifics.
} elseif ( in_array( $block['attrs']['llms_visibility_in'], array( 'this', 'list_all', 'list_any' ), true ) ) {
$relation = 'list_any' === $block['attrs']['llms_visibility_in'] ? 'any' : 'all'; // "this" becomes an "all" relationship
if ( llms_is_user_enrolled( $uid, $this->get_post_ids_from_block_attributes( $block['attrs'] ), $relation ) ) {
$content = '';
}
}
}
}
/**
* Filters a blocks content after it has been run through visibility attribute filters
*
* @since 1.0.0
*
* @param string $content The HTML content for a block. May be an empty string if the block should be invisible to the current user.
* @param array $block Block data array.
*/
return apply_filters( 'llms_blocks_visibility_render_block', $content, $block );
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 2.4.2 | Set the user_login field block's visibility to its default 'logged_out' if not set. |
| 2.0.0 | Added a conditional prior to checking the block's visibility attributes. |
| 1.6.0 | Add logic for logged_in and logged_out block visibility options. |
| 1.0.0 | Introduced. |