PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.60
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.60
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.6.60, at includes/request/class-charitable-request.php

277 lines 6.7 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 Eric Daams
7 * @copyright Copyright (c) 2022, Studio 164a
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 * Campaign ID.
50 *
51 * @var int
52 */
53 private $campaign_id;
54
55 /**
56 * Donation object.
57 *
58 * @var Charitable_Donation
59 */
60 private $donation;
61
62 /**
63 * Set up the class.
64 *
65 * Note that the only way to instantiate an object is with the on_start method,
66 * which can only be called during the start phase. In other words, don't try
67 * to instantiate this object.
68 *
69 * @since 1.0.0
70 */
71 private function __construct() {
72 /* Set the current campaign on the_post hook. */
73 add_action( 'the_post', array( $this, 'set_current_campaign' ) );
74
75 /* Add any supported donation parameters to the session. */
76 add_action( 'charitable_is_donate_page', array( $this, 'add_donation_params_to_session' ) );
77
78 /* Set the current campaign before a donation form is displayed, and reset to original after. */
79 add_action( 'charitable_donation_form_before', array( $this, 'set_current_campaign_before_donation_form' ) );
80 add_action( 'charitable_donation_form_after', array( $this, 'reset_current_campaign_after_donation_form' ) );
81 }
82
83 /**
84 * Returns and/or create the single instance of this class.
85 *
86 * @since 1.2.0
87 *
88 * @return Charitable_Request
89 */
90 public static function get_instance() {
91 if ( is_null( self::$instance ) ) {
92 self::$instance = new self();
93 }
94
95 return self::$instance;
96 }
97
98 /**
99 * When the_post is set, sets the current campaign to the current post if it is a campaign.
100 *
101 * @since 1.0.0
102 *
103 * @param WP_Post $post The Post object.
104 * @return void
105 */
106 public function set_current_campaign( $post ) {
107 if ( 'campaign' == $post->post_type ) {
108 $this->campaign = new Charitable_Campaign( $post );
109 } else {
110 unset( $this->campaign, $this->campaign_id );
111 }
112 }
113
114 /**
115 * Returns the current campaign. If there is no current campaign, return false.
116 *
117 * @since 1.0.0
118 *
119 * @return Charitable_Campaign|false Campaign object if we're viewing a campaign within a loop. False otherwise.
120 */
121 public function get_current_campaign() {
122 if ( ! isset( $this->campaign ) ) {
123 if ( $this->get_current_campaign_id() > 0 ) {
124 $this->campaign = new Charitable_Campaign( $this->get_current_campaign_id() );
125 } else {
126 $this->campaign = false;
127 }
128 }
129
130 return $this->campaign;
131 }
132
133 /**
134 * Returns the current campaign ID. If there is no current campaign, return 0.
135 *
136 * @since 1.0.0
137 *
138 * @global WP_Query $wp_query
139 *
140 * @return int
141 */
142 public function get_current_campaign_id() {
143 global $wp_query;
144
145 if ( isset( $this->campaign ) && $this->campaign ) {
146 $this->campaign_id = $this->campaign->ID;
147 } else {
148 $this->campaign_id = 0;
149
150 if ( get_post_type() == Charitable::CAMPAIGN_POST_TYPE ) {
151 $this->campaign_id = get_the_ID();
152 } elseif ( $wp_query && $wp_query->get( 'donate', false ) ) {
153 $session_donation = charitable_get_session()->get( 'donation' );
154
155 if ( false !== $session_donation ) {
156 $this->campaign_id = $session_donation->get( 'campaign_id' );
157 }
158 }
159 }//end if
160
161 if ( ! $this->campaign_id ) {
162 $this->campaign_id = $this->get_campaign_id_from_submission();
163 }
164
165 return $this->campaign_id;
166 }
167
168 /**
169 * Returns the campaign ID from a form submission.
170 *
171 * @since 1.0.0
172 *
173 * @return int
174 */
175 public function get_campaign_id_from_submission() {
176 if ( ! isset( $_POST['campaign_id'] ) ) {
177 return 0;
178 }
179
180 $campaign_id = absint( $_POST['campaign_id'] );
181
182 if ( Charitable::CAMPAIGN_POST_TYPE !== get_post_type( $campaign_id ) ) {
183 return 0;
184 }
185
186 return $campaign_id;
187 }
188
189 /**
190 * Returns the current donation object. If there is no current donation, return false.
191 *
192 * @since 1.0.0
193 *
194 * @return Charitable_Donation|false
195 */
196 public function get_current_donation() {
197 if ( ! isset( $this->donation ) ) {
198 $donation_id = $this->get_current_donation_id();
199 $this->donation = $donation_id ? charitable_get_donation( $donation_id ) : false;
200 }
201
202 return $this->donation;
203 }
204
205 /**
206 * Returns the current donation ID. If there is no current donation, return 0.
207 *
208 * @since 1.0.0
209 *
210 * @return int
211 */
212 public function get_current_donation_id() {
213 $donation_id = get_query_var( 'donation_id', 0 );
214
215 if ( ! $donation_id && isset( $_GET['donation_id'] ) ) {
216 $donation_id = $_GET['donation_id'];
217 }
218
219 return $donation_id;
220 }
221
222 /**
223 * If set, add supported donation parameters to the session.
224 *
225 * @since 1.6.25
226 *
227 * @param int $campaign_id The campaign receiving the donation.
228 * @return void
229 */
230 public function add_donation_params_to_session( $campaign_id ) {
231 if ( array_key_exists( 'amount', $_REQUEST ) ) {
232 $period = array_key_exists( 'period', $_REQUEST ) ? $_REQUEST['period'] : 'once';
233
234 charitable_get_session()->add_donation( $campaign_id, $_REQUEST['amount'], $period );
235 }
236 }
237
238 /**
239 * Temporarily set the current campaign to the one for the donation form.
240 *
241 * @since 1.6.55
242 *
243 * @param Charitable_Donation_Form $form The donation form object.
244 * @return void
245 */
246 public function set_current_campaign_before_donation_form( Charitable_Donation_form $form ) {
247 if ( isset( $this->campaign_id ) && $this->campaign_id === $form->get_campaign()->ID ) {
248 return;
249 }
250
251 if ( isset( $this->campaign ) ) {
252 $this->original_campaign = $this->campaign;
253 }
254
255 if ( isset( $this->campaign_id ) ) {
256 $this->original_campaign_id = $this->campaign_id;
257 }
258
259 $this->campaign = $form->get_campaign();
260 $this->campaign_id = $this->campaign->ID;
261 }
262
263 /**
264 * Reset the $campaign and $campaign_id properties to what they originally were.
265 *
266 * @since 1.6.55
267 *
268 * @return void
269 */
270 public function reset_current_campaign_after_donation_form() {
271 $this->campaign = $this->original_campaign;
272 $this->campaign_id = $this->original_campaign ? $this->original_campaign->ID : false;
273 }
274 }
275
276 endif;
277