Skip to content

Commit c848f7f

Browse files
author
Antti Kuosmanen
committed
Display a link for submission fields, if value validates as URL
- Only show the View Attachment link if the field key ends with _attachment - Make the "Open Link" and "View Attachment" texts translatable - Make target=_blank default for submission links Fixes #18
1 parent 231b789 commit c848f7f

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

classes/class-cpt-wplf-submission.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,23 @@ function metabox_submission() {
199199
<?php if( '_' != $field[0] ) : ?>
200200
<?php
201201
$value = $postmeta[ $field ][0];
202+
203+
// maybe show a link for the field if suitable
202204
$possible_link = '';
203-
$attachment_url = wp_get_attachment_url( $value ); // get_edit_post_link returns something awkward.
204-
205-
// Maybe add a filter for target="_blank"? Wouldn't enable by default.
206-
207-
if ( $attachment_url ) {
208-
// If this is true, $value was a valid attachment_id.
209-
// Caveat: if user enters a numeric value here, it could be interpreted as attachment.
210-
$attachment_url = get_edit_post_link($value);
211-
$possible_link = "<a href='$attachment_url' style='float: right;'>Edit attachment</a>";
212-
} elseif ( file_exists( $home_path . substr( $value, 1 ) ) ) {
213-
// This is bit less ambiguous. Check if there's a file, and if there is, get link for it.
214-
$attachment_url = get_home_url() . $value;
215-
$possible_link = "<a href='$attachment_url' style='float: right;'>Open file</a>";
205+
206+
// if the field ends with '_attachment' and there is an attachment url that corresponds to the id, show a link
207+
$attachment_suffix = '_attachment';
208+
if ( substr( $field, -strlen( $attachment_suffix ) ) === $attachment_suffix && wp_get_attachment_url( $value ) ) {
209+
$link_text = __( 'View Attachment' );
210+
$possible_link = '<a target="_blank" href="' . get_edit_post_link( $value ) . '" style="float:right">' . $link_text . '</a>';
211+
}
212+
213+
// Show a link if the field corresponds to a URL
214+
// assume values starting with '/' are root relative URLs and should be handled as links
215+
$value_is_url = $value[0] === '/' ? true : filter_var( $value, FILTER_VALIDATE_URL );
216+
if ( $value_is_url ) {
217+
$link_text = __( 'Open Link', 'wp-libre-form' );
218+
$possible_link = '<a target="_blank" href="' . $value . '" style="float:right">' . $link_text . '</a>';
216219
}
217220
?>
218221
<tr>

0 commit comments

Comments
 (0)