PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12.3
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12.3
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / endpoints / class-charitable-campaign-donation-endpoint.php

class-charitable-campaign-donation-endpoint.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.12.3, at includes/endpoints/class-charitable-campaign-donation-endpoint.php

291 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Donate endpoint.
4 *
5 * @package Charitable/Classes/Charitable_Campaign_Donation_Endpoint
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.5.0
10 * @version 1.6.55
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Campaign_Donation_Endpoint' ) ) :
18
19 /**
20 * Charitable_Campaign_Donation_Endpoint
21 *
22 * @since 1.5.0
23 */
24 class Charitable_Campaign_Donation_Endpoint extends Charitable_Endpoint {
25
26 /** Endpoint ID. */
27 const ID = 'campaign_donation';
28
29 /**
30 * Whether to force HTTPS on the endpoint.
31 *
32 * @since 1.6.14
33 *
34 * @var boolean
35 */
36 private $force_https;
37
38 /**
39 * Object instantiation.
40 *
41 * @since 1.5.4
42 */
43 public function __construct() {
44 $this->cacheable = false;
45
46 /**
47 * Whether to force HTTPS on the donation endpoint.
48 *
49 * @since 1.6.14
50 *
51 * @param boolean $force_https Whether HTTPS is forced for the donation endpoint.
52 */
53 $this->force_https = apply_filters( 'charitable_campaign_donation_endpoint_force_https', false );
54 }
55
56 /**
57 * Return the endpoint ID.
58 *
59 * @since 1.5.0
60 *
61 * @return string
62 */
63 public static function get_endpoint_id() {
64 return self::ID;
65 }
66
67 /**
68 * Add rewrite rules for the endpoint.
69 *
70 * @since 1.5.0
71 */
72 public function setup_rewrite_rules() {
73 add_rewrite_endpoint( 'donate', EP_PERMALINK );
74 }
75
76 /**
77 * Return the endpoint URL.
78 *
79 * @since 1.5.0
80 *
81 * @global WP_Rewrite $wp_rewrite
82 * @param array $args Mixed args.
83 * @return string
84 */
85 public function get_page_url( $args = array() ) {
86 global $wp_rewrite;
87
88 /**
89 * A campaign ID must be passed for us to get a
90 * campaign donation endpoint URL.
91 */
92 if ( ! isset( $args['campaign_id'] ) ) {
93 return false;
94 }
95
96 $campaign_url = get_permalink( $args['campaign_id'] );
97
98 if ( $this->force_https ) {
99 $campaign_url = str_replace( 'http://', 'https://', $campaign_url );
100 }
101
102 if ( 'same_page' === charitable_get_option( 'donation_form_display', 'separate_page' ) ) {
103 return $campaign_url;
104 }
105
106 if ( $wp_rewrite->using_permalinks()
107 && ! in_array( get_post_status( $args['campaign_id'] ), array( 'pending', 'draft' ) )
108 && ! isset( $_GET['preview'] ) ) { // phpcs:ignore
109
110 $url = wp_parse_url( $campaign_url );
111
112 return $this->sanitize_endpoint_url( $campaign_url, 'donate' );
113 }
114
115 return esc_url_raw( add_query_arg( array( 'donate' => 1 ), $campaign_url ) );
116 }
117
118 /**
119 * Return whether we are currently viewing the endpoint.
120 *
121 * @since 1.5.0
122 *
123 * @global WP_Query $wp_query
124 * @param array $args Mixed args.
125 * @return boolean
126 */
127 public function is_page( $args = array() ) {
128 global $wp_query;
129
130 if ( is_null( $wp_query->get_queried_object() ) ) {
131 return false;
132 }
133
134 if ( ! $wp_query->is_singular( Charitable::CAMPAIGN_POST_TYPE ) ) {
135 return false;
136 }
137
138 if ( array_key_exists( 'donate', $wp_query->query_vars ) ) {
139 return true;
140 }
141
142 /* If 'strict' is set to `true`, this will only return true if this has the /donate/ endpoint. */
143 if ( $this->is_strict_check( $args ) ) {
144 return false;
145 }
146
147 return 'separate_page' !== charitable_get_option( 'donation_form_display', 'separate_page' );
148 }
149
150 /**
151 * Possibly redirect elsewhere when viewing the endpoint.
152 *
153 * @since 1.6.55
154 *
155 * @return void
156 */
157 public function setup_template() {
158 $campaign_id = charitable_get_current_campaign_id();
159
160 /**
161 * If the campaign doesn't exist or can no longer receive donations,
162 * redirect the user to the campaign page.
163 */
164 if ( ! charitable_campaign_can_receive_donations( $campaign_id ) ) {
165 wp_safe_redirect( get_permalink( $campaign_id ) );
166 exit();
167 }
168
169 if ( $this->force_https && ! is_ssl() ) {
170 wp_safe_redirect( charitable_get_permalink( 'campaign_donation' ) );
171 exit();
172 }
173
174 $donation_id = get_query_var( 'donation_id', false );
175
176 /* If a donation ID is included, make sure it belongs to the current user. */
177 if ( $donation_id && ! charitable_user_can_access_donation( $donation_id ) ) {
178 wp_safe_redirect(
179 charitable_get_permalink(
180 'campaign_donation',
181 array(
182 'campaign_id' => $campaign_id,
183 )
184 )
185 );
186 exit();
187 }
188
189 /**
190 * Do something when the donate page is loaded.
191 *
192 * @since 1.0.0
193 * @since 1.6.25 Added $campaign_id and $donation_id parameters.
194 *
195 * @param int $campaign_id The campaign receiving the donation.
196 * @param int $donation_id The donation id, if this is an update to an existing donation.
197 */
198 do_action( 'charitable_is_donate_page', $campaign_id, $donation_id );
199 }
200
201 /**
202 * Return the template to display for this endpoint.
203 *
204 * @since 1.5.0
205 *
206 * @param string $template The default template.
207 * @return array
208 */
209 public function get_template( $template ) {
210 return array( 'campaign-donation-page.php', 'page.php', 'singular.php', 'index.php' );
211 }
212
213 /**
214 * Get the content to display for the endpoint.
215 *
216 * @since 1.5.0
217 *
218 * @param string $content Default content.
219 * @return string
220 */
221 public function get_content( $content ) {
222 if ( ! charitable_is_main_loop() ) {
223 return $content;
224 }
225
226 if ( 'separate_page' != charitable_get_option( 'donation_form_display', 'separate_page' )
227 && false === get_query_var( 'donate', false ) ) {
228 return $content;
229 }
230
231 /*
232 * On block themes, Charitable_Endpoints::template_loader() returns early
233 * (uses_custom_template() is false), so setup_template() never runs and the
234 * 'charitable_is_donate_page' action never fires. Without it, URL parameters
235 * such as ?amount= and ?period= are never added to the session, and the form
236 * falls back to the campaign default instead of the requested amount. Fire it
237 * here for block themes so the form can pre-select the requested amount.
238 */
239 if ( charitable_is_block_theme() ) {
240 $campaign_id = charitable_get_current_campaign_id();
241 $donation_id = get_query_var( 'donation_id', false );
242
243 /** This action is documented in includes/endpoints/class-charitable-campaign-donation-endpoint.php */
244 do_action( 'charitable_is_donate_page', $campaign_id, $donation_id );
245 }
246
247 ob_start();
248
249 charitable_template( 'content-donation-form.php', array() );
250
251 return ob_get_clean();
252 }
253
254 /**
255 * Returns whether the current request is for a campaign that can receive donations.
256 *
257 * @since 1.5.4
258 *
259 * @return boolean
260 */
261 protected function is_donation_ready_campaign() {
262 return charitable_campaign_can_receive_donations( charitable_get_current_campaign_id() );
263 }
264
265 /**
266 * Returns whether the passed donation is valid for the current user.
267 *
268 * @since 1.5.4
269 *
270 * @param int $donation_id The donation ID in query vars.
271 * @return boolean
272 */
273 protected function is_invalid_donation( $donation_id ) {
274 return ! charitable_user_can_access_donation( $donation_id );
275 }
276
277 /**
278 * Returns whether this is a strict check.
279 *
280 * @since 1.5.4
281 *
282 * @param array $args Mixed args.
283 * @return boolean
284 */
285 protected function is_strict_check( $args ) {
286 return array_key_exists( 'strict', $args ) && $args['strict'];
287 }
288 }
289
290 endif;
291