name = 'link';
$this->label = 'Link';
$this->category = 'relational';
}
public function render( $field, $value = '', $name = null ) {
$name = $name ?: "metaforge[{$field['name']}]";
$url = is_array( $value ) ? ( $value['url'] ?? '' ) : '';
$text = is_array( $value ) ? ( $value['text'] ?? '' ) : '';
$target = is_array( $value ) ? ( $value['target'] ?? '' ) : '';
$has_link = ! empty( $url );
$display_val = $has_link ? ( $text ?: $url ) : '';
echo '
';
echo '
';
// Hidden fields that actually get saved
echo '
';
echo '
';
echo '
';
// Visual display + trigger button
echo '
';
if ( $has_link ) {
echo '
';
echo '
';
echo '
' . esc_html( $display_val ) . '';
echo '
' . esc_url( $url ) . '';
echo '
';
echo '';
echo '';
echo '
';
echo '
';
} else {
echo '
';
}
echo '
'; // .mfe-link-display
echo '
'; // .mfe-field
}
public function format_value( $value, $post_id, $field ) {
if ( is_array( $value ) ) {
if ( ! isset( $value['title'] ) && isset( $value['text'] ) ) {
$value['title'] = $value['text'];
} elseif ( ! isset( $value['text'] ) && isset( $value['title'] ) ) {
$value['text'] = $value['title'];
}
return $value;
}
return array(
'url' => (string) $value,
'title' => '',
'text' => '',
'target' => '',
);
}
public function sanitize( $value, $field ) {
if ( is_array( $value ) ) {
return array(
'url' => esc_url_raw( $value['url'] ?? '' ),
'text' => sanitize_text_field( $value['text'] ?? '' ),
'target' => sanitize_key( $value['target'] ?? '' ),
);
}
return esc_url_raw( $value );
}
}