Validate::validate_badwords( object $field )

Description #

Validate if there are minimum items in an array.

Parameters #

  • $field
    object (Required) Instance of @see AP_Field object.

Source #

File: lib/class-validate.php

	public static function validate_badwords( $field ) {
		$value = $field->unsafe_value();
		$found = array();

		foreach ( (array) self::get_bad_words() as $w ) {
			$w     = trim( $w );
			$count = preg_match_all( '/\b' . preg_quote( $w, '/' ) . '\b/i', $value );

			if ( $count > 0 ) {
				$found[ $w ] = $count;
			}
		}

		if ( ! empty( $found ) ) {
			$field->add_error(
				'bad-words',
				sprintf(
					// Translators: placeholder contain field label.
					__( 'Found bad words in field %s. Remove them and try again.', 'anspress-question-answer' ),
					$field->get( 'label' )
				)
			);
		}
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment