Completed
Pull Request — master (#859)
by Devin
19:40
created

actions.php ➔ give_complete_purchase()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 68
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 31
c 1
b 0
f 1
nc 6
nop 3
dl 0
loc 68
ccs 33
cts 33
cp 1
crap 7
rs 6.9654

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 42
	if ( $old_status == 'publish' || $old_status == 'complete' ) {
35 6
		return;
36
	}
37
38
	// Make sure the payment completion is only processed when new status is complete
39 42
	if ( $new_status != 'publish' && $new_status != 'complete' ) {
40 1
		return;
41
	}
42
	
43 42
	$payment = new Give_Payment( $payment_id );
44
45 42
	$creation_date  = get_post_field( 'post_date', $payment_id, 'raw' );
46 42
	$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...
47 42
	$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...
48 42
	$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...
49 42
	$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...
50 42
	$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...
51 42
	$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...
52 42
	$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...
53
54 42
	do_action( 'give_pre_complete_purchase', $payment_id );
55
56
	// Ensure these actions only run once, ever
57 42
	if ( empty( $completed_date ) ) {
58
59 42
		give_record_sale_in_log( $form_id, $payment_id, $price_id, $creation_date );
60 42
		do_action( 'give_complete_form_donation', $form_id, $payment_id, $payment_meta );
61
62 42
	}
63
64
	// Increase the earnings for this form ID
65 42
	give_increase_earnings( $form_id, $amount );
66 42
	give_increase_purchase_count( $form_id );
67
68
	// Clear the total earnings cache
69 42
	delete_transient( 'give_earnings_total' );
70
	// Clear the This Month earnings (this_monththis_month is NOT a typo)
71 42
	delete_transient( md5( 'give_earnings_this_monththis_month' ) );
72 42
	delete_transient( md5( 'give_earnings_todaytoday' ) );
73
	
74
	// Increase the donor's purchase stats
75 42
	$customer = new Give_Customer( $customer_id );
76 42
	$customer->increase_purchase_count();
77 42
	$customer->increase_value( $amount );
78
79 42
	give_increase_total_earnings( $amount );
80
81
	// Ensure this action only runs once ever
82 42
	if ( empty( $completed_date ) ) {
83
84
		// Save the completed date
85 42
		$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...
86 42
		$payment->save();
87 42
88 42
		/**
89
		 * Fires after a purchase/donation successfully complete.
90 42
		 *
91
		 * @since 1.6
92
		 *
93
		 * @param int $payment_id The ID of the payment.
94
		 */
95
		do_action( 'give_complete_purchase', $payment_id );
96
	}
97
98
}
99
100
add_action( 'give_update_payment_status', 'give_complete_purchase', 100, 3 );
101
102
103
/**
104
 * Record payment status change
105
 *
106
 * @since  1.0
107
 *
108
 * @param  int    $payment_id The ID number of the payment.
109 42
 * @param  string $new_status The status of the payment, probably "publish".
110 42
 * @param  string $old_status The status of the payment prior to being marked as "complete", probably "pending".
111 42
 *
112
 * @return void
113 42
 */
114
function give_record_status_change( $payment_id, $new_status, $old_status ) {
115 42
116 42
	// Get the list of statuses so that status in the payment note can be translated
117
	$stati      = give_get_payment_statuses();
118 42
	$old_status = isset( $stati[ $old_status ] ) ? $stati[ $old_status ] : $old_status;
119
	$new_status = isset( $stati[ $new_status ] ) ? $stati[ $new_status ] : $new_status;
120 42
121 42
	$status_change = sprintf(
122
		/* translators: 1: old status 2: new status */
123
		esc_html__( 'Status changed from %1$s to %2$s.', 'give' ),
124
		$old_status,
125
		$new_status
126
	);
127
128
	give_insert_payment_note( $payment_id, $status_change );
129
}
130
131
add_action( 'give_update_payment_status', 'give_record_status_change', 100, 3 );
132
133
134
/**
135
 * Clear User History Cache
136
 *
137
 * Flushes the current user's purchase history transient when a payment status
138 42
 * is updated.
139
 *
140 42
 * @since  1.0
141 41
 *
142 41
 * @param  int    $payment_id The ID number of the payment.
143
 * @param  string $new_status The status of the payment, probably "publish".
144 42
 * @param  string $old_status The status of the payment prior to being marked as "complete", probably "pending".
145
 *
146
 * @return void
147
 */
148
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...
149
150
	$payment = new Give_Payment( $payment_id );
151
152
	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...
153
		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...
154
	}
155
156
}
157
158
add_action( 'give_update_payment_status', 'give_clear_user_history_cache', 10, 3 );
159
160
/**
161
 * Update Old Payments Totals
162
 *
163
 * Updates all old payments, prior to 1.2, with new meta for the total purchase amount.
164
 *
165
 * It's done to query payments by their totals.
166
 *
167
 * @since  1.0
168
 *
169
 * @param  array $data Arguments passed.
170
 *
171
 * @return void
172
 */
173
function give_update_old_payments_with_totals( $data ) {
174
	if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_upgrade_payments_nonce' ) ) {
175
		return;
176
	}
177
178
	if ( get_option( 'give_payment_totals_upgraded' ) ) {
179
		return;
180
	}
181
182
	$payments = give_get_payments( array(
183
		'offset' => 0,
184
		'number' => - 1,
185
		'mode'   => 'all'
186
	) );
187
188
	if ( $payments ) {
189
		foreach ( $payments as $payment ) {
190
191
			$payment = new Give_Payment( $payment->ID );
192
			$meta    = $payment->get_meta();
193
194
			$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...
195
			$payment->save();
196
197
		}
198
	}
199
200
	add_option( 'give_payment_totals_upgraded', 1 );
201
}
202
203
add_action( 'give_upgrade_payments', 'give_update_old_payments_with_totals' );
204
205
/**
206
 * Mark Abandoned Donations
207
 *
208
 * Updates over a week-old 'pending' donations to 'abandoned' status.
209
 *
210
 * @since  1.0
211
 *
212
 * @return void
213
 */
214
function give_mark_abandoned_donations() {
215
	$args = array(
216
		'status' => 'pending',
217
		'number' => - 1,
218
		'output' => 'give_payments',
219
	);
220
221
	add_filter( 'posts_where', 'give_filter_where_older_than_week' );
222
223
	$payments = give_get_payments( $args );
224
225
	remove_filter( 'posts_where', 'give_filter_where_older_than_week' );
226
227
	if ( $payments ) {
228
        /**
229
         * Filter payment gateways:  Used to set payment gateways which can be skip while transferring pending payment to abandon.
230
         *
231
         * @since 1.6
232
         *
233
         * @param array $skip_payment_gateways Array of payment gateways
234
         */
235
	    $skip_payment_gateways = apply_filters( 'give_mark_abandoned_donation_gateways', array( 'offline' ) );
236
237
		foreach ( $payments as $payment ) {
238
			$gateway = give_get_payment_gateway( $payment );
239
240
			// Skip payment gateways.
241
			if ( in_array( $gateway, $skip_payment_gateways ) ) {
242
				continue;
243
			}
244
245
			$payment->status = 'abandoned';
246
			$payment->save();
247
		}
248
	}
249
}
250
251
add_action( 'give_weekly_scheduled_events', 'give_mark_abandoned_donations' );
252