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