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 |