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

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

229 lines 5.2 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) 2020, Studio 164a
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.0.0
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 * Campaign ID.
43 *
44 * @var int
45 */
46 private $campaign_id;
47
48 /**
49 * Donation object.
50 *
51 * @var Charitable_Donation
52 */
53 private $donation;
54
55 /**
56 * Set up the class.
57 *
58 * Note that the only way to instantiate an object is with the on_start method,
59 * which can only be called during the start phase. In other words, don't try
60 * to instantiate this object.
61 *
62 * @since 1.0.0
63 */
64 private function __construct() {
65 /**
66 * Set the current campaign on the_post hook.
67 */
68 add_action( 'the_post', array( $this, 'set_current_campaign' ) );
69
70 /**
71 * Add any supported donation parameters to the session.
72 */
73 add_action( 'charitable_is_donate_page', array( $this, 'add_donation_params_to_session' ) );
74 }
75
76 /**
77 * Returns and/or create the single instance of this class.
78 *
79 * @since 1.2.0
80 *
81 * @return Charitable_Request
82 */
83 public static function get_instance() {
84 if ( is_null( self::$instance ) ) {
85 self::$instance = new self();
86 }
87
88 return self::$instance;
89 }
90
91 /**
92 * When the_post is set, sets the current campaign to the current post if it is a campaign.
93 *
94 * @since 1.0.0
95 *
96 * @param WP_Post $post The Post object.
97 * @return void
98 */
99 public function set_current_campaign( $post ) {
100 if ( 'campaign' == $post->post_type ) {
101 $this->campaign = new Charitable_Campaign( $post );
102 } else {
103 unset( $this->campaign, $this->campaign_id );
104 }
105 }
106
107 /**
108 * Returns the current campaign. If there is no current campaign, return false.
109 *
110 * @since 1.0.0
111 *
112 * @return Charitable_Campaign|false Campaign object if we're viewing a campaign within a loop. False otherwise.
113 */
114 public function get_current_campaign() {
115 if ( ! isset( $this->campaign ) ) {
116 if ( $this->get_current_campaign_id() > 0 ) {
117 $this->campaign = new Charitable_Campaign( $this->get_current_campaign_id() );
118 } else {
119 $this->campaign = false;
120 }
121 }
122
123 return $this->campaign;
124 }
125
126 /**
127 * Returns the current campaign ID. If there is no current campaign, return 0.
128 *
129 * @since 1.0.0
130 *
131 * @return int
132 */
133 public function get_current_campaign_id() {
134 if ( isset( $this->campaign ) && $this->campaign ) {
135 $this->campaign_id = $this->campaign->ID;
136 } else {
137 $this->campaign_id = 0;
138
139 if ( get_post_type() == Charitable::CAMPAIGN_POST_TYPE ) {
140 $this->campaign_id = get_the_ID();
141 } elseif ( get_query_var( 'donate', false ) ) {
142 $session_donation = charitable_get_session()->get( 'donation' );
143
144 if ( false !== $session_donation ) {
145 $this->campaign_id = $session_donation->get( 'campaign_id' );
146 }
147 }
148 }//end if
149
150 if ( ! $this->campaign_id ) {
151 $this->campaign_id = $this->get_campaign_id_from_submission();
152 }
153
154 return $this->campaign_id;
155 }
156
157 /**
158 * Returns the campaign ID from a form submission.
159 *
160 * @since 1.0.0
161 *
162 * @return int
163 */
164 public function get_campaign_id_from_submission() {
165 if ( ! isset( $_POST['campaign_id'] ) ) {
166 return 0;
167 }
168
169 $campaign_id = absint( $_POST['campaign_id'] );
170
171 if ( Charitable::CAMPAIGN_POST_TYPE !== get_post_type( $campaign_id ) ) {
172 return 0;
173 }
174
175 return $campaign_id;
176 }
177
178 /**
179 * Returns the current donation object. If there is no current donation, return false.
180 *
181 * @since 1.0.0
182 *
183 * @return Charitable_Donation|false
184 */
185 public function get_current_donation() {
186 if ( ! isset( $this->donation ) ) {
187 $donation_id = $this->get_current_donation_id();
188 $this->donation = $donation_id ? charitable_get_donation( $donation_id ) : false;
189 }
190
191 return $this->donation;
192 }
193
194 /**
195 * Returns the current donation ID. If there is no current donation, return 0.
196 *
197 * @since 1.0.0
198 *
199 * @return int
200 */
201 public function get_current_donation_id() {
202 $donation_id = get_query_var( 'donation_id', 0 );
203
204 if ( ! $donation_id && isset( $_GET['donation_id'] ) ) {
205 $donation_id = $_GET['donation_id'];
206 }
207
208 return $donation_id;
209 }
210
211 /**
212 * If set, add supported donation parameters to the session.
213 *
214 * @since 1.6.25
215 *
216 * @param int $campaign_id The campaign receiving the donation.
217 * @return void
218 */
219 public function add_donation_params_to_session( $campaign_id ) {
220 if ( array_key_exists( 'amount', $_REQUEST ) ) {
221 $period = array_key_exists( 'period', $_REQUEST ) ? $_REQUEST['period'] : 'once';
222
223 charitable_get_session()->add_donation( $campaign_id, $_REQUEST['amount'], $period );
224 }
225 }
226 }
227
228 endif;
229