| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donor model. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Donor |
| 6 |
* @version 1.0.0 |
| 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 |
// Exit if accessed directly |
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 14 |
|
| 15 |
if ( ! class_exists( 'Charitable_Donor' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Charitable_Donor |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class Charitable_Donor { |
| 23 |
|
| 24 |
/** |
| 25 |
* The donor ID. |
| 26 |
* |
| 27 |
* @var int |
| 28 |
* @access protected |
| 29 |
*/ |
| 30 |
protected $donor_id; |
| 31 |
|
| 32 |
/** |
| 33 |
* The donation ID. |
| 34 |
* |
| 35 |
* @var int |
| 36 |
* @access protected |
| 37 |
*/ |
| 38 |
protected $donation_id; |
| 39 |
|
| 40 |
/** |
| 41 |
* User object. |
| 42 |
* |
| 43 |
* @var Charitable_User |
| 44 |
* @access protected |
| 45 |
*/ |
| 46 |
protected $user; |
| 47 |
|
| 48 |
/** |
| 49 |
* Donation object. |
| 50 |
* |
| 51 |
* @var Charitable_Donation|null |
| 52 |
* @access protected |
| 53 |
*/ |
| 54 |
protected $donation = null; |
| 55 |
|
| 56 |
/** |
| 57 |
* Donor meta. |
| 58 |
* |
| 59 |
* @var mixed[] |
| 60 |
* @access protected |
| 61 |
*/ |
| 62 |
protected $donor_meta; |
| 63 |
|
| 64 |
/** |
| 65 |
* Create class object. |
| 66 |
* |
| 67 |
* @param int $donor_id |
| 68 |
* @param int $donation_id |
| 69 |
* @access public |
| 70 |
* @since 1.0.0 |
| 71 |
*/ |
| 72 |
public function __construct( $donor_id, $donation_id = false ) { |
| 73 |
$this->donor_id = $donor_id; |
| 74 |
$this->donation_id = $donation_id; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Magic getter method. Looks for the specified key in as a property before using Charitable_User's __get method. |
| 79 |
* |
| 80 |
* @return mixed |
| 81 |
* @access public |
| 82 |
* @since 1.0.0 |
| 83 |
*/ |
| 84 |
public function __get( $key ) { |
| 85 |
if ( isset( $this->$key ) ) { |
| 86 |
return $this->$key; |
| 87 |
} |
| 88 |
|
| 89 |
return $this->get_user()->$key; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* A thin wrapper around the Charitable_User::get() method. |
| 94 |
* |
| 95 |
* @param string $key |
| 96 |
* @return mixed |
| 97 |
* @access public |
| 98 |
* @since 1.2.4 |
| 99 |
*/ |
| 100 |
public function get( $key ) { |
| 101 |
return $this->get_user()->get( $key ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Return the Charitable_User object for this donor. |
| 106 |
* |
| 107 |
* @return Charitable_User |
| 108 |
* @access public |
| 109 |
* @since 1.0.0 |
| 110 |
*/ |
| 111 |
public function get_user() { |
| 112 |
if ( ! isset( $this->user ) ) { |
| 113 |
$this->user = $this->user = Charitable_User::init_with_donor( $this->donor_id ); |
| 114 |
} |
| 115 |
|
| 116 |
return $this->user; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Return the Charitable_Donation object associated with this object. |
| 121 |
* |
| 122 |
* @return Charitable_Donation|false |
| 123 |
* @access public |
| 124 |
* @since 1.0.0 |
| 125 |
*/ |
| 126 |
public function get_donation() { |
| 127 |
if ( ! isset( $this->donation ) ) { |
| 128 |
$this->donation = $this->donation_id ? new Charitable_Donation( $this->donation_id ) : false; |
| 129 |
} |
| 130 |
|
| 131 |
return $this->donation; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Return the donor meta stored for the particular donation. |
| 136 |
* |
| 137 |
* @param string $key Optional key passed to return a particular meta field. |
| 138 |
* @return array|false |
| 139 |
* @access public |
| 140 |
* @since 1.0.0 |
| 141 |
*/ |
| 142 |
public function get_donor_meta( $key = '' ) { |
| 143 |
if ( ! $this->get_donation() ) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! isset( $this->donor_meta ) ) { |
| 148 |
$this->donor_meta = get_post_meta( $this->donation_id, 'donor', true ); |
| 149 |
} |
| 150 |
|
| 151 |
if ( empty( $key ) ) { |
| 152 |
return $this->donor_meta; |
| 153 |
} |
| 154 |
|
| 155 |
return isset( $this->donor_meta[ $key ] ) ? $this->donor_meta[ $key ] : ''; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Return a value from the donor meta. |
| 160 |
* |
| 161 |
* @param string $key |
| 162 |
* @return mixed |
| 163 |
* @access public |
| 164 |
* @since 1.2.4 |
| 165 |
*/ |
| 166 |
public function get_value( $key ) { |
| 167 |
$meta = $this->get_donor_meta(); |
| 168 |
|
| 169 |
if ( ! $meta || ! isset( $meta[ $key ] ) ) { |
| 170 |
return ''; |
| 171 |
} |
| 172 |
|
| 173 |
return $meta[ $key ]; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Return the donor's name stored for the particular donation. |
| 178 |
* |
| 179 |
* @return string |
| 180 |
* @access public |
| 181 |
* @since 1.0.0 |
| 182 |
*/ |
| 183 |
public function get_name() { |
| 184 |
if ( ! $this->get_donor_meta() ) { |
| 185 |
return $this->get_user()->get_name(); |
| 186 |
} |
| 187 |
|
| 188 |
$meta = $this->get_donor_meta(); |
| 189 |
$first_name = isset( $meta[ 'first_name' ] ) ? $meta[ 'first_name' ] : ''; |
| 190 |
$last_name = isset( $meta[ 'last_name' ] ) ? $meta[ 'last_name' ] : ''; |
| 191 |
$name = trim( sprintf( '%s %s', $first_name, $last_name ) ); |
| 192 |
|
| 193 |
return apply_filters( 'charitable_donor_name', $name, $this ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Return the donor's email address. |
| 198 |
* |
| 199 |
* @return string |
| 200 |
* @access public |
| 201 |
* @since 1.2.4 |
| 202 |
*/ |
| 203 |
public function get_email() { |
| 204 |
return $this->get_value( 'email' ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Return the donor's address. |
| 209 |
* |
| 210 |
* @return string |
| 211 |
* @access public |
| 212 |
* @since 1.2.4 |
| 213 |
*/ |
| 214 |
public function get_address() { |
| 215 |
return $this->get_user()->get_address( $this->donation_id ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Return the donor avatar. |
| 220 |
* |
| 221 |
* @param int $size |
| 222 |
* @return string |
| 223 |
* @access public |
| 224 |
* @since 1.0.0 |
| 225 |
*/ |
| 226 |
public function get_avatar( $size = 100 ) { |
| 227 |
return apply_filters( 'charitable_donor_avatar', $this->get_user()->get_avatar(), $this ); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Return the donor location. |
| 232 |
* |
| 233 |
* @return string |
| 234 |
* @access public |
| 235 |
* @since 1.0.0 |
| 236 |
*/ |
| 237 |
public function get_location() { |
| 238 |
if ( ! $this->get_donor_meta() ) { |
| 239 |
return $this->get_user()->get_location(); |
| 240 |
} |
| 241 |
|
| 242 |
$meta = $this->get_donor_meta(); |
| 243 |
$city = isset( $meta[ 'city' ] ) ? $meta[ 'city' ] : ''; |
| 244 |
$state = isset( $meta[ 'state' ] ) ? $meta[ 'state' ] : ''; |
| 245 |
$country = isset( $meta[ 'country' ] ) ? $meta[ 'country' ] : ''; |
| 246 |
|
| 247 |
$region = strlen( $city ) ? $city : $state; |
| 248 |
|
| 249 |
if ( strlen( $country ) ) { |
| 250 |
|
| 251 |
if ( strlen( $region ) ) { |
| 252 |
$location = sprintf( '%s, %s', $region, $country ); |
| 253 |
} |
| 254 |
else { |
| 255 |
$location = $country; |
| 256 |
} |
| 257 |
} |
| 258 |
else { |
| 259 |
$location = $region; |
| 260 |
} |
| 261 |
|
| 262 |
return apply_filters( 'charitable_donor_location', $location, $this ); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Return the donation amount. |
| 267 |
* |
| 268 |
* If a donation ID was passed to the object constructor, this will return |
| 269 |
* the total donated with this particular donation. Otherwise, this will |
| 270 |
* return the total amount ever donated by the donor. |
| 271 |
* |
| 272 |
* @param int $campaign_id Optional. If set, returns total donated to this particular campaign. |
| 273 |
* @return decimal |
| 274 |
* @access public |
| 275 |
* @since 1.0.0 |
| 276 |
*/ |
| 277 |
public function get_amount( $campaign_id = false ) { |
| 278 |
if ( $this->get_donation() ) { |
| 279 |
return $this->get_donation_amount( $campaign_id ); |
| 280 |
} |
| 281 |
|
| 282 |
return $this->get_user()->get_total_donated( $campaign_id ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Return the amount of the donation. |
| 287 |
* |
| 288 |
* @param int $campaign_id Optional. If set, returns the amount donated to the campaign. |
| 289 |
* @return decimal |
| 290 |
* @access public |
| 291 |
* @since 1.2.0 |
| 292 |
*/ |
| 293 |
public function get_donation_amount( $campaign_id = "" ) { |
| 294 |
return charitable_get_table( 'campaign_donations' )->get_donation_amount( $this->donation_id, $campaign_id ); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
endif; // End class_exists check |