| 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 |
|