Advertisement
adczk

custom outside db connection (specific case)

May 4th, 2022
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. function wpmu_form_ex_get_submissions( $order_id ) {
  2.    
  3.     $sub_count = array();
  4.     // connect to external DB
  5.     $edb_host = '';
  6.     $edb_pass = '';
  7.     $edb_user = '';
  8.     $edb_dbname = '';
  9.    
  10.     $external_db = new wpdb( $edb_user, $edb_pass, $edb_dbname, $edb_host );
  11.    
  12.    
  13.     // get max submissions allowed
  14.    
  15.     $results = $external_db->get_results( $external_db->prepare( "SELECT `Identity_Number_Totals` FROM `shop_donor_card_purchases` WHERE `Order_ID` = %s", $order_id ) );
  16.     $sub_count['max'] = $results[0]->Identity_Number_Totals;
  17.    
  18.     // get current number of submissions
  19.    
  20.     $results = $external_db->get_results( $external_db->prepare( 'SELECT COUNT(`Order_ID`) AS `remain`  FROM `shop_donor_card_purchases_print` WHERE `Order_ID` = %s', $order_id ) );
  21.     $sub_count['count'] = $results[0]->remain;
  22.    
  23.    
  24.     // calculate remaining allowed submissions (max - existing)
  25.    
  26.     $sub_count['remaining'] = $sub_count['max'] - $sub_count['count'];
  27.    
  28.     // and return
  29.    
  30.     return $sub_count;
  31.            
  32. }
  33.  
  34. add_filter( 'forminator_render_form_markup', 'wpmu_form_ex_prerender', 10, 4);
  35. function wpmu_form_ex_prerender( $html, $form_fields, $form_type, $form_settings ) {
  36.        
  37.        
  38.     // execute only if specific form is displayed (replace number with your form ID
  39.        
  40.     if ($form_settings['form_id'] == '13660' ) {
  41.        
  42.         // get Order ID from URL (see commented out function at the bottom of this code)
  43.        
  44.         $order_id = get_query_var( 'order_id' );
  45.        
  46.         // get submissions (maximum allowed, number of made already and calculate remaining)
  47.        
  48.         $submission_count = wpmu_form_ex_get_submissions( $order_id );
  49.        
  50.        
  51.         // create custom above the form messsage
  52.        
  53.         $my_header = '<div class="forminator_limit_info">
  54.             <p>Your ORDER ID: '. $order_id . '</p>
  55.             <p>Remaining submissions: ' . $submission_count['remaining'] . ' of ' . $submission_count['max'] . '</p>
  56.             </div>';
  57.            
  58.        
  59.         // if there are remaining submissions, output custom header info and form
  60.         // otherwise "destroy" the form and output only custom header
  61.        
  62.         if ( $submission_count['remaining'] > 0 ) {
  63.             $html = $my_header . $html;
  64.         }
  65.         else {
  66.             $html = $my_header;
  67.         }
  68.            
  69.     }
  70.    
  71.     return $html;
  72.    
  73. }
  74.  
  75. //if you don't see actual ORDER ID above the form but only the "Your Order ID:" message
  76. // without actual ID, uncomment below code:
  77.  
  78. //function wpmu_form_ex_query_vars( $qvars ) {
  79.  //   $qvars[] = 'DataProc';
  80.   //  return $qvars;
  81. //}
  82. //add_filter( 'query_vars', 'wpmu_form_ex_query_vars' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement