Completed
Push — master ( 2f95aa...1df65a )
by Evan
05:36
created

view-user.php ➔ yikes_mc_geocode_subscriber_data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 15
rs 9.4285
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 367 and the first side effect is on line 6.

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
	*	Get and store our variables
4
	*	@since 6.0
5
	*/
6
	$list_id = $_GET['mailchimp-list'];
7
	$email_id = (int) esc_attr( $_GET['email-id'] );
8
	
9
	/*
10
	*	Confirm that our data is set
11
	*	or abort...
12
	*/
13
	if( ! isset( $list_id ) || ! isset( $email_id ) ) {
14
		wp_die( "We've encountered an error. Please go back and try again", "yikes-inc-easy-mailchimp-extender" );
15
		exit;
16
	}
17
	
18
	// run our API call, to get list data..
19
	$api_key = trim( get_option( 'yikes-mc-api-key' , '' ) );
20
	$dash_position = strpos( $api_key, '-' );
21
	
22
	// get this lists data
23
	if( $dash_position !== false ) {
24
		$api_endpoint = 'https://' . substr( $api_key, $dash_position + 1 ) . '.api.mailchimp.com/2.0/lists/member-info.json';
25
	}
26
	$user_data = wp_remote_post( $api_endpoint, array( 
27
		'body' => array( 
28
			'apikey' => $api_key, 
29
			'id' => $list_id, 
30
			'emails' => array( 
31
				array( 'leid' => $email_id )
32
			),
33
		),
34
		'timeout' => 10,
35
		'sslverify' => apply_filters( 'yikes-mailchimp-sslverify', true ),
36
	) );
37
	$user_data = json_decode( wp_remote_retrieve_body( $user_data ), true );	
38
	
39
	if( isset( $user_data['error'] ) ) {	
40
		if( WP_DEBUG || get_option( 'yikes-mailchimp-debug-status' , '' ) == '1' ) {
41
			require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php';
42
			$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
43
			$error_logging->yikes_easy_mailchimp_write_to_error_log( $user_data['error'], __( "Get Member Info" , 'yikes-inc-easy-mailchimp-extender' ), "View User Page" );
44
		}
45
	}
46
		
47
	/*
48
	*	Check for MailChimp returned errors
49
	*/
50
	if( isset( $user_data['error'] ) ) {
51
		echo '<h4>Error</h4>';
52
		echo $user_data['error'] . '.';
53
		return;
54
	}	
55
	
