WPCF7_TagGenerator{}CF7 1.0

The base class for form-tag generators management.

Хуков нет.

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

$WPCF7_TagGenerator = new WPCF7_TagGenerator();
// use class methods

Методы

  1. public static get_instance()
  2. public add( $id, $title, $callback, $options = '' )
  3. public print_buttons()
  4. public print_panels( WPCF7_ContactForm $contact_form )
  5. private __construct()

Код WPCF7_TagGenerator{} CF7 6.1.4

class WPCF7_TagGenerator {

	private static $instance;

	private $panels = array();

	private function __construct() {}


	/**
	 * Returns the singleton instance of this class.
	 */
	public static function get_instance() {
		if ( empty( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}


	/**
	 * Adds a form-tag generator instance.
	 */
	public function add( $id, $title, $callback, $options = '' ) {
		$id = trim( $id );

		if (
			'' === $id or
			! wpcf7_is_name( $id ) or
			! is_callable( $callback )
		) {
			return false;
		}

		$options = wp_parse_args( $options, array(
			'version' => '1',
		) );

		$this->panels[$id] = array(
			'title' => $title,
			'content' => 'tag-generator-panel-' . $id,
			'options' => $options,
			'callback' => $callback,
		);

		if ( version_compare( $options['version'], '2', '<' ) ) {
			$message = sprintf(
				/* translators: 1: version, 2: tag generator title */
				__( 'Use of tag generator instances older than version 2 is deprecated. Version %1$s instance (%2$s) detected.', 'contact-form-7' ),
				$options['version'],
				$title
			);

			wp_trigger_error( __METHOD__, $message, E_USER_DEPRECATED );
		}

		return true;
	}


	/**
	 * Renders form-tag generator calling buttons.
	 */
	public function print_buttons() {
		$formatter = new WPCF7_HTMLFormatter();

		$formatter->append_start_tag( 'span', array(
			'id' => 'tag-generator-list',
			'class' => 'hide-if-no-js',
		) );

		foreach ( (array) $this->panels as $panel ) {
			$formatter->append_start_tag( 'button', array(
				'type' => 'button',
				'data-taggen' => 'open-dialog',
				'data-target' => $panel['content'],
				'title' => sprintf(
					/* translators: %s: title of form-tag */
					__( 'Form-tag Generator: %s', 'contact-form-7' ),
					$panel['title']
				),
			) );

			$formatter->append_preformatted( esc_html( $panel['title'] ) );
		}

		$formatter->print();
	}


	/**
	 * Renders form-tag generator dialog panels (hidden until called).
	 */
	public function print_panels( WPCF7_ContactForm $contact_form ) {
		$formatter = new WPCF7_HTMLFormatter( array(
			'allowed_html' => array_merge( wpcf7_kses_allowed_html(), array(
				'dialog' => array(
					'id' => true,
					'class' => true,
				),
				'form' => array(
					'method' => true,
					'class' => true,
					'data-*' => true,
				),
			) ),
		) );

		foreach ( (array) $this->panels as $id => $panel ) {
			$callback = $panel['callback'];

			$options = array_merge( $panel['options'], array(
				'id' => $id,
				'title' => $panel['title'],
				'content' => $panel['content'],
			) );

			if ( is_callable( $callback ) ) {
				$formatter->append_start_tag( 'dialog', array(
					'id' => $options['content'],
					'class' => 'tag-generator-dialog',
				) );

				$formatter->append_start_tag( 'button', array(
					'class' => 'close-button',
					'title' => __( 'Close this dialog box', 'contact-form-7' ),
					'data-taggen' => 'close-dialog',
				) );

				$formatter->append_preformatted(
					esc_html( __( 'Close', 'contact-form-7' ) )
				);

				$formatter->end_tag( 'button' );

				$formatter->append_start_tag( 'form', array(
					'method' => 'dialog',
					'class' => 'tag-generator-panel',
					'data-id' => $options['id'],
					'data-version' => $options['version'],
				) );

				$formatter->call_user_func( $callback, $contact_form, $options );

				$formatter->close_all_tags();
			}
		}

		$formatter->print();
	}

}