/**
 * Creates a random 15 digit giftcard number
 *
 */
function rpgc_create_number($data, $postarr)
{
    $regen = "no";
    if (isset($_POST["rpgc_regen_number"])) {
        $regen = $_POST["rpgc_regen_number"];
    }
    if ($data['post_type'] == 'rp_shop_giftcard' && $data['post_name'] == '') {
        if (!isset($_POST['original_publish']) || $regen == "yes") {
            $myNumber = rpgc_generate_number();
            $data["post_title"] = $myNumber;
            $data["post_name"] = $myNumber;
        }
    }
    return apply_filters('rpgc_create_number', $data);
}
 /**
  * Save the meta when the post is saved.
  *
  * @param int $post_id The ID of the post being saved.
  */
 public function save($post_id)
 {
     global $post, $wpdb;
     /*
      * We need to verify this came from the our screen and with proper authorization,
      * because save_post can be triggered at other times.
      */
     // Check if our nonce is set.
     if (!isset($_POST['woocommerce_giftcard_nonce'])) {
         return $post_id;
     }
     $nonce = $_POST['woocommerce_giftcard_nonce'];
     // Verify that the nonce is valid.
     if (!wp_verify_nonce($nonce, 'woocommerce_save_data')) {
         return $post_id;
     }
     // If this is an autosave, our form has not been submitted,
     //     so we don't want to do anything.
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
         return $post_id;
     }
     // Check the user's permissions.
     if ('page' == $_POST['post_type']) {
         if (!current_user_can('edit_page', $post_id)) {
             return $post_id;
         }
     } else {
         if (!current_user_can('edit_post', $post_id)) {
             return $post_id;
         }
     }
     /* OK, its safe for us to save the data now. */
     $description = '';
     $to = '';
     $toEmail = '';
     $from = '';
     $fromEmail = '';
     $sendto_from = '';
     $sendautomaticly = '';
     $amount = '';
     $balance = '';
     $note = '';
     $expiry_date = '';
     $sendTheEmail = 0;
     //var_dump( $post );
     //die();
     // Ensure gift card code is correctly formatted
     //$wpdb->update( $wpdb->posts, array( 'post_title' => $post->post_title ), array( 'ID' => $post_id ) );
     /*if ( wpr_get_giftcard_by_code( $post->post_title ) ) {
     			$newNumber = apply_filters( 'rpgc_regen_number', rpgc_generate_number());
     
     			$wpdb->update( $wpdb->posts, array( 'post_title' => $newNumber ), array( 'ID' => $post_id ) );
     			$wpdb->update( $wpdb->posts, array( 'post_name' => $newNumber ), array( 'ID' => $post_id ) );
     		}*/
     if (isset($_POST['rpgc_description'])) {
         $description = woocommerce_clean($_POST['rpgc_description']);
         update_post_meta($post_id, 'rpgc_description', $description);
     }
     if (isset($_POST['rpgc_to'])) {
         $to = woocommerce_clean($_POST['rpgc_to']);
         update_post_meta($post_id, 'rpgc_to', $to);
     }
     if (isset($_POST['rpgc_email_to'])) {
         $toEmail = woocommerce_clean($_POST['rpgc_email_to']);
         update_post_meta($post_id, 'rpgc_email_to', $toEmail);
     }
     if (isset($_POST['rpgc_from'])) {
         $from = woocommerce_clean($_POST['rpgc_from']);
         update_post_meta($post_id, 'rpgc_from', $from);
     }
     if (isset($_POST['rpgc_email_from'])) {
         $fromEmail = woocommerce_clean($_POST['rpgc_email_from']);
         update_post_meta($post_id, 'rpgc_email_from', $fromEmail);
     }
     if (isset($_POST['rpgc_amount'])) {
         $amount = woocommerce_clean($_POST['rpgc_amount']);
         update_post_meta($post_id, 'rpgc_amount', $amount);
         if (!isset($_POST['rpgc_balance'])) {
             $balance = woocommerce_clean($_POST['rpgc_amount']);
             update_post_meta($post_id, 'rpgc_balance', $balance);
             $sendTheEmail = 1;
         }
     }
     if (isset($_POST['rpgc_balance'])) {
         $balance = woocommerce_clean($_POST['rpgc_balance']);
         update_post_meta($post_id, 'rpgc_balance', $balance);
     }
     if (isset($_POST['rpgc_note'])) {
         $note = woocommerce_clean($_POST['rpgc_note']);
         update_post_meta($post_id, 'rpgc_note', $note);
     }
     if (isset($_POST['rpgc_expiry_date'])) {
         $expiry_date = woocommerce_clean($_POST['rpgc_expiry_date']);
         update_post_meta($post_id, 'rpgc_expiry_date', $expiry_date);
     } else {
         $expiry_date = '';
     }
     if (isset($_POST['rpgc_regen_number'])) {
         if ($_POST['rpgc_regen_number'] == 'yes') {
             $newNumber = apply_filters('rpgc_regen_number', rpgc_generate_number());
             $wpdb->update($wpdb->posts, array('post_title' => $newNumber), array('ID' => $post_id));
             $wpdb->update($wpdb->posts, array('post_name' => $newNumber), array('ID' => $post_id));
         }
     }
     if ($sendTheEmail == 1 && $balance != 0 || isset($_POST['rpgc_resend_email'])) {
         $email = new WPR_Giftcard_Email();
         $post = get_post($post_id);
         $email->sendEmail($post);
         update_post_meta($post_id, 'rpgc_email_sent', "true");
     }
     /* Deprecated - same hook name as in the meta */
     do_action('woocommerce_rpgc_options');
     do_action('woocommerce_rpgc_options_save');
 }