bp_core_check_for_blacklist

Check for blocked keys.

Description

bp_core_check_for_blacklist( (int) $user_id = 0, (string) $title = '', (string) $content = '', (string) $error_type = 'bool' ); 

Parameters (4)

0. $user_id — Optional. (int)
The user id.
1. $title — Optional. (string) => ''
The title of the content.
2. $content — Optional. (string) => ''
The content being posted.
3. $error_type — Optional. (string) => 'bool'
The error type to return. Either bool or wp_error..

Usage

  1. if ( !function_exists( 'bp_core_check_for_blacklist' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-moderation.php'; 
  3.  
  4. // The user id. 
  5. $user_id = -1; 
  6.  
  7. // The title of the content. 
  8. $title = ''; 
  9.  
  10. // The content being posted. 
  11. $content = ''; 
  12.  
  13. // The error type to return. Either 'bool' or 'wp_error'. 
  14. $error_type = 'bool'; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = bp_core_check_for_blacklist($user_id, $title, $content, $error_type); 
  18.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-moderation.php  
  1. function bp_core_check_for_blacklist( $user_id = 0, $title = '', $content = '', $error_type = 'bool' ) { 
  2.  
  3. /** 
  4. * Filters whether or not to bypass checking for blocked keys. 
  5. * @since 2.2.0 
  6. * @param bool $value Whether or not to bypass checking. Default false. 
  7. * @param int $user_id Topic of reply author ID. 
  8. * @param string $title The title of the content. 
  9. * @param string $content $the content being posted. 
  10. */ 
  11. if ( apply_filters( 'bp_bypass_check_for_blacklist', false, $user_id, $title, $content ) ) { 
  12. return true; 
  13.  
  14. // Bail if super admin is author. 
  15. if ( is_super_admin( $user_id ) ) { 
  16. return true; 
  17.  
  18. // Define local variable. 
  19. $_post = array(); 
  20.  
  21. /** Blacklist ************************************************************ 
  22. */ 
  23.  
  24. // Get the moderation keys. 
  25. $blacklist = trim( get_option( 'blacklist_keys' ) ); 
  26.  
  27. // Bail if blacklist is empty. 
  28. if ( empty( $blacklist ) ) { 
  29. return true; 
  30.  
  31. /** User Data ************************************************************ 
  32. */ 
  33.  
  34. // Map current user data. 
  35. if ( ! empty( $user_id ) ) { 
  36.  
  37. // Get author data. 
  38. $user = get_userdata( $user_id ); 
  39.  
  40. // If data exists, map it. 
  41. if ( ! empty( $user ) ) { 
  42. $_post['author'] = $user->display_name; 
  43. $_post['email'] = $user->user_email; 
  44. $_post['url'] = $user->user_url; 
  45.  
  46. // Current user IP and user agent. 
  47. $_post['user_ip'] = bp_core_current_user_ip(); 
  48. $_post['user_ua'] = bp_core_current_user_ua(); 
  49.  
  50. // Post title and content. 
  51. $_post['title'] = $title; 
  52. $_post['content'] = $content; 
  53.  
  54. /** Words **************************************************************** 
  55. */ 
  56.  
  57. // Get words separated by new lines. 
  58. $words = explode( "\n", $blacklist ); 
  59.  
  60. // Loop through words. 
  61. foreach ( (array) $words as $word ) { 
  62.  
  63. // Trim the whitespace from the word. 
  64. $word = trim( $word ); 
  65.  
  66. // Skip empty lines. 
  67. if ( empty( $word ) ) { continue; } 
  68.  
  69. // Do some escaping magic so that '#' chars in the 
  70. // spam words don't break things. 
  71. $word = preg_quote( $word, '#' ); 
  72. $pattern = "#$word#i"; 
  73.  
  74. // Loop through post data. 
  75. foreach( $_post as $post_data ) { 
  76.  
  77. // Check each user data for current word. 
  78. if ( preg_match( $pattern, $post_data ) ) { 
  79. if ( 'bool' === $error_type ) { 
  80. return false; 
  81. } else { 
  82. return new WP_Error( 'bp_moderation_blacklist_match', _x( 'You have posted an inappropriate word.', 'Comment blacklist', buddypress ) ); 
  83.  
  84. // Check passed successfully. 
  85. return true;