-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevangelical-magazine-gocardless-filter.php
More file actions
143 lines (136 loc) · 5.22 KB
/
evangelical-magazine-gocardless-filter.php
File metadata and controls
143 lines (136 loc) · 5.22 KB
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
138
139
140
141
142
143
<?php
/**
Plugin Name: Evangelical Magazine GoCardless Filter
Description: Modifications for the Fat Beehive GoCardless Gravity Forms Plugin
Plugin URI: http://www.evangelicalmagazine.com/
Version: 0.1
Author: Mark Barnes
Author URI: http://www.markbarnes.net/
*/
add_filter ('fb_gf_gocardless_action', 'mb_em_filter_gocardless_action', 10, 3);
add_filter ('fb_gf_gocardless_subscription_name', 'mb_em_filter_gocardless_subscription_name', 10, 3);
add_filter ('fb_gf_gocardless_subscription_interval', 'mb_em_filter_gocardless_subscription_interval', 10, 3);
add_filter ('fb_gf_gocardless_subscription_interval_unit', 'mb_em_filter_gocardless_subscription_interval_unit', 10, 3);
add_filter ('gform_addon_pre_process_feeds', 'mb_em_redirect_to_correct_payment_processor', 10, 3);
/**
* Filters the GoCardless action
*
* Changes the payment type to 'subscription' if one of two hidden fields are found.
*
* @param string $action - the existing action
* @param array $entry - the entry submitted by the user
* @param array $form - the form used for this submission
* @return string - the revised action
*/
function mb_em_filter_gocardless_action ($action, $entry, $form) {
if (isset ($form['fields'])) {
foreach ($form ['fields'] as $f) {
if ($f->label == 'gocardless_subscription_interval' || $f->label == 'gocardless_subscription_interval_unit') {
return 'subscription';
}
}
}
return $action;
}
/**
* Filters the GoCardless subscription name
*
* Supplies the GoCardless subscription name for clarity.
*
* @param string $name - the existing name
* @param array $entry - the entry submitted by the user
* @param array $form - the form used for this submission
* @return string - the revised name
*/
function mb_em_filter_gocardless_subscription_name ($name, $entry, $form) {
if (isset ($form['fields'])) {
$name = '';
foreach ($form ['fields'] as $f) {
if (strtolower($f->label) == 'quantity' ) {
$name = $entry[$f->id].' '.($entry[$f->id] > 1 ? 'copies' : 'copy').$name;
}
if (strtolower($f->label) == 'subscription type' ) {
$name = $name.', '.trim(substr($entry[$f->id],0,strcspn ($entry[$f->id], '-–—')));
}
}
}
return 'Evangelical Magazine ('.$name.')';
}
/**
* Filters the GoCardless subscription interval
*
* @param int $interval - the existing interval
* @param array $entry - the entry submitted by the user
* @param array $form - the form used for this submission
* @return int - the revised interval
*/
function mb_em_filter_gocardless_subscription_interval ($interval, $entry, $form) {
if (isset ($form['fields'])) {
foreach ($form ['fields'] as $f) {
if ($f->label == 'gocardless_subscription_interval' ) {
return $entry[$f->id];
}
}
}
return $interval;
}
/**
* Filters the GoCardless subscription interval unit
*
* Will be one of 'yearly', 'monthly', or 'weekly'
*
* @param string $interval_unit - the existing interval unit
* @param array $entry - the entry submitted by the user
* @param array $form - the form used for this submission
* @return string - the revised interval unit
*/
function mb_em_filter_gocardless_subscription_interval_unit ($interval_unit, $entry, $form) {
if (isset ($form['fields'])) {
foreach ($form ['fields'] as $f) {
if ($f->label == 'gocardless_subscription_interval_unit' ) {
return $entry[$f->id];
}
}
}
return $interval_unit;
}
/**
* Ensures the correct payment processor is used
*
* Checks the form fields to see whether direct debit or credit card is selected as the payment type, then redirects as appropriate.
* Filters gform_addon_pre_process_feeds
*
* @param array $feeds - an array of payment feeds assigned to this form
* @param array $entry - the entry submitted by the user
* @param array $form - the form used for this submission
* @return array - the revised feed list
*/
function mb_em_redirect_to_correct_payment_processor ($feeds, $entry, $form) {
if (isset ($form['fields'])) {
// First, check the entry to determine which payment type has been chosen
foreach ($form ['fields'] as $f) {
if (strtolower($f->label) == 'subscription type' ) {
$payment_type = strpos(strtolower($entry[$f->id]), 'credit card') !== FALSE ? 'credit card' : 'direct debit';
break;
}
}
// If necessary, remove the other payment types from the feed
if (isset($payment_type)) {
foreach ($feeds as $k => $feed) {
if ($payment_type == 'credit card' && $feed['addon_slug'] == 'gravityformsfbgfgocardlesshosted') {
unset($feeds[$k]);
} elseif ($payment_type == 'direct debit' && in_array($feed['addon_slug'], array ('gravityformspaypal', 'gravityformspaypalpaymentspro', 'gravityformsstripe', 'gravityformsauthorizenet'))) {
unset($feeds[$k]);
}
}
// The PayPalPro plugin doesn't use the standard methods, so instead we have to remove the filters it has added.
if ($payment_type == 'direct debit') {
remove_filter('gform_validation',array("GFPayPalPro", "paypalpro_validation"), 1000, 4);
remove_filter("gform_confirmation", array("GFPayPalPro", "start_express_checkout"), 1000, 4);
remove_action('gform_after_submission',array("GFPayPalPro", "paypalpro_after_submission"), 10, 2);
remove_filter("gform_get_form_filter", array("GFPayPalPro", "maybe_confirm_express_checkout"));
}
}
}
return $feeds;
}