Skip to content

Commit

Permalink
Merge pull request #2480 from ravinderk/issues/2397
Browse files Browse the repository at this point in the history
Revert #2397
  • Loading branch information
Devin Walker authored Dec 8, 2017
2 parents 4c40fb7 + 942cc48 commit 0f77111
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 104 deletions.
86 changes: 0 additions & 86 deletions includes/payments/class-payments-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ public function __construct( $args = array() ) {
'fields' => null,
'gateway' => null,
'give_forms' => null,

// Currently these params only works with get_payment_by_group
'group_by' => '',
'count' => false,
);

$this->args = $this->_args = wp_parse_args( $args, $defaults );
Expand Down Expand Up @@ -245,88 +241,6 @@ public function get_payments() {
return $this->payments;
}


/**
* Get payments by group
*
* @since 1.8.17
* @access public
*
* @return array
*/
public function get_payment_by_group() {
global $wpdb;

$allowed_groups = array( 'post_status' );
$result = array();


if ( in_array( $this->args['group_by'], $allowed_groups ) ) {
// Set only count in result.
if ( $this->args['count'] ) {
$statuses = get_post_stati();
$status_query = '';

$counter = 0;
foreach ( $statuses as $status ) {
$prefix = $counter ? " OR " : '';
$status_query .= "{$prefix}{$wpdb->posts}.post_status=\"{$status}\"";
$counter ++;
}

$new_results = $wpdb->get_results(
"
SELECT {$wpdb->posts}.post_status, COUNT({$wpdb->posts}.ID)
FROM {$wpdb->posts}
WHERE 1=1
AND {$wpdb->posts}.post_parent = 0
AND {$wpdb->posts}.post_type = 'give_payment'
AND (($status_query))
GROUP BY {$wpdb->posts}.{$this->args['group_by']}
ORDER BY {$wpdb->posts}.ID
DESC
"
, ARRAY_N );

foreach ( $new_results as $results ) {
$result[ $results[0] ] = $results[1];
}

switch ( $this->args['group_by'] ) {
case 'post_status':

if ( isset( $statuses['private'] ) && empty( $args['s'] ) ) {
unset( $statuses['private'] );
}

/* @var Give_Payment $donation */
foreach ( $statuses as $status => $status_label ) {
if ( ! isset( $result[ $status ] ) ) {
$result[ $status ] = 0;
}
}

break;
}
} else {
$donations = $this->get_payments();

/* @var $donation Give_Payment */
foreach ( $donations as $donation ) {
$result[ $donation->{$this->args['group_by']} ][] = $donation;
}
}
}


/**
* Filter the result
*
* @since 1.8.17
*/
return apply_filters( 'give_get_payment_by_group', $result, $this );
}

