Skip to content

Commit 0c83f9d

Browse files
committed
New file input shortcode. v1.0.1
1 parent fd7a888 commit 0c83f9d

6 files changed

Lines changed: 146 additions & 42 deletions

File tree

README.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ You can also follow us on Twitter! **[@paystack](http://twitter.com/paystack)**
7878

7979

8080
== Changelog ==
81+
= 1.0.1 =
82+
* New input file shortcode. For frontend file upload.
8183

8284
= 1.0.0 =
8385
* First release

admin/class-paystack-forms-admin.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ function help_metabox_details( $post ) {
282282

283283
?>
284284
<div class="awesome-meta-admin">
285+
Email field is added automatically, no need to include that.<br /><br />
285286
To make an input field compulsory add <code> required="required" </code> to the shortcode <br /><br />
286287
It should look like this <code> [text name="Full Name" required="required" ]</code>
287288

@@ -472,11 +473,24 @@ public function list_table_page(){
472473
function format_data($data){
473474
$new = json_decode($data);
474475
$text = '';
475-
if (count($new) > 0) {
476+
if (array_key_exists("0", $new)) {
476477
foreach ($new as $key => $item) {
477-
$text.= '<b>'.$key."</b> :".$item."<br />";
478+
if ($item->type == 'text') {
479+
$text.= '<b>'.$item->display_name."</b> :".$item->value."<br />";
480+
}else{
481+
$text.= '<b>'.$item->display_name."</b> : <a target='_blank' href='".$item->value."'>link</a><br />";
482+
}
483+
484+
}
485+
}else{
486+
$text = '';
487+
if (count($new) > 0) {
488+
foreach ($new as $key => $item) {
489+
$text.= '<b>'.$key."</b> :".$item."<br />";
490+
}
478491
}
479492
}
493+
//
480494
return $text;
481495
}
482496

admin/css/paystack-forms-admin.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,10 @@ ul.config-error li a.external {
404404
#custom-metabox-holder-sortables{
405405
margin-top:30px;
406406
}
407+
408+
.wp-list-table .column-id { width: 5%; }
409+
.wp-list-table .column-email { width: 20%; }
410+
.wp-list-table .column-metadata { width: 35%; }
411+
.wp-list-table .column-amount { width: 10%; }
412+
.wp-list-table .column-txn_code { width: 15%; }
413+
.wp-list-table .column-date { width: 10%; }

paystack-forms.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ function insertTextarea(){
9999
function insertSelect(){
100100
QTags.insertContent('[select name="Text Title" options="option 1,option 2,option 2"]');
101101
}
102+
QTags.addButton(
103+
"s_shortcode",
104+
"Insert File Upload",
105+
insertInput
106+
);
107+
function insertInput(){
108+
QTags.insertContent('[input name="File Name"]');
109+
}
102110
</script>
103111
<?php
104112
}

public/class-paystack-forms-public.php

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
4+
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
5+
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
36

47
class Paystack_Forms_Public {
58

@@ -141,7 +144,7 @@ function cf_shortcode($atts) {
141144
// print_r($loggedin);
142145
if ($loggedin == 'no') {
143146
echo "<h1 id='pf-form".$id."'>".$obj->post_title."</h1>";
144-
echo '<form class="paystack-form" action="' . admin_url('admin-ajax.php') . '" url="' . admin_url() . '" method="post">';
147+
echo '<form enctype="multipart/form-data" class="paystack-form" action="' . admin_url('admin-ajax.php') . '" url="' . admin_url() . '" method="post">';
145148
echo '<input type="hidden" name="action" value="paystack_submit_action">';
146149
echo '<input type="hidden" name="pf-id" value="' . $id . '" />';
147150
echo '<input type="hidden" name="pf-user_id" value="' . $user_id. '" />';
@@ -158,6 +161,7 @@ function cf_shortcode($atts) {
158161
}
159162
echo '</p>';
160163
echo(do_shortcode($obj->post_content));
164+
161165
// echo '<br /><p>Transaction charge:'.$currency.'<b class="txn_charge">13,000</b></p>';
162166
// echo '<p>Total charge:'.$currency.'<b class="total_charge">13,000</b></p>';
163167
echo '<p> <br /><input type="submit" class="btn btn-danger" value="'.$paybtn.'" ></p>';
@@ -229,7 +233,19 @@ function textarea_shortcode($atts) {
229233
return $code;
230234
}
231235
add_shortcode('textarea', 'textarea_shortcode');
232-
236+
function input_shortcode($atts) {
237+
extract(shortcode_atts(array(
238+
'name' => 'Title',
239+
'required' => '0',
240+
), $atts));
241+
$code = '<label> '.$name.'<br /><input class="form-control" type="file" name="'.$name.'"';
242+
if ($required == 'required') {
243+
$code.= ' required="required" ';
244+
}
245+
$code.= '" /></label><br />';
246+
return $code;
247+
}
248+
add_shortcode('input', 'input_shortcode');
233249
function to_slug($text){
234250
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
235251
$text = preg_replace('~[^-\w]+~', '', $text);
@@ -298,6 +314,7 @@ function paystack_submit_action() {
298314
// Exit here, for not processing further because of the error
299315
exit(json_encode($response));
300316
}
317+
301318
global $wpdb;
302319
$code = generate_code();
303320

@@ -308,15 +325,47 @@ function paystack_submit_action() {
308325
unset($metadata['pf-pemail']);
309326
unset($metadata['pf-amount']);
310327
unset($metadata['pf-user_id']);
328+
329+
// echo '<pre>';
330+
331+
$fixedmetadata = paystack_meta_as_custom_fields($metadata);
332+
333+
if(!empty($_FILES)){
334+
foreach ($_FILES as $keyname => $value) {
335+
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+
];
345+
}else{
346+
$fixedmetadata[] = [
347+
'display_name' => ucwords(str_replace("_", " ", $keyname)),
348+
'variable_name' => $keyname,
349+
'type' => 'text',
350+
'value' => 'No file Uploaded'
351+
];
352+
}
353+
354+
}
355+
}
356+
311357
$insert = array(
312358
'post_id' => strip_tags($_POST["pf-id"], ""),
313359
'email' => strip_tags($_POST["pf-pemail"], ""),
314360
'user_id' => strip_tags($_POST["pf-user_id"], ""),
315361
'amount' => strip_tags($_POST["pf-amount"], ""),
316362
'ip' => get_the_user_ip(),
317363
'txn_code' => $code,
318-
'metadata' => json_encode($metadata)
364+
'metadata' => json_encode($fixedmetadata)
319365
);
366+
// print_r($fixedmetadata);
367+
// print_r($_FILES);
368+
// die();
320369

321370
$exist = $wpdb->get_results("SELECT * FROM $table WHERE (post_id = '".$insert['post_id']."'
322371
AND email = '".$insert['email']."'
@@ -340,7 +389,7 @@ function paystack_submit_action() {
340389
'code' => $insert['txn_code'],
341390
'email' => $insert['email'],
342391
'total' => $insert['amount']*100,
343-
'custom_fields' => paystack_meta_as_custom_fields($metadata)
392+
'custom_fields' => $fixedmetadata
344393
);
345394
echo json_encode($response);
346395

@@ -352,7 +401,8 @@ function paystack_meta_as_custom_fields($metadata){
352401
foreach ($metadata as $key => $value) {
353402
$custom_fields[] = [
354403
'display_name' => ucwords(str_replace("_", " ", $key)),
355-
'variable_name' => $key,
404+
'variable_name' => $key,
405+
'type' => 'text',
356406
'value' => $value
357407
];
358408
}

public/js/paystack-forms-public.js

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -77,45 +77,68 @@
7777
e.preventDefault();
7878

7979
$.blockUI({ message: 'Please wait...' });
80-
$.post($form.attr('action'), $form.serialize(), function(data) {
81-
$.unblockUI();
82-
if (data.result == 'success'){
83-
var handler = PaystackPop.setup({
84-
key: settings.key,
85-
email: data.email,
86-
amount: data.total,
87-
ref: data.code,
88-
metadata: {'custom_fields': data.custom_fields},
89-
callback: function(response){
90-
$.blockUI({ message: 'Please wait...' });
91-
$.post($form.attr('action'), {'action':'paystack_confirm_payment','code':response.trxref}, function(newdata) {
92-
data = JSON.parse(newdata);
93-
if (data.result == 'success'){
94-
$('.paystack-form')[0].reset();
95-
$('html,body').animate({ scrollTop: $('.paystack-form').offset().top - 110 }, 500);
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);
9694

97-
self.before('<pre>'+data.message+'</pre>');
98-
$(this).find("input, select, textarea").each(function() {
99-
$(this).css({ "border-color":"#d1d1d1" });
100-
});
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);
101121

102-
$.unblockUI();
103-
}else{
104-
self.before('<pre>'+data.message+'</pre>');
105-
$.unblockUI();
106-
}
107-
});
108-
},
109-
onClose: function(){
122+
self.before('<pre>'+data.message+'</pre>');
123+
$(this).find("input, select, textarea").each(function() {
124+
$(this).css({ "border-color":"#d1d1d1" });
125+
});
110126

111-
}
112-
});
113-
handler.openIframe();
127+
$.unblockUI();
128+
}else{
129+
self.before('<pre>'+data.message+'</pre>');
130+
$.unblockUI();
131+
}
132+
});
133+
},
134+
onClose: function(){
114135

115-
}
116-
// alert('This is data returned from the server ' + data);
117-
118-
}, 'json');
136+
}
137+
});
138+
handler.openIframe();
139+
}
140+
}
141+
});
119142
});
120143

121144
});

0 commit comments

Comments
 (0)