Test Failed
Push — issues/2296 ( 5e68bb )
by Ravinder
04:16
created

donor-functions.php ➔ give_delete_bulk_donors()   C

Complexity

Conditions 9
Paths 22

Size

Total Lines 46
Code Lines 21

Duplication

Lines 5
Ratio 10.87 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 22
nop 2
dl 5
loc 46
rs 5.0942
c 0
b 0
f 0
1
<?php
2
/**
3
 * Donors
4
 *
5
 * @package    Give
6
 * @subpackage Admin/Donors
7
 * @copyright  Copyright (c) 2016, WordImpress
8
 * @license    https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since      1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Register a view for the single donor view.
19
 *
20
 * @param array $views An array of existing views.
21
 *
22
 * @since 1.0
23
 *
24
 * @return array        The altered list of views.
25
 */
26
function give_register_default_donor_views( $views ) {
27
28
	$default_views = array(
29
		'overview' => 'give_donor_view',
30
		'delete'   => 'give_donor_delete_view',
31
		'notes'    => 'give_donor_notes_view',
32
	);
33
34
	return array_merge( $views, $default_views );
35
36
}
37
38
add_filter( 'give_donor_views', 'give_register_default_donor_views', 1, 1 );
39
40
/**
41
 * Register a tab for the single donor view.
42
 *
43
 * @param array $tabs An array of existing tabs.
44
 *
45
 * @since 1.0
46
 *
47
 * @return array       The altered list of tabs
48
 */
49
function give_register_default_donor_tabs( $tabs ) {
50
51
	$default_tabs = array(
52
		'overview' => array(
53
			'dashicon' => 'dashicons-admin-users',
54
			'title' => __( 'Donor Profile', 'give' ),
55
		),
56
		'notes'    => array(
57
			'dashicon' => 'dashicons-admin-comments',
58
			'title' => __( 'Donor Notes', 'give' ),
59
		),
60
	);
61
62
	return array_merge( $tabs, $default_tabs );
63
}
64
65
add_filter( 'give_donor_tabs', 'give_register_default_donor_tabs', 1, 1 );
66
67
/**
68
 * Register the Delete icon as late as possible so it's at the bottom.
69
 *
70
 * @param array $tabs An array of existing tabs.
71
 *
72
 * @since 1.0
73
 *
74
 * @return array       The altered list of tabs, with 'delete' at the bottom.
75
 */
76
function give_register_delete_donor_tab( $tabs ) {
77
78
	$tabs['delete'] = array(
79
		'dashicon' => 'dashicons-trash',
80
		'title'    => __( 'Delete Donor', 'give' ),
81
	);
82
83
	return $tabs;
84
}
85
86
add_filter( 'give_donor_tabs', 'give_register_delete_donor_tab', PHP_INT_MAX, 1 );
87
88
/**
89
 * Connect and Reconnect Donor with User profile.
90
 *
91
 * @param object $donor      Donor Object.
92
 * @param array  $donor_data Donor Post Variables.
93
 * @param array  $address    Address Information.
94
 *
95
 * @since 1.8.14
96
 *
97
 * @return array
98
 */
99
function give_connect_user_donor_profile( $donor, $donor_data, $address ) {
100
101
	$donor_id         = $donor->id;
102
	$previous_user_id = $donor->user_id;
103
104
	/**
105
	 * Fires before editing a donor.
106
	 *
107
	 * @param int   $donor_id   The ID of the donor.
108
	 * @param array $donor_data The donor data.
109
	 * @param array $address    The donor's address.
110
	 *
111
	 * @since 1.0
112
	 */
113
	do_action( 'give_pre_edit_donor', $donor_id, $donor_data, $address );
114
115
	$output = array();
116
117
	if ( $donor->update( $donor_data ) ) {
118
119
		if ( ! empty( $donor->user_id ) && $donor->user_id > 0 ) {
120
			update_user_meta( $donor->user_id, '_give_user_address', $address );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
121
		}
122
123
		// Update some donation meta if we need to.
124
		$payments_array = explode( ',', $donor->payment_ids );
125
126
		if ( $donor->user_id !== $previous_user_id ) {
127
			foreach ( $payments_array as $payment_id ) {
128
				give_update_payment_meta( $payment_id, '_give_payment_user_id', $donor->user_id );
129
			}
130
		}
131
132
		// Fetch disconnected user id, if exists.
133
		$disconnected_user_id = $donor->get_meta( '_give_disconnected_user_id', true );
134
135
		// Flag User and Donor Disconnection.
136
		delete_user_meta( $disconnected_user_id, '_give_is_donor_disconnected' );
0 ignored issues
show
introduced by
delete_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
137
138
		// Check whether the disconnected user id and the reconnected user id are same or not.
139
		// If both are same then delete user id store in donor meta.
140
		if( $donor_data['user_id'] === $disconnected_user_id ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
141
			delete_user_meta( $disconnected_user_id, '_give_disconnected_donor_id' );
0 ignored issues
show
introduced by
delete_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
142
			$donor->delete_meta( '_give_disconnected_user_id' );
143
		}
144
145
		$output['success']       = true;
146
		$donor_data              = array_merge( $donor_data, $address );
147
		$output['customer_info'] = $donor_data;
148
149
	} else {
150
151
		$output['success'] = false;
152
153
	}
154
155
	/**
156
	 * Fires after editing a donor.
157
	 *
158
	 * @param int   $donor_id   The ID of the donor.
159
	 * @param array $donor_data The donor data.
160
	 *
161
	 * @since 1.0
162
	 */
163
	do_action( 'give_post_edit_donor', $donor_id, $donor_data );
164
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
165
166
	return $output;
167
}
168
169
/**
170
 * Delete Donor using Bulk Actions.
171
 *
172
 * @param int   $donor_id Donor ID.
173
 * @param array $args     An Array of Additional Donor Arguments.
174
 *
175
 * @since 1.8.17
176
 *
177
 * @return bool
178
 */
179
function give_delete_bulk_donors( $donor_id, $args ) {
180
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
181
182 View Code Duplication
	if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
		wp_die( __( 'You do not have permission to delete donors.', 'give' ), __( 'Error', 'give' ), array(
184
			'response' => 403,
185
		) );
186
	}
187
188
	// Bail Out, if Donor ID doesn't exists.
189
	if ( empty( $donor_id ) ) {
190
		return false;
191
	}
192
193
	$nonce = $args['_wpnonce'];
194
195
	// Verify Nonce for deleting bulk donors.
196
	if ( ! wp_verify_nonce( $nonce, 'delete-bulk-donors' ) ) {
197
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
198
			'response' => 400,
199
		) );
200
	}
201
202
	$donor = new Give_Donor( $donor_id );
0 ignored issues
show
Documentation introduced by
$donor_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
203
204
	if ( $donor->id > 0 ) {
205
		$payments_ids  = explode( ',', $donor->payment_ids );
206
		$donor_deleted = Give()->donors->delete( $donor->id );
207
208
		if ( $donor_deleted ) {
209
210
			// Remove all donations, logs, etc.
211
			foreach ( $payments_ids as $payment_id ) {
212
				give_delete_donation( $payment_id );
213
			}
214
		} else {
215
216
			// Just set the donations to customer_id of 0.
217
			foreach ( $payments_ids as $payment_id ) {
218
				give_update_payment_meta( $payment_id, '_give_payment_customer_id', 0 );
219
			}
220
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
221
222
	}
223
224
}