56
	if( isset( $user_data['data'][0] ) ) {
57
		// reset our data so we can easily use it
58
		$user_data = $user_data['data'][0];
59
		
60
		$other_lists = ( isset( $user_data['lists'] ) && ! empty( $user_data['lists'] ) ) ? $user_data['lists'] : array();
61
		$merge_data_array = ( $user_data['merges'] && ! empty( $user_data['merges'] ) ) ? $user_data['merges'] : array();
62
		
63
		// print_r( $user_data );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
64
		
65
		/* Empty array to populate with list names */
66
		$additional_lists = array();
67
		/* Merge Variable Fields */
68
		$merge_variable_fields = array();
69
		
70
		/* Build the array of mailing lists the user is subscribed to */
71
		if( isset( $other_lists ) && count( $other_lists ) >= 1 ) {
72
			foreach( $other_lists as $list ) {
73
				if( $list['status'] == 'subscribed' ) {	
74
					if( $dash_position !== false ) {
75
						$api_endpoint = 'https://' . substr( $api_key, $dash_position + 1 ) . '.api.mailchimp.com/2.0/lists/list.json';
76
					}
77
					$list_data = wp_remote_post( $api_endpoint, array( 
78
						'body' => array( 
79
							'apikey' => $api_key, 
80
							'filters' => array(
81
								'list_id' => $list['id'] 
82
							) 
83
						),
84
						'timeout' => 10,
85
						'sslverify' => apply_filters( 'yikes-mailchimp-sslverify', true )
86
					) );
87
					$list_data = json_decode( wp_remote_retrieve_body( $list_data ), true );	
88
					if( isset( $list_data['error'] ) ) {	
89
						if( WP_DEBUG || get_option( 'yikes-mailchimp-debug-status' , '' ) == '1' ) {
90
							require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php';
91
							$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
92
							$error_logging->yikes_easy_mailchimp_write_to_error_log( $list_data['error'], __( "Get Account Lists" , 'yikes-inc-easy-mailchimp-extender' ), "View User Page" );
93
						}
94
					}
95
					if( $list_data && isset( $list_data['data'][0] ) ) {
96
						$additional_lists[$list_data['data'][0]['id']] = $list_data['data'][0]['name'];
97
					}
98
				}
99
			}	
100
		}
101
		
102
		/* Build the array of merge variables => value */
103
		if( isset( $merge_data_array ) && count( $merge_data_array ) >= 1 ) {
104
			if( $dash_position !== false ) {
105
				$api_endpoint = 'https://' . substr( $api_key, $dash_position + 1 ) . '.api.mailchimp.com/2.0/lists/merge-vars.json';
106
			}
107
			$merge_variables = wp_remote_post( $api_endpoint, array( 
108
				'body' => array( 
109
					'apikey' => $api_key, 
110
					'id' => array( $list_id ),
111
				),
112
				'timeout' => 10,
113
				'sslverify' => apply_filters( 'yikes-mailchimp-sslverify', true )
114
			) );
115
			$merge_variables = json_decode( wp_remote_retrieve_body( $merge_variables ), true );
116
			if( isset( $merge_variables['error'] ) ) {	
117
				if( WP_DEBUG || get_option( 'yikes-mailchimp-debug-status' , '' ) == '1' ) {
118
					require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php';
119
					$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
120
					$error_logging->yikes_easy_mailchimp_write_to_error_log( $merge_variables['error'], __( "Get Merge Variables" , 'yikes-inc-easy-mailchimp-extender' ), "View User Page" );
121
				}
122
			}
123
			// loop and display
124
			if( $merge_variables ) {
125
				foreach( $merge_variables['data'][0]['merge_vars'] as $merge_variable ) {
126
					if( $merge_variable['tag'] != 'EMAIL' ) {					
127
						$merge_variable_fields[$merge_variable['name']] = ( isset( $merge_data_array[$merge_variable['tag']] ) ) ? $merge_data_array[$merge_variable['tag']] : '';
128
					}
129
				}
130
			}
131
		}
132
		
133
				
134
		// store usable data
135
		$user_email = sanitize_email( $user_data['email'] );
136
		// prepend our email address to the beginning
137
		$merge_variable_fields = array( 'Email Address' => $user_email ) + $merge_variable_fields;
138
		$gravatar_image = get_avatar( $user_email, 120 );
139
		$email_type = $user_data['email_type'];
140
		$member_rating = ( ! empty( $user_data['member_rating'] ) ) ? (int) $user_data['member_rating'] : 0;
141
		$member_rating_stars = '';
142
		if( $member_rating > 0 ) {
143
			$x = 1;
144
			while( $x <= 5 ) {
145
				if( $x <= $member_rating ) {
146
					$member_rating_stars .= '<span class="yikes-mc-member-rating-star dashicons dashicons-star-filled"></span>';
147
				} else {
148
					$member_rating_stars .= '<span class="yikes-mc-member-rating-star dashicons dashicons-star-empty"></span>';
149
				}
150
				$x++;
151
			}
152
		} else {
153
			$y = 1;
154
			while( $y <= 5 ) {
155
				$member_rating_stars .= '<span class="yikes-mc-member-rating-star dashicons dashicons-star-empty"></span>';
156
				$y++;
157
			}
158
		}
159
		$last_changed = strtotime( $user_data['info_changed'] );
160
		$user_language = ( $user_data['language'] && $user_data['language'] != '' ) ? $user_data['language'] : '';
161
		$list_name = $user_data['list_name'];
162
		
163
		// Generate our display page
164
		?>
165
			<div class="wrap view-user-data-wrap">
166
				<!-- Freddie Logo -->
167
				<img src="<?php echo YIKES_MC_URL . 'includes/images/MailChimp_Assets/Freddie_60px.png'; ?>" alt="<?php __( 'Freddie - MailChimp Mascot' , 'yikes-inc-easy-mailchimp-extender' ); ?>" class="yikes-mc-freddie-logo" />
168
					
169
				<h1>YIKES Easy Forms for MailChimp | <?php _e( 'Subscriber Details' , 'yikes-inc-easy-mailchimp-extender' ); ?></h1>				
170
						
171
				<!-- Settings Page Description -->
172
				<p class="yikes-easy-mc-about-text about-text"><?php printf( __( 'View %s subscriber details below.' , 'yikes-inc-easy-mailchimp-extender' ), $user_email ); ?></p>
173
					
174
				<section class="yikes-mc-view-list-breadcrumbs">
175
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=yikes-inc-easy-mailchimp-lists' ) ); ?>" title="<?php _e( 'View List', 'yikes-inc-easy-mailchimp-extender' ); ?>">
176
						<?php _e( 'Optin Forms', 'yikes-inc-easy-mailchimp-extender' ); ?>
177
					</a>
178
					&nbsp;&#187;&nbsp;
179
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=yikes-mailchimp-view-list&list-id=' . $list_id ) ); ?>" title="<?php echo $list_name; ?>">
180
						<?php echo $list_name; ?>
181
					</a>
182
					&nbsp;&#187;&nbsp;
183
					<span title="<?php echo $user_email; ?>">
184
						<?php echo $user_email; ?>
185
					</span>
186
				</section>
187
					
188
				<!-- Customer Container -->
189
				<div id="yikes-mc-subscriber-card-wrapper">
190
					<section class="yikes-mc-card-top">
191
						<?php echo $gravatar_image; ?>
192
						<h2><?php echo $user_email; ?></h2>
193
						<?php echo '<span class="member-star-rating-container" title="' . sprintf( _n( 'Member Rating: %s star', 'Member Rating: %s stars', $member_rating, 'yikes-inc-easy-mailchimp-extender' ), $member_rating ) . '">' . $member_rating_stars . '</span>'; ?>
194
						<span class="member-subscription-date">
195
							<?php echo __( 'Subscribed:', 'yikes-inc-easy-mailchimp-extender' ) . ' ' . get_date_from_gmt( $user_data['info_changed'], 'F jS, Y h:i a' ); ?>
196
						</span>
197
						<?php if( isset( $user_data['geo'] ) && ! empty( $user_data['geo'] ) ) { ?>
198
							<?php if( isset( $user_data['geo']['latitude'] ) && isset( $user_data['geo']['longitude'] ) ) { ?>
199
								<span class="member-location-data">
200
									<?php echo __( 'Location:', 'yikes-inc-easy-mailchimp-extender' ) . ' ' . yikes_mc_geocode_subscriber_data( $user_data['geo']['latitude'], $user_data['geo']['longitude'] ); ?>
201
								</span>
202
							<?php } else { ?>
203
							<span class="member-location-data">
204
								<?php echo __( 'Location:', 'yikes-inc-easy-mailchimp-extender' ) . ' ' . $user_data['geo']['region'] . ', ' . $user_data['geo']['cc']; ?>
205
							</span>
206
						<?php 
207
								}
208
							} 
209
						?>
210
					</section>
211
					
212
					<hr class="yikes-mc-subscriber-hr" />
213
					
214
					<?php
215
						if( ! isset( $_GET['section'] ) || ( isset( $_GET['section'] ) && $_GET['section'] == 'subscriber-data' ) ) { 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
216
							?>
217
								<section class="yikes-mc-card-body merge-variable-section">
218
								<h3><?php _e( 'Fields:', 'yikes-inc-easy-mailchimp-extender' ); ?></h3>
219
								<?php
220
									if( ! empty( $merge_variable_fields ) ) {	
221
									?>
222
										<?php foreach( $merge_variable_fields as $field_name => $value ) { ?>
223
											<li>
224
												<label>
225
													<strong class="section-label"><?php echo $field_name; ?></strong>
226
													<p class="section-value"><em><?php echo $value; ?></em></p>
227
												</label>
228
											</li>
229
										<?php }
230
									} else {
231
										?>
232
											<strong><?php _e( 'No Subscriber Data Found', 'yikes-inc-easy-mailchimp-extender' ); ?></strong>
233
										<?php
234
									}
235 View Code Duplication
									if( isset( $user_data['ip_signup'] ) && $user_data['ip_signup'] != '' ) {
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...
236
										?>
237
											<li>
238
												<label>
239
													<strong class="section-label"><?php echo __( 'Signup IP', 'yikes-inc-easy-mailchimp-extender' ) . '</strong><p class="section-value"><em>' . $user_data['ip_signup']; ?></em></p>
240
												</label>
241
											</li>
242
										<?php
243
									}
244
								}
245
								?>
246
								</section>
247
							<?php
248
249
						
250
						if( isset( $_GET['section'] ) && $_GET['section'] == 'additional-subscriptions' ) { 
251
							?>
252
								<section class="yikes-mc-card-body">
253
								<?php
254
								if( ! empty( $additional_lists ) ) {	
255
									// remove this list from the additional lists list
256
									unset( $additional_lists[$list_id] );
257
									if( ! empty( $additional_lists ) ) {
258
										?>	
259
										<h3><?php _e( 'Additional Subscriptions:', 'yikes-inc-easy-mailchimp-extender' ); ?></h3>
260
										<?php foreach( $additional_lists as $listid => $name ) { ?>
261
											<?php
262
												$user_redirect_url = esc_url_raw( admin_url( 'admin.php?page=yikes-mailchimp-view-list&list-id=' . $listid ) );
263
											?>
264
											<li><a href="<?php echo $user_redirect_url; ?>"><?php echo $name; ?></a></li>
265
										<?php }
266
									}
267
								} else {
268
								?>
269
									<strong><?php _e( 'No Other Subscriptions Found.', 'yikes-inc-easy-mailchimp-extender' ); ?></strong>
270
								<?php
271
								}
272
							?>
273
								</section>
274
							<?php
275
						}
276
					?>
277
					
278
					<?php
279
						if( isset( $_GET['section'] ) && $_GET['section'] == 'delete-subscriber' ) { 
280
							$unsubscribe_subscriber_url = esc_url_raw( add_query_arg( array( 'action' => 'yikes-easy-mc-unsubscribe-user', 'mailchimp-list' => $list_id , 'nonce' => wp_create_nonce( 'unsubscribe-user-'.$email_id ), 'email_id' => $email_id ) ) );
281
							?>	
282
								<form id="delete_subscriber" method="POST" action="<?php echo $unsubscribe_subscriber_url; ?>">
283
									<p class="description">
284
										<?php printf( __( 'Deleting this subscriber will completely remove %s from the "%s" MailChimp list.', 'yikes-inc-easy-mailchimp-extender' ), '<strong>' . $user_email . '</strong>', '<strong>' . $list_name . '</strong>' ); ?>
285
									</p>
286
									<br />
287
									<label>
288
										<input type="checkbox" name="confirm_delete_subscriber" value="1" onclick="toggleDeleteSubscriberButton(jQuery(this));">
289
										<?php printf( __( 'Are you sure you want to delete "%s" from "%s?"', 'yikes-inc-easy-mailchimp-extender' ), '<strong>' . $user_email . '</strong>', '<strong>' . $list_name . '</strong>' ); ?>
290
									</label>
291
									<?php echo submit_button( __( 'Delete Subscriber', 'yikes-inc-easy-mailchimp-extender' ), 'primary', 'delete-mailchimp-subscriber', true, array( 'disabled' => 'disabled' ) ); ?>
292
								</form>
293
							<?php
294
						}
295
					?>
296
					
297
				</div>
298
				
299
				<?php
300
					// setup the redirect url for our additional subscriptions
301
					$subscriber_details = esc_url_raw(
302
						add_query_arg(
303
							array(
304
								'section' => 'subscriber-data'
305
							)
306
						)
307
					);
308
					// setup the redirect url for our additional subscriptions
309
					$additional_subscription_url = esc_url_raw(
310
						add_query_arg(
311
							array(
312
								'section' => 'additional-subscriptions'
313
							)
314
						)
315
					);
316
					// setup the redirect url for our delete subscriber
317
					$delete_subscriber_url = esc_url_raw(
318
						add_query_arg(
319
							array(
320
								'section' => 'delete-subscriber'
321
							)
322
						)
323
					);
324
				?>	
325
				<!-- Tabs -->
326
				<div id="customer-tab-wrapper">
327
						<ul id="customer-tab-wrapper-list">
328
							
329
							<?php if( isset( $_GET['section'] ) && $_GET['section'] != 'subscriber-data' ) { ?>
330
								<a title="<?php _e( 'Subscriber Details', 'yikes-inc-easy-mailchimp-extender' ); ?>" aria-label="<?php _e( 'Subscriber Details', 'yikes-inc-easy-mailchimp-extender' ); ?>" href="<?php echo $subscriber_details; ?>">
331
							<?php } ?>
332
							
333
								<li <?php if( ! isset( $_GET['section'] ) || ( isset( $_GET['section'] ) && $_GET['section'] == 'subscriber-data' ) ) { ?>class="active"<?php } else { ?>class="inactive"<?php } ?>><span class="dashicons  dashicons-id"></span></li>
334
										
335
							<?php if( isset( $_GET['section'] ) && $_GET['section'] != 'subscriber-data' ) { ?>
336
								</a>
337
							<?php } ?>
338
							
339 View Code Duplication
							<?php if( ! isset( $_GET['section'] ) || ( isset( $_GET['section'] ) && $_GET['section'] != 'additional-subscriptions' ) ) { ?>
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...
340
								<a title="<?php _e( 'Additional Subscriptions', 'yikes-inc-easy-mailchimp-extender' ); ?>" aria-label="<?php _e( 'Additional Subscriptions', 'yikes-inc-easy-mailchimp-extender' ); ?>" href="<?php echo $additional_subscription_url; ?>">
341
							<?php } ?>
342
								
343
								<li <?php if( isset( $_GET['section'] ) && $_GET['section'] == 'additional-subscriptions' ) { ?>class="active"<?php } else { ?>class="inactive"<?php } ?>><span class="dashicons dashicons-portfolio"></span></li>
344
							
345
							<?php if( ! isset( $_GET['section'] ) || ( isset( $_GET['section'] ) && $_GET['section'] == 'additional-subscriptions' ) ) { ?>
346
								</a>
347
							<?php } ?>
348
																
349 View Code Duplication
							<?php if( ! isset( $_GET['section'] ) || isset( $_GET['section'] ) && $_GET['section'] != 'delete-subscriber' ) { ?>
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...
350
								<a title="<?php _e( 'Delete Subscriber', 'yikes-inc-easy-mailchimp-extender' ); ?>" aria-label="<?php _e( 'Delete Subscriber', 'yikes-inc-easy-mailchimp-extender' ); ?>" href="<?php echo $delete_subscriber_url; ?>">
351
							<?php } ?>
352
							
353
								<li <?php if( isset( $_GET['section'] ) && $_GET['section'] == 'delete-subscriber' ) { ?>class="active"<?php } else { ?>class="inactive"<?php } ?>><span class="dashicons dashicons-trash"></span></li>
354
								
355
							<?php if( ! isset( $_GET['section'] ) || ( isset( $_GET['section'] ) && $_GET['section'] == 'delete-subscriber' ) ) { ?>
356
								</a>
357
							<?php } ?>
358
							
359
						</ul>
360
					</div>
361
				
362
			</div>
363
		<?php
364
	}
365
	
366
	
367
	function yikes_mc_geocode_subscriber_data( $latitude, $longitude ) {
368
		$geocode_url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' . $latitude . ',' . $longitude;
369
		$geocode_response = wp_remote_get( $geocode_url );
370
		if( is_wp_error( $geocode_response ) ) {
371
			return;
372
		}
373
		$geocode_response_body = json_decode( wp_remote_retrieve_body( $geocode_response ), true );
374
		if( is_wp_error( $geocode_response_body ) ) {
375
			return;
376
		}
377
		$city = $geocode_response_body['results'][0]['address_components'][2]['short_name'];
378
		$state = $geocode_response_body['results'][0]['address_components'][5]['short_name'];
379
		$country = $geocode_response_body['results'][0]['address_components'][6]['short_name'];
380
		return $link = '<a href="http://maps.google.com/maps?q=' . $latitude . ',' . $longitude . '" target="_blank" title="' . __( 'View Google Map', 'yikes-inc-easy-mailchimp-extender' ) . '">' . $city . ', ' . $state . ', ' . $country . '</a>&nbsp;<span class="flag-icon flag-icon-' . strtolower( $country ) . '"></span>';
0 ignored issues
show
Unused Code introduced by
$link 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...
381
	}