PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.3
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.3
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 / donations / class-charitable-donations.php

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

126 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The class that is responsible for querying data about donations.
4 *
5 * @package Charitable/Classes/Charitable_Donations
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.6.39
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Donations' ) ) :
18
19 /**
20 * Charitable_Donations
21 *
22 * @since 1.0.0
23 */
24 class Charitable_Donations {
25
26 /**
27 * Return WP_Query object with predefined defaults to query only donations.
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( Charitable::DONATION_POST_TYPE ),
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 the number of all donations.
47 *
48 * @since 1.0.0
49 *
50 * @global WPDB $wpdb
51 * @param string $post_type Type of post to count.
52 * @return int
53 */
54 public static function count_all( $post_type = 'donation' ) {
55 global $wpdb;
56
57 $sql = "SELECT COUNT( * )
58 FROM $wpdb->posts
59 WHERE post_type = %s";
60
61 return $wpdb->get_var( $wpdb->prepare( $sql, $post_type ) );
62 }
63
64 /**
65 * Return count of donations grouped by status.
66 *
67 * @since 1.0.0
68 *
69 * @global WPDB $wpdb
70 * @param array $args Additional query arguments.
71 * @return array
72 */
73 public static function count_by_status( $args = array() ) {
74 global $wpdb;
75
76 $defaults = array(
77 's' => null,
78 'start_date' => null,
79 'end_date' => null,
80 'post_type' => 'donation',
81 'author' => null,
82 );
83
84 $args = wp_parse_args( $args, $defaults );
85
86 $where_clause = $wpdb->prepare( 'post_type = %s', $args['post_type'] );
87
88 if ( ! empty( $args['s'] ) ) {
89 $where_clause .= " AND ((post_title LIKE '%{$args['s']}%') OR (post_content LIKE '%{$args['s']}%'))";
90 }
91
92 if ( ! empty( $args['start_date'] ) ) {
93 $year = $args['start_date']['year'];
94 $month = $args['start_date']['month'];
95 $day = $args['start_date']['day'];
96
97 if ( false !== checkdate( $month, $day, $year ) ) {
98 $where_clause .= $wpdb->prepare( " AND post_date >= '%s'", date( 'Y-m-d', mktime( 0, 0, 0, $month, $day, $year ) ) );
99 }
100 }
101
102 if ( ! empty( $args['end_date'] ) ) {
103 $year = $args['end_date']['year'];
104 $month = $args['end_date']['month'];
105 $day = $args['end_date']['day'];
106
107 if ( false !== checkdate( $month, $day, $year ) ) {
108 $where_clause .= $wpdb->prepare( " AND post_date <= '%s'", date( 'Y-m-d', mktime( 0, 0, 0, $month, $day, $year ) ) );
109 }
110 }
111
112 if ( ! is_null( $args['author'] ) ) {
113 $where_clause .= $wpdb->prepare( " AND post_author = %d", $args['author'] );
114 }
115
116 $sql = "SELECT post_status, COUNT( * ) AS num_donations
117 FROM $wpdb->posts
118 WHERE $where_clause
119 GROUP BY post_status";
120
121 return $wpdb->get_results( $sql, OBJECT_K );
122 }
123 }
124
125 endif;
126