| 1 |
<?php |
| 2 |
/** |
| 3 |
* A helper class to retrieve Donations. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Donations_Query |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.4.0 |
| 10 |
* @version 1.6.54 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Donations_Query' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Donations_Query |
| 22 |
* |
| 23 |
* @since 1.4.0 |
| 24 |
*/ |
| 25 |
class Charitable_Donations_Query extends Charitable_Query { |
| 26 |
|
| 27 |
/** |
| 28 |
* Create class object. |
| 29 |
* |
| 30 |
* @since 1.4.0 |
| 31 |
* |
| 32 |
* @param array $args Arguments used in query. |
| 33 |
*/ |
| 34 |
public function __construct( $args = array() ) { |
| 35 |
$defaults = array( |
| 36 |
// Use 'posts' to get standard post objects. |
| 37 |
'output' => 'donations', |
| 38 |
// Set to an array with statuses to only show certain statuses. |
| 39 |
'status' => false, |
| 40 |
// Currently only supports 'date'. |
| 41 |
'orderby' => 'date', |
| 42 |
// May be 'DESC' or 'ASC'. |
| 43 |
'order' => 'DESC', |
| 44 |
// Number of donations to retrieve. |
| 45 |
'number' => 20, |
| 46 |
// For paged results. |
| 47 |
'paged' => 1, |
| 48 |
// Only get donations for a specific campaign. |
| 49 |
'campaign' => 0, |
| 50 |
// Only get donations by a specific donor. |
| 51 |
'donor_id' => 0, |
| 52 |
// Only get donations by a specific user. |
| 53 |
'user_id' => 0, |
| 54 |
// Donation plan is essentially post_parent. |
| 55 |
'donation_plan' => 0, |
| 56 |
// Filter donations by date. |
| 57 |
'date_query' => array(), |
| 58 |
// Filter donations by meta. |
| 59 |
'meta_query' => array(), |
| 60 |
); |
| 61 |
|
| 62 |
$this->args = wp_parse_args( $args, $defaults ); |
| 63 |
|
| 64 |
$this->position = 0; |
| 65 |
$this->prepare_query(); |
| 66 |
$this->results = $this->get_donations(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Return list of donation IDs together with the number of donations they have made. |
| 71 |
* |
| 72 |
* @since 1.4.0 |
| 73 |
* |
| 74 |
* @return object[] |
| 75 |
*/ |
| 76 |
public function get_donations() { |
| 77 |
$records = $this->query(); |
| 78 |
|
| 79 |
/* Return the raw records. */ |
| 80 |
if ( in_array( $this->get( 'output' ), array( 'count', 'ids' ) ) ) { |
| 81 |
return $records; |
| 82 |
} |
| 83 |
|
| 84 |
/* Return Donations objects. */ |
| 85 |
if ( 'donations' == $this->get( 'output' ) ) { |
| 86 |
return array_map( 'charitable_get_donation', wp_list_pluck( $records, 'ID' ) ); |
| 87 |
} |
| 88 |
|
| 89 |
$currency_helper = charitable_get_currency_helper(); |
| 90 |
|
| 91 |
/** |
| 92 |
* When the currency uses commas for decimals and periods for thousands, |
| 93 |
* the amount returned from the database needs to be sanitized. |
| 94 |
*/ |
| 95 |
if ( $currency_helper->is_comma_decimal() ) { |
| 96 |
foreach ( $records as $i => $row ) { |
| 97 |
$records[ $i ]->amount = $currency_helper->sanitize_database_amount( $row->amount ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return $records; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Run the IDs query, returning the results. |
| 106 |
* |
| 107 |
* @global WPDB $wpdb |
| 108 |
* |
| 109 |
* @since 1.6.54 |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function run_query_ids() { |
| 114 |
global $wpdb; |
| 115 |
|
| 116 |
$sql = "SELECT {$this->fields()} {$this->from()} {$this->join()} {$this->where()} {$this->groupby()} {$this->orderby()} {$this->order()} {$this->limit()} {$this->offset()};"; |
| 117 |
|
| 118 |
return $wpdb->get_col( $this->get_prepared_sql( $sql ) ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Set up fields query argument. |
| 123 |
* |
| 124 |
* @since 1.4.0 |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
public function setup_fields() { |
| 129 |
/* If we are returning IDs or Donation objects, we only need to return the donation IDs. */ |
| 130 |
if ( in_array( $this->get( 'output' ), array( 'donations', 'ids' ) ) ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
add_filter( 'charitable_query_fields', array( $this, 'donation_fields' ), 4 ); |
| 135 |
add_filter( 'charitable_query_fields', array( $this, 'donation_calc_fields' ), 5 ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Set up orderby query argument. |
| 140 |
* |
| 141 |
* @since 1.4.0 |
| 142 |
* |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function setup_orderby() { |
| 146 |
$orderby = $this->get( 'orderby', false ); |
| 147 |
|
| 148 |
if ( ! $orderby ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
switch ( $orderby ) { |
| 153 |
case 'date': |
| 154 |
add_filter( 'charitable_query_orderby', array( $this, 'orderby_date' ) ); |
| 155 |
break; |
| 156 |
|
| 157 |
case 'amount': |
| 158 |
add_filter( 'charitable_query_orderby', array( $this, 'orderby_donation_amount' ) ); |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Remove any hooks that have been attached by the class to prevent contaminating other queries. |
| 165 |
* |
| 166 |
* @since 1.4.0 |
| 167 |
* |
| 168 |
* @return void |
| 169 |
*/ |
| 170 |
public function unhook_callbacks() { |
| 171 |
remove_action( 'charitable_pre_query', array( $this, 'setup_fields' ) ); |
| 172 |
remove_filter( 'charitable_query_fields', array( $this, 'donation_fields' ), 4 ); |
| 173 |
remove_filter( 'charitable_query_fields', array( $this, 'donation_calc_fields' ), 5 ); |
| 174 |
remove_filter( 'charitable_query_join', array( $this, 'join_campaign_donations_table_on_donation' ), 5 ); |
| 175 |
remove_filter( 'charitable_query_join', array( $this, 'join_meta' ), 6 ); |
| 176 |
remove_filter( 'charitable_query_where', array( $this, 'where_donation_plan_is_in' ), 4 ); |
| 177 |
remove_filter( 'charitable_query_where', array( $this, 'where_status_is_in' ), 5 ); |
| 178 |
remove_filter( 'charitable_query_where', array( $this, 'where_campaign_is_in' ), 6 ); |
| 179 |
remove_filter( 'charitable_query_where', array( $this, 'where_donor_id_is_in' ), 7 ); |
| 180 |
remove_filter( 'charitable_query_where', array( $this, 'where_user_id_is_in' ), 8 ); |
| 181 |
remove_filter( 'charitable_query_where', array( $this, 'where_date' ), 9 ); |
| 182 |
remove_filter( 'charitable_query_where', array( $this, 'where_meta' ), 10 ); |
| 183 |
remove_action( 'charitable_pre_query', array( $this, 'setup_orderby' ) ); |
| 184 |
remove_filter( 'charitable_query_orderby', array( $this, 'orderby_date' ) ); |
| 185 |
remove_filter( 'charitable_query_orderby', array( $this, 'orderby_donation_amount' ) ); |
| 186 |
remove_filter( 'charitable_query_groupby', array( $this, 'groupby_donation_id' ) ); |
| 187 |
remove_action( 'charitable_post_query', array( $this, 'unhook_callbacks' ) ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Set up callbacks for WP_Query filters. |
| 192 |
* |
| 193 |
* @since 1.4.0 |
| 194 |
* |
| 195 |
* @return void |
| 196 |
*/ |
| 197 |
protected function prepare_query() { |
| 198 |
add_action( 'charitable_pre_query', array( $this, 'setup_fields' ) ); |
| 199 |
add_action( 'charitable_pre_query', array( $this, 'setup_orderby' ) ); |
| 200 |
add_filter( 'charitable_query_join', array( $this, 'join_campaign_donations_table_on_donation' ), 5 ); |
| 201 |
add_filter( 'charitable_query_join', array( $this, 'join_meta' ), 6 ); |
| 202 |
add_filter( 'charitable_query_where', array( $this, 'where_donation_plan_is_in' ), 4 ); |
| 203 |
add_filter( 'charitable_query_where', array( $this, 'where_status_is_in' ), 5 ); |
| 204 |
add_filter( 'charitable_query_where', array( $this, 'where_campaign_is_in' ), 6 ); |
| 205 |
add_filter( 'charitable_query_where', array( $this, 'where_donor_id_is_in' ), 7 ); |
| 206 |
add_filter( 'charitable_query_where', array( $this, 'where_user_id_is_in' ), 8 ); |
| 207 |
add_filter( 'charitable_query_where', array( $this, 'where_date' ), 9 ); |
| 208 |
add_filter( 'charitable_query_where', array( $this, 'where_meta' ), 10 ); |
| 209 |
add_filter( 'charitable_query_groupby', array( $this, 'groupby_donation_id' ) ); |
| 210 |
add_action( 'charitable_post_query', array( $this, 'unhook_callbacks' ) ); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
endif; |
| 215 |
|