Completed
Push — master ( 982dc9...2e2f98 )
by Devin
92:16 queued 72:17
created
includes/class-give-email-access.php 1 patch
Spacing   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -10,7 +10,7 @@  discard block
 block discarded – undo
10 10
  * @since       1.4
11 11
  */
12 12
 
13
-defined( 'ABSPATH' ) or exit;
13
+defined('ABSPATH') or exit;
14 14
 
15 15
 /**
16 16
  * Class Give_Email_Access
@@ -30,7 +30,7 @@  discard block
 block discarded – undo
30 30
 	function __construct() {
31 31
 
32 32
 		// get it started
33
-		add_action( 'init', array( $this, 'init' ) );
33
+		add_action('init', array($this, 'init'));
34 34
 	}
35 35
 
36 36
 
@@ -39,30 +39,30 @@  discard block
 block discarded – undo
39 39
 	 */
40 40
 	function init() {
41 41
 
42
-		$is_enabled = give_get_option( 'email_access' );
42
+		$is_enabled = give_get_option('email_access');
43 43
 
44 44
 		//Non-logged in users only
45
-		if ( is_user_logged_in() || $is_enabled !== 'on' || is_admin() ) {
45
+		if (is_user_logged_in() || $is_enabled !== 'on' || is_admin()) {
46 46
 			return;
47 47
 		}
48 48
 
49 49
 		//Are db columns setup?
50
-		$is_setup = give_get_option( 'email_access_installed' );
51
-		if ( empty( $is_setup ) ) {
50
+		$is_setup = give_get_option('email_access_installed');
51
+		if (empty($is_setup)) {
52 52
 			$this->create_columns();
53 53
 		}
54 54
 
55 55
 		// Timeouts
56
-		$this->verify_throttle  = apply_filters( 'give_nl_verify_throttle', 300 );
57
-		$this->token_expiration = apply_filters( 'give_nl_token_expiration', 7200 );
56
+		$this->verify_throttle  = apply_filters('give_nl_verify_throttle', 300);
57
+		$this->token_expiration = apply_filters('give_nl_token_expiration', 7200);
58 58
 
59 59
 		// Setup login
60 60
 		$this->check_for_token();
61 61
 
62
-		if ( $this->token_exists ) {
63
-			add_filter( 'give_can_view_receipt', '__return_true' );
64
-			add_filter( 'give_user_pending_verification', '__return_false' );
65
-			add_filter( 'give_get_users_purchases_args', array( $this, 'users_purchases_args' ) );
62
+		if ($this->token_exists) {
63
+			add_filter('give_can_view_receipt', '__return_true');
64
+			add_filter('give_user_pending_verification', '__return_false');
65
+			add_filter('give_get_users_purchases_args', array($this, 'users_purchases_args'));
66 66
 		}
67 67
 	}
68 68
 
@@ -73,24 +73,24 @@  discard block
 block discarded – undo
73 73
 	 *
74 74
 	 * @return bool
75 75
 	 */
76
-	function can_send_email( $customer_id ) {
76
+	function can_send_email($customer_id) {
77 77
 		global $wpdb;
78 78
 
79 79
 		// Prevent multiple emails within X minutes
80
-		$throttle = date( 'Y-m-d H:i:s', time() - $this->verify_throttle );
80
+		$throttle = date('Y-m-d H:i:s', time() - $this->verify_throttle);
81 81
 
82 82
 		// Does a user row exist?
83 83
 		$exists = (int) $wpdb->get_var(
84
-			$wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}give_customers WHERE id = %d", $customer_id )
84
+			$wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}give_customers WHERE id = %d", $customer_id)
85 85
 		);
86 86
 
87
-		if ( 0 < $exists ) {
87
+		if (0 < $exists) {
88 88
 			$row_id = (int) $wpdb->get_var(
89
-				$wpdb->prepare( "SELECT id FROM {$wpdb->prefix}give_customers WHERE id = %d AND (verify_throttle < %s OR verify_key = '') LIMIT 1", $customer_id, $throttle )
89
+				$wpdb->prepare("SELECT id FROM {$wpdb->prefix}give_customers WHERE id = %d AND (verify_throttle < %s OR verify_key = '') LIMIT 1", $customer_id, $throttle)
90 90
 			);
91 91
 
92
-			if ( $row_id < 1 ) {
93
-				give_set_error( 'give_email_access_attempts_exhausted', __( 'Please wait a few minutes before requesting a new email access link.', 'give' ) );
92
+			if ($row_id < 1) {
93
+				give_set_error('give_email_access_attempts_exhausted', __('Please wait a few minutes before requesting a new email access link.', 'give'));
94 94
 
95 95
 				return false;
96 96
 			}
@@ -106,38 +106,38 @@  discard block
 block discarded – undo
106 106
 	 * @param $customer_id
107 107
 	 * @param $email
108 108
 	 */
109
-	function send_email( $customer_id, $email ) {
109
+	function send_email($customer_id, $email) {
110 110
 
111
-		$verify_key = wp_generate_password( 20, false );
111
+		$verify_key = wp_generate_password(20, false);
112 112
 
113 113
 		// Generate a new verify key
114
-		$this->set_verify_key( $customer_id, $email, $verify_key );
114
+		$this->set_verify_key($customer_id, $email, $verify_key);
115 115
 
116 116
 		// Get the purchase history URL
117
-		$page_id = give_get_option( 'history_page' );
117
+		$page_id = give_get_option('history_page');
118 118
 
119
-		$access_url = add_query_arg( array(
119
+		$access_url = add_query_arg(array(
120 120
 			'give_nl' => $verify_key,
121
-		), get_permalink( $page_id ) );
121
+		), get_permalink($page_id));
122 122
 
123 123
 		//Nice subject and message
124
-		$subject = apply_filters( 'give_email_access_token_subject', sprintf( __( 'Your Access Link to %1$s', 'give' ), get_bloginfo( 'name' ) ) );
124
+		$subject = apply_filters('give_email_access_token_subject', sprintf(__('Your Access Link to %1$s', 'give'), get_bloginfo('name')));
125 125
 
126
-		$message = __( 'You or someone in your organization requested an access link be sent to this email address. This is a temporary access link for you to view your donation information. Click on the link below to view:', 'give' ) . "\n\n";
126
+		$message = __('You or someone in your organization requested an access link be sent to this email address. This is a temporary access link for you to view your donation information. Click on the link below to view:', 'give')."\n\n";
127 127
 
128
-		$message .= '<a href="' . esc_url( $access_url ) . '" target="_blank">' . __( 'Access My Donation Details', 'give' ) . ' &raquo;</a>';
128
+		$message .= '<a href="'.esc_url($access_url).'" target="_blank">'.__('Access My Donation Details', 'give').' &raquo;</a>';
129 129
 
130 130
 		$message .= "\n\n";
131 131
 		$message .= "\n\n";
132
-		$message .= __( 'Sincerely,', 'give' );
133
-		$message .= "\n" . get_bloginfo( 'name' ) . "\n";
132
+		$message .= __('Sincerely,', 'give');
133
+		$message .= "\n".get_bloginfo('name')."\n";
134 134
 
135
-		$message = apply_filters( 'give_email_access_token_message', $message );
135
+		$message = apply_filters('give_email_access_token_message', $message);
136 136
 
137 137
 
138 138
 		// Send the email
139
-		Give()->emails->__set( 'heading', apply_filters( 'give_email_access_token_heading', __( 'Your Access Link', 'give' ) ) );
140
-		Give()->emails->send( $email, $subject, $message );
139
+		Give()->emails->__set('heading', apply_filters('give_email_access_token_heading', __('Your Access Link', 'give')));
140
+		Give()->emails->send($email, $subject, $message);
141 141
 
142 142
 	}
143 143
 
@@ -147,23 +147,23 @@  discard block
 block discarded – undo
147 147
 	 */
148 148
 	function check_for_token() {
149 149
 
150
-		$token = isset( $_GET['give_nl'] ) ? $_GET['give_nl'] : '';
150
+		$token = isset($_GET['give_nl']) ? $_GET['give_nl'] : '';
151 151
 
152 152
 		// Check for cookie
153
-		if ( empty( $token ) ) {
154
-			$token = isset( $_COOKIE['give_nl'] ) ? $_COOKIE['give_nl'] : '';
153
+		if (empty($token)) {
154
+			$token = isset($_COOKIE['give_nl']) ? $_COOKIE['give_nl'] : '';
155 155
 		}
156 156
 
157
-		if ( ! empty( $token ) ) {
158
-			if ( ! $this->is_valid_token( $token ) ) {
159
-				if ( ! $this->is_valid_verify_key( $token ) ) {
157
+		if ( ! empty($token)) {
158
+			if ( ! $this->is_valid_token($token)) {
159
+				if ( ! $this->is_valid_verify_key($token)) {
160 160
 					return;
161 161
 				}
162 162
 			}
163 163
 
164 164
 			$this->token_exists = true;
165 165
 			// Set cookie
166
-			setcookie( 'give_nl', $token );
166
+			setcookie('give_nl', $token);
167 167
 		}
168 168
 	}
169 169
 
@@ -174,18 +174,18 @@  discard block
 block discarded – undo
174 174
 	 *
175 175
 	 * @return bool
176 176
 	 */
177
-	function is_valid_token( $token ) {
177
+	function is_valid_token($token) {
178 178
 
179 179
 		global $wpdb;
180 180
 
181 181
 		// Make sure token isn't expired
182
-		$expires = date( 'Y-m-d H:i:s', time() - $this->token_expiration );
182
+		$expires = date('Y-m-d H:i:s', time() - $this->token_expiration);
183 183
 
184 184
 		$email = $wpdb->get_var(
185
-			$wpdb->prepare( "SELECT email FROM {$wpdb->prefix}give_customers WHERE token = %s AND verify_throttle >= %s LIMIT 1", $token, $expires )
185
+			$wpdb->prepare("SELECT email FROM {$wpdb->prefix}give_customers WHERE token = %s AND verify_throttle >= %s LIMIT 1", $token, $expires)
186 186
 		);
187 187
 
188
-		if ( ! empty( $email ) ) {
188
+		if ( ! empty($email)) {
189 189
 			$this->token_email = $email;
190 190
 			$this->token       = $token;
191 191
 
@@ -193,8 +193,8 @@  discard block
 block discarded – undo
193 193
 		}
194 194
 
195 195
 		//Set error only if email access form isn't being submitted
196
-		if ( ! isset( $_POST['give_email'] ) && ! isset( $_POST['_wpnonce'] ) ) {
197
-			give_set_error( 'give_email_token_expired', apply_filters( 'give_email_token_expired_message', 'Sorry, your access token has expired. Please request a new one below:', 'give' ) );
196
+		if ( ! isset($_POST['give_email']) && ! isset($_POST['_wpnonce'])) {
197
+			give_set_error('give_email_token_expired', apply_filters('give_email_token_expired_message', 'Sorry, your access token has expired. Please request a new one below:', 'give'));
198 198
 		}
199 199
 
200 200
 
@@ -209,25 +209,25 @@  discard block
 block discarded – undo
209 209
 	 * @param $email
210 210
 	 * @param $verify_key
211 211
 	 */
212
-	function set_verify_key( $customer_id, $email, $verify_key ) {
212
+	function set_verify_key($customer_id, $email, $verify_key) {
213 213
 		global $wpdb;
214 214
 
215
-		$now = date( 'Y-m-d H:i:s' );
215
+		$now = date('Y-m-d H:i:s');
216 216
 
217 217
 		// Insert or update?
218 218
 		$row_id = (int) $wpdb->get_var(
219
-			$wpdb->prepare( "SELECT id FROM {$wpdb->prefix}give_customers WHERE id = %d LIMIT 1", $customer_id )
219
+			$wpdb->prepare("SELECT id FROM {$wpdb->prefix}give_customers WHERE id = %d LIMIT 1", $customer_id)
220 220
 		);
221 221
 
222 222
 		// Update
223
-		if ( ! empty( $row_id ) ) {
223
+		if ( ! empty($row_id)) {
224 224
 			$wpdb->query(
225
-				$wpdb->prepare( "UPDATE {$wpdb->prefix}give_customers SET verify_key = %s, verify_throttle = %s WHERE id = %d LIMIT 1", $verify_key, $now, $row_id )
225
+				$wpdb->prepare("UPDATE {$wpdb->prefix}give_customers SET verify_key = %s, verify_throttle = %s WHERE id = %d LIMIT 1", $verify_key, $now, $row_id)
226 226
 			);
227 227
 		} // Insert
228 228
 		else {
229 229
 			$wpdb->query(
230
-				$wpdb->prepare( "INSERT INTO {$wpdb->prefix}give_customers ( verify_key, verify_throttle) VALUES (%s, %s)", $verify_key, $now )
230
+				$wpdb->prepare("INSERT INTO {$wpdb->prefix}give_customers ( verify_key, verify_throttle) VALUES (%s, %s)", $verify_key, $now)
231 231
 			);
232 232
 		}
233 233
 	}
@@ -239,20 +239,20 @@  discard block
 block discarded – undo
239 239
 	 *
240 240
 	 * @return bool
241 241
 	 */
242
-	function is_valid_verify_key( $token ) {
242
+	function is_valid_verify_key($token) {
243 243
 		global $wpdb;
244 244
 
245 245
 		// See if the verify_key exists
246 246
 		$row = $wpdb->get_row(
247
-			$wpdb->prepare( "SELECT id, email FROM {$wpdb->prefix}give_customers WHERE verify_key = %s LIMIT 1", $token )
247
+			$wpdb->prepare("SELECT id, email FROM {$wpdb->prefix}give_customers WHERE verify_key = %s LIMIT 1", $token)
248 248
 		);
249 249
 
250
-		$now = date( 'Y-m-d H:i:s' );
250
+		$now = date('Y-m-d H:i:s');
251 251
 
252 252
 		// Set token
253
-		if ( ! empty( $row ) ) {
253
+		if ( ! empty($row)) {
254 254
 			$wpdb->query(
255
-				$wpdb->prepare( "UPDATE {$wpdb->prefix}give_customers SET verify_key = '', token = %s, verify_throttle = %s WHERE id = %d LIMIT 1", $token, $now, $row->id )
255
+				$wpdb->prepare("UPDATE {$wpdb->prefix}give_customers SET verify_key = '', token = %s, verify_throttle = %s WHERE id = %d LIMIT 1", $token, $now, $row->id)
256 256
 			);
257 257
 
258 258
 			$this->token_email = $row->email;
@@ -271,7 +271,7 @@  discard block
 block discarded – undo
271 271
 	 *
272 272
 	 * @return mixed
273 273
 	 */
274
-	function users_purchases_args( $args ) {
274
+	function users_purchases_args($args) {
275 275
 		$args['user'] = $this->token_email;
276 276
 
277 277
 		return $args;
@@ -288,11 +288,11 @@  discard block
 block discarded – undo
288 288
 		global $wpdb;
289 289
 
290 290
 		//Create columns in customers table
291
-		$query = $wpdb->query( "ALTER TABLE {$wpdb->prefix}give_customers ADD `token` VARCHAR(255) CHARACTER SET utf8 NOT NULL, ADD `verify_key` VARCHAR(255) CHARACTER SET utf8 NOT NULL AFTER `token`, ADD `verify_throttle` DATETIME NOT NULL AFTER `verify_key`" );
291
+		$query = $wpdb->query("ALTER TABLE {$wpdb->prefix}give_customers ADD `token` VARCHAR(255) CHARACTER SET utf8 NOT NULL, ADD `verify_key` VARCHAR(255) CHARACTER SET utf8 NOT NULL AFTER `token`, ADD `verify_throttle` DATETIME NOT NULL AFTER `verify_key`");
292 292
 
293 293
 		//Columns added properly
294
-		if ( $query ) {
295
-			give_update_option( 'email_access_installed', 1 );
294
+		if ($query) {
295
+			give_update_option('email_access_installed', 1);
296 296
 		}
297 297
 
298 298
 	}
Please login to merge, or discard this patch.