WPCF7_FormTag::get_default_option() │ public │ CF7 1.0
Retrieves the default option value from the form-tag.
Метод класса: WPCF7_FormTag{}
Хуков нет.
Возвращает
Строку|Массив
. The option value. If the multiple option is enabled, an array of option values.
Использование
$WPCF7_FormTag = new WPCF7_FormTag();
$WPCF7_FormTag->get_default_option( $default_value, $args );
- $default_value(строка|массив)
- Optional default value.
По умолчанию: ''
- $args(строка|массив)
- Optional options for the option value retrieval.
По умолчанию: ''
Код WPCF7_FormTag::get_default_option() WPCF7 FormTag::get default option
CF7 5.8
public function get_default_option( $default_value = '', $args = '' ) {
$args = wp_parse_args( $args, array(
'multiple' => false,
'shifted' => false,
) );
$options = (array) $this->get_option( 'default' );
$values = array();
if ( empty( $options ) ) {
return $args['multiple'] ? $values : $default_value;
}
foreach ( $options as $opt ) {
$opt = sanitize_key( $opt );
if ( 'user_' == substr( $opt, 0, 5 ) and is_user_logged_in() ) {
$primary_props = array( 'user_login', 'user_email', 'user_url' );
$opt = in_array( $opt, $primary_props ) ? $opt : substr( $opt, 5 );
$user = wp_get_current_user();
$user_prop = $user->get( $opt );
if ( ! empty( $user_prop ) ) {
if ( $args['multiple'] ) {
$values[] = $user_prop;
} else {
return $user_prop;
}
}
} elseif ( 'post_meta' === $opt and in_the_loop() ) {
if ( $args['multiple'] ) {
$values = array_merge( $values,
get_post_meta( get_the_ID(), $this->name )
);
} else {
$val = (string) get_post_meta( get_the_ID(), $this->name, true );
if ( strlen( $val ) ) {
return $val;
}
}
} elseif ( 'get' === $opt and isset( $_GET[$this->name] ) ) {
$vals = (array) $_GET[$this->name];
$vals = array_map( 'wpcf7_sanitize_query_var', $vals );
if ( $args['multiple'] ) {
$values = array_merge( $values, $vals );
} else {
$val = isset( $vals[0] ) ? (string) $vals[0] : '';
if ( strlen( $val ) ) {
return $val;
}
}
} elseif ( 'post' === $opt and isset( $_POST[$this->name] ) ) {
$vals = (array) $_POST[$this->name];
$vals = array_map( 'wpcf7_sanitize_query_var', $vals );
if ( $args['multiple'] ) {
$values = array_merge( $values, $vals );
} else {
$val = isset( $vals[0] ) ? (string) $vals[0] : '';
if ( strlen( $val ) ) {
return $val;
}
}
} elseif ( 'shortcode_attr' === $opt ) {
if ( $contact_form = WPCF7_ContactForm::get_current() ) {
$val = $contact_form->shortcode_attr( $this->name );
if ( strlen( $val ) ) {
if ( $args['multiple'] ) {
$values[] = $val;
} else {
return $val;
}
}
}
} elseif ( preg_match( '/^[0-9_]+$/', $opt ) ) {
$nums = explode( '_', $opt );
foreach ( $nums as $num ) {
$num = absint( $num );
$num = $args['shifted'] ? $num : $num - 1;
if ( isset( $this->values[$num] ) ) {
if ( $args['multiple'] ) {
$values[] = $this->values[$num];
} else {
return $this->values[$num];
}
}
}
}
}
if ( $args['multiple'] ) {
$values = array_unique( $values );
return $values;
} else {
return $default_value;
}
}