Completed
Pull Request — master (#664)
by Devin
19:01
created

actions.php ➔ give_complete_purchase()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 62
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7.392

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 31
c 1
b 1
f 0
nc 6
nop 3
dl 0
loc 62
rs 7.3333
ccs 24
cts 30
cp 0.8
crap 7.392

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Payment Actions
4
 *
5
 * @package     Give
6
 * @subpackage  Payments
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Complete a purchase aka donation
19
 *
20
 * Performs all necessary actions to complete a purchase.
21
 * Triggered by the give_update_payment_status() function.
22
 *
23
 * @since 1.0
24
 *
25
 * @param int $payment_id the ID number of the payment
26
 * @param string $new_status the status of the payment, probably "publish"
27
 * @param string $old_status the status of the payment prior to being marked as "complete", probably "pending"
28
 *
29
 * @return void
30
 */
31
function give_complete_purchase( $payment_id, $new_status, $old_status ) {
32
33
	// Make sure that payments are only completed once
34 34
	if ( $old_status == 'publish' || $old_status == 'complete' ) {
35
		return;
36
	}
37
38
	// Make sure the payment completion is only processed when new status is complete
39 34
	if ( $new_status != 'publish' && $new_status != 'complete' ) {
40
		return;
41
	}
42
43 34
44 34
	$payment = new Give_Payment( $payment_id );
45 34
46 34
	$creation_date  = get_post_field( 'post_date', $payment_id, 'raw' );
47 34
	$payment_meta   = $payment->payment_meta;
0 ignored issues
show
Documentation introduced by
The property $payment_meta is declared private in Give_Payment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
48 34
	$completed_date = $payment->completed_date;
0 ignored issues
show
Documentation introduced by
The property $completed_date is declared protected in Give_Payment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
	$user_info      = $payment->user_info;
0 ignored issues
show
Documentation introduced by
The property $user_info is declared private in Give_Payment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50 34
	$customer_id    = $payment->customer_id;
0 ignored issues
show
Documentation introduced by
The property $customer_id is declared protected in Give_Payment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
	$amount         = $payment->total;
0 ignored issues
show
Documentation introduced by
The property $total is declared protected in Give_Payment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
52 34
	$price_id       = $payment->price_id;
0 ignored issues
show
Documentation introduced by
The property $price_id is declared protected in Give_Payment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
53
	$form_id        = $payment->form_id;
0 ignored issues
show
Documentation introduced by
The property $form_id is declared protected in Give_Payment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
55 34
	do_action( 'give_pre_complete_purchase', $payment_id );
56
57 34
	// Ensure these actions only run once, ever
58
	if ( empty( $completed_date ) ) {
59
60
		give_record_sale_in_log( $form_id, $payment_id, $price_id, $creation_date );
61
		do_action( 'give_complete_form_donation', $form_id, $payment_id, $payment_meta );
62
63
	}
64
65 34
	// Increase the earnings for this form ID
66 34
	give_increase_earnings( $form_id, $amount );
67
	give_increase_purchase_count( $form_id );
68
69
	// Clear the total earnings cache
70 34
	delete_transient( 'give_earnings_total' );
71
	// Clear the This Month earnings (this_monththis_month is NOT a typo)
72 34
	delete_transient( md5( 'give_earnings_this_monththis_month' ) );
73 34
	delete_transient( md5( 'give_earnings_todaytoday' ) );
74
75
76
	// Increase the donor's purchase stats
77 34
	$customer = new Give_Customer( $customer_id );
78
	$customer->increase_purchase_count();
79 34
	$customer->increase_value( $amount );
80
81
	give_increase_total_earnings( $amount );
82 34
83
	// Ensure this action only runs once ever
84
	if ( empty( $completed_date ) ) {
85 34
86
		// Save the completed date
87 34
		$payment->completed_date = current_time( 'mysql' );
0 ignored issues
show
Documentation introduced by
The property $completed_date is declared protected in Give_Payment. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88 34
		$payment->save();
89
		do_action( 'give_complete_purchase', $payment_id );
90 34
	}
91
92
}
93
94
add_action( 'give_update_payment_status', 'give_complete_purchase', 100, 3 );
95
96
97
/**
98
 * Record payment status change
99
 *
100
 * @since 1.0
101
 *
102
 * @param int $payment_id the ID number of the payment
103
 * @param string $new_status the status of the payment, probably "publish"
104
 * @param string $old_status the status of the payment prior to being marked as "complete", probably "pending"
105
 *
106
 * @return void
107
 */
108
function give_record_status_change( $payment_id, $new_status, $old_status ) {
109 34
110 34
	// Get the list of statuses so that status in the payment note can be translated
111 34
	$stati      = give_get_payment_statuses();
112
	$old_status = isset( $stati[ $old_status ] ) ? $stati[ $old_status ] : $old_status;
113 34
	$new_status = isset( $stati[ $new_status ] ) ? $stati[ $new_status ] : $new_status;
114
115 34
	$status_change = sprintf( __( 'Status changed from %s to %s', 'give' ), $old_status, $new_status );
116 34
117
	give_insert_payment_note( $payment_id, $status_change );
118
}
119
120
add_action( 'give_update_payment_status', 'give_record_status_change', 100, 3 );
121
122
123
/**
124
 * Flushes the current user's purchase history transient when a payment status
125
 * is updated
126
 *
127
 * @since 1.0
128
 *
129
 * @param $payment_id
130
 * @param $new_status the status of the payment, probably "publish"
131
 * @param $old_status the status of the payment prior to being marked as "complete", probably "pending"
132
 */
133 34
function give_clear_user_history_cache( $payment_id, $new_status, $old_status ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $old_status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
134 34
135
	$payment = new Give_Payment( $payment_id );
136
137
	if ( ! empty( $payment->user_id ) ) {
0 ignored issues
show
Documentation introduced by
The property $user_id is declared protected in Give_Payment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
138
		delete_transient( 'give_user_' . $payment->user_id . '_purchases' );
0 ignored issues
show
Documentation introduced by
The property $user_id is declared protected in Give_Payment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
	}
140
141
}
142
143
add_action( 'give_update_payment_status', 'give_clear_user_history_cache', 10, 3 );
144
145
/**
146
 * Updates all old payments, prior to 1.2, with new
147
 * meta for the total purchase amount
148
 *
149
 * This is so that payments can be queried by their totals
150
 *
151
 * @since 1.0
152
 *
153
 * @param array $data Arguments passed
154
 *
155
 * @return void
156
 */
157
function give_update_old_payments_with_totals( $data ) {
158
	if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_upgrade_payments_nonce' ) ) {
159
		return;
160
	}
161
162
	if ( get_option( 'give_payment_totals_upgraded' ) ) {
163
		return;
164
	}
165
166
	$payments = give_get_payments( array(
167
		'offset' => 0,
168
		'number' => - 1,
169
		'mode'   => 'all'
170
	) );
171
172
	if ( $payments ) {
173
		foreach ( $payments as $payment ) {
174
175
			$payment = new Give_Payment( $payment->ID );
176
			$meta    = $payment->get_meta();
177
178 34
			$payment->total = $meta['amount'];
0 ignored issues
show
Documentation introduced by
The property $total is declared protected in Give_Payment. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
179
			$payment->save();
180 34
181 34
		}
182
	}
183
184
	add_option( 'give_payment_totals_upgraded', 1 );
185
}
186
187
add_action( 'give_upgrade_payments', 'give_update_old_payments_with_totals' );
188
189
/**
190
 * Updates week-old+ 'pending' orders to 'abandoned'
191
 *
192
 * @since 1.0
193
 * @return void
194
 */
195
function give_mark_abandoned_donations() {
196
	$args = array(
197
		'status' => 'pending',
198
		'number' => - 1,
199
		'output' => 'give_payments',
200
	);
201
202
	add_filter( 'posts_where', 'give_filter_where_older_than_week' );
203
204
	$payments = give_get_payments( $args );
205
206
	remove_filter( 'posts_where', 'give_filter_where_older_than_week' );
207
208
	if ( $payments ) {
209
		foreach ( $payments as $payment ) {
210
			$gateway = give_get_payment_gateway( $payment );
211
			//Skip offline gateway payments
212
			if ( $gateway == 'offline' ) {
213
				continue;
214
			}
215
			$payment->status = 'abandoned';
216
			$payment->save();
217
		}
218
	}
219
}
220
221
add_action( 'give_weekly_scheduled_events', 'give_mark_abandoned_donations' );