/**
 * Get Magic Tag Conditional Data
 * 
 * @description Example function that returns Custom field data if present, for any form ID
 * @param $payment_id
 *
 * @return string|void
 */
function rum_wohh_get_wohh_magic_tag_data($payment_id)
{
    $form_id = give_get_payment_form_id($payment_id);
    $payment_meta = give_get_payment_meta($payment_id);
    $meta_vals = get_post_custom($payment_id);
    $output = '';
    // Check if this payment's donation form ID matches the donation form we want custom email body copy
    if ($form_id == '1650') {
        // Online Donation form
        $donation_reason = '';
        // get the custom field data for this donation
        if (isset($meta_vals['donation_reason'])) {
            $donation_reason = $meta_vals['donation_reason'][0];
        }
        if ($donation_reason != '') {
            $output = '<strong>Reason for Donation:</strong> ' . $donation_reason;
        }
    }
    // Check if this payment's donation form ID matches the donation form we want custom email body copy
    if ($form_id == '1865') {
        // Annual Memorial Walk Butterfly Campaign
        $in_memory_of = '';
        // get the custom field data for this donation
        if (isset($meta_vals['my_donation_is_in_memory_of'])) {
            $in_memory_of = $meta_vals['my_donation_is_in_memory_of'][0];
        }
        if ($in_memory_of != '') {
            $output = '<strong>In Memory Of:</strong> ' . $in_memory_of;
        }
    }
    return $output;
}
/**
 * Customize Give's Email Content Per Donation Form
 *
 * Use this snippet to update the donation email's main content.
 *
 * @param $email_body
 * @param $payment_id
 * @param $payment_data
 *
 * @return string
 */
function my_custom_give_email_content($email_body, $payment_id, $payment_data)
{
    $form_id = give_get_payment_form_id($payment_id);
    //Check if this payment's donation form ID matches the donation form we want custom email body copy
    if ($form_id == '1150') {
        //Here you can output custom content or pull from custom post meta using get_post_meta, or use an ACF field, WordPress functions, etc.
        //You can also use Give's email tags like {price}, {fullname}, etc.
        $email_body = '<p>Hi {name},</p>';
        $email_body .= '<p>Thank you for your donation to . ' . get_the_title($form_id) . '. This is my custom content!</p>';
        $email_body .= '<p>We can add anything here we like...</p>';
        $email_body .= '<p>Even our custom email tags: {receipt_link}</p>';
        //Make sure we return the new $email_body;
        return $email_body;
    } elseif ($form_id == '460') {
        //Here you can output custom content or pull from custom post meta using get_post_meta, or use an ACF field, WordPress functions, etc.
        //You can also use Give's email tags like {price}, {fullname}, etc.
        $email_body = '<p>Hi {name},</p>';
        $email_body .= '<p>Thank you for your donation to . ' . get_the_title($form_id) . '. This is some different custom content!</p>';
        $email_body .= '<p>Some different content...</p>';
        $email_body .= '<p>Even our custom email tags: {receipt_link}</p>';
        //Make sure we return the new $email_body;
        return $email_body;
    } else {
        //Be sure to return default email body if we don't make a match
        return $email_body;
    }
}
function per_form_email_notifications($payment_id)
{
    $emails = isset($give_options['admin_notice_emails']) && strlen(trim($give_options['admin_notice_emails'])) > 0 ? $give_options['admin_notice_emails'] : get_bloginfo('admin_email');
    $emails = array_map('trim', explode("\n", $emails));
    $form_id = give_get_payment_form_id($payment_id);
    if ($form_id == '5183') {
        $emails = '*****@*****.**';
    }
    return $emails;
}
Example #4
0
/**
 * Deletes a Donation
 *
 * @since 1.0
 * @global    $give_logs
 * @uses  Give_Logging::delete_logs()
 *
 * @param int $payment_id Payment ID (default: 0)
 *
 * @return void
 */
function give_delete_purchase($payment_id = 0)
{
    global $give_logs;
    $post = get_post($payment_id);
    if (!$post) {
        return;
    }
    $form_id = give_get_payment_form_id($payment_id);
    give_undo_purchase($form_id, $payment_id);
    $amount = give_get_payment_amount($payment_id);
    $status = $post->post_status;
    $donor_id = give_get_payment_customer_id($payment_id);
    if ($status == 'revoked' || $status == 'publish') {
        // Only decrease earnings if they haven't already been decreased (or were never increased for this payment)
        give_decrease_total_earnings($amount);
        // Clear the This Month earnings (this_monththis_month is NOT a typo)
        delete_transient(md5('give_earnings_this_monththis_month'));
        if ($donor_id) {
            // Decrement the stats for the donor
            Give()->customers->decrement_stats($donor_id, $amount);
        }
    }
    do_action('give_payment_delete', $payment_id);
    if ($donor_id) {
        // Remove the payment ID from the donor
        Give()->customers->remove_payment($donor_id, $payment_id);
    }
    // Remove the payment
    wp_delete_post($payment_id, true);
    // Remove related sale log entries
    $give_logs->delete_logs(null, 'sale', array(array('key' => '_give_log_payment_id', 'value' => $payment_id)));
    do_action('give_payment_deleted', $payment_id);
}
Example #5
0
/**
 *
 * Process the payment details edit
 *
 * @access      private
 *
 * @param $data
 *
 * @since       1.0
 * @return      void
 *
 */
