Skip to content

Commit 550d8be

Browse files
committed
Set file upload limits.
1 parent 0c83f9d commit 550d8be

3 files changed

Lines changed: 87 additions & 80 deletions

File tree

admin/class-paystack-forms-admin.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,10 @@ function help_metabox_details( $post ) {
284284
<div class="awesome-meta-admin">
285285
Email field is added automatically, no need to include that.<br /><br />
286286
To make an input field compulsory add <code> required="required" </code> to the shortcode <br /><br />
287-
It should look like this <code> [text name="Full Name" required="required" ]</code>
287+
It should look like this <code> [text name="Full Name" required="required" ]</code><br /><br />
288+
289+
<b style="color:red;">Warning:</b> Using the file input field may cause data overload on your server.
290+
Be sure you have enough server space before using it. You also have the ability to set file upload limits.
288291

289292
</div>
290293

@@ -311,9 +314,11 @@ function wpt_form_data() {
311314
$successmsg = get_post_meta($post->ID, '_successmsg', true);
312315
$txncharge = get_post_meta($post->ID, '_txncharge', true);
313316
$loggedin = get_post_meta($post->ID, '_loggedin', true);
314-
$currency = get_post_meta($post->ID, '_currency', true);
317+
$currency = get_post_meta($post->ID, '_currency', true);
318+
$filelimit = get_post_meta($post->ID, '_filelimit', true);
315319

316320
if ($amount == "") {$amount = 0;}
321+
if ($filelimit == "") {$filelimit = 2;}
317322
if ($paybtn == "") {$paybtn = 'Pay';}
318323
if ($successmsg == "") {$successmsg = 'Thank you for paying!';}
319324
if ($currency == "") {$currency = 'NGN';}
@@ -337,6 +342,8 @@ function wpt_form_data() {
337342
</select>';
338343
echo '<p>Success Message after Payment</p>';
339344
echo '<textarea rows="3" name="_successmsg" class="widefat" >'.$successmsg.'</textarea>';
345+
echo '<p>File Upload Limit(MB):</p>';
346+
echo '<input ttype="number" name="_filelimit" value="' . $filelimit . '" class="widefat pf-number" />';
340347

341348
}
342349

@@ -355,16 +362,17 @@ function wpt_form_data_meta($post_id, $post) {
355362
// OK, we're authenticated: we need to find and save the data
356363
// We'll put it into an array to make it easier to loop though.
357364

358-
$events_meta['_amount'] = $_POST['_amount'];
359-
$events_meta['_paybtn'] = $_POST['_paybtn'];
360-
$events_meta['_currency'] = $_POST['_currency'];
361-
$events_meta['_successmsg'] = $_POST['_successmsg'];
362-
$events_meta['_txncharge'] = $_POST['_txncharge'];
363-
$events_meta['_loggedin'] = $_POST['_loggedin'];
365+
$$form_meta['_amount'] = $_POST['_amount'];
366+
$$form_meta['_paybtn'] = $_POST['_paybtn'];
367+
$$form_meta['_currency'] = $_POST['_currency'];
368+
$$form_meta['_successmsg'] = $_POST['_successmsg'];
369+
$$form_meta['_txncharge'] = $_POST['_txncharge'];
370+
$$form_meta['_loggedin'] = $_POST['_loggedin'];
371+
$$form_meta['_filelimit'] = $_POST['_filelimit'];
364372

365-
// Add values of $events_meta as custom fields
373+
// Add values of $$form_meta as custom fields
366374

367-
foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
375+
foreach ($$form_meta as $key => $value) { // Cycle through the $$form_meta array!
368376
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
369377
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
370378
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value

public/class-paystack-forms-public.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ function get_the_user_ip() {
308308
add_action( 'wp_ajax_nopriv_paystack_submit_action', 'paystack_submit_action' );
309309
function paystack_submit_action() {
310310
if (trim($_POST['pf-pemail']) == '') {
311-
$response['error'] = true;
312-
$response['error_message'] = 'Email is required';
311+
$response['result'] = 'failed';
312+
$response['message'] = 'Email is required';
313313

314314
// Exit here, for not processing further because of the error
315315
exit(json_encode($response));
@@ -330,18 +330,27 @@ function paystack_submit_action() {
330330

331331
$fixedmetadata = paystack_meta_as_custom_fields($metadata);
332332

333+
$filelimit = get_post_meta($_POST["pf-id"],'_filelimit',true);
334+
335+
$maxFileSize = $filelimit * 1024 * 1024;
336+
333337
if(!empty($_FILES)){
334338
foreach ($_FILES as $keyname => $value) {
335339
if ($value['size'] > 0) {
336-
$attachment_id = media_handle_upload($keyname, $_POST["pf-id"]);
337-
$url = wp_get_attachment_url( $attachment_id);
338-
// $metadata[$keyname] = $url;
339-
$fixedmetadata[] = [
340-
'display_name' => ucwords(str_replace("_", " ", $keyname)),
341-
'variable_name' => $keyname,
342-
'type' => 'link',
343-
'value' => $url
344-
];
340+
if ($value['size'] > $maxFileSize) {
341+
$response['result'] = 'failed';
342+
$response['message'] = 'Max upload size is '.$filelimit."MB";
343+
exit(json_encode($response));
344+
}else{
345+
$attachment_id = media_handle_upload($keyname, $_POST["pf-id"]);
346+
$url = wp_get_attachment_url( $attachment_id);
347+
$fixedmetadata[] = [
348+
'display_name' => ucwords(str_replace("_", " ", $keyname)),
349+
'variable_name' => $keyname,
350+
'type' => 'link',
351+
'value' => $url
352+
];
353+
}
345354
}else{
346355
$fixedmetadata[] = [
347356
'display_name' => ucwords(str_replace("_", " ", $keyname)),

public/js/paystack-forms-public.js

Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -77,69 +77,59 @@
7777
e.preventDefault();
7878

7979
$.blockUI({ message: 'Please wait...' });
80-
// $.ajax({
81-
// type: 'POST',
82-
// url: $form.attr('action'),
83-
// data: fd,
84-
// contentType: false,
85-
// processData: false,
86-
// dataType: "json",
87-
// success: function(response){
88-
//
89-
// $('#image_gallery').val(response['attachment_idss']);
90-
//
91-
// }
92-
// });
93-
var formdata = new FormData(this);
9480

95-
$.ajax({
96-
url: $form.attr('action'),
97-
type: "POST",
98-
data: formdata,
99-
mimeTypes:"multipart/form-data",
100-
contentType: false,
101-
cache: false,
102-
processData: false,
103-
dataType:"JSON",
104-
success: function(data){
105-
$.unblockUI();
106-
// console.log(data);
107-
if (data.result == 'success'){
108-
var handler = PaystackPop.setup({
109-
key: settings.key,
110-
email: data.email,
111-
amount: data.total,
112-
ref: data.code,
113-
metadata: {'custom_fields': data.custom_fields},
114-
callback: function(response){
115-
$.blockUI({ message: 'Please wait...' });
116-
$.post($form.attr('action'), {'action':'paystack_confirm_payment','code':response.trxref}, function(newdata) {
117-
data = JSON.parse(newdata);
118-
if (data.result == 'success'){
119-
$('.paystack-form')[0].reset();
120-
$('html,body').animate({ scrollTop: $('.paystack-form').offset().top - 110 }, 500);
81+
var formdata = new FormData(this);
12182

122-
self.before('<pre>'+data.message+'</pre>');
123-
$(this).find("input, select, textarea").each(function() {
124-
$(this).css({ "border-color":"#d1d1d1" });
125-
});
83+
$.ajax({
84+
url: $form.attr('action'),
85+
type: "POST",
86+
data: formdata,
87+
mimeTypes:"multipart/form-data",
88+
contentType: false,
89+
cache: false,
90+
processData: false,
91+
dataType:"JSON",
92+
success: function(data){
93+
$.unblockUI();
94+
if (data.result == 'success'){
95+
var handler = PaystackPop.setup({
96+
key: settings.key,
97+
email: data.email,
98+
amount: data.total,
99+
ref: data.code,
100+
metadata: {'custom_fields': data.custom_fields},
101+
callback: function(response){
102+
$.blockUI({ message: 'Please wait...' });
103+
$.post($form.attr('action'), {'action':'paystack_confirm_payment','code':response.trxref}, function(newdata) {
104+
data = JSON.parse(newdata);
105+
if (data.result == 'success'){
106+
$('.paystack-form')[0].reset();
107+
$('html,body').animate({ scrollTop: $('.paystack-form').offset().top - 110 }, 500);
126108

127-
$.unblockUI();
128-
}else{
129-
self.before('<pre>'+data.message+'</pre>');
130-
$.unblockUI();
131-
}
132-
});
133-
},
134-
onClose: function(){
109+
self.before('<pre>'+data.message+'</pre>');
110+
$(this).find("input, select, textarea").each(function() {
111+
$(this).css({ "border-color":"#d1d1d1" });
112+
});
135113

136-
}
137-
});
138-
handler.openIframe();
139-
}
140-
}
141-
});
142-
});
114+
$.unblockUI();
115+
}else{
116+
self.before('<pre>'+data.message+'</pre>');
117+
$.unblockUI();
118+
}
119+
});
120+
},
121+
onClose: function(){
122+
123+
}
124+
});
125+
handler.openIframe();
126+
}else{
127+
alert(data.message);
128+
}
129+
}
130+
131+
});
132+
});
143133

144134
});
145135

0 commit comments

Comments
 (0)