function wppizza_order_email($orderid, $blogid = false)
 {
     global $wpdb;
     /**select the right blog table */
     if ($blogid && is_int($blogid) && $blogid > 1) {
         $wpdb->prefix = $wpdb->base_prefix . $blogid . '_';
     }
     $res = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . $this->pluginOrderTable . " WHERE id='" . $orderid . "' ");
     /*initialize vars**/
     if ($res) {
         /*********************************************************************
          *
          *
          *	get and filter customer and order details as required
          *
          *
          **********************************************************************/
         /*cutstomer details**/
         $cDetails = maybe_unserialize($res->customer_ini);
         /****************************************************************
         				include wpml to also send store/emails translated.
         				will not affect items (they will always be the translated one's
         				or - more accurately - be the ones that were put in the cart
         				don't use require once
         			****************************************************************/
         /**set appropriate language. as this can be a language agnostic ipn request, set it specifically depending on what was stored in the db**/
         if (function_exists('icl_translate') && isset($cDetails['wppizza_wpml_lang']) && $cDetails['wppizza_wpml_lang'] != '') {
             global $sitepress;
             /*polylang for instance does not recognise switch_lang*/
             if (method_exists($sitepress, 'switch_lang')) {
                 $sitepress->switch_lang($cDetails['wppizza_wpml_lang']);
             }
             require WPPIZZA_PATH . 'inc/wpml.inc.php';
             require WPPIZZA_PATH . 'inc/wpml.gateways.inc.php';
         }
         /***get (possibly wpml'ed) options**/
         $pOptions = $this->pluginOptions;
         /*order details: unserialize and filter**/
         $oIni = maybe_unserialize($res->order_ini);
         $oDetails = apply_filters('wppizza_filter_order_db_return', $oIni);
         /*********************************************************************
          *
          *		[currency position]
          *
          *********************************************************************/
         $currency_left = wppizza_email_decode_entities($oDetails['currency'], $this->blogCharset) . ' ';
         $currency_right = '';
         if ($this->pluginOptions['layout']['currency_symbol_position'] == 'right') {
             /*right aligned*/
             $currency_left = '';
             $currency_right = ' ' . wppizza_email_decode_entities($oDetails['currency'], $this->blogCharset);
         }
         /*********************************************************************
          *
          *		[update user meta data]
          *
          *********************************************************************/
         if ($res->wp_user_id > 0) {
             /**update profile**/
             if (!empty($oIni['update_profile'])) {
                 $ff = $pOptions['order_form'];
                 $ff = apply_filters('wppizza_filter_formfields_update_profile', $ff);
                 foreach ($ff as $field) {
                     if (!empty($field['enabled'])) {
                         if ($field['type'] != 'select') {
                             update_user_meta($res->wp_user_id, 'wppizza_' . $field['key'], wppizza_validate_string($cDetails[$field['key']]));
                             /*we've validated already, but lets just be save*/
                         } else {
                             $selKey = array_search($cDetails[$field['key']], $field['value']);
                             update_user_meta($res->wp_user_id, 'wppizza_' . $field['key'], $selKey);
                         }
                     }
                 }
                 /**also update WP email...hmmm better not*/
                 //if(!empty($field['enabled']) && $field['key']=='cemail' && !empty($cDetails['cemail'])) {
                 //	wp_update_user( array ( 'ID' => $res->wp_user_id, 'user_email' => $cDetails['cemail'] ) ) ;
                 //}
             }
             /**the below isnt really needed anymore, but - for legacy reasons - let's keep it for the moment*/
             $userMeta = $cDetails;
             /*tidy up a bit*/
             if ($userMeta['wppizza-gateway']) {
                 unset($userMeta['wppizza-gateway']);
             }
             if ($userMeta['wppizza_hash']) {
                 unset($userMeta['wppizza_hash']);
             }
             if (isset($userMeta['update_profile'])) {
                 unset($userMeta['update_profile']);
             }
             update_user_meta($res->wp_user_id, 'wppizza_user_meta', $userMeta);
         }
         /*********************************************************************
          *
          *
          *	customer details : posted and stored variables from order page
          *
          *
          **********************************************************************/
         /*********************************************
          *	[posted input fields of this plugin]
          *********************************************/
         $wppizzaEmailCustomerDetails = array();
         /**protect these keys, so no other extension uses it*/
         $protectedKeys = array();
         foreach ($this->pluginOptions['order_form'] as $k => $v) {
             $protectedKeys[$v['key']] = $v;
         }
         $protectedKeys = apply_filters('wppizza_filter_order_form_fields', $protectedKeys);
         $i = 0;
         foreach ($cDetails as $k => $v) {
             /*****default input fields of this plugin*****/
             if (isset($protectedKeys[$k])) {
                 $wppizzaEmailCustomerDetails[] = array('label' => $protectedKeys[$k]['lbl'], 'value' => $cDetails[$k], 'type' => $protectedKeys[$k]['type'], 'key' => $k);
             }
             /**********************************************************************************************************
              *
              *
              *	THIS BIT BELOW SHOULD NOW BE REDUNDANT NOW AS WE CAN UST USE THE FILTER ABOVE TO ADD TO THE - SO CALLED - PROTECTED KEYS
              *	let's leave it here for now, but it will probably be removed in future versions, as it has never been documnted publicly anyway
              *	as far as i know......
              *
              **********************************************************************************************************/
             /*********************************************************************
             				if another plugin/extension wants to add field value pairs, make sure
             				its an array having [label] and [value] to display in email
             				i.e:
             				<input type="hidden" name="distinct_name[label]" value="some value"/>';
             				<input type="text" name="distinct_name[value]" value="some value"/>';
             				(make sure there are no clashes with other input fields)
             				ought to make this "classable" at some point anyway
             			**********************************************************************/
             if (!isset($protectedKeys[$k])) {
                 if (is_array($v) && isset($v['label']) && isset($v['value']) && !isset($protectedKeys[$k])) {
                     $wppizzaEmailCustomerDetails[] = array('label' => $v['label'], 'value' => $v['value'], 'type' => 'text', 'key' => $k);
                 }
             }
         }
         /*****************************************************************************************
          *
          *
          *	[order items: individual items]
          *
          *
          ****************************************************************************************/
         $wppizzaEmailOrderItems = array();
         foreach ($oDetails['item'] as $k => $v) {
             $wppizzaEmailOrderItems[$k] = $v;
             /**for convenience, we concat vars into label and value and add them to array */
             $wppizzaEmailOrderItems[$k]['label'] = '' . $v['quantity'] . 'x ' . $v['name'] . ' ' . $v['size'] . ' [' . $currency_left . '' . $v['price'] . '' . $currency_right . ']';
             $wppizzaEmailOrderItems[$k]['value'] = '' . $currency_left . $v['pricetotal'] . $currency_right . '';
         }
         /**********************************************************************************************
          *
          *
          *	[order summary]
          *	--we can probably loose all the currencies here. oh well. who knows maybe useful one day
          *
          *********************************************************************************************/
         $wppizzaEmailOrderSummary = array();
         /**********************************************************
          *	[cart items
          **********************************************************/
         $wppizzaEmailOrderSummary['cartitems'] = array('label' => $pOptions['localization']['order_items']['lbl'], 'price' => $oDetails['total_price_items'], 'currency' => $oDetails['currency']);
         /**********************************************************
          *	[discount]
          **********************************************************/
         if ($oDetails['discount'] > 0) {
             $wppizzaEmailOrderSummary['discount'] = array('label' => $pOptions['localization']['discount']['lbl'], 'price' => $oDetails['discount'], 'currency' => $oDetails['currency']);
         }
         /**********************************************************
          *	[item tax - tax applied to items only]
          **********************************************************/
         if ($oDetails['item_tax'] > 0 && !$pOptions['order']['shipping_tax']) {
             $wppizzaEmailOrderSummary['item_tax'] = array('label' => $pOptions['localization']['item_tax_total']['lbl'], 'price' => $oDetails['item_tax'], 'currency' => $oDetails['currency']);
         }
         /**********************************************************
          *	[delivery charges - no self pickup enabled or selected]
          **********************************************************/
         if ($pOptions['order']['delivery_selected'] != 'no_delivery') {
             /*delivery disabled*/
             if (!isset($oDetails['selfPickup']) || $oDetails['selfPickup'] == 0) {
                 if ($oDetails['delivery_charges'] != '') {
                     $wppizzaEmailOrderSummary['delivery'] = array('label' => $pOptions['localization']['delivery_charges']['lbl'], 'price' => $oDetails['delivery_charges'], 'currency' => $oDetails['currency']);
                 } else {
                     $wppizzaEmailOrderSummary['delivery'] = array('label' => $pOptions['localization']['free_delivery']['lbl'], 'price' => '', 'currency' => '');
                 }
             }
         }
         /**********************************************************
          *	[item tax - tax applied to items only]
          **********************************************************/
         if ($oDetails['item_tax'] > 0 && $pOptions['order']['shipping_tax']) {
             $wppizzaEmailOrderSummary['item_tax'] = array('label' => $pOptions['localization']['item_tax_total']['lbl'], 'price' => $oDetails['item_tax'], 'currency' => $oDetails['currency']);
         }
         /**********************************************************
          *	[taxes included]
          **********************************************************/
         if ($oDetails['taxes_included'] > 0 && $pOptions['order']['taxes_included']) {
             $wppizzaEmailOrderSummary['taxes_included'] = array('label' => sprintf('' . $pOptions['localization']['taxes_included']['lbl'] . '', $pOptions['order']['item_tax']), 'price' => $oDetails['taxes_included'], 'currency' => $oDetails['currency']);
         }
         /**********************************************************
          *	[handling charges - (most likely to be used for vv payment)]
          **********************************************************/
         if (isset($oDetails['handling_charge']) && $oDetails['handling_charge'] > 0) {
             $wppizzaEmailOrderSummary['handling_charge'] = array('label' => $pOptions['localization']['order_page_handling']['lbl'], 'price' => wppizza_output_format_price($oDetails['handling_charge'], $pOptions['layout']['hide_decimals']), 'currency' => $oDetails['currency']);
         }
         /**********************************************************
          *	[tips )]
          **********************************************************/
         if (isset($oDetails['tips']) && $oDetails['tips'] > 0) {
             $wppizzaEmailOrderSummary['tips'] = array('label' => $pOptions['localization']['tips']['lbl'], 'price' => wppizza_output_format_price($oDetails['tips'], $pOptions['layout']['hide_decimals']), 'currency' => $oDetails['currency']);
         }
         /**********************************************************
         				[order total]
         			**********************************************************/
         $wppizzaEmailOrderSummary['total'] = array('label' => $pOptions['localization']['order_total']['lbl'], 'price' => $oDetails['total'], 'currency' => $oDetails['currency']);
         /****************************************************
         				[self pickup (enabled and selected) / no delivery offered ]
         			****************************************************/
         if (isset($oDetails['selfPickup']) && $oDetails['selfPickup'] >= 1) {
             if ($oDetails['selfPickup'] == 1) {
                 $wppizzaEmailOrderSummary['self_pickup'] = array('label' => $pOptions['localization']['order_page_self_pickup']['lbl'], 'price' => '', 'currency' => '');
             }
             if ($oDetails['selfPickup'] == 2) {
                 $wppizzaEmailOrderSummary['self_pickup'] = array('label' => $pOptions['localization']['order_page_no_delivery']['lbl'], 'price' => '', 'currency' => '');
             }
         }
         /*********************************************************************************************************************************
          *
          *
          *
          *	[now lets do something with it all, like filtering etc]
          *
          *
          *
          **********************************************************************************************************************************/
         /**filter old legacy additional info keys**/
         $wppizzaEmailOrderItems = apply_filters('wppizza_filter_order_additional_info', $wppizzaEmailOrderItems);
         /**filter new/current extend additional info keys**/
         $wppizzaEmailOrderItems = apply_filters('wppizza_filter_order_extend', $wppizzaEmailOrderItems);
         /****************************************************
         				[allow filtering of summary too]
         			****************************************************/
         $wppizzaEmailOrderSummary = apply_filters('wppizza_filter_order_summary_parameters_emails', $wppizzaEmailOrderSummary, $oDetails);
         /***********************************************************************************************
          *
          *	[set the relevant class vars]
          *
          ***********************************************************************************************/
         /**********************************************
         				[all db vals - maybe useful at some point in the future]
         			************************************************/
         $this->orderResults = $res;
         /***********************************************
         				[set currency etc]
         			************************************************/
         $this->orderCurrency = $oDetails['currency'];
         $this->orderTransactionId = $res->transaction_id;
         $this->orderGatewayUsed = $res->initiator;
         /***********************************************
         				[set localization vars]
         			************************************************/
         foreach ($pOptions['localization'] as $k => $v) {
             $orderLabel['html'][$k] = $v['lbl'];
             $orderLabel['plaintext'][$k] = wppizza_email_decode_entities($v['lbl'], $this->blogCharset);
         }
         $this->orderLabels = $orderLabel;
         /************************************************************************************************************************
          *
          *	[set plaintext variables for emails and order history]
          *	lets get all the plaintext things we need, making htmldecoded strings out of customer details and summary
          *	, and htmldecoded array out of order items to be used in plaintext email template and to save into order history
          *
          ************************************************************************************************************************/
         /***********************************************
         				[set general vars]
         			************************************************/
         $gatewayUsed = $res->initiator;
         /**get gateway frontend label instead of just COD or similar**/
         $gatewayLabel = $res->initiator;
         $wppizzaGateways = new WPPIZZA_GATEWAYS();
         $this->pluginGateways = $wppizzaGateways->wppizza_instanciate_gateways_frontend();
         if (isset($this->pluginGateways[$res->initiator])) {
             $gatewayLabel = !empty($this->pluginGateways[$res->initiator]->gatewayOptions['gateway_label']) ? $this->pluginGateways[$res->initiator]->gatewayOptions['gateway_label'] : $gatewayLabel;
         }
         /**********************/
         $transactionId = $res->transaction_id;
         /**filter as required**/
         $transactionId = apply_filters('wppizza_email_filter_transaction_id', $transactionId, $res->id);
         $nowdate = $this->orderTimestamp;
         $orderLabel = $this->orderLabels['plaintext'];
         /**customer details as plaintext string: to use in plaintext emails and save into order history->customer details**/
         $emailPlaintext['customer_details'] = apply_filters('wppizza_filter_customer_details_to_plaintext', $wppizzaEmailCustomerDetails);
         /**order details as plaintext string: to use in plaintext emails and save into order history->order details**/
         $emailPlaintext['items'] = apply_filters('wppizza_filter_order_items_to_plaintext', $wppizzaEmailOrderItems);
         /**for plaintext email template**/
         /**items as string to insert into db**/
         $emailPlaintext['db_items'] = '';
         /***allow filtering of items (sort, add categories and whatnot)****/
         $emailPlaintext['items'] = apply_filters('wppizza_emailplaintext_filter_items', $emailPlaintext['items'], 'plaintextemail');
         foreach ($emailPlaintext['items'] as $k => $v) {
             /***allow action per item - probably to use in conjunction with filter above****/
             $emailPlaintext['db_items'] = apply_filters('wppizza_emailplaintext_item', $v, $emailPlaintext['db_items']);
             $strPartLeft = '' . $v['label'] . '';
             /*made up of => '.$v['quantity'].'x '.$v['name'].' '.$v['size'].' ['.$v['currency'].' '.$v['price'].']'*/
             $spaces = 75 - strlen($strPartLeft);
             $strPartRight = '' . $v['value'] . '';
             /*made up of => '.$v['currency'].' '.$v['pricetotal'].'*/
             /**add to string, spacing left and right out somewhat and put linebreak before any additional info**/
             $emailPlaintext['db_items'] .= '' . $strPartLeft . '' . str_pad($strPartRight, $spaces, " ", STR_PAD_LEFT) . PHP_EOL . '';
             /**NOTE: DO NOT DELETE OR ALTER THE ADDITIONAL INFO DECLARATIONS OR YOU MIGHT BREAK THINGS. IF NOT NOW THAN POSSIBLY IN THE FUTURE AS OTHER EXTENSIONS MAY RELY ON THIS!!!*/
             if (isset($v['additional_info']) && trim($v['additional_info']) != '') {
                 $emailPlaintext['db_items'] .= '' . $v['additional_info'] . '';
                 /**add additional line break as spacer between items**/
                 $emailPlaintext['db_items'] .= PHP_EOL;
             }
         }
         /**summary details as plaintext string: to use in plaintext emails and save into order history->order details**/
         $emailPlaintext['order_summary'] = apply_filters('wppizza_filter_order_summary_to_plaintext', $wppizzaEmailOrderSummary);
         /**include plaintext template**/
         $orderEmailPlaintext = '';
         if (file_exists($this->pluginTemplateDir . '/wppizza-order-email-plaintext.php')) {
             ob_start();
             require_once $this->pluginTemplateDir . '/wppizza-order-email-plaintext.php';
             $orderEmailPlaintext = ob_get_clean();
         } else {
             ob_start();
             require_once WPPIZZA_PATH . 'templates/wppizza-order-email-plaintext.php';
             $orderEmailPlaintext = ob_get_clean();
         }
         $this->orderMessage['plaintext'] = "" . PHP_EOL . PHP_EOL . $orderEmailPlaintext . PHP_EOL;
         /***********************************************
         				[set html email vars]
         			************************************************/
         $this->orderMessage['html']['customer_details'] = $wppizzaEmailCustomerDetails;
         $this->orderMessage['html']['order_items'] = $wppizzaEmailOrderItems;
         $this->orderMessage['html']['order_summary'] = $wppizzaEmailOrderSummary;
         /***********************************************
         				[customer and order details to be saved in db and displayed in history]
         			************************************************/
         $this->customerDetails = esc_sql($emailPlaintext['customer_details']);
         $this->orderDetails = esc_sql(PHP_EOL . $emailPlaintext['db_items'] . PHP_EOL . $emailPlaintext['order_summary'] . PHP_EOL);
         /***********************************************************
         				[set name and email of the the person that is ordering]
         			***********************************************************/
         $recipientName = !empty($cDetails['cname']) ? wppizza_validate_string($cDetails['cname']) : '';
         $fromEmails = !empty($cDetails['cemail']) ? wppizza_validate_email_array($cDetails['cemail']) : '';
         $this->orderClientName = wppizza_email_decode_entities($recipientName, $this->blogCharset) . '';
         $this->orderClientEmail = !empty($fromEmails[0]) ? $fromEmails[0] : '';
         /***********************************************
         				[overwrite subject vars for email subject]
         			************************************************/
         if (file_exists($this->pluginTemplateDir . '/wppizza-order-email-subject.php')) {
             /**copy to template directory to keep settings**/
             include $this->pluginTemplateDir . '/wppizza-order-email-subject.php';
         } else {
             include WPPIZZA_PATH . 'templates/wppizza-order-email-subject.php';
         }
         /**update db entry with the current time timestamp of when the order was actually send**/
         $orderDate = date('Y-m-d H:i:s', $this->currentTime);
         /**add timestamp to order_ini**/
         $oIni['time'] = $this->currentTime;
         $wpdb->query("UPDATE " . $wpdb->prefix . $this->pluginOrderTable . " SET order_date='" . $orderDate . "',order_ini='" . esc_sql(serialize($oIni)) . "' WHERE id='" . $orderid . "' ");
     }
     return;
 }
    /*makes keys == values*/
    $options['order']['discount_calculation_exclude_cat'] = !empty($input['order']['discount_calculation_exclude_cat']) ? array_combine($input['order']['discount_calculation_exclude_cat'], $input['order']['discount_calculation_exclude_cat']) : array();
    /*makes keys == values*/
    $options['order']['item_tax'] = wppizza_validate_float_pc($input['order']['item_tax'], 5);
    //5 decimals should really be enough i would have thought
    $options['order']['item_tax_alt'] = wppizza_validate_float_pc($input['order']['item_tax_alt'], 5);
    //5 decimals should really be enough i would have thought
    $options['order']['taxes_included'] = !empty($input['order']['taxes_included']) ? true : false;
    $options['order']['shipping_tax'] = !empty($input['order']['shipping_tax']) ? true : false;
    $options['order']['shipping_tax_rate'] = wppizza_validate_float_pc($input['order']['shipping_tax_rate'], 5);
    //5 decimals should really be enough i would have thought
    $options['order']['append_internal_id_to_transaction_id'] = !empty($input['order']['append_internal_id_to_transaction_id']) ? true : false;
    $options['order']['order_email_to'] = wppizza_validate_email_array($input['order']['order_email_to']);
    $options['order']['order_email_bcc'] = wppizza_validate_email_array($input['order']['order_email_bcc']);
    $options['order']['order_email_attachments'] = wppizza_strtoarray($input['order']['order_email_attachments']);
    $emailFrom = wppizza_validate_email_array($input['order']['order_email_from']);
    /*validated as array but we only store the first value as string*/
    $options['order']['order_email_from'] = !empty($emailFrom[0]) ? '' . $emailFrom[0] . '' : '';
    $options['order']['order_email_from_name'] = wppizza_validate_string($input['order']['order_email_from_name']);
    /**dmarc nag**/
    $options['order']['dmarc_nag_off'] = !empty($input['order']['dmarc_nag_off']) ? true : false;
}
/**validate order form***/
if (isset($_POST['' . $this->pluginSlug . '_order_form'])) {
    foreach ($input['order_form'] as $a => $b) {
        $options['order_form'][$a]['sort'] = (int) $input['order_form'][$a]['sort'];
        $options['order_form'][$a]['key'] = $options['order_form'][$a]['key'];
        $options['order_form'][$a]['lbl'] = wppizza_validate_string($input['order_form'][$a]['lbl']);
        $options['order_form'][$a]['type'] = wppizza_validate_letters_only($input['order_form'][$a]['type']);
        $options['order_form'][$a]['enabled'] = !empty($input['order_form'][$a]['enabled']) ? true : false;
        $options['order_form'][$a]['required'] = !empty($input['order_form'][$a]['required']) ? true : false;