Skip to content

Commit fa3fd6d

Browse files
author
Antti Kuosmanen
authored
Merge pull request #13 from k1sul1/master
File upload features
2 parents 013f7c0 + 31c6495 commit fa3fd6d

5 files changed

Lines changed: 92 additions & 12 deletions

File tree

assets/scripts/wplf-form.js

Lines changed: 14 additions & 1 deletion
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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,25 @@ function save_cpt( $post_id ) {
384384

385385
// save email copy
386386
if ( isset( $_POST['wplf_email_copy_to'] ) ) {
387-
update_post_meta( $post_id, '_wplf_email_copy_to', sanitize_email( $_POST['wplf_email_copy_to'] ) );
387+
$emailField = $_POST['wplf_email_copy_to'];
388+
$to = '';
389+
390+
if( strpos( $emailField, "," ) > 0 ) {
391+
// Intentional. Makes no sense if the first character is a comma, so pass it along as a single address.
392+
// sanitize_email() should take care of the rest.
393+
$emailArray = explode( ",", $emailField );
394+
foreach($emailArray as $email){
395+
$email = trim($email);
396+
$email = sanitize_email( trim( $email ) ) . ", ";
397+
$to .= $email;
398+
}
399+
$to = rtrim( $to, ", " );
400+
}
401+
else {
402+
$to = sanitize_email( $emailField );
403+
}
404+
405+
update_post_meta( $post_id, '_wplf_email_copy_to', $to );
388406
}
389407

390408
// save title format

classes/class-cpt-wplf-submission.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,29 @@ function custom_columns_cpt( $columns ) {
106106
function form_filter_dropdown() {
107107
global $pagenow;
108108

109-
if( 'edit.php' != $pagenow ) {
109+
$allowed = array("wplf-submission"); // show filter on these post types (currently only one?)
110+
$allowed = apply_filters("wplf-dropdown-filter", $allowed);
111+
$post_type = get_query_var("post_type");
112+
113+
if( 'edit.php' != $pagenow || !in_array($post_type, $allowed)) {
110114
return;
111115
}
112116

113-
// TODO: put this in a transient
114-
$forms = get_posts( array(
115-
'post_per_page' => '-1',
116-
'post_type' => 'wplf-form',
117-
) );
117+
$transient = get_transient("wplf-form-filter");
118+
119+
if($transient){
120+
$forms = $transient;
121+
}
122+
123+
else{
124+
$forms = get_posts( array(
125+
'post_per_page' => '-1',
126+
'post_type' => 'wplf-form',
127+
) );
128+
129+
set_transient("wplf-form-filter", $forms, 15 * MINUTE_IN_SECONDS);
130+
}
131+
118132
?>
119133
<label for="filter-by-form" class="screen-reader-text">Filter by form</label>
120134
<select name="form" id="filter-by-form">
@@ -170,6 +184,7 @@ function metabox_submission() {
170184
global $post;
171185
$postmeta = get_post_meta( $post->ID );
172186
$fields = array_keys( $postmeta );
187+
$home_path = get_home_path();
173188
?>
174189
<p>
175190
<table class="wp-list-table widefat striped">
@@ -182,9 +197,26 @@ function metabox_submission() {
182197
<tbody>
183198
<?php foreach( $fields as $field ) : ?>
184199
<?php if( '_' != $field[0] ) : ?>
185-
<?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+
?>
186218
<tr>
187-
<th><strong><?php echo $field; ?></strong></th>
219+
<th><strong><?php echo $field; ?></strong> <?php echo $possible_link; ?></th>
188220
<?php if( strlen( $value ) > 60 || strpos( $value, "\n" ) ) : ?>
189221
<td><textarea style="width:100%" readonly><?php echo esc_textarea( $value ); ?></textarea></td>
190222
<?php else : ?>
@@ -201,4 +233,3 @@ function metabox_submission() {
201233
}
202234

203235
endif;
204-

inc/wplf-ajax.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ function wplf_ajax_submit_handler() {
5959
// Is this enough security wise?
6060
// Currenly only supports 1 file per input
6161
$attach_id = media_handle_upload( $key, 0, array(), array( "test_form" => false ) );
62-
add_post_meta($post_id, $key, $attach_id);
62+
add_post_meta( $post_id, $key, wp_get_attachment_url($attach_id) );
63+
add_post_meta( $post_id, $key . "_attachment", $attach_id );
6364
}
6465

6566

6667

6768
$return->submission_id = $post_id;
6869
$return->submission_title = $post_title;
70+
$return->form_id = $form->ID;
6971

7072
// return the success message for the form
7173
$return->success = apply_filters( 'the_content', get_post_meta( $form->ID, '_wplf_thank_you', true ) );

readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ 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+
### 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+
92108
### Add own css classes to form output
93109

94110
You can use the attribute xclass inside the shortcode to set own extra css classes.

0 commit comments

Comments
 (0)