bp_core_check_for_moderation

Check for moderation keys and too many links.

Description

bp_core_check_for_moderation( (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_moderation' ) ) { 
  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_moderation($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_moderation( $user_id = 0, $title = '', $content = '', $error_type = 'bool' ) { 
  2.  
  3. /** 
  4. * Filters whether or not to bypass checking for moderation keys and too many links. 
  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_moderation', 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(s). 
  19. $_post = array(); 
  20. $match_out = ''; 
  21.  
  22. /** User Data ************************************************************ 
  23. */ 
  24.  
  25. if ( ! empty( $user_id ) ) { 
  26.  
  27. // Get author data. 
  28. $user = get_userdata( $user_id ); 
  29.  
  30. // If data exists, map it. 
  31. if ( ! empty( $user ) ) { 
  32. $_post['author'] = $user->display_name; 
  33. $_post['email'] = $user->user_email; 
  34. $_post['url'] = $user->user_url; 
  35.  
  36. // Current user IP and user agent. 
  37. $_post['user_ip'] = bp_core_current_user_ip(); 
  38. $_post['user_ua'] = bp_core_current_user_ua(); 
  39.  
  40. // Post title and content. 
  41. $_post['title'] = $title; 
  42. $_post['content'] = $content; 
  43.  
  44. /** Max Links ************************************************************ 
  45. */ 
  46.  
  47. $max_links = get_option( 'comment_max_links' ); 
  48. if ( ! empty( $max_links ) ) { 
  49.  
  50. // How many links? 
  51. $num_links = preg_match_all( '/(http|ftp|https):\/\//i', $content, $match_out ); 
  52.  
  53. // Allow for bumping the max to include the user's URL. 
  54. if ( ! empty( $_post['url'] ) ) { 
  55.  
  56. /** 
  57. * Filters the maximum amount of links allowed to include the user's URL. 
  58. * @since 1.6.0 
  59. * @param string $num_links How many links found. 
  60. * @param string $value User's url. 
  61. */ 
  62. $num_links = apply_filters( 'comment_max_links_url', $num_links, $_post['url'] ); 
  63.  
  64. // Das ist zu viele links! 
  65. if ( $num_links >= $max_links ) { 
  66. if ( 'bool' === $error_type ) { 
  67. return false; 
  68. } else { 
  69. return new WP_Error( 'bp_moderation_too_many_links', __( 'You have posted too many links', buddypress ) ); 
  70.  
  71. /** Blacklist ************************************************************ 
  72. */ 
  73.  
  74. // Get the moderation keys. 
  75. $blacklist = trim( get_option( 'moderation_keys' ) ); 
  76.  
  77. // Bail if blacklist is empty. 
  78. if ( ! empty( $blacklist ) ) { 
  79.  
  80. // Get words separated by new lines. 
  81. $words = explode( "\n", $blacklist ); 
  82.  
  83. // Loop through words. 
  84. foreach ( (array) $words as $word ) { 
  85.  
  86. // Trim the whitespace from the word. 
  87. $word = trim( $word ); 
  88.  
  89. // Skip empty lines. 
  90. if ( empty( $word ) ) { 
  91. continue; 
  92.  
  93. // Do some escaping magic so that '#' chars in the 
  94. // spam words don't break things. 
  95. $word = preg_quote( $word, '#' ); 
  96. $pattern = "#$word#i"; 
  97.  
  98. // Loop through post data. 
  99. foreach ( $_post as $post_data ) { 
  100.  
  101. // Check each user data for current word. 
  102. if ( preg_match( $pattern, $post_data ) ) { 
  103. if ( 'bool' === $error_type ) { 
  104. return false; 
  105. } else { 
  106. return new WP_Error( 'bp_moderation_word_match', _x( 'You have posted an inappropriate word.', 'Comment moderation', buddypress ) ); 
  107.  
  108. // Check passed successfully. 
  109. return true;