PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.11.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.11.2
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 1.7.0.9 1.8.0 All 210 releases
charitable / includes / request / class-charitable-request.php

class-charitable-request.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.11.2, at includes/request/class-charitable-request.php

361 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class used to provide information about the current request.
4 *
5 * @package Charitable/Classes/Charitable_Request
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.6.58
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Request' ) ) :
18
19 /**
20 * Charitable_Request.
21 *
22 * @since 1.0.0
23 * @final
24 */
25 final class Charitable_Request {
26
27 /**
28 * The single instance of this class.
29 *
30 * @var Charitable_Request|null
31 */
32 private static $instance = null;
33
34 /**
35 * Campaign object.
36 *
37 * @var Charitable_Campaign|false
38 */
39 private $campaign;
40
41 /**
42 * Original campaign object.
43 *
44 * @var Charitable_Campaign|false
45 */
46 private $original_campaign;
47
48 /**
49 * Original campaign ID.
50 *
51 * @var int
52 */
53 private $original_campaign_id;
54
55 /**
56 * Campaign ID.
57 *
58 * @var int
59 */
60 private $campaign_id;
61
62 /**
63 * Donation object.
64 *
65 * @var Charitable_Donation
66 */
67 private $donation;
68
69 /**
70 * Set up the class.
71 *
72 * Note that the only way to instantiate an object is with the on_start method,
73 * which can only be called during the start phase. In other words, don't try
74 * to instantiate this object.
75 *
76 * @since 1.0.0
77 */
78 private function __construct() {
79 /* Set the current campaign on the_post hook. */
80 add_action( 'the_post', array( $this, 'set_current_campaign' ) );
81
82 /* Add any supported donation parameters to the session. */
83 add_action( 'charitable_is_donate_page', array( $this, 'add_donation_params_to_session' ) );
84
85 /* Set the current campaign before a donation form is displayed, and reset to original after. */
86 add_action( 'charitable_donation_form_before', array( $this, 'set_current_campaign_before_donation_form' ) );
87 add_action( 'charitable_donation_form_after', array( $this, 'reset_current_campaign_after_donation_form' ) );
88 }
89
90 /**
91 * Returns and/or create the single instance of this class.
92 *
93 * @since 1.2.0
94 *
95 * @return Charitable_Request
96 */
97 public static function get_instance() {
98 if ( is_null( self::$instance ) ) {
99 self::$instance = new self();
100 }
101
102 return self::$instance;
103 }
104
105 /**
106 * When the_post is set, sets the current campaign to the current post if it is a campaign.
107 *
108 * @since 1.0.0
109 *
110 * @param WP_Post $post The Post object.
111 * @return void
112 */
113 public function set_current_campaign( $post ) {
114 if ( 'campaign' == $post->post_type ) {
115 $this->campaign = new Charitable_Campaign( $post );
116 } else {
117 unset( $this->campaign, $this->campaign_id );
118 }
119 }
120
121 /**
122 * Returns the current campaign. If there is no current campaign, return false.
123 *
124 * @since 1.0.0
125 *
126 * @return Charitable_Campaign|false Campaign object if we're viewing a campaign within a loop. False otherwise.
127 */
128 public function get_current_campaign() {
129 if ( ! isset( $this->campaign ) ) {
130 if ( $this->get_current_campaign_id() > 0 ) {
131 $this->campaign = new Charitable_Campaign( $this->get_current_campaign_id() );
132 } else {
133 $this->campaign = false;
134 }
135 }
136
137 return $this->campaign;
138 }
139
140 /**
141 * Returns the current campaign ID. If there is no current campaign, return 0.
142 *
143 * @since 1.0.0
144 *
145 * @global WP_Query $wp_query
146 *
147 * @return int
148 */
149 public function get_current_campaign_id() {
150 global $wp_query;
151
152 if ( isset( $this->campaign ) && $this->campaign ) {
153 $this->campaign_id = $this->campaign->ID;
154 } else {
155 $this->campaign_id = 0;
156
157 if ( get_post_type() == Charitable::CAMPAIGN_POST_TYPE ) {
158 $this->campaign_id = get_the_ID();
159 } elseif ( $wp_query && $wp_query->get( 'donate', false ) ) {
160 $session_donation = charitable_get_session()->get( 'donation' );
161
162 if ( false !== $session_donation ) {
163 $this->campaign_id = $session_donation->get( 'campaign_id' );
164 }
165 }
166 }//end if
167
168 if ( ! $this->campaign_id ) {
169 $this->campaign_id = $this->get_campaign_id_from_submission();
170 }
171
172 return $this->campaign_id;
173 }
174
175 /**
176 * Returns the current campaign template. If there is no current template (like with legacy 1.x campaigns), return 0.
177 *
178 * @since 1.8.0
179 *
180 * @global WP_Query $wp_query
181 *
182 * @return string
183 */
184 public function get_current_campaign_template() {
185
186 $campaign_settings = get_post_meta( $this->get_current_campaign_id(), 'campaign_settings_v2', true );
187
188 if ( false === $campaign_settings ) { // This is a legacy campaign. Probably.
189 return false;
190 }
191
192 $campaign_template_id = ! empty( $campaign_settings['template_id'] ) ? esc_attr( $campaign_settings['template_id'] ) : false;
193
194 $templates_data = get_option( 'charitable_campaign_builder_templates' );
195 // if false, then the template data was not stored and we will need to get it manually.
196 if ( false === $templates_data ) {
197 require_once charitable()->get_path( 'includes' ) . 'admin/campaign-builder/templates/class-templates.php';
198 $builder_template = new Charitable_Campaign_Builder_Templates();
199 $templates_data = $builder_template->get_templates_data( $campaign_template_id, $campaign_settings );
200 }
201
202 $template_data = ! empty( $templates_data['templates'][ $campaign_template_id ] ) ? $templates_data['templates'][ $campaign_template_id ] : false;
203
204 return $template_data;
205 }
206
207 /**
208 * Returns the current campaign template. If there is no current template (like with legacy 1.x campaigns), return 0.
209 *
210 * @since 1.8.0
211 *
212 * @global WP_Query $wp_query
213 *
214 * @return string
215 */
216 public function get_current_campaign_template_id() {
217
218 $campaign_settings = get_post_meta( $this->get_current_campaign_id(), 'campaign_settings_v2', true );
219 $campaign_template_id = ! empty( $campaign_settings['template_id'] ) ? esc_attr( $campaign_settings['template_id'] ) : false;
220
221 return $campaign_template_id;
222 }
223
224 /**
225 * Returns the current campaign template. If there is no current template (like with legacy 1.x campaigns), return 0.
226 *
227 * @since 1.8.0
228 *
229 * @global WP_Query $wp_query
230 *
231 * @return string
232 */
233 public function get_current_campaign_builder_data() {
234 global $wp_query;
235
236 $campaign_settings = get_post_meta( $this->get_current_campaign_id(), 'campaign_settings_v2', true );
237
238 return $campaign_settings;
239 }
240
241
242
243 /**
244 * Returns the campaign ID from a form submission.
245 *
246 * @since 1.0.0
247 *
248 * @return int
249 */
250 public function get_campaign_id_from_submission() {
251 // phpcs:disable WordPress.Security.NonceVerification.Missing
252 if ( ! isset( $_POST['campaign_id'] ) ) {
253 return 0;
254 }
255
256 $campaign_id = absint( sanitize_text_field( wp_unslash( $_POST['campaign_id'] ) ) );
257 // phpcs:enable WordPress.Security.NonceVerification.Missing
258
259 if ( Charitable::CAMPAIGN_POST_TYPE !== get_post_type( $campaign_id ) ) {
260 return 0;
261 }
262
263 return $campaign_id;
264 }
265
266 /**
267 * Returns the current donation object. If there is no current donation, return false.
268 *
269 * @since 1.0.0
270 *
271 * @return Charitable_Donation|false
272 */
273 public function get_current_donation() {
274 if ( ! isset( $this->donation ) ) {
275 $donation_id = $this->get_current_donation_id();
276 $this->donation = $donation_id ? charitable_get_donation( $donation_id ) : false;
277 }
278
279 return $this->donation;
280 }
281
282 /**
283 * Returns the current donation ID. If there is no current donation, return 0.
284 *
285 * @since 1.0.0
286 * @version 1.8.9.1
287 *
288 * @return int
289 */
290 public function get_current_donation_id() {
291 $donation_id = get_query_var( 'donation_id', 0 );
292
293 // phpcs:disable WordPress.Security.NonceVerification.Recommended
294 if ( ! $donation_id && isset( $_GET['donation_id'] ) ) {
295 $donation_id = absint( sanitize_text_field( wp_unslash( $_GET['donation_id'] ) ) );
296 }
297 // phpcs:enable WordPress.Security.NonceVerification.Recommended
298
299 return $donation_id;
300 }
301
302 /**
303 * If set, add supported donation parameters to the session.
304 *
305 * @since 1.6.25
306 * @version 1.8.9.1
307 *
308 * @param int $campaign_id The campaign receiving the donation.
309 * @return void
310 */
311 public function add_donation_params_to_session( $campaign_id ) {
312 // phpcs:disable WordPress.Security.NonceVerification.Recommended
313 if ( array_key_exists( 'amount', $_REQUEST ) ) {
314 $amount = sanitize_text_field( wp_unslash( $_REQUEST['amount'] ) );
315 $period = array_key_exists( 'period', $_REQUEST ) ? sanitize_text_field( wp_unslash( $_REQUEST['period'] ) ) : 'once';
316
317 charitable_get_session()->add_donation( $campaign_id, $amount, $period );
318 }
319 // phpcs:enable WordPress.Security.NonceVerification.Recommended
320 }
321
322 /**
323 * Temporarily set the current campaign to the one for the donation form.
324 *
325 * @since 1.6.55
326 *
327 * @param Charitable_Donation_Form $form The donation form object.
328 * @return void
329 */
330 public function set_current_campaign_before_donation_form( Charitable_Donation_form $form ) {
331 if ( isset( $this->campaign_id ) && $this->campaign_id === $form->get_campaign()->ID ) {
332 return;
333 }
334
335 if ( isset( $this->campaign ) ) {
336 $this->original_campaign = $this->campaign;
337 }
338
339 if ( isset( $this->campaign_id ) ) {
340 $this->original_campaign_id = $this->campaign_id;
341 }
342
343 $this->campaign = $form->get_campaign();
344 $this->campaign_id = $this->campaign->ID;
345 }
346
347 /**
348 * Reset the $campaign and $campaign_id properties to what they originally were.
349 *
350 * @since 1.6.55
351 *
352 * @return void
353 */
354 public function reset_current_campaign_after_donation_form() {
355 $this->campaign = $this->original_campaign;
356 $this->campaign_id = $this->original_campaign ? $this->original_campaign->ID : false;
357 }
358 }
359
360 endif;
361