bbp_check_for_moderation( array $anonymous_data = array(), int $author_id, string $title = '', string $content = '', mixed $strict = false )
Checks topics and replies against the discussion moderation of blocked keys
Description Description
Parameters Parameters
- $anonymous_data
-
(Optional) - if it's an anonymous post. Do not supply if supplying $author_id. Should be sanitized (see bbp_filter_anonymous_post_data()
Default value: array()
- $author_id
-
(Optional) Topic or reply author ID
- $title
-
(Optional) The title of the content
Default value: ''
- $content
-
(Optional) The content being posted
Default value: ''
- $strict
-
(Optional) False for moderation_keys. True for blacklist_keys. String for custom keys.
Default value: false
Return Return
(bool) True if test is passed, false if fail
Source Source
File: includes/common/functions.php
function bbp_check_for_moderation( $anonymous_data = array(), $author_id = 0, $title = '', $content = '', $strict = false ) {
// Custom moderation option key
if ( is_string( $strict ) ) {
$strict = sanitize_key( $strict );
// Use custom key
if ( ! empty( $strict ) ) {
$hook_name = $strict;
$option_name = "{$strict}_keys";
// Key was invalid, so default to moderation keys
} else {
$strict = false;
}
}
// Strict mode uses WordPress "blacklist" settings
if ( true === $strict ) {
$hook_name = 'blacklist';
$option_name = 'blacklist_keys';
// Non-strict uses WordPress "moderation" settings
} elseif ( false === $strict ) {
$hook_name = 'moderation';
$option_name = 'moderation_keys';
}
// Allow for moderation check to be skipped
if ( apply_filters( "bbp_bypass_check_for_{$hook_name}", false, $anonymous_data, $author_id, $title, $content, $strict ) ) {
return true;
}
// Maybe perform some author-specific capability checks
if ( ! empty( $author_id ) ) {
// Bail if user is a keymaster
if ( bbp_is_user_keymaster( $author_id ) ) {
return true;
// Bail if user can moderate
// https://bbpress.trac.wordpress.org/ticket/2726
} elseif ( ( false === $strict ) && user_can( $author_id, 'moderate' ) ) {
return true;
}
}
// Define local variable(s)
$_post = array();
$match_out = '';
/** Max Links *************************************************************/
// Only check max_links when not being strict
if ( false === $strict ) {
$max_links = get_option( 'comment_max_links' );
if ( ! empty( $max_links ) ) {
// How many links?
$num_links = preg_match_all( '/(http|ftp|https):\/\//i', $content, $match_out );
// Allow for bumping the max to include the user's URL
if ( ! empty( $_post['url'] ) ) {
$num_links = apply_filters( 'comment_max_links_url', $num_links, $_post['url'], $content );
}
// Das ist zu viele links!
if ( $num_links >= $max_links ) {
return false;
}
}
}
/** Moderation ************************************************************/
/**
* Filters the bbPress moderation keys.
*
* @since 2.6.0 bbPress (r6050)
*
* @param string $moderation List of moderation keys. One per new line.
*/
$moderation = apply_filters( "bbp_{$hook_name}_keys", trim( get_option( $option_name ) ) );
// Bail if no words to look for
if ( empty( $moderation ) ) {
return true;
}
/** User Data *************************************************************/
// Map anonymous user data
if ( ! empty( $anonymous_data ) ) {
$_post['author'] = $anonymous_data['bbp_anonymous_name'];
$_post['email'] = $anonymous_data['bbp_anonymous_email'];
$_post['url'] = $anonymous_data['bbp_anonymous_website'];
// Map current user data
} elseif ( ! empty( $author_id ) ) {
// Get author data
$user = get_userdata( $author_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'] = bbp_current_author_ip();
$_post['user_ua'] = bbp_current_author_ua();
// Post title and content
$_post['title'] = $title;
$_post['content'] = $content;
// Ensure HTML tags are not being used to bypass the moderation list.
$_post['comment_without_html'] = wp_strip_all_tags( $content );
/** Words *****************************************************************/
// Get words separated by new lines
$words = explode( "\n", $moderation );
// 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 ) ) {
// Post does not pass
return false;
}
}
}
// Check passed successfully
return true;
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |