| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class responsible for querying data about campaigns. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Campaigns |
| 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.0.0 |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Campaigns' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Campaigns. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
class Charitable_Campaigns { |
| 25 |
|
| 26 |
/** |
| 27 |
* Return WP_Query object with predefined defaults to query only campaigns. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* |
| 31 |
* @param array $args Query arguments. |
| 32 |
* @return WP_Query |
| 33 |
*/ |
| 34 |
public static function query( $args = array() ) { |
| 35 |
$defaults = array( |
| 36 |
'post_type' => array( 'campaign' ), |
| 37 |
'posts_per_page' => get_option( 'posts_per_page' ), |
| 38 |
); |
| 39 |
|
| 40 |
$args = wp_parse_args( $args, $defaults ); |
| 41 |
|
| 42 |
return new WP_Query( $args ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns a WP_Query that will return active campaigns, ordered by the date they're ending. |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
* |
| 50 |
* @param array $args Additional arguments to pass to WP_Query. |
| 51 |
* @return WP_Query |
| 52 |
*/ |
| 53 |
public static function ordered_by_ending_soon( $args = array() ) { |
| 54 |
$defaults = array( |
| 55 |
'meta_query' => array( |
| 56 |
array( |
| 57 |
'key' => '_campaign_end_date', |
| 58 |
'value' => date( 'Y-m-d H:i:s' ), |
| 59 |
'compare' => '>=', |
| 60 |
'type' => 'datetime', |
| 61 |
), |
| 62 |
), |
| 63 |
'meta_key' => '_campaign_end_date', |
| 64 |
'orderby' => 'meta_value', |
| 65 |
'order' => 'ASC', |
| 66 |
); |
| 67 |
|
| 68 |
$args = wp_parse_args( $args, $defaults ); |
| 69 |
|
| 70 |
return Charitable_Campaigns::query( $args ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Returns a WP_Query that will return campaigns, ordered by the amount they raised. |
| 75 |
* |
| 76 |
* @since 1.0.0 |
| 77 |
* |
| 78 |
* @param array $args Additional arguments to pass to WP_Query. |
| 79 |
* @return WP_Query |
| 80 |
*/ |
| 81 |
public static function ordered_by_amount( $args = array() ) { |
| 82 |
$defaults = array( |
| 83 |
'order' => 'DESC', |
| 84 |
); |
| 85 |
|
| 86 |
$args = wp_parse_args( $args, $defaults ); |
| 87 |
|
| 88 |
/* Set up filters to order by amount */ |
| 89 |
add_filter( 'posts_join_paged', array( 'Charitable_Campaigns', 'join_campaign_donations_table' ) ); |
| 90 |
add_filter( 'posts_groupby', array( 'Charitable_Campaigns', 'groupby_campaign_id' ) ); |
| 91 |
add_filter( 'posts_orderby', array( 'Charitable_Campaigns', 'orderby_campaign_donation_amount' ), 10, 2 ); |
| 92 |
|
| 93 |
$query = Charitable_Campaigns::query( $args ); |
| 94 |
|
| 95 |
/* Clean up filters */ |
| 96 |
remove_filter( 'posts_join_paged', array( 'Charitable_Campaigns', 'join_campaign_donations_table' ) ); |
| 97 |
remove_filter( 'posts_groupby', array( 'Charitable_Campaigns', 'groupby_campaign_id' ) ); |
| 98 |
remove_filter( 'posts_orderby', array( 'Charitable_Campaigns', 'orderby_campaign_donation_amount' ), 10, 2 ); |
| 99 |
|
| 100 |
return $query; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* A method used to join the campaign donations table on the campaigns query. |
| 105 |
* |
| 106 |
* @since 1.0.0 |
| 107 |
* |
| 108 |
* @global WPDB $wpdb |
| 109 |
* @param string $join_statement The join statement. |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
public static function join_campaign_donations_table( $join_statement ) { |
| 113 |
global $wpdb; |
| 114 |
|
| 115 |
$statuses = "'" . implode( "','", charitable_get_approval_statuses() ) . "'"; |
| 116 |
|
| 117 |
return $join_statement . " LEFT JOIN ( SELECT cd1.campaign_donation_id, cd1.donation_id, cd1.donor_id, cd1.amount, cd1.campaign_id |
| 118 |
FROM {$wpdb->prefix}charitable_campaign_donations cd1 |
| 119 |
INNER JOIN $wpdb->posts po1 ON cd1.donation_id = po1.ID |
| 120 |
WHERE po1.post_status IN ( $statuses ) |
| 121 |
) cd ON cd.campaign_id = $wpdb->posts.ID"; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* A method used to change the group by parameter of the campaigns query. |
| 126 |
* |
| 127 |
* @since 1.0.0 |
| 128 |
* |
| 129 |
* @global WPDB $wpdb |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
public static function groupby_campaign_id() { |
| 133 |
global $wpdb; |
| 134 |
return "$wpdb->posts.ID"; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* A method used to change the ordering of the campaigns query, to order by the amount donated. |
| 139 |
* |
| 140 |
* @since 1.0.0 |
| 141 |
* |
| 142 |
* @param string $orderby The current orderby value. |
| 143 |
* @param WP_Query $wp_query The WP_Query object. |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
public static function orderby_campaign_donation_amount( $orderby, WP_Query $wp_query ) { |
| 147 |
return 'COALESCE(SUM(cd.amount), 0) ' . $wp_query->get( 'order' ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
endif; |
| 152 |
|