WPCF7_ConfigValidator::validate_mail()publicCF7 1.0

Runs error detection for the mail sections.

{} Это метод класса: WPCF7_ConfigValidator{}

Хуков нет.

Возвращает

null. Ничего.

Использование

$WPCF7_ConfigValidator = new WPCF7_ConfigValidator();
$WPCF7_ConfigValidator->validate_mail( $template );
$template **
-
По умолчанию: 'mail'

Код WPCF7_ConfigValidator::validate_mail() CF7 5.6.3

public function validate_mail( $template = 'mail' ) {
	$components = (array) $this->contact_form->prop( $template );

	if ( ! $components ) {
		return;
	}

	if ( 'mail' !== $template and empty( $components['active'] ) ) {
		return;
	}

	$components = wp_parse_args( $components, array(
		'subject' => '',
		'sender' => '',
		'recipient' => '',
		'additional_headers' => '',
		'body' => '',
		'attachments' => '',
	) );

	$callback = array( $this, 'replace_mail_tags_with_minimum_input' );

	$subject = new WPCF7_MailTaggedText(
		$components['subject'],
		array( 'callback' => $callback )
	);

	$subject = $subject->replace_tags();
	$subject = wpcf7_strip_newline( $subject );

	$this->detect_maybe_empty( sprintf( '%s.subject', $template ), $subject );

	$sender = new WPCF7_MailTaggedText(
		$components['sender'],
		array( 'callback' => $callback )
	);

	$sender = $sender->replace_tags();
	$sender = wpcf7_strip_newline( $sender );

	$invalid_mailbox = $this->detect_invalid_mailbox_syntax(
		sprintf( '%s.sender', $template ),
		$sender
	);

	if ( ! $invalid_mailbox and ! wpcf7_is_email_in_site_domain( $sender ) ) {
		$this->add_error( sprintf( '%s.sender', $template ),
			self::error_email_not_in_site_domain, array(
				'link' => self::get_doc_link( 'email_not_in_site_domain' ),
			)
		);
	}

	$recipient = new WPCF7_MailTaggedText(
		$components['recipient'],
		array( 'callback' => $callback )
	);

	$recipient = $recipient->replace_tags();
	$recipient = wpcf7_strip_newline( $recipient );

	$this->detect_invalid_mailbox_syntax(
		sprintf( '%s.recipient', $template ),
		$recipient
	);

	$additional_headers = new WPCF7_MailTaggedText(
		$components['additional_headers'],
		array( 'callback' => $callback )
	);

	$additional_headers = $additional_headers->replace_tags();
	$additional_headers = explode( "\n", $additional_headers );
	$mailbox_header_types = array( 'reply-to', 'cc', 'bcc' );
	$invalid_mail_header_exists = false;

	foreach ( $additional_headers as $header ) {
		$header = trim( $header );

		if ( '' === $header ) {
			continue;
		}

		if ( ! preg_match( '/^([0-9A-Za-z-]+):(.*)$/', $header, $matches ) ) {
			$invalid_mail_header_exists = true;
		} else {
			$header_name = $matches[1];
			$header_value = trim( $matches[2] );

			if ( in_array( strtolower( $header_name ), $mailbox_header_types )
			and '' !== $header_value ) {
				$this->detect_invalid_mailbox_syntax(
					sprintf( '%s.additional_headers', $template ),
					$header_value,
					array(
						'message' =>
							__( "Invalid mailbox syntax is used in the %name% field.", 'contact-form-7' ),
						'params' => array( 'name' => $header_name )
					)
				);
			}
		}
	}

	if ( $invalid_mail_header_exists ) {
		$this->add_error( sprintf( '%s.additional_headers', $template ),
			self::error_invalid_mail_header, array(
				'link' => self::get_doc_link( 'invalid_mail_header' ),
			)
		);
	}

	$body = new WPCF7_MailTaggedText(
		$components['body'],
		array( 'callback' => $callback )
	);

	$body = $body->replace_tags();

	$this->detect_maybe_empty( sprintf( '%s.body', $template ), $body );

	if ( '' !== $components['attachments'] ) {
		$attachables = array();

		$tags = $this->contact_form->scan_form_tags(
			array( 'type' => array( 'file', 'file*' ) )
		);

		foreach ( $tags as $tag ) {
			$name = $tag->name;

			if ( false === strpos( $components['attachments'], "[{$name}]" ) ) {
				continue;
			}

			$limit = (int) $tag->get_limit_option();

			if ( empty( $attachables[$name] )
			or $attachables[$name] < $limit ) {
				$attachables[$name] = $limit;
			}
		}

		$total_size = array_sum( $attachables );

		$has_file_not_found = false;
		$has_file_not_in_content_dir = false;

		foreach ( explode( "\n", $components['attachments'] ) as $line ) {
			$line = trim( $line );

			if ( '' === $line or '[' == substr( $line, 0, 1 ) ) {
				continue;
			}

			$has_file_not_found = $this->detect_file_not_found(
				sprintf( '%s.attachments', $template ), $line
			);

			if ( ! $has_file_not_found and ! $has_file_not_in_content_dir ) {
				$has_file_not_in_content_dir = $this->detect_file_not_in_content_dir(
					sprintf( '%s.attachments', $template ), $line
				);
			}

			if ( ! $has_file_not_found ) {
				$path = path_join( WP_CONTENT_DIR, $line );
				$total_size += (int) @filesize( $path );
			}
		}

		$max = 25 * MB_IN_BYTES; // 25 MB

		if ( $max < $total_size ) {
			$this->add_error( sprintf( '%s.attachments', $template ),
				self::error_attachments_overweight,
				array(
					'message' => __( "The total size of attachment files is too large.", 'contact-form-7' ),
					'link' => self::get_doc_link( 'attachments_overweight' ),
				)
			);
		}
	}
}