WPCF7_TagGenerator{} │ CF7 1.0
The base class for form-tag generators management.
Хуков нет.
Использование
$WPCF7_TagGenerator = new WPCF7_TagGenerator();
// use class methods
Методы
- private __construct()
- public add( $id, $title, $callback, $options = '' )
- public static get_instance()
- public print_buttons()
- public print_panels( WPCF7_ContactForm $contact_form )
Код WPCF7_TagGenerator{} WPCF7 TagGenerator{}
CF7 6.0.1
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() {
echo '<span id="tag-generator-list" class="hide-if-no-js">';
foreach ( (array) $this->panels as $panel ) {
echo sprintf(
'<button %1$s>%2$s</button>',
wpcf7_format_atts( 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']
),
) ),
esc_html( $panel['title'] )
);
}
echo '</span>';
}
/**
* Renders form-tag generator dialog panels (hidden until called).
*/
public function print_panels( WPCF7_ContactForm $contact_form ) {
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 ) ) {
echo "\n";
echo sprintf(
'<dialog id="%s" class="tag-generator-dialog">',
esc_attr( $options['content'] )
);
echo "\n";
echo sprintf(
'<button %1$s>%2$s</button>',
wpcf7_format_atts( array(
'class' => 'close-button',
'title' => __( 'Close this dialog box', 'contact-form-7' ),
'data-taggen' => 'close-dialog',
) ),
esc_html( __( 'Close', 'contact-form-7' ) )
);
echo "\n";
echo sprintf(
'<form %s>',
wpcf7_format_atts( array(
'method' => 'dialog',
'class' => 'tag-generator-panel',
'data-id' => $options['id'],
'data-version' => $options['version'],
) )
);
echo "\n";
call_user_func( $callback, $contact_form, $options );
echo "\n";
echo '</form>';
echo "\n";
echo '</dialog>';
echo "\n\n";
}
}
}
}