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