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

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

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