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

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