bp_core_check_for_blacklist( int $user_id, string $title = '', string $content = '', string $error_type = 'bool' )
Check for blocked keys.
Description
Parameters
- $user_id
-
(Required) User ID.
- $title
-
(Optional) The title of the content.
Default value: ''
- $content
-
(Optional) The content being posted.
Default value: ''
- $error_type
-
(Optional) The error type to return. Either 'bool' or 'wp_error'.
Default value: 'bool'
Return
(bool|WP_Error) True if test is passed, false if fail.
Source
File: bp-core/bp-core-moderation.php
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | function bp_core_check_for_blacklist( $user_id = 0, $title = '' , $content = '' , $error_type = 'bool' ) { /** * Filters whether or not to bypass checking for blocked keys. * * @since BuddyPress 2.2.0 * * @param bool $value Whether or not to bypass checking. Default false. * @param int $user_id Topic of reply author ID. * @param string $title The title of the content. * @param string $content $the content being posted. */ if ( apply_filters( 'bp_bypass_check_for_blacklist' , false, $user_id , $title , $content ) ) { return true; } // Bail if super admin is author. if ( is_super_admin( $user_id ) ) { return true; } // Define local variable. $_post = array (); /** Blacklist ************************************************************ */ // Get the moderation keys. $blacklist = trim( get_option( 'blacklist_keys' ) ); // Bail if blacklist is empty. if ( empty ( $blacklist ) ) { return true; } /** User Data ************************************************************ */ // Map current user data. if ( ! empty ( $user_id ) ) { // Get author data. $user = get_userdata( $user_id ); // If data exists, map it. if ( ! empty ( $user ) ) { $_post [ 'author' ] = $user ->display_name; $_post [ 'email' ] = $user ->user_email; $_post [ 'url' ] = $user ->user_url; } } // Current user IP and user agent. $_post [ 'user_ip' ] = bp_core_current_user_ip(); $_post [ 'user_ua' ] = bp_core_current_user_ua(); // Post title and content. $_post [ 'title' ] = $title ; $_post [ 'content' ] = $content ; /** Words **************************************************************** */ // Get words separated by new lines. $words = explode ( "\n" , $blacklist ); // Loop through words. foreach ( ( array ) $words as $word ) { // Trim the whitespace from the word. $word = trim( $word ); // Skip empty lines. if ( empty ( $word ) ) { continue ; } // Do some escaping magic so that '#' chars in the // spam words don't break things. $word = preg_quote( $word , '#' ); $pattern = "#$word#i" ; // Loop through post data. foreach ( $_post as $post_data ) { // Check each user data for current word. if ( preg_match( $pattern , $post_data ) ) { if ( 'bool' === $error_type ) { return false; } else { return new WP_Error( 'bp_moderation_blacklist_match' , __( 'You have posted an inappropriate word.' , 'buddyboss' ) ); } } } } // Check passed successfully. return true; } |
Changelog
Version | Description |
---|---|
BuddyPress 2.6.0 Added $error_type parameter. | BuddyPress 2.6.0 Added $error_type parameter. |
BuddyPress 1.6.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.