bbp_check_for_duplicate( array $post_data = array() )

Check for duplicate topics/replies


Description Description

Check to make sure that a user is not making a duplicate post


Parameters Parameters

$post_data

(Optional) Contains information about the comment

Default value: array()


Top ↑

Return Return

(bool) True if it is not a duplicate, false if it is


Top ↑

Source Source

File: includes/common/functions.php

function bbp_check_for_duplicate( $post_data = array() ) {

	// Parse arguments against default values
	$r = bbp_parse_args( $post_data, array(
		'post_author'    => 0,
		'post_type'      => array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ),
		'post_parent'    => 0,
		'post_content'   => '',
		'post_status'    => bbp_get_trash_status_id(),
		'anonymous_data' => array()
	), 'check_for_duplicate' );

	// No duplicate checks for those who can throttle
	if ( user_can( (int) $r['post_author'], 'throttle' ) ) {
		return true;
	}

	// Get the DB
	$bbp_db = bbp_db();

	// Default clauses
	$join = $where = '';

	// Check for anonymous post
	if ( empty( $r['post_author'] ) && ( ! empty( $r['anonymous_data'] ) && ! empty( $r['anonymous_data']['bbp_anonymous_email'] ) ) ) {

		// Sanitize the email address for querying
		$email = sanitize_email( $r['anonymous_data']['bbp_anonymous_email'] );

		// Only proceed
		if ( ! empty( $email ) && is_email( $email ) ) {

			// Get the meta SQL
			$clauses = get_meta_sql( array( array(
				'key'   => '_bbp_anonymous_email',
				'value' => $email,
			) ), 'post', $bbp_db->posts, 'ID' );

			// Set clauses
			$join  = $clauses['join'];

			// "'", "%", "$" and are valid characters in email addresses
			$where = $bbp_db->remove_placeholder_escape( $clauses['where'] );
		}
	}

	// Unslash $r to pass through DB->prepare()
	//
	// @see: https://bbpress.trac.wordpress.org/ticket/2185/
	// @see: https://core.trac.wordpress.org/changeset/23973/
	$r = wp_unslash( $r );

	// Prepare duplicate check query
	$query  = "SELECT ID FROM {$bbp_db->posts} {$join}";
	$query .= $bbp_db->prepare( "WHERE post_type = %s AND post_status != %s AND post_author = %d AND post_content = %s", $r['post_type'], $r['post_status'], $r['post_author'], $r['post_content'] );
	$query .= ! empty( $r['post_parent'] )
		? $bbp_db->prepare( " AND post_parent = %d", $r['post_parent'] )
		: '';
	$query .= $where;
	$query .= " LIMIT 1";
	$dupe   = apply_filters( 'bbp_check_for_duplicate_query', $query, $r );

	// Dupe found
	if ( $bbp_db->get_var( $dupe ) ) {
		do_action( 'bbp_check_for_duplicate_trigger', $post_data );
		return false;
	}

	// Dupe not found
	return true;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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