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

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