| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Helpers\Form\Template\Utils; |
| 4 |
|
| 5 |
use Give\Framework\Permissions\Facades\UserPermissions; |
| 6 |
use Give\Session\SessionDonation\DonationAccessor; |
| 7 |
use WP_Post; |
| 8 |
|
| 9 |
class Frontend |
| 10 |
{ |
| 11 |
/** |
| 12 |
* This function will return form id. |
| 13 |
* |
| 14 |
* There are two ways to auto detect form id: |
| 15 |
* 1. If global $post is give_forms post type then we assume that we are on donation form page and return id. |
| 16 |
* 2. if we are not on donation form page and process donation then we will return form id from submitted donation form data. |
| 17 |
* 3. if we are not on donation form page then we will get donation form id from session. |
| 18 |
* |
| 19 |
* This function can be use in donation processing flow i.e from donation form to receipt/failed transaction |
| 20 |
* |
| 21 |
* @since 2.7.0 |
| 22 |
* @return int|null |
| 23 |
* @global WP_Post $post |
| 24 |
*/ |
| 25 |
public static function getFormId() |
| 26 |
{ |
| 27 |
global $post; |
| 28 |
|
| 29 |
if ('give_forms' === get_post_type($post)) { |
| 30 |
return $post->ID; |
| 31 |
} |
| 32 |
|
| 33 |
if ($formId = Give()->routeForm->getQueriedFormID()) { |
| 34 |
return $formId; |
| 35 |
} |
| 36 |
|
| 37 |
// Check if admin previewing donation form. |
| 38 |
if ($formId = self::getPreviewDonationFormId()) { |
| 39 |
return $formId; |
| 40 |
} |
| 41 |
|
| 42 |
// Get form Id on ajax request. |
| 43 |
if (isset($_REQUEST['give_form_id']) && ($formId = absint($_REQUEST['give_form_id']))) { |
| 44 |
return $formId; |
| 45 |
} |
| 46 |
|
| 47 |
// Get form Id on ajax request. |
| 48 |
if (isset($_REQUEST['form_id']) && ($formId = absint($_REQUEST['form_id']))) { |
| 49 |
return $formId; |
| 50 |
} |
| 51 |
|
| 52 |
// Get form id on ajax request by donation id. |
| 53 |
if ( |
| 54 |
! empty($_REQUEST['donation_id']) && |
| 55 |
($donationId = absint($_REQUEST['donation_id'])) |
| 56 |
) { |
| 57 |
return give_get_payment_form_id($donationId); |
| 58 |
} |
| 59 |
|
| 60 |
// Get form id from donor purchase session. |
| 61 |
$session = new DonationAccessor(); |
| 62 |
$formId = $session->getFormId(); |
| 63 |
|
| 64 |
if ($formId) { |
| 65 |
return $formId; |
| 66 |
} |
| 67 |
|
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Return form id if admin previewing donation form. |
| 73 |
* Note: only for internal use. This function can be update or remove in future. |
| 74 |
* |
| 75 |
* @since 4.14.0 update permission capability to use facade |
| 76 |
* @since 2.7.0 |
| 77 |
* @return int|null |
| 78 |
*/ |
| 79 |
public static function getPreviewDonationFormId() |
| 80 |
{ |
| 81 |
if ( ! UserPermissions::donationForms()->canEdit()) { |
| 82 |
return null; |
| 83 |
} |
| 84 |
|
| 85 |
if ( |
| 86 |
isset($_GET['preview'], $_GET['p'], $_GET['post_type']) && |
| 87 |
filter_var($_GET['preview'], FILTER_VALIDATE_BOOLEAN) && |
| 88 |
('give_forms' === give_clean($_GET['post_type'])) && |
| 89 |
($formId = absint($_GET['p'])) |
| 90 |
) { |
| 91 |
return $formId; |
| 92 |
} |
| 93 |
|
| 94 |
return null; |
| 95 |
} |
| 96 |
} |
| 97 |
|