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
47class 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}
231235add_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 ' );
233249function 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 }
0 commit comments