WPCF7_MailTaggedText{} │ CF7 1.0
Class that represents a single-line text including mail-tags.
Использование
$WPCF7_MailTaggedText = new WPCF7_MailTaggedText();
// use class methods
Методы
- public __construct( $content, $options = '' )
- public format( $original, $format )
- public get_replaced_tags()
- public replace_tags()
- private replace_tags_callback( $matches, $html = false )
- private replace_tags_callback_html( $matches )
Код WPCF7_MailTaggedText{} WPCF7 MailTaggedText{}
CF7 5.9.8
class WPCF7_MailTaggedText {
private $html = false;
private $callback = null;
private $content = '';
private $replaced_tags = array();
/**
* The constructor method.
*/
public function __construct( $content, $options = '' ) {
$options = wp_parse_args( $options, array(
'html' => false,
'callback' => null,
) );
$this->html = (bool) $options['html'];
if ( null !== $options['callback']
and is_callable( $options['callback'] ) ) {
$this->callback = $options['callback'];
} elseif ( $this->html ) {
$this->callback = array( $this, 'replace_tags_callback_html' );
} else {
$this->callback = array( $this, 'replace_tags_callback' );
}
$this->content = $content;
}
/**
* Retrieves mail-tags that have been replaced by this instance.
*
* @return array List of mail-tags replaced.
*/
public function get_replaced_tags() {
return $this->replaced_tags;
}
/**
* Replaces mail-tags based on regexp.
*/
public function replace_tags() {
$regex = '/(\[?)\[[\t ]*'
. '([a-zA-Z_][0-9a-zA-Z:._-]*)' // [2] = name
. '((?:[\t ]+"[^"]*"|[\t ]+\'[^\']*\')*)' // [3] = values
. '[\t ]*\](\]?)/';
return preg_replace_callback( $regex, $this->callback, $this->content );
}
/**
* Callback function for replacement. For HTML message body.
*/
private function replace_tags_callback_html( $matches ) {
return $this->replace_tags_callback( $matches, true );
}
/**
* Callback function for replacement.
*/
private function replace_tags_callback( $matches, $html = false ) {
// allow [[foo]] syntax for escaping a tag
if ( $matches[1] == '['
and $matches[4] == ']' ) {
return substr( $matches[0], 1, -1 );
}
$tag = $matches[0];
$tagname = $matches[2];
$values = $matches[3];
$mail_tag = new WPCF7_MailTag( $tag, $tagname, $values );
$field_name = $mail_tag->field_name();
$submission = WPCF7_Submission::get_instance();
$submitted = $submission
? $submission->get_posted_data( $field_name )
: null;
if ( $mail_tag->get_option( 'do_not_heat' ) ) {
$submitted = wp_unslash( $_POST[$field_name] ?? '' );
}
$replaced = $submitted;
if ( null !== $replaced ) {
if ( $format = $mail_tag->get_option( 'format' ) ) {
$replaced = $this->format( $replaced, $format );
}
$separator = ( 'body' === WPCF7_Mail::get_current_component_name() )
? wp_get_list_item_separator()
: ', ';
$replaced = wpcf7_flat_join( $replaced, array(
'separator' => $separator,
) );
if ( $html ) {
$replaced = esc_html( $replaced );
$replaced = wptexturize( $replaced );
}
}
if ( $form_tag = $mail_tag->corresponding_form_tag() ) {
$type = $form_tag->type;
$replaced = apply_filters(
"wpcf7_mail_tag_replaced_{$type}", $replaced,
$submitted, $html, $mail_tag
);
}
$replaced = apply_filters(
'wpcf7_mail_tag_replaced', $replaced,
$submitted, $html, $mail_tag
);
if ( null !== $replaced ) {
$replaced = trim( $replaced );
$this->replaced_tags[$tag] = $replaced;
return $replaced;
}
$special = apply_filters( 'wpcf7_special_mail_tags', null,
$mail_tag->tag_name(), $html, $mail_tag
);
if ( null !== $special ) {
$this->replaced_tags[$tag] = $special;
return $special;
}
return $tag;
}
/**
* Formats string based on the formatting option in the form-tag.
*/
public function format( $original, $format ) {
$original = (array) $original;
foreach ( $original as $key => $value ) {
if ( preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $value ) ) {
$datetime = date_create( $value, wp_timezone() );
if ( false !== $datetime ) {
$original[$key] = wp_date( $format, $datetime->getTimestamp() );
}
}
}
return $original;
}
}