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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 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. |