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