| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donation amount form model class. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Donation_Amount_Form |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
* @version 1.5.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Donation_Amount_Form' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Donation_Amount_Form |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class Charitable_Donation_Amount_Form extends Charitable_Donation_Form implements Charitable_Donation_Form_Interface { |
| 26 |
|
| 27 |
/** |
| 28 |
* The current campaign. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* |
| 32 |
* @var Charitable_Campaign |
| 33 |
*/ |
| 34 |
protected $campaign; |
| 35 |
|
| 36 |
/** |
| 37 |
* Form fields. |
| 38 |
* |
| 39 |
* @since 1.0.0 |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
protected $form_fields; |
| 44 |
|
| 45 |
/** |
| 46 |
* Nonce action. |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
protected $nonce_action = 'charitable_donation_amount'; |
| 53 |
|
| 54 |
/** |
| 55 |
* Nonce name. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
protected $nonce_name = '_charitable_donation_amount_nonce'; |
| 62 |
|
| 63 |
/** |
| 64 |
* Action to be executed upon form submission. |
| 65 |
* |
| 66 |
* @since 1.0.0 |
| 67 |
* |
| 68 |
* @var string |
| 69 |
*/ |
| 70 |
protected $form_action = 'make_donation_streamlined'; |
| 71 |
|
| 72 |
/** |
| 73 |
* Return the donation form fields. |
| 74 |
* |
| 75 |
* @since 1.0.0 |
| 76 |
* |
| 77 |
* @return array[] |
| 78 |
*/ |
| 79 |
public function get_fields() { |
| 80 |
return $this->get_donation_fields(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Validate the form submission. |
| 85 |
* |
| 86 |
* @since 1.4.4 |
| 87 |
* |
| 88 |
* @return boolean |
| 89 |
*/ |
| 90 |
public function validate_submission() { |
| 91 |
/* If we have already validated the submission, return the value. */ |
| 92 |
if ( $this->validated ) { |
| 93 |
return $this->valid; |
| 94 |
} |
| 95 |
|
| 96 |
$this->validated = true; |
| 97 |
|
| 98 |
$this->valid = $this->validate_security_check() |
| 99 |
&& $this->check_required_fields( $this->get_merged_fields() ) |
| 100 |
&& $this->validate_amount(); |
| 101 |
|
| 102 |
$this->valid = apply_filters( 'charitable_validate_donation_amount_form_submission', $this->valid, $this ); |
| 103 |
|
| 104 |
return $this->valid; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Return the donation values. |
| 109 |
* |
| 110 |
* @since 1.0.0 |
| 111 |
* |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public function get_donation_values() { |
| 115 |
$submitted = $this->get_submitted_values(); |
| 116 |
|
| 117 |
$values = array( |
| 118 |
'campaign_id' => $submitted['campaign_id'], |
| 119 |
'amount' => self::get_donation_amount( $submitted ), |
| 120 |
); |
| 121 |
|
| 122 |
/** |
| 123 |
* Filter the donation widget form submission amount. |
| 124 |
* |
| 125 |
* @since 1.0.0 |
| 126 |
* |
| 127 |
* @param array $values The donation values, including a campaign_id and amount. |
| 128 |
* @param array $submitted All the submitted values. |
| 129 |
* @param Charitable_Donation_Amount_Form $form The form object. |
| 130 |
*/ |
| 131 |
return apply_filters( 'charitable_donation_amount_form_submission_values', $values, $submitted, $this ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Redirect to payment form after submission. |
| 136 |
* |
| 137 |
* @since 1.0.0 |
| 138 |
* @version 1.8.8.6 |
| 139 |
* |
| 140 |
* @param int $campaign_id The campaign we are donating to. |
| 141 |
* @param int $amount The donation amount. |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function redirect_after_submission( $campaign_id, $amount ) { |
| 145 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$redirect_url = charitable_get_permalink( 'campaign_donation_page', array( 'campaign_id' => $campaign_id ) ); |
| 150 |
|
| 151 |
if ( 'same_page' == charitable_get_option( 'donation_form_display', 'separate_page' ) ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual |
| 152 |
$redirect_url .= '#charitable-donation-form'; |
| 153 |
} |
| 154 |
|
| 155 |
$redirect_url = apply_filters( 'charitable_donation_amount_form_redirect', $redirect_url, $campaign_id, $amount ); |
| 156 |
|
| 157 |
// Check if URL is external and add to allowed hosts if needed. |
| 158 |
$parsed_url = wp_parse_url( $redirect_url ); |
| 159 |
if ( ! empty( $parsed_url['host'] ) ) { |
| 160 |
$redirect_host = $parsed_url['host']; |
| 161 |
$site_host = wp_parse_url( home_url(), PHP_URL_HOST ); |
| 162 |
|
| 163 |
// If redirecting to external host, add it to allowed redirect hosts. |
| 164 |
if ( $redirect_host !== $site_host ) { |
| 165 |
add_filter( |
| 166 |
'allowed_redirect_hosts', |
| 167 |
function( $hosts ) use ( $redirect_host ) { |
| 168 |
if ( ! in_array( $redirect_host, $hosts, true ) ) { |
| 169 |
$hosts[] = $redirect_host; |
| 170 |
} |
| 171 |
return $hosts; |
| 172 |
} |
| 173 |
); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
wp_safe_redirect( esc_url_raw( $redirect_url ) ); |
| 178 |
|
| 179 |
exit(); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Render the donation form. |
| 184 |
* |
| 185 |
* @since 1.0.0 |
| 186 |
* |
| 187 |
* @return void |
| 188 |
*/ |
| 189 |
public function render() { |
| 190 |
/* Load the script if it hasn't been loaded yet. */ |
| 191 |
if ( ! wp_script_is( 'charitable-script', 'enqueued' ) ) { |
| 192 |
|
| 193 |
if ( ! class_exists( 'Charitable_Public' ) ) { |
| 194 |
require_once( charitable()->get_path( 'public' ) . 'class-charitable-public.php' ); |
| 195 |
} |
| 196 |
|
| 197 |
Charitable_Public::get_instance()->enqueue_donation_form_scripts(); |
| 198 |
} |
| 199 |
|
| 200 |
charitable_template( |
| 201 |
'donation-form/form-donation.php', |
| 202 |
array( |
| 203 |
'campaign' => $this->get_campaign(), |
| 204 |
'form_template' => $this->get_form_template(), |
| 205 |
'form' => $this, |
| 206 |
'form_id' => 'charitable-donation-amount-form', |
| 207 |
) |
| 208 |
); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
endif; |
| 213 |
|