| 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) 2017, 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 |
add_action( 'the_post', array( $this, 'set_current_campaign' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns and/or create the single instance of this class. |
| 68 |
* |
| 69 |
* @since 1.2.0 |
| 70 |
* |
| 71 |
* @return Charitable_Request |
| 72 |
*/ |
| 73 |
public static function get_instance() { |
| 74 |
if ( is_null( self::$instance ) ) { |
| 75 |
self::$instance = new self(); |
| 76 |
} |
| 77 |
|
| 78 |
return self::$instance; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* When the_post is set, sets the current campaign to the current post if it is a campaign. |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* |
| 86 |
* @param WP_Post $post The Post object. |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function set_current_campaign( $post ) { |
| 90 |
if ( 'campaign' == $post->post_type ) { |
| 91 |
$this->campaign = new Charitable_Campaign( $post ); |
| 92 |
} else { |
| 93 |
unset( $this->campaign, $this->campaign_id ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the current campaign. If there is no current campaign, return false. |
| 99 |
* |
| 100 |
* @since 1.0.0 |
| 101 |
* |
| 102 |
* @return Charitable_Campaign|false Campaign object if we're viewing a campaign within a loop. False otherwise. |
| 103 |
*/ |
| 104 |
public function get_current_campaign() { |
| 105 |
if ( ! isset( $this->campaign ) ) { |
| 106 |
if ( $this->get_current_campaign_id() > 0 ) { |
| 107 |
$this->campaign = new Charitable_Campaign( $this->get_current_campaign_id() ); |
| 108 |
} else { |
| 109 |
$this->campaign = false; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return $this->campaign; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Returns the current campaign ID. If there is no current campaign, return 0. |
| 118 |
* |
| 119 |
* @since 1.0.0 |
| 120 |
* |
| 121 |
* @return int |
| 122 |
*/ |
| 123 |
public function get_current_campaign_id() { |
| 124 |
if ( isset( $this->campaign ) && $this->campaign ) { |
| 125 |
$this->campaign_id = $this->campaign->ID; |
| 126 |
} else { |
| 127 |
$this->campaign_id = 0; |
| 128 |
|
| 129 |
if ( get_post_type() == Charitable::CAMPAIGN_POST_TYPE ) { |
| 130 |
$this->campaign_id = get_the_ID(); |
| 131 |
} elseif ( get_query_var( 'donate', false ) ) { |
| 132 |
$session_donation = charitable_get_session()->get( 'donation' ); |
| 133 |
|
| 134 |
if ( false !== $session_donation ) { |
| 135 |
$this->campaign_id = $session_donation->get( 'campaign_id' ); |
| 136 |
} |
| 137 |
} |
| 138 |
}//end if |
| 139 |
|
| 140 |
if ( ! $this->campaign_id ) { |
| 141 |
$this->campaign_id = $this->get_campaign_id_from_submission(); |
| 142 |
} |
| 143 |
|
| 144 |
return $this->campaign_id; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the campaign ID from a form submission. |
| 149 |
* |
| 150 |
* @since 1.0.0 |
| 151 |
* |
| 152 |
* @return int |
| 153 |
*/ |
| 154 |
public function get_campaign_id_from_submission() { |
| 155 |
if ( ! isset( $_POST['campaign_id'] ) ) { |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
$campaign_id = absint( $_POST['campaign_id'] ); |
| 160 |
|
| 161 |
if ( Charitable::CAMPAIGN_POST_TYPE !== get_post_type( $campaign_id ) ) { |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
return $campaign_id; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Returns the current donation object. If there is no current donation, return false. |
| 170 |
* |
| 171 |
* @since 1.0.0 |
| 172 |
* |
| 173 |
* @return Charitable_Donation|false |
| 174 |
*/ |
| 175 |
public function get_current_donation() { |
| 176 |
if ( ! isset( $this->donation ) ) { |
| 177 |
$donation_id = $this->get_current_donation_id(); |
| 178 |
$this->donation = $donation_id ? charitable_get_donation( $donation_id ) : false; |
| 179 |
} |
| 180 |
|
| 181 |
return $this->donation; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Returns the current donation ID. If there is no current donation, return 0. |
| 186 |
* |
| 187 |
* @since 1.0.0 |
| 188 |
* |
| 189 |
* @return int |
| 190 |
*/ |
| 191 |
public function get_current_donation_id() { |
| 192 |
$donation_id = get_query_var( 'donation_id', 0 ); |
| 193 |
|
| 194 |
if ( ! $donation_id && isset( $_GET['donation_id'] ) ) { |
| 195 |
$donation_id = $_GET['donation_id']; |
| 196 |
} |
| 197 |
|
| 198 |
return $donation_id; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
endif; |
| 203 |
|