← All changes
|
includes/queries/class-charitable-donor-query.php
+44
-11
1.7.0.7
→
1.8.13
View file →
| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package Charitable/Classes/Charitable_Donor_Query |
| 6 | 6 | * @version 1.0.0 |
| 7 | 7 | * @author David Bisset |
| 8 | - * @copyright Copyright (c) 2022, WP Charitable LLC | |
| 8 | + * @copyright Copyright (c) 2023, WP Charitable LLC | |
| 9 | 9 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | // Exit if accessed directly. |
| @@ -223,9 +223,10 @@ | ||
| 223 | 223 | |
| 224 | 224 | /** |
| 225 | 225 | * Remove any hooks that have been attached by the class to prevent contaminating other queries. |
| 226 | 226 | * |
| 227 | - * @since 1.0.0 | |
| 227 | + * @since 1.0.0 | |
| 228 | + * @version 1.8.8 | |
| 228 | 229 | * |
| 229 | 230 | * @return void |
| 230 | 231 | */ |
| 231 | 232 | public function unhook_callbacks() { |
| @@ -239,14 +240,22 @@ | ||
| 239 | 240 | remove_filter( 'charitable_query_fields', array( $this, 'donation_amount_sum_field' ), 6 ); |
| 240 | 241 | remove_filter( 'charitable_query_join', array( $this, 'join_campaign_donations_table_on_donation' ), 5 ); |
| 241 | 242 | remove_filter( 'charitable_query_join', array( $this, 'join_donors_table' ), 6 ); |
| 242 | 243 | remove_filter( 'charitable_query_join', array( $this, 'join_meta' ), 7 ); |
| 243 | - remove_filter( 'charitable_query_where', array( $this, 'where_status_is_in' ), 5 ); | |
| 244 | - remove_filter( 'charitable_query_where', array( $this, 'where_campaign_is_in' ), 6 ); | |
| 244 | + | |
| 245 | + // Check if we should skip donation-based filters when including donors without donations. | |
| 246 | + $skip_donation_filters = apply_filters( 'charitable_donor_query_skip_donation_filters', false, $this ); | |
| 247 | + | |
| 248 | + // Only remove donation-based filters if they were added. | |
| 249 | + if ( ! $this->get( 'include_donors_without_donations' ) || ! $skip_donation_filters ) { | |
| 250 | + remove_filter( 'charitable_query_where', array( $this, 'where_status_is_in' ), 5 ); | |
| 251 | + remove_filter( 'charitable_query_where', array( $this, 'where_campaign_is_in' ), 6 ); | |
| 252 | + remove_filter( 'charitable_query_where', array( $this, 'where_date' ), 9 ); | |
| 253 | + remove_filter( 'charitable_query_where', array( $this, 'where_meta' ), 10 ); | |
| 254 | + } | |
| 255 | + | |
| 245 | 256 | remove_filter( 'charitable_query_where', array( $this, 'where_donor_id_is_in' ), 7 ); |
| 246 | 257 | remove_filter( 'charitable_query_where', array( $this, 'where_donor_is_not_erased' ), 8 ); |
| 247 | - remove_filter( 'charitable_query_where', array( $this, 'where_date' ), 9 ); | |
| 248 | - remove_filter( 'charitable_query_where', array( $this, 'where_meta' ), 10 ); | |
| 249 | 258 | remove_filter( 'charitable_query_groupby', array( $this, 'groupby_donor_id' ) ); |
| 250 | 259 | remove_filter( 'charitable_query_orderby', array( $this, 'orderby_date' ) ); |
| 251 | 260 | remove_filter( 'charitable_query_orderby', array( $this, 'orderby_count' ) ); |
| 252 | 261 | remove_filter( 'charitable_query_orderby', array( $this, 'orderby_donation_amount' ) ); |
| @@ -256,9 +265,10 @@ | ||
| 256 | 265 | |
| 257 | 266 | /** |
| 258 | 267 | * Set up callbacks for WP_Query filters. |
| 259 | 268 | * |
| 260 | - * @since 1.0.0 | |
| 269 | + * @since 1.0.0 | |
| 270 | + * @version 1.8.8 | |
| 261 | 271 | * |
| 262 | 272 | * @return void |
| 263 | 273 | */ |
| 264 | 274 | protected function prepare_query() { |
| @@ -268,14 +278,37 @@ | ||
| 268 | 278 | add_filter( 'charitable_select_count_fields', array( $this, 'select_donor_count' ) ); |
| 269 | 279 | add_filter( 'charitable_query_join', array( $this, 'join_campaign_donations_table_on_donation' ), 5 ); |
| 270 | 280 | add_filter( 'charitable_query_join', array( $this, 'join_donors_table' ), 6 ); |
| 271 | 281 | add_filter( 'charitable_query_join', array( $this, 'join_meta' ), 7 ); |
| 272 | - add_filter( 'charitable_query_where', array( $this, 'where_status_is_in' ), 5 ); | |
| 273 | - add_filter( 'charitable_query_where', array( $this, 'where_campaign_is_in' ), 6 ); | |
| 282 | + | |
| 283 | + /** | |
| 284 | + * Check if we should skip donation-based filters when including donors without donations. | |
| 285 | + * | |
| 286 | + * This filter allows you to opt into the new behavior where donation-based filters | |
| 287 | + * are skipped when querying donors without donations. By default, this is false | |
| 288 | + * to maintain backward compatibility. | |
| 289 | + * | |
| 290 | + * Usage: | |
| 291 | + * add_filter( 'charitable_donor_query_skip_donation_filters', '__return_true' ); | |
| 292 | + * | |
| 293 | + * @since 1.8.8 | |
| 294 | + * | |
| 295 | + * @param boolean $skip_donation_filters Whether to skip donation-based filters. | |
| 296 | + * @param Charitable_Donor_Query $query The current query instance. | |
| 297 | + */ | |
| 298 | + $skip_donation_filters = apply_filters( 'charitable_donor_query_skip_donation_filters', false, $this ); | |
| 299 | + | |
| 300 | + // Only apply donation-based filters when not including donors without donations | |
| 301 | + // or when the skip filter is not enabled. | |
| 302 | + if ( ! $this->get( 'include_donors_without_donations' ) || ! $skip_donation_filters ) { | |
| 303 | + add_filter( 'charitable_query_where', array( $this, 'where_status_is_in' ), 5 ); | |
| 304 | + add_filter( 'charitable_query_where', array( $this, 'where_campaign_is_in' ), 6 ); | |
| 305 | + add_filter( 'charitable_query_where', array( $this, 'where_date' ), 9 ); | |
| 306 | + add_filter( 'charitable_query_where', array( $this, 'where_meta' ), 10 ); | |
| 307 | + } | |
| 308 | + | |
| 274 | 309 | add_filter( 'charitable_query_where', array( $this, 'where_donor_id_is_in' ), 7 ); |
| 275 | 310 | add_filter( 'charitable_query_where', array( $this, 'where_donor_is_not_erased' ), 8 ); |
| 276 | - add_filter( 'charitable_query_where', array( $this, 'where_date' ), 9 ); | |
| 277 | - add_filter( 'charitable_query_where', array( $this, 'where_meta' ), 10 ); | |
| 278 | 311 | add_action( 'charitable_post_query', array( $this, 'unhook_callbacks' ) ); |
| 279 | 312 | } |
| 280 | 313 | |
| 281 | 314 | /** |