WPCF7_ContactForm::form_html()publicCF7 1.0

Generates HTML that represents a form.

Метод класса: WPCF7_ContactForm{}

Возвращает

Строку. HTML output.

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

$WPCF7_ContactForm = new WPCF7_ContactForm();
$WPCF7_ContactForm->form_html( $args );
$args(строка|массив)
Form options.
По умолчанию: ''

Код WPCF7_ContactForm::form_html() CF7 5.7.3

public function form_html( $args = '' ) {
	$args = wp_parse_args( $args, array(
		'html_id' => '',
		'html_name' => '',
		'html_title' => '',
		'html_class' => '',
		'output' => 'form',
	) );

	$this->shortcode_atts = $args;

	if ( 'raw_form' == $args['output'] ) {
		return sprintf(
			'<pre class="wpcf7-raw-form"><code>%s</code></pre>',
			esc_html( $this->prop( 'form' ) )
		);
	}

	if ( $this->is_true( 'subscribers_only' )
	and ! current_user_can( 'wpcf7_submit', $this->id() ) ) {
		$notice = __(
			"This contact form is available only for logged in users.",
			'contact-form-7'
		);

		$notice = sprintf(
			'<p class="wpcf7-subscribers-only">%s</p>',
			esc_html( $notice )
		);

		return apply_filters( 'wpcf7_subscribers_only_notice', $notice, $this );
	}

	$this->unit_tag = self::generate_unit_tag( $this->id );

	$lang_tag = str_replace( '_', '-', $this->locale );

	if ( preg_match( '/^([a-z]+-[a-z]+)-/i', $lang_tag, $matches ) ) {
		$lang_tag = $matches[1];
	}

	$html = "\n" . sprintf( '<div %s>',
		wpcf7_format_atts( array(
			'class' => 'wpcf7 no-js',
			'id' => $this->unit_tag(),
			( get_option( 'html_type' ) == 'text/html' ) ? 'lang' : 'xml:lang'
				=> $lang_tag,
			'dir' => wpcf7_is_rtl( $this->locale ) ? 'rtl' : 'ltr',
		) )
	);

	$html .= "\n" . $this->screen_reader_response() . "\n";

	$url = wpcf7_get_request_uri();

	if ( $frag = strstr( $url, '#' ) ) {
		$url = substr( $url, 0, -strlen( $frag ) );
	}

	$url .= '#' . $this->unit_tag();

	$url = apply_filters( 'wpcf7_form_action_url', $url );

	$id_attr = apply_filters( 'wpcf7_form_id_attr',
		preg_replace( '/[^A-Za-z0-9:._-]/', '', $args['html_id'] )
	);

	$name_attr = apply_filters( 'wpcf7_form_name_attr',
		preg_replace( '/[^A-Za-z0-9:._-]/', '', $args['html_name'] )
	);

	$title_attr = apply_filters( 'wpcf7_form_title_attr', $args['html_title'] );

	$class = 'wpcf7-form';

	if ( $this->is_posted() ) {
		$submission = WPCF7_Submission::get_instance();

		$data_status_attr = $this->form_status_class_name(
			$submission->get_status()
		);

		$class .= sprintf( ' %s', $data_status_attr );
	} else {
		$data_status_attr = 'init';
		$class .= ' init';
	}

	if ( $args['html_class'] ) {
		$class .= ' ' . $args['html_class'];
	}

	if ( $this->in_demo_mode() ) {
		$class .= ' demo';
	}

	$class = explode( ' ', $class );
	$class = array_map( 'sanitize_html_class', $class );
	$class = array_filter( $class );
	$class = array_unique( $class );
	$class = implode( ' ', $class );
	$class = apply_filters( 'wpcf7_form_class_attr', $class );

	$enctype = wpcf7_enctype_value( apply_filters( 'wpcf7_form_enctype', '' ) );
	$autocomplete = apply_filters( 'wpcf7_form_autocomplete', '' );

	$atts = array(
		'action' => esc_url( $url ),
		'method' => 'post',
		'class' => ( '' !== $class ) ? $class : null,
		'id' => ( '' !== $id_attr ) ? $id_attr : null,
		'name' => ( '' !== $name_attr ) ? $name_attr : null,
		'aria-label' => ( '' !== $title_attr )
			? $title_attr : __( 'Contact form', 'contact-form-7' ),
		'enctype' => ( '' !== $enctype ) ? $enctype : null,
		'autocomplete' => ( '' !== $autocomplete ) ? $autocomplete : null,
		'novalidate' => true,
		'data-status' => $data_status_attr,
	);

	$atts = wpcf7_format_atts( $atts );

	$html .= sprintf( '<form %s>', $atts ) . "\n";
	$html .= $this->form_hidden_fields();
	$html .= $this->form_elements();

	if ( ! $this->responses_count ) {
		$html .= $this->form_response_output();
	}

	$html .= "\n" . '</form>';
	$html .= "\n" . '</div>';

	return $html . "\n";
}