PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
1.8.13.1 1.8.13 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 All 212 releases
charitable / includes / addons / charitable-benefactors / class-charitable-benefactor.php

class-charitable-benefactor.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at includes/addons/charitable-benefactors/class-charitable-benefactor.php

191 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The model for Benefactor relationships between Charitable campaigns and products in 3rd party extensions (EDD, WooCommerce, etc).
4 *
5 * @package Charitable/Classes/Charitable_Benefactor
6 * @version 1.0.0
7 * @author David Bisset
8 * @copyright Copyright (c) 2023, WP Charitable LLC
9 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Benefactor' ) ) :
18
19 /**
20 * Charitable_Benefactor
21 *
22 * @since 1.0.0
23 */
24 abstract class Charitable_Benefactor {
25
26 /**
27 * Core benefactor record.
28 *
29 * @var Object
30 */
31 protected $benefactor;
32
33 /**
34 * Create class object.
35 *
36 * @since 1.0.0
37 *
38 * @param mixed $benefactor Benefactor ID.
39 */
40 public function __construct( $benefactor ) {
41 if ( ! is_object( $benefactor ) ) {
42 $this->benefactor = charitable_get_table( 'benefactors' )->get( $benefactor );
43 }
44 }
45
46 /**
47 * Return an object of type Charitable_Benefactor, given a benefactor record and an extension.
48 *
49 * @since 1.0.0
50 *
51 * @param Object $benefactor Benefactor object.
52 * @param string $extension Extension generating the benefactor object.
53 * @return Charitable_Benefactor
54 */
55 public static function get_object( $benefactor, $extension ) {
56 $class = apply_filters( 'charitable_benefactor_class_' . $extension, false );
57
58 if ( ! class_exists( $class ) ) {
59 charitable_get_deprecated()->doing_it_wrong(
60 __METHOD__,
61 __( 'Benefactor class does not exist for given extension.', 'charitable' ),
62 '1.0.0'
63 );
64 }
65
66 return new $class( $benefactor );
67 }
68
69 /**
70 * Display a short one-line summary of a benefactor (how much is contributed and from where).
71 *
72 * @since 1.0.0
73 *
74 * @return string
75 */
76 public function __toString() {
77 return apply_filters( 'charitable_benefactor_summary', $this->get_contribution_description(), $this );
78 }
79
80 /**
81 * Magic getter method.
82 *
83 * @since 1.0.0
84 *
85 * @param string $key Key of the field to get.
86 * @return string
87 */
88 public function __get( $key ) {
89 return isset( $this->benefactor->$key ) ? $this->benefactor->$key : null;
90 }
91
92 /**
93 * Return the details of the benefactor (i.e. the 3rd party extension).
94 *
95 * @since 1.0.0
96 *
97 * @return Object
98 */
99 public function get_benefactor() {
100 return $this->benefactor;
101 }
102
103 /**
104 * Return a one-line description of the contribution.
105 *
106 * @return string
107 * @abstract
108 * @since 1.0.0
109 */
110 abstract public function get_contribution_description();
111
112 /**
113 * Return the contribution as a nicely formatted amount.
114 *
115 * @since 1.0.0
116 *
117 * @return string
118 */
119 public function get_contribution_amount() {
120 if ( $this->benefactor->contribution_amount_is_percentage ) {
121 $amount = apply_filters( 'charitable_benefactor_contribution_amount_percentage', $this->benefactor->contribution_amount . '%', $this->benefactor->contribution_amount, $this );
122 } else {
123 $amount = apply_filters( 'charitable_benefactor_contribution_amount_fixed', charitable_format_money( $this->benefactor->contribution_amount ), $this->benefactor->contribution_amount, $this );
124 }
125
126 return $amount;
127 }
128
129 /**
130 * Returns whether the benefit rule is active.
131 *
132 * @since 1.2.0
133 *
134 * @return boolean
135 */
136 public function is_active() {
137 return ! $this->is_expired;
138 }
139
140 /**
141 * Returns whether the benefit rule is active.
142 *
143 * @since 1.4.6
144 *
145 * @return boolean
146 */
147 public function is_expired() {
148 return '0000-00-00 00:00:00' != $this->benefactor->date_deactivated && strtotime( $this->benefactor->date_deactivated ) < time();
149 }
150
151 /**
152 * Return the benefit amount of a product based on the price, quantity and percent going to benefit.
153 *
154 * @since 1.0.0
155 *
156 * @param float $price Price of the item.
157 * @param int $quantity Number of items.
158 * @return float
159 */
160 protected function calculate_line_item_percent_contribution( $price, $quantity ) {
161 return $price * $quantity * ( $this->benefactor->contribution_amount / 100 );
162 }
163
164 /**
165 * Return the benefit amount of a product based on the quantity and fixed amount going per item.
166 *
167 * @since 1.0.0
168 *
169 * @param int $quantity The number of items.
170 * @return float
171 */
172 protected function calculate_line_item_fixed_contribution( $quantity = 1 ) {
173 return $quantity * $this->benefactor->contribution_amount;
174 }
175
176 /**
177 * Returns whether the benefactor's benefit is applied once per cart.
178 *
179 * False means that more benefit is added for every applicable item in the cart.
180 *
181 * @since 1.0.0
182 *
183 * @return boolean
184 */
185 protected function benefit_is_per_cart() {
186 return false === ( $this->benefactor->contribution_amount_is_per_item || $this->benefactor->contribution_amount_is_percentage );
187 }
188 }
189
190 endif;
191