Skip to content

Commit 5eee793

Browse files
author
Antti Kuosmanen
committed
Merge branch 'feature-fileuploads'
2 parents 24cec6b + fa3fd6d commit 5eee793

5 files changed

Lines changed: 134 additions & 35 deletions

File tree

assets/scripts/wplf-form.js

Lines changed: 74 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

classes/class-cpt-wplf-form.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,16 @@ function wplf_form( $id , $content = '', $xclass = '' ) {
440440
// you can override the content via a parameter
441441
$content = get_post( $id )->post_content;
442442
}
443+
444+
$multipart = "";
445+
// check if form contains file inputs
446+
if(strpos($content, "type='file'") > -1 || strpos($content, "type=\"file\"") > -1){
447+
$multipart = "enctype='multipart/form-data'";
448+
}
449+
443450
ob_start();
444451
?>
445-
<form class="libre-form libre-form-<?php echo $id . ' ' . $xclass; ?>">
452+
<form class="libre-form libre-form-<?php echo $id . ' ' . $xclass; ?>" <?php echo $multipart; ?>>
446453
<?php echo apply_filters( 'wplf_form', $content ); ?>
447454
<input type="hidden" name="referrer" value="<?php the_permalink(); ?>">
448455
<input type="hidden" name="_form_id" value="<?php esc_attr_e( $id ); ?>">
@@ -464,7 +471,7 @@ function maybe_enqueue_frontend_script() {
464471
global $post;
465472

466473
// register the script, but only enqueue it if the current post contains a form in it
467-
wp_register_script( 'wplf-form-js', plugins_url( 'assets/scripts/wplf-form.js', dirname(__FILE__) ), array( 'jquery' ) );
474+
wp_register_script( 'wplf-form-js', plugins_url( 'assets/scripts/wplf-form.js', dirname(__FILE__) ), array() );
468475

469476
if( is_a( $post, 'WP_Post' ) && ( has_shortcode( $post->post_content, 'libre-form') || $post->post_type === 'wplf-form') ) {
470477
wp_enqueue_script( 'wplf-form-js' );

classes/class-cpt-wplf-submission.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ function metabox_submission() {
184184
global $post;
185185
$postmeta = get_post_meta( $post->ID );
186186
$fields = array_keys( $postmeta );
187+
$home_path = get_home_path();
187188
?>
188189
<p>
189190
<table class="wp-list-table widefat striped">
@@ -196,9 +197,26 @@ function metabox_submission() {
196197
<tbody>
197198
<?php foreach( $fields as $field ) : ?>
198199
<?php if( '_' != $field[0] ) : ?>
199-
<?php $value = $postmeta[ $field ][0]; ?>
200+
<?php
201+
$value = $postmeta[ $field ][0];
202+
$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>";
216+
}
217+
?>
200218
<tr>
201-
<th><strong><?php echo $field; ?></strong></th>
219+
<th><strong><?php echo $field; ?></strong> <?php echo $possible_link; ?></th>
202220
<?php if( strlen( $value ) > 60 || strpos( $value, "\n" ) ) : ?>
203221
<td><textarea style="width:100%" readonly><?php echo esc_textarea( $value ); ?></textarea></td>
204222
<?php else : ?>

inc/wplf-ajax.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ function wplf_ajax_submit_handler() {
1818

1919
if( $return->ok ) {
2020
// form existence has already been validated via filters
21+
22+
2123
$form = get_post( intval( $_POST['_form_id'] ) );
2224

2325
// the title is the value of whatever the first field was in the form
@@ -52,8 +54,20 @@ function wplf_ajax_submit_handler() {
5254
}
5355
}
5456

57+
// handle files
58+
foreach( $_FILES as $key => $file) {
59+
// Is this enough security wise?
60+
// Currenly only supports 1 file per input
61+
$attach_id = media_handle_upload( $key, 0, array(), array( "test_form" => false ) );
62+
add_post_meta( $post_id, $key, wp_get_attachment_url($attach_id) );
63+
add_post_meta( $post_id, $key . "_attachment", $attach_id );
64+
}
65+
66+
67+
5568
$return->submission_id = $post_id;
5669
$return->submission_title = $post_title;
70+
$return->form_id = $form->ID;
5771

5872
// return the success message for the form
5973
$return->success = apply_filters( 'the_content', get_post_meta( $form->ID, '_wplf_thank_you', true ) );
@@ -68,4 +82,3 @@ function wplf_ajax_submit_handler() {
6882
wp_send_json( $return );
6983
wp_die();
7084
}
71-

readme.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,23 @@ wp_enqueue_script('wplf-form-js');
8989
wp_localize_script( 'wplf-form-js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
9090
```
9191

92-
### Add your own css classes to the form element
92+
### Client side callbacks
93+
94+
WP Libre Form supports client side callbacks after form submission using window.wplf object. Example usage:
95+
96+
```
97+
window.wplf.successCallbacks.push(function(response){
98+
alert("You succesfully submitted form " + response.form_id);
99+
});
100+
101+
window.wplf.errorCallbacks.push(function(response){
102+
alert("Form submission failed!");
103+
});
104+
```
105+
106+
These callbacks are executed in the order they appear.
107+
108+
### Add CSS classes to form output
93109

94110
You can use the xclass attribute inside the shortcode to add your own extra classes.
95111

0 commit comments

Comments
 (0)