bp_core_check_for_disallowed_keys( int $user_id, string $title = '', string $content = '', string $error_type = 'bool' )

Check for blocked keys.


Parameters Parameters

$user_id

(int) (Required) User ID.

$title

(string) (Optional) The title of the content.

Default value: ''

$content

(string) (Optional) The content being posted.

Default value: ''

$error_type

(string) (Optional) The error type to return. Either 'bool' or 'wp_error'.

Default value: 'bool'


Top ↑

Return Return

(bool|WP_Error) True if test is passed, false if fail.


Source Source

File: bp-core/bp-core-moderation.php

function bp_core_check_for_disallowed_keys( $user_id = 0, $title = '', $content = '', $error_type = 'bool' ) {

	/**
	 * Filters whether or not to bypass checking for blocked keys.
	 *
	 * @since 2.2.0
	 * @deprecated 7.0.0 Use 'bp_bypass_check_for_disallowed_keys' instead.
	 *
	 * @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_deprecated( 'bp_bypass_check_for_blacklist', array( false, $user_id, $title, $content ), '7.0.0', 'bp_bypass_check_for_disallowed_keys' ) ) {
		return true;
	}

	/**
	 * Filters whether or not to bypass checking for blocked keys.
	 *
	 * @since 7.0.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_disallowed_keys', 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.
	$disallowed = get_option( 'disallowed_keys' );

	// Support for WP < 5.5.
	if ( false === $disallowed ) {
		$disallowed = get_option( 'blacklist_keys' );
	}

	$disallowed = trim( $disallowed );

	// Bail if disallowed list is empty.
	if ( empty( $disallowed ) ) {
		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", $disallowed );

	// 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_disallowed_key_match', _x( 'You have posted an inappropriate word.', 'Comment disallowed key', 'buddypress' ) );
				}
			}
		}
	}

	// Check passed successfully.
	return true;
}


Top ↑

Changelog Changelog

Changelog
Version Description
7.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.