/**
* If querying a specific date, add the proper filters.
*
Expand Down
177 changes: 160 additions & 17 deletions includes/payments/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,31 +419,174 @@ function give_undo_donation( $payment_id ) {
* @return object $stats Contains the number of payments per payment status.
*/
function give_count_payments( $args = array() ) {
// Backward compatibility.
if( ! empty( $args['start-date'] ) ) {
$args['start_date'] = $args['start-date'];
unset( $args['start-date'] );

global $wpdb;

$defaults = array(
'user' => null,
's' => null,
'start-date' => null,
'end-date' => null,
'form_id' => null,
);

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

$select = 'SELECT p.post_status,count( * ) AS num_posts';
$join = '';
$where = "WHERE p.post_type = 'give_payment' AND p.post_status IN ('" . implode( "','", give_get_payment_status_keys() ) . "')";

// Count payments for a specific user.
if ( ! empty( $args['user'] ) ) {

if ( is_email( $args['user'] ) ) {
$field = 'email';
} elseif ( is_numeric( $args['user'] ) ) {
$field = 'id';
} else {
$field = '';
}

$join = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";

if ( ! empty( $field ) ) {
$where .= "
AND m.meta_key = '_give_payment_user_{$field}'
AND m.meta_value = '{$args['user']}'";
}
} elseif ( ! empty( $args['donor'] ) ) {

$join = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
$where .= "
AND m.meta_key = '_give_payment_customer_id'
AND m.meta_value = '{$args['donor']}'";

// Count payments for a search.
} elseif ( ! empty( $args['s'] ) ) {

if ( is_email( $args['s'] ) || strlen( $args['s'] ) == 32 ) {

if ( is_email( $args['s'] ) ) {
$field = '_give_payment_user_email';
} else {
$field = '_give_payment_purchase_key';
}

$join = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
$where .= $wpdb->prepare( '
AND m.meta_key = %s
AND m.meta_value = %s', $field, $args['s'] );

} elseif ( '#' == substr( $args['s'], 0, 1 ) ) {

$search = str_replace( '#:', '', $args['s'] );
$search = str_replace( '#', '', $search );

$select = 'SELECT p.post_status,count( * ) AS num_posts ';
$join = '';
$where = $wpdb->prepare( 'WHERE p.post_type=%s AND p.ID = %d ', 'give_payment', $search );

} elseif ( is_numeric( $args['s'] ) ) {

$join = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
$where .= $wpdb->prepare( "
AND m.meta_key = '_give_payment_user_id'
AND m.meta_value = %d", $args['s'] );

} else {
$search = $wpdb->esc_like( $args['s'] );
$search = '%' . $search . '%';

$where .= $wpdb->prepare( 'AND ((p.post_title LIKE %s) OR (p.post_content LIKE %s))', $search, $search );
}// End if().
}// End if().

if ( ! empty( $args['form_id'] ) && is_numeric( $args['form_id'] ) ) {

$join = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
$where .= $wpdb->prepare( '
AND m.meta_key = %s
AND m.meta_value = %s', '_give_payment_form_id', $args['form_id'] );
}

if( ! empty( $args['end-date'] ) ) {
$args['end_date'] = $args['end-date'];
unset( $args['end-date'] );
// Limit payments count by date.
if ( ! empty( $args['start-date'] ) && false !== strpos( $args['start-date'], '/' ) ) {

$date_parts = explode( '/', $args['start-date'] );
$month = ! empty( $date_parts[0] ) && is_numeric( $date_parts[0] ) ? $date_parts[0] : 0;
$day = ! empty( $date_parts[1] ) && is_numeric( $date_parts[1] ) ? $date_parts[1] : 0;
$year = ! empty( $date_parts[2] ) && is_numeric( $date_parts[2] ) ? $date_parts[2] : 0;

$is_date = checkdate( $month, $day, $year );
if ( false !== $is_date ) {

$date = new DateTime( $args['start-date'] );
$where .= $wpdb->prepare( " AND p.post_date >= '%s'", $date->format( 'Y-m-d' ) );

}

// Fixes an issue with the payments list table counts when no end date is specified (with stats class).
if ( empty( $args['end-date'] ) ) {
$args['end-date'] = $args['start-date'];
}
}

if( ! empty( $args['form_id'] ) ) {
$args['give_forms'] = $args['form_id'];
unset( $args['form_id'] );
if ( ! empty( $args['end-date'] ) && false !== strpos( $args['end-date'], '/' ) ) {

$date_parts = explode( '/', $args['end-date'] );

$month = ! empty( $date_parts[0] ) ? $date_parts[0] : 0;
$day = ! empty( $date_parts[1] ) ? $date_parts[1] : 0;
$year = ! empty( $date_parts[2] ) ? $date_parts[2] : 0;

$is_date = checkdate( $month, $day, $year );
if ( false !== $is_date ) {

$date = new DateTime( $args['end-date'] );
$where .= $wpdb->prepare( " AND p.post_date <= '%s'", $date->format( 'Y-m-d' ) );

}
}

// Extract all donations
$args['number'] = - 1;
$args['group_by'] = 'post_status';
$args['count'] = 'true';
$query = "$select
FROM $wpdb->posts p
$join
$where
GROUP BY p.post_status
";

$cache_key = md5( $query );

$donations_obj = new Give_Payments_Query( $args );
$donations_count = $donations_obj->get_payment_by_group();
$count = wp_cache_get( $cache_key, 'counts' );
if ( false !== $count ) {
return $count;
}

return (object) apply_filters( 'give_count_payments', $donations_count, $args, $donations_obj );
$count = $wpdb->get_results( $query, ARRAY_A );

$stats = array();
$statuses = get_post_stati();
if ( isset( $statuses['private'] ) && empty( $args['s'] ) ) {
unset( $statuses['private'] );
}

foreach ( $statuses as $state ) {
$stats[ $state ] = 0;
}

foreach ( (array) $count as $row ) {

if ( 'private' == $row['post_status'] && empty( $args['s'] ) ) {
continue;
}

$stats[ $row['post_status'] ] = $row['num_posts'];
}

$stats = (object) $stats;
wp_cache_set( $cache_key, $stats, 'counts' );

return $stats;
}


Expand Down
1 change: 0 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ Use Give for donations, and let WooCommerce or WP eCommerce or the like handle y
* Tweak: Enforced a font size of 16px for input fields within a Give modal window on mobile devices to prevent the zoom effect that happens and can cause issues with the cursor position. [#2239](https://github.com/WordImpress/Give/issues/2239)
* Tweak: The default country is now set to the base country within address fieldsets. [#2343](https://github.com/WordImpress/Give/issues/2343)
* Tweak: Performance improvement via checking before deleting any payment for quick exit within give_delete_donation(). [#2393](https://github.com/WordImpress/Give/issues/2393)
* Tweak: Performance improvement for get_payment_by_group(). [#2432](https://github.com/WordImpress/Give/issues/2432)
* Tweak: Donation goals now display without decimal amounts. [#2372](https://github.com/WordImpress/Give/issues/2372)
* Fix: Resolved WP dashboard timeouts due to inefficient stats query for sites with many donations. [#2383](https://github.com/WordImpress/Give/issues/2383)
* Fix: Floating labels were broken within the Give modal due to a CSS change in 1.8.16 - the floating labels functionality has been restored. [#2341](https://github.com/WordImpress/Give/issues/2341)
Expand Down

0 comments on commit 0f77111

Please sign in to comment.