← All changes
|
includes/campaigns/class-charitable-campaigns.php
+53
-13
1.6.7
→
1.8.13
View file →
| @@ -2,16 +2,18 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * The class responsible for querying data about campaigns. |
| 4 | 4 | * |
| 5 | 5 | * @package Charitable/Classes/Charitable_Campaigns |
| 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.0.0 |
| 10 | - * @version 1.0.0 | |
| 10 | + * @version 1.8.9.1 | |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | -if ( ! defined( 'ABSPATH' ) ) { exit; } | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit; | |
| 15 | +} | |
| 14 | 16 | |
| 15 | 17 | if ( ! class_exists( 'Charitable_Campaigns' ) ) : |
| 16 | 18 | |
| 17 | 19 | /** |
| @@ -40,11 +42,45 @@ | ||
| 40 | 42 | return new WP_Query( $args ); |
| 41 | 43 | } |
| 42 | 44 | |
| 43 | 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 | + /** | |
| 44 | 79 | * Returns a WP_Query that will return active campaigns, ordered by the date they're ending. |
| 45 | 80 | * |
| 46 | 81 | * @since 1.0.0 |
| 82 | + * @version 1.8.9.1 | |
| 47 | 83 | * |
| 48 | 84 | * @param array $args Additional arguments to pass to WP_Query. |
| 49 | 85 | * @return WP_Query |
| 50 | 86 | */ |
| @@ -52,21 +88,21 @@ | ||
| 52 | 88 | $defaults = array( |
| 53 | 89 | 'meta_query' => array( |
| 54 | 90 | array( |
| 55 | 91 | 'key' => '_campaign_end_date', |
| 56 | - 'value' => date( 'Y-m-d H:i:s' ), | |
| 92 | + 'value' => date( 'Y-m-d H:i:s' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date | |
| 57 | 93 | 'compare' => '>=', |
| 58 | 94 | 'type' => 'datetime', |
| 59 | 95 | ), |
| 60 | 96 | ), |
| 61 | - 'meta_key' => '_campaign_end_date', | |
| 62 | - 'orderby' => 'meta_value', | |
| 63 | - 'order' => 'ASC', | |
| 97 | + 'meta_key' => '_campaign_end_date', | |
| 98 | + 'orderby' => 'meta_value', | |
| 99 | + 'order' => 'ASC', | |
| 64 | 100 | ); |
| 65 | 101 | |
| 66 | 102 | $args = wp_parse_args( $args, $defaults ); |
| 67 | 103 | |
| 68 | - return Charitable_Campaigns::query( $args ); | |
| 104 | + return self::query( $args ); | |
| 69 | 105 | } |
| 70 | 106 | |
| 71 | 107 | /** |
| 72 | 108 | * Returns a WP_Query that will return campaigns, ordered by the amount they raised. |
| @@ -87,9 +123,9 @@ | ||
| 87 | 123 | add_filter( 'posts_join_paged', array( 'Charitable_Campaigns', 'join_campaign_donations_table' ) ); |
| 88 | 124 | add_filter( 'posts_groupby', array( 'Charitable_Campaigns', 'groupby_campaign_id' ) ); |
| 89 | 125 | add_filter( 'posts_orderby', array( 'Charitable_Campaigns', 'orderby_campaign_donation_amount' ), 10, 2 ); |
| 90 | 126 | |
| 91 | - $query = Charitable_Campaigns::query( $args ); | |
| 127 | + $query = self::query( $args ); | |
| 92 | 128 | |
| 93 | 129 | /* Clean up filters */ |
| 94 | 130 | remove_filter( 'posts_join_paged', array( 'Charitable_Campaigns', 'join_campaign_donations_table' ) ); |
| 95 | 131 | remove_filter( 'posts_groupby', array( 'Charitable_Campaigns', 'groupby_campaign_id' ) ); |
| @@ -112,9 +148,9 @@ | ||
| 112 | 148 | |
| 113 | 149 | $statuses = "'" . implode( "','", charitable_get_approval_statuses() ) . "'"; |
| 114 | 150 | |
| 115 | 151 | return $join_statement . " LEFT JOIN ( SELECT cd1.campaign_donation_id, cd1.donation_id, cd1.donor_id, cd1.amount, cd1.campaign_id |
| 116 | - FROM {$wpdb->prefix}charitable_campaign_donations cd1 | |
| 152 | + FROM {$wpdb->prefix}charitable_campaign_donations cd1 | |
| 117 | 153 | INNER JOIN $wpdb->posts po1 ON cd1.donation_id = po1.ID |
| 118 | 154 | WHERE po1.post_status IN ( $statuses ) |
| 119 | 155 | ) cd ON cd.campaign_id = $wpdb->posts.ID"; |
| 120 | 156 | } |
| @@ -134,16 +170,20 @@ | ||
| 134 | 170 | |
| 135 | 171 | /** |
| 136 | 172 | * A method used to change the ordering of the campaigns query, to order by the amount donated. |
| 137 | 173 | * |
| 138 | - * @since 1.0.0 | |
| 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. | |
| 139 | 176 | * |
| 177 | + * @since 1.0.0 | |
| 178 | + * @version 1.8.12.2 | |
| 179 | + * | |
| 140 | 180 | * @param string $orderby The current orderby value. |
| 141 | 181 | * @param WP_Query $wp_query The WP_Query object. |
| 142 | 182 | * @return string |
| 143 | 183 | */ |
| 144 | 184 | public static function orderby_campaign_donation_amount( $orderby, WP_Query $wp_query ) { |
| 145 | - return 'COALESCE(SUM(cd.amount), 0) ' . $wp_query->get( 'order' ); | |
| 185 | + return 'COALESCE(SUM(cd.amount), 0) ' . charitable_validate_sort_direction( $wp_query->get( 'order' ) ); | |
| 146 | 186 | } |
| 147 | 187 | } |
| 148 | 188 | |
| 149 | 189 | endif; |