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