PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 2.17.0
GiveWP – Donation Plugin and Fundraising Platform v2.17.0
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 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 All 253 releases
give / src / Controller / Form.php
Form.php
392 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @since 2.16.2 Co-locate $formTemplate with related conditional and usage.
122 * @global WP_Post $post
123 */
124 public function loadDonationFormView() {
125
126 // Handle failed donation error.
127 if ( FormUtils::canShowFailedDonationError() ) {
128 add_action( 'give_pre_form', [ $this, 'setFailedTransactionError' ] );
129 }
130
131 // Handle donation form.
132 if ( FormUtils::isViewingForm() ) {
133
134 /* @var Template $formTemplate */
135 $formTemplate = Give()->templates->getTemplate();
136
137 // Set header.
138 nocache_headers();
139 header( 'HTTP/1.1 200 OK' );
140
141 include $formTemplate->getFormView();
142 exit();
143 }
144 }
145
146 /**
147 * Show failed transaction error on donation form.
148 *
149 * @since 2.7.0
150 */
151 public function setFailedTransactionError() {
152 Give_Notices::print_frontend_notice(
153 Give()->templates->getTemplate( FormTemplateUtils::getActiveID() )->getFailedDonationMessage(),
154 true,
155 'error'
156 );
157 }
158
159 /**
160 * Handle receipt shortcode on success page
161 *
162 * @since 2.7.0
163 * @param string $content
164 *
165 * @return string
166 */
167 public function showReceiptInIframeOnSuccessPage( $content ) {
168 $receiptShortcode = ShortcodeUtils::getReceiptShortcodeFromConfirmationPage();
169 $content = str_replace( $receiptShortcode, give_form_shortcode( [] ), $content );
170
171 return $content;
172 }
173
174
175 /**
176 * Load form template
177 *
178 * @return LoadTemplate
179 * @since 2.7.0
180 */
181 private function loadTemplate() {
182 $templateLoader = new LoadTemplate();
183 $templateLoader->init();
184
185 return $templateLoader;
186 }
187
188 /**
189 * Load form template on ajax request.
190 *
191 * @since 2.7.0
192 */
193 public function loadTemplateOnAjaxRequest() {
194 if ( FormUtils::isProcessingGiveActionOnAjax() && ! FormUtils::isLegacyForm() ) {
195 $this->loadTemplate();
196 }
197 }
198
199
200 /**
201 * Handle donor redirect when process donations.
202 *
203 * This function handle donor redirect when process donation with offsite checkout and on-site checkout.
204 * 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).
205 * 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.
206 *
207 * @since 2.7.0
208 */
209 public function embedFormRedirectURIHandler() {
210 if ( FormUtils::isProcessingForm() ) {
211 add_filter( 'give_get_success_page_uri', [ self::class, 'editSuccessPageURI' ] );
212 add_filter( 'give_get_failed_transaction_uri', [ self::class, 'editFailedPageURI' ] );
213 add_filter( 'give_send_back_to_checkout', [ $this, 'handlePrePaymentProcessingErrorRedirect' ] );
214 add_filter( 'wp_redirect', [ $this, 'handleOffSiteCheckoutRedirect' ] );
215 }
216 }
217
218
219 /**
220 * Return current page aka embed form parent url as success page.
221 *
222 * @param string $url
223 *
224 * @return string
225 * @since 2.7.0
226 */
227 public static function editSuccessPageURI( $url ) {
228 /* @var Template $template */
229 $template = Give()->templates->getTemplate();
230
231 return $template->openSuccessPageInIframe ?
232 FormUtils::createSuccessPageURL( Utils::switchRequestedURL( $url, FormUtils::getIframeParentURL() ) ) :
233 $url;
234 }
235
236 /**
237 * Return current page aka embed form parent url as failed page.
238 *
239 * @param string $url
240 *
241 * @return string
242 * @since 2.7.0
243 */
244 public static function editFailedPageURI( $url ) {
245 /* @var Template $template */
246 $template = Give()->templates->getTemplate( FormTemplateUtils::getActiveID() );
247
248 return $template->openFailedPageInIframe ?
249 FormUtils::createFailedPageURL( Utils::switchRequestedURL( $url, FormUtils::getIframeParentURL() ) ) :
250 $url;
251 }
252
253
254 /**
255 * Handle pre payment processing redirect.
256 *
257 * These redirects mainly happen when donation form data is not valid.
258 *
259 * @since 2.7.0
260 * @since 2.9.6 Adds giveDonationFormInIframe query param to url
261 *
262 * @param string $redirect
263 *
264 * @return string
265 */
266 public function handlePrePaymentProcessingErrorRedirect( $redirect ) {
267 $redirect = add_query_arg(
268 [
269 'showDonationProcessingError' => 1,
270 'giveDonationFormInIframe' => 1,
271 ],
272 $redirect
273 );
274
275 $url = explode( '?', $redirect, 2 );
276 $url[0] = Give()->routeForm->getURL( get_post_field( 'post_name', absint( $_REQUEST['give-form-id'] ) ) );
277
278 return implode( '?', $url );
279 }
280
281 /**
282 * Handle offsite payment checkout.
283 *
284 * In case of offsite checkout, this function will load a intermediate template to redirect embed parent page.
285 *
286 * @since 2.7.0
287 * @param string $location
288 *
289 * @return mixed
290 */
291 public function handleOffSiteCheckoutRedirect( $location ) {
292 /* @var Template $template */
293 $template = Give()->templates->getTemplate();
294
295 // Exit if redirect is on same website.
296 if ( 0 === strpos( $location, home_url() ) ) {
297 if ( FormUtils::isIframeParentSuccessPageURL( $location ) ) {
298 $location = FormUtils::getSuccessPageURL();
299 $location = Utils::removeDonationAction( $location );
300
301 // Open link in window?
302 if ( ! $template->openSuccessPageInIframe ) {
303 $this->openLinkInWindow( $location );
304 }
305
306 return $location;
307 }
308
309 if ( FormUtils::isIframeParentFailedPageURL( $location ) ) {
310 $location = add_query_arg( [ 'showFailedDonationError' => 1 ], $template->getFailedPageURL( FrontendFormTemplateUtils::getFormId() ) );
311 $location = Utils::removeDonationAction( $location );
312
313 // Open link in window?
314 if ( ! $template->openFailedPageInIframe ) {
315 $this->openLinkInWindow( FormUtils::getLegacyFailedPageURL() );
316 }
317
318 return $location;
319 }
320
321 // Add comment here.
322 if (
323 ( ! $template->openSuccessPageInIframe && 0 === strpos( $location, FormUtils::getSuccessPageURL() ) ) ||
324 ( ! $template->openFailedPageInIframe && 0 === strpos( $location, FormUtils::getLegacyFailedPageURL() ) )
325 ) {
326 $this->openLinkInWindow( $location );
327 }
328
329 return $location;
330 }
331
332 $this->openLinkInWindow( $location );
333 }
334
335
336 /**
337 * Handle link opening in window instead of iframe.
338 *
339 * @since 2.7.0
340 * @param string $location
341 */
342 private function openLinkInWindow( $location ) {
343 include GIVE_PLUGIN_DIR . 'src/Views/Form/defaultRedirectHandlerTemplate.php';
344 exit();
345 }
346
347 /**
348 * Handle single donation form page.
349 *
350 * @since 2.7.0
351 */
352 public function handleSingleDonationFormPage() {
353 // Exit if current form is legacy
354 if ( FormUtils::isLegacyForm() ) {
355 return;
356 }
357
358 // Disable sidebar.
359 add_action( 'give_get_option_form_sidebar', [ $this, 'disableLegacyDonationFormSidebar' ] );
360
361 // Remove title.
362 remove_action( 'give_single_form_summary', 'give_template_single_title', 5 );
363
364 // Remove donation form renderer.
365 remove_action( 'give_single_form_summary', 'give_get_donation_form', 10 );
366
367 add_action( 'give_single_form_summary', [ $this, 'renderFormOnSingleDonationFormPage' ], 10 );
368 }
369
370 /**
371 * Return 'disabled' as donation form sidebar status.
372 *
373 * @since 2.7.0
374 * @return string
375 */
376 public function disableLegacyDonationFormSidebar() {
377 return 'disabled';
378 }
379
380
381 /**
382 * This function handle donation form style for single donation page.
383 *
384 * Note: it will render style on basis on selected form template.
385 *
386 * @since 2.7.0
387 */
388 public function renderFormOnSingleDonationFormPage() {
389 echo give_form_shortcode( [] );
390 }
391 }
392