# charitable/1.8.13/includes/donations/class-charitable-donations.php

Charitable – Donation &amp; Fundraising Platform (Donation Forms, Recurring Donations &amp; Fundraising Campaigns), version 1.8.13. 127 lines.

- Page: https://pluginprobe.com/plugins/charitable/1.8.13/code/includes/donations/class-charitable-donations.php
- Raw: https://pluginprobe.com/plugins/charitable/1.8.13/raw/includes/donations/class-charitable-donations.php
- Modified: 2026-05-11T20:12:40+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/charitable/1.8.13/code/includes/donations/class-charitable-donations.php#L10-L20`.

```php
<?php
/**
 * The class that is responsible for querying data about donations.
 *
 * @package   Charitable/Classes/Charitable_Donations
 * @author    David Bisset
 * @copyright Copyright (c) 2023, WP Charitable LLC
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since     1.0.0
 * @version   1.6.39
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Charitable_Donations' ) ) :

	/**
	 * Charitable_Donations
	 *
	 * @since 1.0.0
	 */
	class Charitable_Donations {

		/**
		 * Return WP_Query object with predefined defaults to query only donations.
		 *
		 * @since  1.0.0
		 *
		 * @param  array $args Query arguments.
		 * @return WP_Query
		 */
		public static function query( $args = array() ) {
			$defaults = array(
				'post_type'      => array( Charitable::DONATION_POST_TYPE ),
				'posts_per_page' => get_option( 'posts_per_page' ),
			);

			$args = wp_parse_args( $args, $defaults );

			return new WP_Query( $args );
		}

		/**
		 * Return the number of all donations.
		 *
		 * @since  1.0.0
		 *
		 * @global WPDB $wpdb
		 * @param  string $post_type Type of post to count.
		 * @return int
		 */
		public static function count_all( $post_type = 'donation' ) {
			global $wpdb;

			$sql = "SELECT COUNT( * )
					FROM $wpdb->posts
					WHERE post_type = %s";

			return $wpdb->get_var( $wpdb->prepare( $sql, $post_type ) ); // phpcs:ignore
		}

		/**
		 * Return count of donations grouped by status.
		 *
		 * @since  1.0.0
		 *
		 * @global WPDB $wpdb
		 * @param  array $args Additional query arguments.
		 * @return array
		 */
		public static function count_by_status( $args = array() ) {
			global $wpdb;

			$defaults = array(
				's'          => null,
				'start_date' => null,
				'end_date'   => null,
				'post_type'  => 'donation',
				'author'     => null,
			);

			$args = wp_parse_args( $args, $defaults );

			$where_clause = $wpdb->prepare( 'post_type = %s', $args['post_type'] );

			if ( ! empty( $args['s'] ) ) {
				$like           = '%' . $wpdb->esc_like( $args['s'] ) . '%';
				$where_clause  .= $wpdb->prepare( ' AND ((post_title LIKE %s) OR (post_content LIKE %s))', $like, $like );
			}

			if ( ! empty( $args['start_date'] ) ) {
				$year  = $args['start_date']['year'];
				$month = $args['start_date']['month'];
				$day   = $args['start_date']['day'];

				if ( false !== checkdate( $month, $day, $year ) ) {
					$where_clause .= $wpdb->prepare( " AND post_date >= '%s'", date( 'Y-m-d', mktime( 0, 0, 0, $month, $day, $year ) ) ); // phpcs:ignore
				}
			}

			if ( ! empty( $args['end_date'] ) ) {
				$year  = $args['end_date']['year'];
				$month = $args['end_date']['month'];
				$day   = $args['end_date']['day'];

				if ( false !== checkdate( $month, $day, $year ) ) {
					$where_clause .= $wpdb->prepare( " AND post_date <= '%s'", date( 'Y-m-d', mktime( 0, 0, 0, $month, $day, $year ) ) ); // phpcs:ignore
				}
			}

			if ( ! is_null( $args['author'] ) ) {
				$where_clause .= $wpdb->prepare( " AND post_author = %d", $args['author'] ); // phpcs:ignore
			}

			$sql = "SELECT post_status, COUNT( * ) AS num_donations
				FROM $wpdb->posts
				WHERE $where_clause
				GROUP BY post_status";

			return $wpdb->get_results( $sql, OBJECT_K ); // phpcs:ignore
		}
	}

endif;

```
