Skip to content

Commit

Permalink
Deprecated give_get_purchase_stats_by_user() for give_get_donatiion_s…
Browse files Browse the repository at this point in the history
…tats_by_user() #896
  • Loading branch information
DevinWalker committed Jun 1, 2017
1 parent f3643d6 commit dd55482
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 45 deletions.
23 changes: 23 additions & 0 deletions includes/deprecated/deprecated-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,26 @@ function give_count_purchases_of_customer( $user = null ) {

return give_count_donations_of_donor( $user );
}


/**
* Get Donation Status for User.
*
* Retrieves the donation count and the total amount spent for a specific user.
*
* @access public
* @since 1.0
*
* @param int|string $user The ID or email of the donor to retrieve stats for.
*
* @return array
*/
function give_get_purchase_stats_by_user( $user = '' ) {

$backtrace = debug_backtrace();

_give_deprecated_function( __FUNCTION__, '1.8.9', 'give_count_donations_of_donor', $backtrace );

return give_get_donation_stats_by_user( $user );

}
75 changes: 36 additions & 39 deletions includes/user-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,43 +189,38 @@ function give_has_purchases( $user_id = null ) {


/**
* Get Donation Status for User
* Get Donation Status for User.
*
* Retrieves the donation count and the total amount spent for a specific user
* Retrieves the donation count and the total amount spent for a specific user.
*
* @access public
* @since 1.0
*
* @param int|string $user The ID or email of the donor to retrieve stats for
* @param int|string $user The ID or email of the donor to retrieve stats for.
*
* @return array
*/
function give_get_purchase_stats_by_user( $user = '' ) {
function give_get_donation_stats_by_user( $user = '' ) {

if ( is_email( $user ) ) {
$field = '';

if ( is_email( $user ) ) {
$field = 'email';

} elseif ( is_numeric( $user ) ) {

$field = 'user_id';

}

$stats = array();
$customer = Give()->donors->get_donor_by( $field, $user );

if ( $customer ) {

$customer = new Give_Donor( $customer->id );

$stats['purchases'] = absint( $customer->purchase_count );
$stats['total_spent'] = give_sanitize_amount( $customer->purchase_value );
$donor = Give()->donors->get_donor_by( $field, $user );

if ( $donor ) {
$donor = new Give_Donor( $donor->id );
$stats['purchases'] = absint( $donor->purchase_count );
$stats['total_spent'] = give_sanitize_amount( $donor->purchase_value );
}

/**
* Filter the donation stats
* Filter the donation stats.
*
* @since 1.7
*/
Expand Down Expand Up @@ -259,7 +254,7 @@ function give_count_donations_of_donor( $user = null ) {
$user = Give()->email_access->token_email;
}

$stats = ! empty( $user ) ? give_get_purchase_stats_by_user( $user ) : false;
$stats = ! empty( $user ) ? give_get_donation_stats_by_user( $user ) : false;

return isset( $stats['purchases'] ) ? $stats['purchases'] : 0;
}
Expand All @@ -276,7 +271,7 @@ function give_count_donations_of_donor( $user = null ) {
*/
function give_purchase_total_of_user( $user = null ) {

$stats = give_get_purchase_stats_by_user( $user );
$stats = give_get_donation_stats_by_user( $user );

return $stats['total_spent'];
}
Expand Down Expand Up @@ -455,13 +450,15 @@ function give_add_past_purchases_to_new_user( $user_id ) {

$email = get_the_author_meta( 'user_email', $user_id );

$payments = give_get_payments( array( 's' => $email ) );
$payments = give_get_payments( array(
's' => $email,
) );

if ( $payments ) {
foreach ( $payments as $payment ) {
if ( intval( give_get_payment_user_id( $payment->ID ) ) > 0 ) {
continue;
} // This payment already associated with an account
} // End if().

$meta = give_get_payment_meta( $payment->ID );
$meta['user_info'] = maybe_unserialize( $meta['user_info'] );
Expand Down Expand Up @@ -603,38 +600,38 @@ function give_new_user_notification( $user_id = 0, $user_data = array() ) {
* @access public
* @since 1.8.9
*
* @param int $id The ID of donation or donor
* @param string $from From will be a string to be passed as donation or donor
* @param int $id The ID of donation or donor
* @param string $from From will be a string to be passed as donation or donor
*
* @return string
*/
function give_get_donor_name_by( $id = 0, $from = 'donation' ) {

// ID shouldn't be empty
if( empty( $id ) ){
return;
}
// ID shouldn't be empty
if ( empty( $id ) ) {
return;
}

$name = '';
$name = '';

switch( $from ){
switch ( $from ) {

case 'donation':
case 'donation':

$user_info = give_get_payment_meta_user_info( $id );
$name = $user_info['first_name'] . ' ' . $user_info['last_name'];
$user_info = give_get_payment_meta_user_info( $id );
$name = $user_info['first_name'] . ' ' . $user_info['last_name'];

break;
break;

case 'donor':
case 'donor':

$donor = new Give_Donor( $id );
$name = $donor->name;
$donor = new Give_Donor( $id );
$name = $donor->name;

break;
break;

}
}

return trim( $name );
return trim( $name );

}
21 changes: 15 additions & 6 deletions tests/unit-tests/tests-donors.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ public function test_donor_notes() {
$this->assertEquals( $second_note[0], $note_1 );
}

/**
*
*/
public function test_users_purchases() {

$out = give_get_users_purchases( $this->_user_id );
Expand All @@ -306,6 +309,9 @@ public function test_users_purchases() {

}

/**
* Test users completed donations.
*/
public function test_give_get_users_completed_donations() {

$out2 = give_get_users_completed_donations( $this->_user_id );
Expand All @@ -317,14 +323,17 @@ public function test_give_get_users_completed_donations() {

}

public function test_get_purchase_stats_by_user() {
/**
* Test donation stats by user.
*/
public function test_get_donation_stats_by_user() {

$purchase_stats = give_get_purchase_stats_by_user( $this->_user_id );
$donation_stats = give_get_donation_stats_by_user( $this->_user_id );

$this->assertInternalType( 'array', $purchase_stats );
$this->assertEquals( 2, count( $purchase_stats ) );
$this->assertTrue( isset( $purchase_stats['purchases'] ) );
$this->assertTrue( isset( $purchase_stats['total_spent'] ) );
$this->assertInternalType( 'array', $donation_stats );
$this->assertEquals( 2, count( $donation_stats ) );
$this->assertTrue( isset( $donation_stats['purchases'] ) );
$this->assertTrue( isset( $donation_stats['total_spent'] ) );

}

Expand Down

0 comments on commit dd55482

Please sign in to comment.