PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / trunk
GiveWP – Donation Plugin and Fundraising Platform vtrunk
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 1 year ago PayPalWebhooks.php 3 years ago
Form.php
415 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 3.16.0 add action give_donation_confirmation_page_enqueue_scripts
66 * @since 2.7.0
67 */
68 public function loadReceiptView()
69 {
70 // Do not handle legacy donation form.
71 if (FormUtils::isLegacyForm()) {
72 return;
73 }
74
75 do_action('give_donation_confirmation_page_enqueue_scripts');
76
77 // Handle success page.
78 if (FormUtils::isViewingFormReceipt() && ! FormUtils::isLegacyForm()) {
79 /* @var Template $formTemplate */
80 $formTemplate = Give()->templates->getTemplate();
81
82 if (FormUtils::inIframe() || ($formTemplate->openSuccessPageInIframe && FormUtils::isProcessingForm())) {
83 // Set header.
84 nocache_headers();
85 header('HTTP/1.1 200 OK');
86
87 // Show donation processing template.
88 if (ConfirmDonation::isConfirming()) {
89 $session = new DonationAccessor();
90 $donationId = $session->getDonationId();
91
92 /**
93 * Fire the action hook.
94 *
95 * If developer wants to verify payment before showing receipt then use `give_handle_donation_confirm` action hook to verify donation.
96 * Developer can access query parameters return by payment gateway. for example
97 * $session = new DonationAccessor();
98 * $session->getByKey( "postDataFor{$paymentGatewayId}" )
99 *
100 * @since 2.7.0
101 *
102 * @param int $donationId
103 */
104 do_action('give_handle_donation_confirmation', $donationId);
105
106 ConfirmDonation::removePostedDataFromDonationSession();
107
108 // Load payment processing view only if donation is in pending status.
109 if ('pending' === get_post_status($donationId)) {
110 include GIVE_PLUGIN_DIR . 'src/Views/Form/defaultFormDonationProcessing.php';
111 exit();
112 }
113 }
114
115 // Render receipt with in iframe.
116 include $formTemplate->getReceiptView();
117 exit();
118 }
119 // Render receipt on success page in iframe.
120 add_filter('the_content', [$this, 'showReceiptInIframeOnSuccessPage'], 1);
121 }
122 }
123
124 /**
125 * Load donation form view.
126 *
127 * @since 2.7.0
128 * @since 2.16.2 Co-locate $formTemplate with related conditional and usage.
129 * @global WP_Post $post
130 */
131 public function loadDonationFormView()
132 {
133 // Handle failed donation error.
134 if (FormUtils::canShowFailedDonationError()) {
135 add_action('give_pre_form', [$this, 'setFailedTransactionError']);
136 }
137
138 // Handle donation form.
139 if (FormUtils::isViewingForm()) {
140 /* @var Template $formTemplate */
141 $formTemplate = Give()->templates->getTemplate();
142
143 // Set header.
144 nocache_headers();
145 header('HTTP/1.1 200 OK');
146
147 include $formTemplate->getFormView();
148 exit();
149 }
150 }
151
152 /**
153 * Show failed transaction error on donation form.
154 *
155 * @since 2.7.0
156 */
157 public function setFailedTransactionError()
158 {
159 Give_Notices::print_frontend_notice(
160 Give()->templates->getTemplate(FormTemplateUtils::getActiveID())->getFailedDonationMessage(),
161 true,
162 'error'
163 );
164 }
165
166 /**
167 * Handle receipt shortcode on success page
168 *
169 * @since 3.16.0 add filter give_donation_confirmation_success_page_shortcode_view
170 * @since 2.7.0
171 *
172 * @param string $content
173 *
174 * @return string
175 */
176 public function showReceiptInIframeOnSuccessPage($content)
177 {
178 $receiptShortcode = ShortcodeUtils::getReceiptShortcodeFromConfirmationPage();
179
180 $view = apply_filters('give_donation_confirmation_success_page_shortcode_view', give_form_shortcode([]));
181
182 return str_replace($receiptShortcode, $view, $content);
183 }
184
185 /**
186 * Load form template
187 *
188 * @since 2.7.0
189 * @return LoadTemplate
190 */
191 private function loadTemplate()
192 {
193 $templateLoader = new LoadTemplate();
194 $templateLoader->init();
195
196 return $templateLoader;
197 }
198
199 /**
200 * Load form template on ajax request.
201 *
202 * @since 2.7.0
203 */
204 public function loadTemplateOnAjaxRequest()
205 {
206 if (FormUtils::isProcessingGiveActionOnAjax() && ! FormUtils::isLegacyForm()) {
207 $this->loadTemplate();
208 }
209 }
210
211 /**
212 * Handle donor redirect when process donations.
213 *
214 * This function handle donor redirect when process donation with offsite checkout and on-site checkout.
215 * 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).
216 * 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.
217 *
218 * @since 2.7.0
219 */
220 public function embedFormRedirectURIHandler()
221 {
222 if (FormUtils::isProcessingForm()) {
223 add_filter('give_get_success_page_uri', [self::class, 'editSuccessPageURI']);
224 add_filter('give_get_failed_transaction_uri', [self::class, 'editFailedPageURI']);
225 add_filter('give_send_back_to_checkout', [$this, 'handlePrePaymentProcessingErrorRedirect']);
226 add_filter('wp_redirect', [$this, 'handleOffSiteCheckoutRedirect']);
227 }
228 }
229
230 /**
231 * Return current page aka embed form parent url as success page.
232 *
233 * @since 2.7.0
234 *
235 * @param string $url
236 *
237 * @return string
238 */
239 public static function editSuccessPageURI($url)
240 {
241 /* @var Template $template */
242 $template = Give()->templates->getTemplate();
243
244 return $template->openSuccessPageInIframe ?
245 FormUtils::createSuccessPageURL(Utils::switchRequestedURL($url, FormUtils::getIframeParentURL())) :
246 $url;
247 }
248
249 /**
250 * Return current page aka embed form parent url as failed page.
251 *
252 * @since 2.7.0
253 *
254 * @param string $url
255 *
256 * @return string
257 */
258 public static function editFailedPageURI($url)
259 {
260 /* @var Template $template */
261 $template = Give()->templates->getTemplate(FormTemplateUtils::getActiveID());
262
263 return $template->openFailedPageInIframe ?
264 FormUtils::createFailedPageURL(Utils::switchRequestedURL($url, FormUtils::getIframeParentURL())) :
265 $url;
266 }
267
268 /**
269 * Handle pre payment processing redirect.
270 *
271 * These redirects mainly happen when donation form data is not valid.
272 *
273 * @since 2.7.0
274 * @since 2.9.6 Adds giveDonationFormInIframe query param to url
275 *
276 * @param string $redirect
277 *
278 * @return string
279 */
280 public function handlePrePaymentProcessingErrorRedirect($redirect)
281 {
282 $redirect = add_query_arg(
283 [
284 'showDonationProcessingError' => 1,
285 'giveDonationFormInIframe' => 1,
286 ],
287 $redirect
288 );
289
290 $url = explode('?', esc_url_raw($redirect), 2);
291 $url[0] = Give()->routeForm->getURL(get_post_field('post_name', absint($_REQUEST['give-form-id'])));
292
293 return implode('?', $url);
294 }
295
296 /**
297 * Handle offsite payment checkout.
298 *
299 * In case of offsite checkout, this function will load a intermediate template to redirect embed parent page.
300 *
301 * @since 2.7.0
302 *
303 * @param string $location
304 *
305 * @return mixed
306 */
307 public function handleOffSiteCheckoutRedirect($location)
308 {
309 /* @var Template $template */
310 $template = Give()->templates->getTemplate();
311
312 // Exit if redirect is on same website.
313 if (0 === strpos($location, home_url())) {
314 if (FormUtils::isIframeParentSuccessPageURL($location)) {
315 $location = FormUtils::getSuccessPageURL();
316 $location = Utils::removeDonationAction($location);
317
318 // Open link in window?
319 if ( ! $template->openSuccessPageInIframe) {
320 $this->openLinkInWindow($location);
321 }
322
323 return $location;
324 }
325
326 if (FormUtils::isIframeParentFailedPageURL($location)) {
327 $location = add_query_arg(
328 ['showFailedDonationError' => 1],
329 $template->getFailedPageURL(FrontendFormTemplateUtils::getFormId())
330 );
331 $location = Utils::removeDonationAction($location);
332
333 // Open link in window?
334 if ( ! $template->openFailedPageInIframe) {
335 $this->openLinkInWindow(FormUtils::getLegacyFailedPageURL());
336 }
337
338 return $location;
339 }
340
341 // Add comment here.
342 if (
343 ( ! $template->openSuccessPageInIframe && 0 === strpos($location, FormUtils::getSuccessPageURL())) ||
344 ( ! $template->openFailedPageInIframe && 0 === strpos($location, FormUtils::getLegacyFailedPageURL()))
345 ) {
346 $this->openLinkInWindow($location);
347 }
348
349 return $location;
350 }
351
352 $this->openLinkInWindow($location);
353 }
354
355 /**
356 * Handle link opening in window instead of iframe.
357 *
358 * @since 2.7.0
359 *
360 * @param string $location
361 */
362 private function openLinkInWindow($location)
363 {
364 include GIVE_PLUGIN_DIR . 'src/Views/Form/defaultRedirectHandlerTemplate.php';
365 exit();
366 }
367
368 /**
369 * Handle single donation form page.
370 *
371 * @since 2.7.0
372 */
373 public function handleSingleDonationFormPage()
374 {
375 // Exit if current form is legacy
376 if (FormUtils::isLegacyForm()) {
377 return;
378 }
379
380 // Disable sidebar.
381 add_action('give_get_option_form_sidebar', [$this, 'disableLegacyDonationFormSidebar']);
382
383 // Remove title.
384 remove_action('give_single_form_summary', 'give_template_single_title', 5);
385
386 // Remove donation form renderer.
387 remove_action('give_single_form_summary', 'give_get_donation_form', 10);
388
389 add_action('give_single_form_summary', [$this, 'renderFormOnSingleDonationFormPage'], 10);
390 }
391
392 /**
393 * Return 'disabled' as donation form sidebar status.
394 *
395 * @since 2.7.0
396 * @return string
397 */
398 public function disableLegacyDonationFormSidebar()
399 {
400 return 'disabled';
401 }
402
403 /**
404 * This function handle donation form style for single donation page.
405 *
406 * Note: it will render style on basis on selected form template.
407 *
408 * @since 2.7.0
409 */
410 public function renderFormOnSingleDonationFormPage()
411 {
412 echo give_form_shortcode([]);
413 }
414 }
415