Test Failed
Push — issues/2397 ( f367c1...92dbfa )
by Ravinder
04:29
created

actions.php ➔ give_verify_donation_history_access()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 5
nop 0
dl 0
loc 41
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Front-end Actions
4
 *
5
 * @package     Give
6
 * @subpackage  Functions
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
 * Hooks Give actions, when present in the $_GET superglobal. Every give_action
19
 * present in $_GET is called using WordPress's do_action function. These
20
 * functions are called on init.
21
 *
22
 * @since  1.0
23
 *
24
 * @return void
25
 */
26 View Code Duplication
function give_get_actions() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
27
28
	$_get_action = ! empty( $_GET['give_action'] ) ? $_GET['give_action'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
29
30
	// Add backward compatibility to give-action param ( $_GET )
31
	if ( empty( $_get_action ) ) {
32
		$_get_action = ! empty( $_GET['give-action'] ) ? $_GET['give-action'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
33
	}
34
35
	if ( isset( $_get_action ) ) {
36
		/**
37
		 * Fires in WordPress init or admin init, when give_action is present in $_GET.
38
		 *
39
		 * @since 1.0
40
		 *
41
		 * @param array $_GET Array of HTTP GET variables.
42
		 */
43
		do_action( "give_{$_get_action}", $_GET );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
44
	}
45
46
}
47
48
add_action( 'init', 'give_get_actions' );
49
50
/**
51
 * Hooks Give actions, when present in the $_POST super global. Every give_action
52
 * present in $_POST is called using WordPress's do_action function. These
53
 * functions are called on init.
54
 *
55
 * @since  1.0
56
 *
57
 * @return void
58
 */
59 View Code Duplication
function give_post_actions() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
60
61
	$_post_action = ! empty( $_POST['give_action'] ) ? $_POST['give_action'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
62
63
	// Add backward compatibility to give-action param ( $_POST ).
64
	if ( empty( $_post_action ) ) {
65
		$_post_action = ! empty( $_POST['give-action'] ) ? $_POST['give-action'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
66
	}
67
68
	if ( isset( $_post_action ) ) {
69
		/**
70
		 * Fires in WordPress init or admin init, when give_action is present in $_POST.
71
		 *
72
		 * @since 1.0
73
		 *
74
		 * @param array $_POST Array of HTTP POST variables.
75
		 */
76
		do_action( "give_{$_post_action}", $_POST );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
77
	}
78
79
}
80
81
add_action( 'init', 'give_post_actions' );
82
83
/**
84
 * Connect WordPress user with Donor.
85
 *
86
 * @param  int   $user_id   User ID.
87
 * @param  array $user_data User Data.
88
 *
89
 * @since  1.7
90
 *
91
 * @return void
92
 */
93
function give_connect_donor_to_wpuser( $user_id, $user_data ) {
94
	/* @var Give_Donor $donor */
95
	$donor = new Give_Donor( $user_data['user_email'] );
96
97
	// Validate donor id and check if do nor is already connect to wp user or not.
98
	if ( $donor->id && ! $donor->user_id ) {
99
100
		// Update donor user_id.
101
		if ( $donor->update( array( 'user_id' => $user_id ) ) ) {
102
			$donor_note = sprintf( esc_html__( 'WordPress user #%d is connected to #%d', 'give' ), $user_id, $donor->id );
103
			$donor->add_note( $donor_note );
104
105
			// Update user_id meta in payments.
106
			if ( ! empty( $donor->payment_ids ) && ( $donations = explode( ',', $donor->payment_ids ) ) ) {
107
				foreach ( $donations as $donation ) {
108
					give_update_meta( $donation, '_give_payment_user_id', $user_id );
109
				}
110
			}
111
		}
112
	}
113
}
114
115
add_action( 'give_insert_user', 'give_connect_donor_to_wpuser', 10, 2 );
116
117
118
/**
119
 * Setup site home url check
120
 *
121
 * Note: if location of site changes then run cron to validate licenses
122
 *
123
 * @since   1.7
124
 * @updated 1.8.15 - Resolved issue with endless looping because of URL mismatches.
125
 * @return void
126
 */
127
function give_validate_license_when_site_migrated() {
128
	// Store current site address if not already stored.
129
	$home_url_parts              = parse_url( home_url() );
130
	$home_url                    = isset( $home_url_parts['host'] ) ? $home_url_parts['host'] : false;
131
	$home_url                    .= isset( $home_url_parts['path'] ) ? $home_url_parts['path'] : '';
132
	$site_address_before_migrate = get_option( 'give_site_address_before_migrate' );
133
134
	// Need $home_url to proceed.
135
	if ( ! $home_url ) {
136
		return;
137
	}
138
139
	// Save site address.
140
	if ( ! $site_address_before_migrate ) {
141
		// Update site address.
142
		update_option( 'give_site_address_before_migrate', $home_url );
143
144
		return;
145
	}
146
147
	// Backwards compat. for before when we were storing URL scheme.
148
	if ( strpos( $site_address_before_migrate, 'http' ) ) {
149
		$site_address_before_migrate = parse_url( $site_address_before_migrate );
150
		$site_address_before_migrate = isset( $site_address_before_migrate['host'] ) ? $site_address_before_migrate['host'] : false;
151
152
		// Add path for multisite installs.
153
		$site_address_before_migrate .= isset( $site_address_before_migrate['path'] ) ? $site_address_before_migrate['path'] : '';
154
	}
155
156
	// If the two URLs don't match run CRON.
157
	if ( $home_url !== $site_address_before_migrate ) {
158
		// Immediately run cron.
159
		wp_schedule_single_event( time(), 'give_validate_license_when_site_migrated' );
160
161
		// Update site address.
162
		update_option( 'give_site_address_before_migrate', $home_url );
163
	}
164
165
}
166
167
add_action( 'admin_init', 'give_validate_license_when_site_migrated' );
168
169
170
/**
171
 * Processing after donor batch export complete
172
 *
173
 * @since 1.8
174
 *
175
 * @param $data
176
 */
177
function give_donor_batch_export_complete( $data ) {
178
	// Remove donor ids cache.
179
	if (
180
		isset( $data['class'] )
181
		&& 'Give_Batch_Donors_Export' === $data['class']
182
		&& ! empty( $data['forms'] )
183
		&& isset( $data['give_export_option']['query_id'] )
184
	) {
185
		Give_Cache::delete( Give_Cache::get_key( $data['give_export_option']['query_id'] ) );
186
	}
187
}
188
189
add_action( 'give_file_export_complete', 'give_donor_batch_export_complete' );
190
191
/**
192
 * Print css for wordpress setting pages.
193
 *
194
 * @since 1.8.7
195
 */
196
function give_admin_quick_css() {
197
	/* @var WP_Screen $screen */
198
	$screen = get_current_screen();
199
200
	if ( ! ( $screen instanceof WP_Screen ) ) {
0 ignored issues
show
Bug introduced by
The class WP_Screen does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
201
		return false;
202
	}
203
204
	switch ( true ) {
205
		case ( 'plugins' === $screen->base || 'plugins-network' === $screen->base ):
206
			?>
207
			<style>
208
				tr.active.update + tr.give-addon-notice-tr td {
209
					box-shadow: none;
210
					-webkit-box-shadow: none;
211
				}
212
213
				tr.active + tr.give-addon-notice-tr td {
214
					position: relative;
215
					top: -1px;
216
				}
217
218
				tr.active + tr.give-addon-notice-tr .notice {
219
					margin: 5px 20px 15px 40px;
220
				}
221
222
				tr.give-addon-notice-tr .dashicons {
223
					color: #f56e28;
224
				}
225
226
				tr.give-addon-notice-tr td {
227
					border-left: 4px solid #00a0d2;
228
				}
229
230
				tr.give-addon-notice-tr td {
231
					padding: 0 !important;
232
				}
233
234
				tr.active.update + tr.give-addon-notice-tr .notice {
235
					margin: 5px 20px 5px 40px;
236
				}
237
			</style>
238
			<?php
239
	}
240
}
241
242
add_action( 'admin_head', 'give_admin_quick_css' );
243
244
245
/**
246
 * Set Donation Amount for Multi Level Donation Forms
247
 *
248
 * @param int $form_id Donation Form ID.
249
 *
250
 * @since 1.8.9
251
 *
252
 * @return void
253
 */
254
function give_set_donation_levels_max_min_amount( $form_id ) {
255
	if (
256
		( 'set' === $_POST['_give_price_option'] ) ||
257
		( in_array( '_give_donation_levels', $_POST ) && count( $_POST['_give_donation_levels'] ) <= 0 ) ||
258
		! ( $donation_levels_amounts = wp_list_pluck( $_POST['_give_donation_levels'], '_give_amount' ) )
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
259
	) {
260
		// Delete old meta.
261
		give_delete_meta( $form_id, '_give_levels_minimum_amount' );
262
		give_delete_meta( $form_id, '_give_levels_maximum_amount' );
263
264
		return;
265
	}
266
267
	// Sanitize donation level amounts.
268
	$donation_levels_amounts = array_map( 'give_maybe_sanitize_amount', $donation_levels_amounts );
269
270
	$min_amount = min( $donation_levels_amounts );
271
	$max_amount = max( $donation_levels_amounts );
272
273
	// Set Minimum and Maximum amount for Multi Level Donation Forms.
274
	give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount_for_db( $min_amount ) : 0 );
275
	give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount_for_db( $max_amount ) : 0 );
276
}
277
278
add_action( 'give_pre_process_give_forms_meta', 'give_set_donation_levels_max_min_amount', 30 );
279
280
281
/**
282
 * Verify that the donation history access is available or not.
283
 *
284
 * @since 1.8.17
285
 */
