Completed
Pull Request — master (#682)
by Devin
04:17
created

upgrade-functions.php ➔ give_v152_cleanup_users()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 78
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 56
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 78
ccs 0
cts 60
cp 0
crap 42
rs 8.4316

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 26 and the first side effect is on line 17.

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
 * Upgrade Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Upgrades
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
 * NOTICE: When adding new upgrade notices, please be sure to put the action into the upgrades array during install: /includes/install.php @ Appox Line 156
12
 *
13
 */
14
15
// Exit if accessed directly
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
18
}
19
20
/**
21
 * Display Upgrade Notices
22
 *
23
 * @since 1.0
24
 * @return void
25
 */
26
function give_show_upgrade_notices() {
27
28
	if ( isset( $_GET['page'] ) && $_GET['page'] == 'give-upgrades' ) {
29
		return;
30
	} // Don't show notices on the upgrades page
31
32
	$give_version = get_option( 'give_version' );
33
34
	if ( ! $give_version ) {
35
		// 1.0 is the first version to use this option so we must add it
36
		$give_version = '1.0';
37
	}
38
39
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
40
41
	/*
42
	 *  NOTICE:
43
	 *
44
	 *  When adding new upgrade notices, please be sure to put the action into the upgrades array during install:
45
	 *  /includes/install.php @ Appox Line 156
46
	 *
47
	 */
48
49
	//v1.3.2 Upgrades
50
	if ( version_compare( $give_version, '1.3.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_payment_customer_id' ) ) {
51
		printf(
52
			'<div class="updated"><p>' . __( 'Give needs to upgrade the donor database, click <a href="%s">here</a> to start the upgrade.', 'give' ) . '</p></div>',
53
			esc_url( admin_url( 'index.php?page=give-upgrades&give-upgrade=upgrade_give_payment_customer_id' ) )
54
		);
55
	}
56
57
	//v1.3.4 Upgrades //ensure the user has gone through 1.3.4
58
	if ( version_compare( $give_version, '1.3.4', '<' ) || ( ! give_has_upgrade_completed( 'upgrade_give_offline_status' ) && give_has_upgrade_completed( 'upgrade_give_payment_customer_id' ) ) ) {
59
		printf(
60
			'<div class="updated"><p>' . __( 'Give needs to upgrade the transaction database, click <a href="%s">here</a> to start the upgrade.', 'give' ) . '</p></div>',
61
			esc_url( admin_url( 'index.php?page=give-upgrades&give-upgrade=upgrade_give_offline_status' ) )
62
		);
63
	}
64
65
66
	// End 'Stepped' upgrade process notices
67
68
69
}
70
71
add_action( 'admin_notices', 'give_show_upgrade_notices' );
72
73
/**
74
 * Triggers all upgrade functions
75
 *
76
 * This function is usually triggered via AJAX
77
 *
78
 * @since 1.0
79
 * @return void
80
 */
