PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.6.2
GiveWP – Donation Plugin and Fundraising Platform v3.6.2
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / Controller / Form.php
give / src / Controller Last commit date
Form.php 4 years ago PayPalWebhooks.php 3 years ago
Form.php
411 lines
1 <?php
2
3 /**
4 * Handle Embed Donation Form Route
5 *
6 * @package Give/Coontroller
7 * @since 2.7.0
8 */
9
10 namespace Give\Controller;
11
12 use Give\Form\LoadTemplate;
13 use Give\Form\Template;
14 use Give\Helpers\Form\Template as FormTemplateUtils;
15 use Give\Helpers\Form\Template\Utils\Frontend as FrontendFormTemplateUtils;
16 use Give\Helpers\Form\Utils as FormUtils;
17 use Give\Helpers\Frontend\ConfirmDonation;
18 use Give\Helpers\Frontend\Shortcode as ShortcodeUtils;
19 use Give\Helpers\Utils;
20 use Give\Session\SessionDonation\DonationAccessor;
21 use Give_Notices;
22 use WP_Post;
23
24 defined('ABSPATH') || exit;
25
26 /**
27 * Form class.
28 *
29 * @since 2.7.0
30 */
31 class Form
32 {
33
34 /**
35 * Initialize
36 *
37 * @since 2.7.0
38 */
39 public function init()
40 {
41 add_action('wp', [$this, 'loadTemplateOnFrontend'], 11, 0);
42 add_action('admin_init', [$this, 'loadTemplateOnAjaxRequest']);
43 add_action('init', [$this, 'embedFormRedirectURIHandler'], 1);
44 add_action('template_redirect', [$this, 'loadReceiptView'], 1);
45 add_action('give_before_single_form_summary', [$this, 'handleSingleDonationFormPage'], 0);
46 }
47
48 /**
49 * Load form template on frontend.
50 *
51 * @since 2.7.0
52 */
53 public function loadTemplateOnFrontend()
54 {
55 if (FormUtils::isProcessingForm()) {
56 $this->loadTemplate();
57
58 add_action('template_redirect', [$this, 'loadDonationFormView'], 1);
59 }
60 }
61
62 /**
63 * Load receipt view.
64 *
65 * @since 2.7.0
66 */
67 public function loadReceiptView()
68 {
69 // Do not handle legacy donation form.
70 if (FormUtils::isLegacyForm()) {
71 return;
72 }
73
74 // Handle success page.
75 if (FormUtils::isViewingFormReceipt() && ! FormUtils::isLegacyForm()) {
76 /* @var Template $formTemplate */
77 $formTemplate = Give()->templates->getTemplate();
78
79 if (FormUtils::inIframe() || ($formTemplate->openSuccessPageInIframe && FormUtils::isProcessingForm())) {
80 // Set header.
81 nocache_headers();
82 header('HTTP/1.1 200 OK');
83
84 // Show donation processing template.
85 if (ConfirmDonation::isConfirming()) {
86 $session = new DonationAccessor();
87 $donationId = $session->getDonationId();
88
89 /**
90 * Fire the action hook.
91 *
92 * If developer wants to verify payment before showing receipt then use `give_handle_donation_confirm` action hook to verify donation.
93 * Developer can access query parameters return by payment gateway. for example
94 * $session = new DonationAccessor();
95 * $session->getByKey( "postDataFor{$paymentGatewayId}" )
96 *
97 * @since 2.7.0
98 *
99 * @param int $donationId
100 */
101 do_action('give_handle_donation_confirmation', $donationId);
102
103 ConfirmDonation::removePostedDataFromDonationSession();
104
105 // Load payment processing view only if donation is in pending status.
106 if ('pending' === get_post_status($donationId)) {
107 include GIVE_PLUGIN_DIR . 'src/Views/Form/defaultFormDonationProcessing.php';
108 exit();
109 }
110 }
111
112 // Render receipt with in iframe.
113 include $formTemplate->getReceiptView();
114 exit();
115 }
116
117 // Render receipt on success page in iframe.
118 add_filter('the_content', [$this, 'showReceiptInIframeOnSuccessPage'], 1);
119 }
120 }
121
122 /**
123 * Load donation form view.
124 *
125 * @since 2.7.0
126 * @since 2.16.2 Co-locate $formTemplate with related conditional and usage.
127 * @global WP_Post $post
128 */
129 public function loadDonationFormView()
130 {
131 // Handle failed donation error.
132 if (FormUtils::canShowFailedDonationError()) {
133 add_action('give_pre_form', [$this, 'setFailedTransactionError']);
134 }
135
136 // Handle donation form.
137 if (FormUtils::isViewingForm()) {
138 /* @var Template $formTemplate */
139 $formTemplate = Give()->templates->getTemplate();
140
141 // Set header.
142 nocache_headers();
143 header('HTTP/1.1 200 OK');
144
145 include $formTemplate->getFormView();
146 exit();
147 }
148 }
149
150 /**
151 * Show failed transaction error on donation form.
152 *
153 * @since 2.7.0
154 */
155 public function setFailedTransactionError()
156 {
157 Give_Notices::print_frontend_notice(
158 Give()->templates->getTemplate(FormTemplateUtils::getActiveID())->getFailedDonationMessage(),
159 true,
160 'error'
161 );
162 }
163
164 /**
165 * Handle receipt shortcode on success page
166 *
167 * @since 2.7.0
168 *
169 * @param string $content
170 *
171 * @return string
172 */
173 public function showReceiptInIframeOnSuccessPage($content)
174 {
175 $receiptShortcode = ShortcodeUtils::getReceiptShortcodeFromConfirmationPage();
176 $content = str_replace($receiptShortcode, give_form_shortcode([]), $content);
177
178 return $content;
179 }
180
181 /**
182 * Load form template
183 *
184 * @since 2.7.0
185 * @return LoadTemplate
186 */
187 private function loadTemplate()
188 {
189 $templateLoader = new LoadTemplate();
190 $templateLoader->init();
191
192 return $templateLoader;
193 }
194
195 /**
196 * Load form template on ajax request.
197 *
198 * @since 2.7.0
199 */
200 public function loadTemplateOnAjaxRequest()
201 {
202 if (FormUtils::isProcessingGiveActionOnAjax() && ! FormUtils::isLegacyForm()) {
203 $this->loadTemplate();
204 }
205 }
206
207 /**
208 * Handle donor redirect when process donations.
209 *
210 * This function handle donor redirect when process donation with offsite checkout and on-site checkout.
211 * Donor will immediately redirect to success page aka receipt page for on-site payment process. That means success page remain same (as set in admin settings).
212 * For offsite checkout donor will redirect to embed form parent page. A query parameter will be add to url giveDonationAction=showReceipt to handle further cases.
213 *
214 * @since 2.7.0
215 */
216 public function embedFormRedirectURIHandler()
217 {
218 if (FormUtils::isProcessingForm()) {
219 add_filter('give_get_success_page_uri', [self::class, 'editSuccessPageURI']);
220 add_filter('give_get_failed_transaction_uri', [self::class, 'editFailedPageURI']);
221 add_filter('give_send_back_to_checkout', [$this, 'handlePrePaymentProcessingErrorRedirect']);
222 add_filter('wp_redirect', [$this, 'handleOffSiteCheckoutRedirect']);
223 }
224 }
225
226 /**
227 * Return current page aka embed form parent url as success page.
228 *
229 * @since 2.7.0
230 *
231 * @param string $url
232 *
233 * @return string
234 */
235 public static function editSuccessPageURI($url)
236 {
237 /* @var Template $template */
238 $template = Give()->templates->getTemplate();
239
240 return $template->openSuccessPageInIframe ?
241 FormUtils::createSuccessPageURL(Utils::switchRequestedURL($url, FormUtils::getIframeParentURL())) :
242 $url;
243 }
244
245 /**
246 * Return current page aka embed form parent url as failed page.
247 *
248 * @since 2.7.0
249 *
250 * @param string $url
251 *
252 * @return string
253 */
254 public static function editFailedPageURI($url)
255 {
256 /* @var Template $template */
257 $template = Give()->templates->getTemplate(FormTemplateUtils::getActiveID());
258
259 return $template->openFailedPageInIframe ?
260 FormUtils::createFailedPageURL(Utils::switchRequestedURL($url, FormUtils::getIframeParentURL())) :
261 $url;
262 }
263
264 /**
265 * Handle pre payment processing redirect.
266 *
267 * These redirects mainly happen when donation form data is not valid.
268 *
269 * @since 2.7.0
270 * @since 2.9.6 Adds giveDonationFormInIframe query param to url
271 *
272 * @param string $redirect
273 *
274 * @return string
275 */
276 public function handlePrePaymentProcessingErrorRedirect($redirect)
277 {
278 $redirect = add_query_arg(
279 [
280 'showDonationProcessingError' => 1,
281 'giveDonationFormInIframe' => 1,
282 ],
283 $redirect
284 );
285
286 $url = explode('?', esc_url_raw($redirect), 2);
287 $url[0] = Give()->routeForm->getURL(get_post_field('post_name', absint($_REQUEST['give-form-id'])));
288
289 return implode('?', $url);
290 }
291
292 /**
293 * Handle offsite payment checkout.
294 *
295 * In case of offsite checkout, this function will load a intermediate template to redirect embed parent page.
296 *
297 * @since 2.7.0
298 *
299 * @param string $location
300 *
301 * @return mixed
302 */
303 public function handleOffSiteCheckoutRedirect($location)
304 {
305 /* @var Template $template */
306 $template = Give()->templates->getTemplate();
307
308 // Exit if redirect is on same website.
309 if (0 === strpos($location, home_url())) {
310 if (FormUtils::isIframeParentSuccessPageURL($location)) {
311 $location = FormUtils::getSuccessPageURL();
312 $location = Utils::removeDonationAction($location);
313
314 // Open link in window?
315 if ( ! $template->openSuccessPageInIframe) {
316 $this->openLinkInWindow($location);
317 }
318
319 return $location;
320 }
321
322 if (FormUtils::isIframeParentFailedPageURL($location)) {
323 $location = add_query_arg(
324 ['showFailedDonationError' => 1],
325 $template->getFailedPageURL(FrontendFormTemplateUtils::getFormId())
326 );
327 $location = Utils::removeDonationAction($location);
328
329 // Open link in window?
330 if ( ! $template->openFailedPageInIframe) {
331 $this->openLinkInWindow(FormUtils::getLegacyFailedPageURL());
332 }
333
334 return $location;
335 }
336
337 // Add comment here.
338 if (
339 ( ! $template->openSuccessPageInIframe && 0 === strpos($location, FormUtils::getSuccessPageURL())) ||
340 ( ! $template->openFailedPageInIframe && 0 === strpos($location, FormUtils::getLegacyFailedPageURL()))
341 ) {
342 $this->openLinkInWindow($location);
343 }
344
345 return $location;
346 }
347
348 $this->openLinkInWindow($location);
349 }
350
351 /**
352 * Handle link opening in window instead of iframe.
353 *
354 * @since 2.7.0
355 *
356 * @param string $location
357 */
358 private function openLinkInWindow($location)
359 {
360 include GIVE_PLUGIN_DIR . 'src/Views/Form/defaultRedirectHandlerTemplate.php';
361 exit();
362 }
363
364 /**
365 * Handle single donation form page.
366 *
367 * @since 2.7.0
368 */
369 public function handleSingleDonationFormPage()
370 {
371 // Exit if current form is legacy
372 if (FormUtils::isLegacyForm()) {
373 return;
374 }
375
376 // Disable sidebar.
377 add_action('give_get_option_form_sidebar', [$this, 'disableLegacyDonationFormSidebar']);
378
379 // Remove title.
380 remove_action('give_single_form_summary', 'give_template_single_title', 5);
381
382 // Remove donation form renderer.
383 remove_action('give_single_form_summary', 'give_get_donation_form', 10);
384
385 add_action('give_single_form_summary', [$this, 'renderFormOnSingleDonationFormPage'], 10);
386 }
387
388 /**
389 * Return 'disabled' as donation form sidebar status.
390 *
391 * @since 2.7.0
392 * @return string
393 */
394 public function disableLegacyDonationFormSidebar()
395 {
396 return 'disabled';
397 }
398
399 /**
400 * This function handle donation form style for single donation page.
401 *
402 * Note: it will render style on basis on selected form template.
403 *
404 * @since 2.7.0
405 */
406 public function renderFormOnSingleDonationFormPage()
407 {
408 echo give_form_shortcode([]);
409 }
410 }
411