function give_update_payment_details($data)
{
    if (!current_user_can('edit_give_payments', $data['give_payment_id'])) {
        wp_die(__('You do not have permission to edit this payment record', 'give'), __('Error', 'give'), array('response' => 403));
    }
    check_admin_referer('give_update_payment_details_nonce');
    // Retrieve the payment ID
    $payment_id = absint($data['give_payment_id']);
    // Retrieve existing payment meta
    $meta = give_get_payment_meta($payment_id);
    $user_info = give_get_payment_meta_user_info($payment_id);
    $status = $data['give-payment-status'];
    $user_id = isset($data['give-payment-user-id']) ? intval($data['give-payment-user-id']) : '';
    $date = sanitize_text_field($data['give-payment-date']);
    $hour = sanitize_text_field($data['give-payment-time-hour']);
    $form_id = give_get_payment_form_id($payment_id);
    // Restrict to our high and low
    if ($hour > 23) {
        $hour = 23;
    } elseif ($hour < 0) {
        $hour = 00;
    }
    $minute = sanitize_text_field($data['give-payment-time-min']);
    // Restrict to our high and low
    if ($minute > 59) {
        $minute = 59;
    } elseif ($minute < 0) {
        $minute = 00;
    }
    $address = array_map('trim', $data['give-payment-address'][0]);
    $date = date('Y-m-d', strtotime($date)) . ' ' . $hour . ':' . $minute . ':00';
    $curr_total = give_sanitize_amount(give_get_payment_amount($payment_id));
    $new_total = give_sanitize_amount($_POST['give-payment-total']);
    $curr_customer_id = sanitize_text_field($data['give-current-customer']);
    $new_customer_id = sanitize_text_field($data['customer-id']);
    do_action('give_update_edited_purchase', $payment_id);
    // Update main payment record
    $updated = wp_update_post(array('ID' => $payment_id, 'edit_date' => true, 'post_date' => $date));
    if (0 === $updated) {
        wp_die(esc_attr__('Error Updating Payment', 'give'), esc_attr__('Error', 'give'), array('response' => 400));
    }
    $customer_changed = false;
    if (isset($data['give-new-customer']) && $data['give-new-customer'] == '1') {
        $email = isset($data['give-new-customer-email']) ? sanitize_text_field($data['give-new-customer-email']) : '';
        $names = isset($data['give-new-customer-name']) ? sanitize_text_field($data['give-new-customer-name']) : '';
        if (empty($email) || empty($names)) {
            wp_die(esc_attr__('New Customers require a name and email address', 'give'));
        }
        $customer = new Give_Customer($email);
        if (empty($customer->id)) {
            $customer_data = array('name' => $names, 'email' => $email);
            $user_id = email_exists($email);
            if (false !== $user_id) {
                $customer_data['user_id'] = $user_id;
            }
            if (!$customer->create($customer_data)) {
                // Failed to crete the new customer, assume the previous customer
                $customer_changed = false;
                $customer = new Give_Customer($curr_customer_id);
                give_set_error('give-payment-new-customer-fail', __('Error creating new customer', 'give'));
            }
        }
        $new_customer_id = $customer->id;
        $previous_customer = new Give_Customer($curr_customer_id);
        $customer_changed = true;
    } elseif ($curr_customer_id !== $new_customer_id) {
        $customer = new Give_Customer($new_customer_id);
        $email = $customer->email;
        $names = $customer->name;
        $previous_customer = new Give_Customer($curr_customer_id);
        $customer_changed = true;
    } else {
        $customer = new Give_Customer($curr_customer_id);
        $email = $customer->email;
        $names = $customer->name;
    }
    // Setup first and last name from input values
    $names = explode(' ', $names);
    $first_name = !empty($names[0]) ? $names[0] : '';
    $last_name = '';
    if (!empty($names[1])) {
        unset($names[0]);
        $last_name = implode(' ', $names);
    }
    if ($customer_changed) {
        // Remove the stats and payment from the previous customer and attach it to the new customer
        $previous_customer->remove_payment($payment_id, false);
        $customer->attach_payment($payment_id, false);
        // If purchase was completed and not ever refunded, adjust stats of customers
        if ('revoked' == $status || 'publish' == $status) {
            $previous_customer->decrease_purchase_count();
            $previous_customer->decrease_value($new_total);
            $customer->increase_purchase_count();
            $customer->increase_value($new_total);
        }
        update_post_meta($payment_id, '_give_payment_customer_id', $customer->id);
    }
    // Set new meta values
    $user_info['id'] = $customer->user_id;
    $user_info['email'] = $customer->email;
    $user_info['first_name'] = $first_name;
    $user_info['last_name'] = $last_name;
    $user_info['address'] = $address;
    $meta['user_info'] = $user_info;
    // Check for payment notes
    if (!empty($data['give-payment-note'])) {
        $note = wp_kses($data['give-payment-note'], array());
        give_insert_payment_note($payment_id, $note);
    }
    // Set new status
    give_update_payment_status($payment_id, $status);
    give_update_payment_meta($payment_id, '_give_payment_user_id', $customer->user_id);
    give_update_payment_meta($payment_id, '_give_payment_user_email', $customer->email);
    give_update_payment_meta($payment_id, '_give_payment_meta', $meta);
    give_update_payment_meta($payment_id, '_give_payment_total', $new_total);
    // Adjust total store earnings if the payment total has been changed
    if ($new_total !== $curr_total && ('publish' == $status || 'revoked' == $status)) {
        if ($new_total > $curr_total) {
            // Increase if our new total is higher
            $difference = $new_total - $curr_total;
            give_increase_total_earnings($difference);
            $form = new Give_Donate_Form($form_id);
            $form->increase_earnings($difference);
        } elseif ($curr_total > $new_total) {
            // Decrease if our new total is lower
            $difference = $curr_total - $new_total;
            give_decrease_total_earnings($difference);
            $form = new Give_Donate_Form($form_id);
            $form->decrease_earnings($difference);
        }
    }
    do_action('give_updated_edited_purchase', $payment_id);
    wp_safe_redirect(admin_url('edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&give-message=payment-updated&id=' . $payment_id));
    exit;
}