81
function give_trigger_upgrades() {
82
83
	if ( ! current_user_can( 'manage_give_settings' ) ) {
84
		wp_die( __( 'You do not have permission to do Give upgrades', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
85
	}
86
87
	$give_version = get_option( 'give_version' );
88
89
	if ( ! $give_version ) {
90
		// 1.0 is the first version to use this option so we must add it
91
		$give_version = '1.0';
92
		add_option( 'give_version', $give_version );
93
	}
94
95
	update_option( 'give_version', GIVE_VERSION );
96
97
	if ( DOING_AJAX ) {
98
		die( 'complete' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_trigger_upgrades() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
99
	} // Let AJAX know that the upgrade is complete
100
}
101
102
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
103
104
/**
105
 * Check if the upgrade routine has been run for a specific action
106
 *
107
 * @since  1.0
108
 *
109
 * @param  string $upgrade_action The upgrade action to check completion for
110
 *
111
 * @return bool                   If the action has been added to the completed actions array
112
 */
113
function give_has_upgrade_completed( $upgrade_action = '' ) {
114
115 1
	if ( empty( $upgrade_action ) ) {
116
		return false;
117
	}
118
119 1
	$completed_upgrades = give_get_completed_upgrades();
120
121 1
	return in_array( $upgrade_action, $completed_upgrades );
122
123
}
124
125
/**
126
 * Adds an upgrade action to the completed upgrades array
127
 *
128
 * @since  1.0
129
 *
130
 * @param  string $upgrade_action The action to add to the copmleted upgrades array
131
 *
132
 * @return bool                   If the function was successfully added
133
 */
134
function give_set_upgrade_complete( $upgrade_action = '' ) {
135
136 1
	if ( empty( $upgrade_action ) ) {
137
		return false;
138
	}
139
140 1
	$completed_upgrades   = give_get_completed_upgrades();
141 1
	$completed_upgrades[] = $upgrade_action;
142
143
	// Remove any blanks, and only show uniques
144 1
	$completed_upgrades = array_unique( array_values( $completed_upgrades ) );
145
146 1
	return update_option( 'give_completed_upgrades', $completed_upgrades );
147
}
148
149
/**
150
 * Get's the array of completed upgrade actions
151
 *
152
 * @since  1.0
153
 * @return array The array of completed upgrades
154
 */
155
function give_get_completed_upgrades() {
156
157 1
	$completed_upgrades = get_option( 'give_completed_upgrades' );
158
159 1
	if ( false === $completed_upgrades ) {
160
		$completed_upgrades = array();
161
	}
162
163 1
	return $completed_upgrades;
164
165
}
166
167
168
/**
169
 * Upgrades the
170
 *
171
 * @description: Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
172
 *
173
 * @since      1.3.2
174
 *
175
 */
176
function give_v132_upgrade_give_payment_customer_id() {
177
	global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
178
	if ( ! current_user_can( 'manage_give_settings' ) ) {
179
		wp_die( __( 'You do not have permission to do Give upgrades', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
180
	}
181
182
	ignore_user_abort( true );
183
184
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
185
		@set_time_limit( 0 );
186
	}
187
188
	//UPDATE DB METAKEYS
189
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
190
	$query = $wpdb->query( $sql );
191
192
	update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
193
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
194
	delete_option( 'give_doing_upgrade' );
195
	wp_redirect( admin_url() );
196
	exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_v132_upgrade_give_payment_customer_id() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
197
198
199
}
200
201
add_action( 'give_upgrade_give_payment_customer_id', 'give_v132_upgrade_give_payment_customer_id' );
202
203
/**
204
 * Upgrades the Offline Status
205
 *
206
 * @description: Reverses the issue where offline donation transactions in "pending" status where inappropriately marked as abandoned
207
 *
208
 * @since      1.3.4
209
 *
210
 */
211
function give_v134_upgrade_give_offline_status() {
212
213
	global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
214
215
	if ( ! current_user_can( 'manage_give_settings' ) ) {
216
		wp_die( __( 'You do not have permission to do Give upgrades', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
217
	}
218
219
	ignore_user_abort( true );
220
221
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
222
		@set_time_limit( 0 );
223
	}
224
225
	// Get abandoned offline payments
226
	$select = "SELECT ID FROM $wpdb->posts p ";
227
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
228
	$where  = "WHERE p.post_type = 'give_payment' ";
229
	$where .= "AND ( p.post_status = 'abandoned' )";
230
	$where .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
231
232
	$sql            = $select . $join . $where;
233
	$found_payments = $wpdb->get_col( $sql );
234
235
236
	foreach ( $found_payments as $payment ) {
237
238
		//Only change ones marked abandoned since our release last week
239
		//because the admin may have marked some abandoned themselves
240
		$modified_time = get_post_modified_time( 'U', false, $payment );
241
242
		//1450124863 =  12/10/2015 20:42:25
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
243
		if ( $modified_time >= 1450124863 ) {
244
245
			give_update_payment_status( $payment, 'pending' );
246
247
		}
248
249
	}
250
251
	update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
252
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
253
	delete_option( 'give_doing_upgrade' );
254
	wp_redirect( admin_url() );
255
	exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_v134_upgrade_give_offline_status() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
256
257
258
}
259
260
add_action( 'give_upgrade_give_offline_status', 'give_v134_upgrade_give_offline_status' );
261
262
263
/**
264
 * Cleanup User Roles
265
 *
266
 * @description: This upgrade routine removes unused roles and roles with typos
267
 *
268
 * @since      1.5.2
269
 */
270
function give_v152_cleanup_users() {
271
272
	$give_version = get_option( 'give_version' );
273
274
	if ( ! $give_version ) {
275
		// 1.0 is the first version to use this option so we must add it
276
		$give_version = '1.0';
277
	}
278
279
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
280
281
	//v1.5.2 Upgrades
282
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
283
284
		//Delete all caps with "ss"
285
		//Also delete all unused "campaign" roles
286
		$delete_caps = array(
287
			'delete_give_formss',
288
			'delete_others_give_formss',
289
			'delete_private_give_formss',
290
			'delete_published_give_formss',
291
			'read_private_forms',
292
			'edit_give_formss',
293
			'edit_others_give_formss',
294
			'edit_private_give_formss',
295
			'edit_published_give_formss',
296
			'publish_give_formss',
297
			'read_private_give_formss',
298
			'assign_give_campaigns_terms',
299
			'delete_give_campaigns',
300
			'delete_give_campaigns_terms',
301
			'delete_give_campaignss',
302
			'delete_others_give_campaignss',
303
			'delete_private_give_campaignss',
304
			'delete_published_give_campaignss',
305
			'edit_give_campaigns',
306
			'edit_give_campaigns_terms',
307
			'edit_give_campaignss',
308
			'edit_others_give_campaignss',
309
			'edit_private_give_campaignss',
310
			'edit_published_give_campaignss',
311
			'manage_give_campaigns_terms',
312
			'publish_give_campaignss',
313
			'read_give_campaigns',
314
			'read_private_give_campaignss',
315
			'view_give_campaigns_stats',
316
			'delete_give_paymentss',
317
			'delete_others_give_paymentss',
318
			'delete_private_give_paymentss',
319
			'delete_published_give_paymentss',
320
			'edit_give_paymentss',
321
			'edit_others_give_paymentss',
322
			'edit_private_give_paymentss',
323
			'edit_published_give_paymentss',
324
			'publish_give_paymentss',
325
			'read_private_give_paymentss',
326
		);
327
	
328
		global $wp_roles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
329
		foreach ( $delete_caps as $cap ) {
330
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
331
				$wp_roles->remove_cap( $role, $cap );
332
			}
333
		}
334
	
335
		// Create Give plugin roles
336
		$roles = new Give_Roles();
337
		$roles->add_roles();
338
		$roles->add_caps();
339
		
340
		//The Update Ran
341
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
342
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
343
		delete_option( 'give_doing_upgrade' );
344
345
	}
346
347
}
348
349
add_action( 'admin_init', 'give_v152_cleanup_users' );