Completed
Push — issues/1426 ( c50691...6431ac )
by Ravinder
27:29 queued 07:26
created

actions.php ➔ give_donor_batch_export_complete()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 8.8571
c 0
b 0
f 0
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 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
 * 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
function give_get_actions() {
27
28
	$_get_action = ! empty( $_GET['give_action'] ) ? $_GET['give_action'] : null;
29
30
	// Add backward compatibility to give-action param ( $_GET or $_POST )
31
	if(  doing_action( 'admin_init' ) && empty( $_get_action ) ) {
32
		$_get_action = ! empty( $_GET['give-action'] ) ? $_GET['give-action'] : null;
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 );
44
	}
45
46
}
47
48
add_action( 'init', 'give_get_actions' );
49
add_action( 'admin_init', 'give_get_actions' );
50
51
/**
52
 * Hooks Give actions, when present in the $_POST superglobal. Every give_action
53
 * present in $_POST is called using WordPress's do_action function. These
54
 * functions are called on init.
55
 *
56
 * @since  1.0
57
 *
58
 * @return void
59
 */
60
function give_post_actions() {
61
62
	$_post_action = ! empty( $_POST['give_action'] ) ? $_POST['give_action'] : null;
63
64
65
	// Add backward compatibility to give-action param ( $_GET or $_POST )
66
	if(  doing_action( 'admin_init' ) && empty( $_post_action ) ) {
67
		$_post_action = ! empty( $_POST['give-action'] ) ? $_POST['give-action'] : null;
68
	}
69
70
	if ( isset( $_post_action ) ) {
71
		/**
72
		 * Fires in WordPress init or admin init, when give_action is present in $_POST.
73
		 *
74
		 * @since 1.0
75
		 *
76
		 * @param array $_POST Array of HTTP POST variables.
77
		 */
78
		do_action( "give_{$_post_action}", $_POST );
79
	}
80
81
}
82
83
add_action( 'init', 'give_post_actions' );
84
add_action( 'admin_init', 'give_post_actions' );
85
86
/**
87
 * Connect WordPress user with Donor.
88
 *
89
 * @since  1.7
90
 * @param  int   $user_id   User ID
91
 * @param  array $user_data User Data
92
 * @return void
93
 */
94
function give_connect_donor_to_wpuser( $user_id, $user_data ){
95
	$donor = new Give_Customer( $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
					update_post_meta( $donation, '_give_payment_user_id', $user_id );
109
				}
110
			}
111
		}
112
	}
113
}
114
add_action( 'give_insert_user', 'give_connect_donor_to_wpuser', 10, 2 );
115
116
117
/**
118
 * Setup site home url check
119
 *
120
 * Note: if location of site changes then run cron to validate licenses
121
 *
122
 * @since  1.7
123
 * @return void
124
 */
125
function give_validate_license_when_site_migrated() {
126
	// Store current site address if not already stored.
127
	$homeurl = home_url();
128
	if( ! get_option( 'give_site_address_before_migrate' ) ) {
129
		// Update site address.
130
		update_option( 'give_site_address_before_migrate', $homeurl );
131
132
		return;
133
	}
134
135
	if( $homeurl !== get_option( 'give_site_address_before_migrate' ) ) {
136
		// Immediately run cron.
137
		wp_schedule_single_event( time() , 'give_validate_license_when_site_migrated' );
138
139
		// Update site address.
140
		update_option( 'give_site_address_before_migrate', home_url() );
141
	}
142
143
}
144
add_action( 'init', 'give_validate_license_when_site_migrated' );
145
add_action( 'admin_init', 'give_validate_license_when_site_migrated' );
146
147
148
/**
149
 * Processing after donor batch export complete
150
 *
151
 * @since 1.8
152
 * @param $data
153
 */
154
function give_donor_batch_export_complete( $data ) {
155
	// Remove donor ids cache.
156
	if(
157
		isset( $data['class'] )
158
		&& 'Give_Batch_Customers_Export' === $data['class']
159
		&& ! empty( $data['forms'] )
160
		&& isset( $data['give_export_option']['query_id'] )
161
	) {
162
		delete_transient( $data['give_export_option']['query_id'] );
163
	}
164
}
165
add_action('give_file_export_complete', 'give_donor_batch_export_complete' );
166