286
function give_verify_donation_history_access() {
287
288
	if ( give_get_option( 'history_page' ) === url_to_postid( $_SERVER['REQUEST_URI'] ) ) {
289
290
		$donation = '';
291
		if ( ! empty( $_POST['give_email'] ) && empty( $_GET['payment_key'] ) ) {
292
			$donation_ids = array();
0 ignored issues
show
Unused Code introduced by
$donation_ids is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
293
			$donor        = Give()->donors->get_donor_by( 'email', $_POST['give_email'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
294
295
			// Bail Out, if donation by specific donor doesn't exists.
296
			if( empty( $donor->payment_ids ) ) {
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...
297
				return false;
298
			}
299
300
			$donation_ids = explode( ',', $donor->payment_ids );
301
302
			foreach ( $donation_ids as $donation_id ) {
303
				$donation = new Give_Payment( $donation_id );
304
				break;
305
			}
306
307
			$donation_data = array(
308
				'price'         => $donation->total,
309
				'purchase_key'  => $donation->key,
310
				'user_email'    => $donation->email,
311
				'date'          => $donation->post_date,
312
				'user_info'     => $donation->user_info,
313
				'gateway'       => $donation->gateway,
314
			);
315
316
			// Set History Access Session.
317
			Give()->session->set( 'history_access', true );
318
319
			// Set Purchase Session.
320
			give_set_purchase_session( $donation_data );
321
322
			// Set Session Cookies.
323
			Give()->session->set_session_cookies();
324
		}
325
	}
326
}
327
328
add_action( 'init', 'give_verify_donation_history_access', 99999 );