| 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) 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.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 ) ); // phpcs:ignore |
| 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 |
$like = '%' . $wpdb->esc_like( $args['s'] ) . '%'; |
| 90 |
$where_clause .= $wpdb->prepare( ' AND ((post_title LIKE %s) OR (post_content LIKE %s))', $like, $like ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! empty( $args['start_date'] ) ) { |
| 94 |
$year = $args['start_date']['year']; |
| 95 |
$month = $args['start_date']['month']; |
| 96 |
$day = $args['start_date']['day']; |
| 97 |
|
| 98 |
if ( false !== checkdate( $month, $day, $year ) ) { |
| 99 |
$where_clause .= $wpdb->prepare( " AND post_date >= '%s'", date( 'Y-m-d', mktime( 0, 0, 0, $month, $day, $year ) ) ); // phpcs:ignore |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if ( ! empty( $args['end_date'] ) ) { |
| 104 |
$year = $args['end_date']['year']; |
| 105 |
$month = $args['end_date']['month']; |
| 106 |
$day = $args['end_date']['day']; |
| 107 |
|
| 108 |
if ( false !== checkdate( $month, $day, $year ) ) { |
| 109 |
$where_clause .= $wpdb->prepare( " AND post_date <= '%s'", date( 'Y-m-d', mktime( 0, 0, 0, $month, $day, $year ) ) ); // phpcs:ignore |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if ( ! is_null( $args['author'] ) ) { |
| 114 |
$where_clause .= $wpdb->prepare( " AND post_author = %d", $args['author'] ); // phpcs:ignore |
| 115 |
} |
| 116 |
|
| 117 |
$sql = "SELECT post_status, COUNT( * ) AS num_donations |
| 118 |
FROM $wpdb->posts |
| 119 |
WHERE $where_clause |
| 120 |
GROUP BY post_status"; |
| 121 |
|
| 122 |
return $wpdb->get_results( $sql, OBJECT_K ); // phpcs:ignore |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
endif; |
| 127 |
|