-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapppresser-givewp.php
137 lines (107 loc) · 3.85 KB
/
apppresser-givewp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
Plugin Name: GiveWP Integration for AppPresser
Plugin URI: https://apppresser.com
Description: Auto-populates the donation form when linked to from the app.
Version: 1.0.0
Author: AppPresser Team
Author URI: https://apppresser.com
License: GPLv2
*/
defined( 'ABSPATH' ) || exit;
if( !class_exists( 'AppPresser_GiveWP' ) ) {
/**
* Main AppPresser_GiveWP class
*
* @since 0.1.0
*/
class AppPresser_GiveWP {
/**
* @var AppPresser_GiveWP $instance The one true AppPresser_GiveWP
* @since 0.1.0
*/
private static $instance;
/**
* Get active instance
*
* @access public
* @since 0.1.0
* @return self
*/
public static function instance() {
if( !self::$instance ) {
self::$instance = new AppPresser_GiveWP();
self::$instance->hooks();
}
return self::$instance;
}
public function hooks() {
// Hooking into the single form view.
add_action( 'give_post_form_output', array( $this, 'give_populate_amount_name_email' ) );
}
/**
* AUTO-POPULATE AMOUNT, NAME, and EMAIL FROM URL STRING
*
* This jQuery snippet will auto-populate the Give form amount,
* first and last name, and email address from a URL you provide
* EXAMPLE: https://example.com/donations/give-form/?amount=46.00&first=Peter&last=Joseph&[email protected]
*
* CAVEATS:
* -- Your form must support custom amounts
* -- This snippet only supports one form per page as-is
*/
public function give_populate_amount_name_email() {
?>
<script>
// use an enclosure so we don't pollute the global space
(function(window, document, $, undefined){
'use strict';
var giveCustom = {};
giveCustom.init = function() {
// Get the amount from the URL
var getamount = giveCustom.getQueryVariable("amount");
var amount = '1.00';
// Set fallback in case URL variable isn't set
if ( getamount !== false ) {
amount = getamount;
}
var firstname = ( giveCustom.getQueryVariable("first") !== false ) ? decodeURIComponent(giveCustom.getQueryVariable("first")) : '';
var lastname = ( giveCustom.getQueryVariable("last") !== false ) ? decodeURIComponent(giveCustom.getQueryVariable("last")) : '';
var email = ( giveCustom.getQueryVariable("email") !== false ) ? decodeURIComponent(giveCustom.getQueryVariable("email")) : '';
// Populate the amount field, then update the total
if ( $('#give-amount').length > 0 ) {
$('#give-amount')
.attr('value', amount).attr('data-amount', amount);
$('#give-final-total-wrap .give-final-total-amount').attr('data-total', amount ).text(amount);
}
if ( firstname !== false && $('#give-first-name-wrap input.give-input').length > 0 ) {
$('#give-first-name-wrap input.give-input')
.val(firstname);
}
if ( lastname !== false && $('#give-last-name-wrap input.give-input').length > 0 ) {
$('#give-last-name-wrap input.give-input')
.val(lastname);
}
if ( email !== false && $('#give-email-wrap input.give-input').length > 0 ) {
$('#give-email-wrap input.give-input')
.val(email);
}
}
giveCustom.getQueryVariable = function( variable ) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
giveCustom.init();
})(window, document, jQuery);
</script>
<?php
}
} // end class
$appp_givewp = new AppPresser_GiveWP();
$appp_givewp->instance();
} // end if