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