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.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 1.7.0.9 All 211 releases
charitable / includes / campaigns / class-charitable-campaigns.php

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

190 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.8.9.1
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 * Return array
47 *
48 * @since 1.8.1
49 *
50 * @param array $args Query arguments.
51 * @return array
52 */
53 public static function get_campaign_title_id( $args = array() ) {
54 $defaults = array(
55 'post_status' => false,
56 );
57
58 $args = wp_parse_args( $args, $defaults );
59
60 $query = self::query( $args );
61
62 $posts = $query->posts;
63
64 $titles = array();
65
66 if ( $posts ) {
67 foreach ( $posts as $post ) {
68 $titles[] = array(
69 'id' => $post->ID,
70 'title' => $post->post_title,
71 );
72 }
73 }
74
75 return $titles;
76 }
77
78 /**
79 * Returns a WP_Query that will return active campaigns, ordered by the date they're ending.
80 *
81 * @since 1.0.0
82 * @version 1.8.9.1
83 *
84 * @param array $args Additional arguments to pass to WP_Query.
85 * @return WP_Query
86 */
87 public static function ordered_by_ending_soon( $args = array() ) {
88 $defaults = array(
89 'meta_query' => array(
90 array(
91 'key' => '_campaign_end_date',
92 'value' => date( 'Y-m-d H:i:s' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
93 'compare' => '>=',
94 'type' => 'datetime',
95 ),
96 ),
97 'meta_key' => '_campaign_end_date',
98 'orderby' => 'meta_value',
99 'order' => 'ASC',
100 );
101
102 $args = wp_parse_args( $args, $defaults );
103
104 return self::query( $args );
105 }
106
107 /**
108 * Returns a WP_Query that will return campaigns, ordered by the amount they raised.
109 *
110 * @since 1.0.0
111 *
112 * @param array $args Additional arguments to pass to WP_Query.
113 * @return WP_Query
114 */
115 public static function ordered_by_amount( $args = array() ) {
116 $defaults = array(
117 'order' => 'DESC',
118 );
119
120 $args = wp_parse_args( $args, $defaults );
121
122 /* Set up filters to order by amount */
123 add_filter( 'posts_join_paged', array( 'Charitable_Campaigns', 'join_campaign_donations_table' ) );
124 add_filter( 'posts_groupby', array( 'Charitable_Campaigns', 'groupby_campaign_id' ) );
125 add_filter( 'posts_orderby', array( 'Charitable_Campaigns', 'orderby_campaign_donation_amount' ), 10, 2 );
126
127 $query = self::query( $args );
128
129 /* Clean up filters */
130 remove_filter( 'posts_join_paged', array( 'Charitable_Campaigns', 'join_campaign_donations_table' ) );
131 remove_filter( 'posts_groupby', array( 'Charitable_Campaigns', 'groupby_campaign_id' ) );
132 remove_filter( 'posts_orderby', array( 'Charitable_Campaigns', 'orderby_campaign_donation_amount' ), 10, 2 );
133
134 return $query;
135 }
136
137 /**
138 * A method used to join the campaign donations table on the campaigns query.
139 *
140 * @since 1.0.0
141 *
142 * @global WPDB $wpdb
143 * @param string $join_statement The join statement.
144 * @return string
145 */
146 public static function join_campaign_donations_table( $join_statement ) {
147 global $wpdb;
148
149 $statuses = "'" . implode( "','", charitable_get_approval_statuses() ) . "'";
150
151 return $join_statement . " LEFT JOIN ( SELECT cd1.campaign_donation_id, cd1.donation_id, cd1.donor_id, cd1.amount, cd1.campaign_id
152 FROM {$wpdb->prefix}charitable_campaign_donations cd1
153 INNER JOIN $wpdb->posts po1 ON cd1.donation_id = po1.ID
154 WHERE po1.post_status IN ( $statuses )
155 ) cd ON cd.campaign_id = $wpdb->posts.ID";
156 }
157
158 /**
159 * A method used to change the group by parameter of the campaigns query.
160 *
161 * @since 1.0.0
162 *
163 * @global WPDB $wpdb
164 * @return string
165 */
166 public static function groupby_campaign_id() {
167 global $wpdb;
168 return "$wpdb->posts.ID";
169 }
170
171 /**
172 * A method used to change the ordering of the campaigns query, to order by the amount donated.
173 *
174 * WP_Query::get() returns the raw, unparsed query var, so the sort direction
175 * is validated against ASC/DESC before being added to the ORDER BY clause.
176 *
177 * @since 1.0.0
178 * @version 1.8.12.2
179 *
180 * @param string $orderby The current orderby value.
181 * @param WP_Query $wp_query The WP_Query object.
182 * @return string
183 */
184 public static function orderby_campaign_donation_amount( $orderby, WP_Query $wp_query ) {
185 return 'COALESCE(SUM(cd.amount), 0) ' . charitable_validate_sort_direction( $wp_query->get( 'order' ) );
186 }
187 }
188
189 endif;
190