-
Notifications
You must be signed in to change notification settings - Fork 192
/
Utils.php
253 lines (225 loc) · 6.4 KB
/
Utils.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace Give\Helpers\Form;
use Give\Controller\Form;
use Give\Helpers\Form\Template\Utils\Frontend;
use Give\Helpers\Utils as GlobalUtils;
class Utils
{
/**
* Get result if we are viewing embed form or not
*
* @since 2.7.0
* @return bool
*/
public static function isViewingForm()
{
$base = Give()->routeForm->getBase();
return (
$base === get_query_var('url_prefix') ||
(wp_doing_ajax() && false !== strpos(wp_get_referer(), "/{$base}/")) // for ajax
);
}
/**
* Get result if we are processing embed form or not
*
* @since 2.7.0
* @return bool
*/
public static function isProcessingForm()
{
$base = Give()->routeForm->getBase();
$formName = get_post_field('post_name', Frontend::getFormId());
$referer = trailingslashit(wp_get_referer()) ?: '';
return ! empty($_REQUEST['give_embed_form']) ||
false !== strpos($referer, "/{$base}/{$formName}/") ||
self::inIframe() ||
false !== strpos($referer, 'giveDonationFormInIframe');
}
/**
* Get result whether or not performing Give core action on ajax or not.
*
* @since 2.7.0
* @return bool
*/
public static function isProcessingGiveActionOnAjax()
{
$action = isset($_REQUEST['action']) ? give_clean($_REQUEST['action']) : '';
$whiteListedAction = ['get_receipt'];
return $action && wp_doing_ajax() && (0 === strpos($action, 'give_') || in_array(
$action,
$whiteListedAction,
true
));
}
/**
* Get result whether or not show failed transaction error.
*
* @since 2.7.0
* @return bool
*/
public static function canShowFailedDonationError()
{
return ! empty($_REQUEST['showFailedDonationError']);
}
/**
* This function check whether or not given url is of iframe parent failed page.
*
* @since 2.7.0
*
* @param string $url
*
* @return string
*/
public static function isIframeParentFailedPageURL($url)
{
$action = GlobalUtils::getQueryParamFromURL($url, 'giveDonationAction');
return $action && 'failedDonation' === $action;
}
/**
* This function check whether or not given url is of iframe parent success page.
*
* @since 2.7.0
*
* @param string $url
*
* @return string
*/
public static function isIframeParentSuccessPageURL($url)
{
$action = GlobalUtils::getQueryParamFromURL($url, 'giveDonationAction');
return $action && 'showReceipt' === $action;
}
/**
* This function will create success page URL.
*
* Note: this function is use to get success page url for parent page when perform donation with off-site checkout.
*
* @since 2.7.0
*
* @param array $args
* @param string|null $url
*
* @return string
*/
public static function createSuccessPageURL($url, $args = [])
{
$args = array_merge($args, ['giveDonationAction' => 'showReceipt']);
return esc_url_raw(add_query_arg(
$args,
$url
));
}
/**
* Get Iframe parent page URL.
*
* Note: must be use only inside iframe logic.
*
* @since 2.7.0
*/
public static function getIframeParentURL()
{
return isset($_REQUEST['give-current-url']) ? give_clean($_REQUEST['give-current-url']) : '';
}
/**
* Return legacy failed page url.
*
* Wrapper function for give_get_failed_transaction_uri and without embed form filter
*
* @since 2.7.0
* @return string
*/
public static function getLegacyFailedPageURL()
{
remove_filter('give_get_failed_transaction_uri', [Form::class, 'editFailedPageURI']);
$url = give_get_failed_transaction_uri();
add_filter('give_get_failed_transaction_uri', [Form::class, 'editFailedPageURI']);
return $url;
}
/**
* Return success page url.
*
* Wrapper function for give_get_success_page_uri and without embed form filter.
*
* @since 2.7.0
* @return string
*/
public static function getSuccessPageURL()
{
remove_filter('give_get_success_page_uri', [Form::class, 'editSuccessPageURI']);
$url = give_get_success_page_uri();
add_filter('give_get_success_page_uri', [Form::class, 'editSuccessPageURI']);
return $url;
}
/**
* Get result if we are viewing embed form receipt or not
*
* @since 2.7.0
* @return bool
*/
public static function isViewingFormReceipt()
{
return give_is_success_page();
}
/**
* This function will create failed transaction page URL.
*
* Note: this function is use to get failed page url for parent page when perform donation with off-site checkout.
*
* @since 2.7.0
*
* @param array $args
* @param string|null $url
*
* @return string
*/
public static function createFailedPageURL($url, $args = [])
{
$args = array_merge($args, ['giveDonationAction' => 'failedDonation']);
return esc_url_raw(add_query_arg(
$args,
$url
));
}
/**
* Return if current URL loading in iframe or not.
*
* @since 2.7.0
* @return bool
*/
public static function inIframe()
{
return ! empty($_GET['giveDonationFormInIframe']);
}
/**
* Returns whether or not the given form uses the legacy form template
*
* @since 2.7.0
*
* @param int|null $formID
*
* @return bool
*/
public static function isLegacyForm($formID = null)
{
$formID = $formID ?: Frontend::getFormId();
$formTemplate = Template::getActiveID($formID);
return ! $formTemplate || 'legacy' === Template::getActiveID($formID);
}
/**
* Return whether or not disable donate now button.
*
* @since 2.7.0
* @return bool
*/
public static function canDisableDonationNowButton()
{
return ! empty($_GET['giveDisableDonateNowButton']);
}
/**
* @since 3.0.0
*/
public static function isV3Form(int $formId): bool
{
return (bool)give()->form_meta->get_meta($formId, 'formBuilderSettings', true);
}
}