
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php defined( 'ABSPATH' ) OR die( 'This script cannot be accessed directly.' );

/**
 * Theme Options Field: Link
 *
 * @var   $name  string Field name
 * @var   $id    string Field ID
 * @var   $field array Field options
 *
 * @param $field ['title'] string Field title
 * @param $field ['description'] string Field title
 * @param $field ['text'] string Field additional text
 *
 * @var   $value string Current value
 * @var   $context string Context param states which builder is it
 *
 * @param $value ['url'] string Link URL
 * @param $value ['target'] string Link Target
 */

$input_atts = array(
	'type' => 'hidden',
	'name' => $name,
);

// Utilize WPB format for links
if ( $context === 'shortcode' AND is_string( $value ) ) {
	$input_atts['value'] = $value;
	$input_atts['data-format'] = 'serialized';

	if (
		strpos( $value, 'url:' ) === 0
		OR strpos( $value, '|' ) !== FALSE
	) {
		$params_pairs = explode( '|', $value );
		$value = array();
		if ( ! empty( $params_pairs ) ) {
			foreach ( $params_pairs as $pair ) {
				$param = explode( ':', $pair, 2 );
				if ( ! empty( $param[0] ) AND isset( $param[1] ) ) {
					$value[ $param[0] ] = rawurldecode( $param[1] );
				}
			}
		}
	}
}

// Fallback for cases when URL was set in WPBakery editor
if ( ! is_array( $value ) ) {
	$value = array(
		'url' => is_string( $value ) ? $value : '',
	);

}
if ( ! isset( $value['url'] ) ) {
	$value['url'] = '';
}
if ( ! isset( $value['target'] ) ) {
	$value['target'] = '';
}

if ( $context !== 'shortcode' ) {
	$input_atts['value'] = json_encode( $value );
}

// Output the html
$output = '<input' . us_implode_atts( $input_atts ) . '>';
$output .= '<input type="text" value="' . esc_attr( rawurldecode( $value['url'] ) ) . '"';
if ( ! empty( $field['placeholder'] ) ) {
	$output .= ' placeholder="' . esc_attr( $field['placeholder'] ) . '"';
}
$output .= '>';

// "Open in the new tab" checkbox
$output .= '<div class="usof-checkbox">';
$output .= '<label>';
$output .= '<input type="checkbox"' . checked( $value['target'], '_blank', FALSE ) . '>';
$output .= '<span class="usof-checkbox-text">' . strip_tags( us_translate( 'Open link in a new tab' ) ) . '</span>';
$output .= '</label>';
$output .= '</div>';

echo $output;
