Completed
Pull Request — master (#808)
by
unknown
07:45 queued 05:21
created

view-user.php ➔ yikes_mc_geocode_subscriber_data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/*
3
*	Get and store our variables
4
*	@since 6.0
5
*/
6
$list_id     = $_GET['mailchimp-list'];
7
$email_id    = esc_attr( $_GET['email-id'] );
8
$list_helper = yikes_get_mc_api_manager()->get_list_handler();
9
10
/*
11
*	Confirm that our data is set
12
*	or abort...
13
*/
14
if ( ! isset( $list_id ) || ! isset( $email_id ) ) {
15
	wp_die( "We've encountered an error. Please go back and try again", 'yikes-inc-easy-mailchimp-extender' );
16
	exit;
17
}
18
19
$user_data = $list_helper->get_member( $list_id, $email_id );
20
if ( is_wp_error( $user_data ) ) {
21
	$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
22
	$error_logging->maybe_write_to_log(
23
		$user_data->get_error_code(),
24
		__( 'Get Member Info', 'yikes-inc-easy-mailchimp-extender' ),
25
		'View User Page'
26
	);
27
28
	echo '<h4>Error</h4>';
29
	echo $user_data->get_error_code() . '.';
30
31
	return;
32
}
33
34
if ( empty( $user_data ) ) {
35
	return;
36
}
37
38
$other_lists      = $list_helper->get_members_lists( $email_id );
39
$merge_data_array = $user_data['merge_fields'];
40
41
$additional_lists = array();
42
$merge_variable_fields = array();
43
44
/* Build the array of mailing lists the user is subscribed to */
45
foreach ( $other_lists as $id => $value ) {
46
	if ( 'subscribed' !== $value['status'] ) {
47
		continue;
48
	}
49
50
	$list_data = $list_helper->get_list( $id );
51 View Code Duplication
	if ( is_wp_error( $list_data ) ) {
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...
52
		$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
53
		$error_logging->maybe_write_to_log(
54
			$list_data->get_error_code(),
55
			__( "Get Account Lists", 'yikes-inc-easy-mailchimp-extender' ),
56
			"View User Page"
57
		);
58
		continue;
59
	}
60
61
	$additional_lists[ $list_data['id'] ] = $list_data['name'];
62
}
63
64
/* Build the array of merge variables => value */
65
$merge_variables = $list_helper->get_merge_fields( $list_id );
66
if ( is_wp_error( $merge_variables ) ) {
67
	$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
68
	$error_logging->maybe_write_to_log(
69
		$merge_variables->get_error_code(),
70
		__( "Get Merge Variables", 'yikes-inc-easy-mailchimp-extender' ),
71
		"View User Page"
72
	);
73
	$merge_variables = array();
74
}
75
// loop and display
76
if ( $merge_variables ) {
77
	foreach ( $merge_variables['merge_fields'] as $merge_variable ) {
78
		if ( $merge_variable['tag'] != 'EMAIL' ) {
79
			$merge_variable_fields[ $merge_variable['name'] ] = ( isset( $merge_data_array[ $merge_variable['tag'] ] ) ) ? $merge_data_array[ $merge_variable['tag'] ] : '';
80
		}
81
	}
82
}
83
84
85
// store usable data
86
$user_email = sanitize_email( $user_data['email_address'] );
87
// prepend our email address to the beginning
88
$merge_variable_fields = array( 'Email Address' => $user_email ) + $merge_variable_fields;
89
$gravatar_image        = get_avatar( $user_email, 120 );
90
$email_type            = $user_data['email_type'];
91
$member_rating         = ( ! empty( $user_data['member_rating'] ) ) ? (int) $user_data['member_rating'] : 0;
92
$member_rating_stars   = '';
93
94
// Create member rating stars
95
for ( $i = 1; $i <= 5; $i++ ) {
96
	if ( $i <= $member_rating ) {
97
		$member_rating_stars .= '<span class="yikes-mc-member-rating-star dashicons dashicons-star-filled"></span>';
98
	} else {
99
		$member_rating_stars .= '<span class="yikes-mc-member-rating-star dashicons dashicons-star-empty"></span>';
100
	}
101
}
102
103
$last_changed  = strtotime( $user_data['last_changed'] );
104
$user_language = ( ! empty( $user_data['language'] ) ) ? $user_data['language'] : '';
105
$list_name     = $additional_lists[ $list_id ];
106
107
// Generate our display page
108
?>
109
	<div class="wrap view-user-data-wrap">
110
		<!-- Freddie Logo -->
111
		<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" />
112
113
		<h1>YIKES Easy Forms for MailChimp | <?php _e( 'Subscriber Details', 'yikes-inc-easy-mailchimp-extender' ); ?></h1>
114
115
		<!-- Settings Page Description -->
116
		<p class="yikes-easy-mc-about-text about-text"><?php printf( __( 'View %s subscriber details below.', 'yikes-inc-easy-mailchimp-extender' ), $user_email ); ?></p>
117
118
		<section class="yikes-mc-view-list-breadcrumbs">
119
			<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' ); ?>">
120
				<?php _e( 'Optin Forms', 'yikes-inc-easy-mailchimp-extender' ); ?>
121
			</a>
122
			&nbsp;&#187;&nbsp;
123
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=yikes-mailchimp-view-list&list-id=' . $list_id ) ); ?>" title="<?php echo $list_name; ?>">
124
				<?php echo $list_name; ?>
125
			</a>
126
			&nbsp;&#187;&nbsp;
127
			<span title="<?php echo $user_email; ?>">
128
				<?php echo esc_html( $user_email ); ?>
129
			</span>
130
		</section>
131
132
		<!-- Customer Container -->
133
		<div id="yikes-mc-subscriber-card-wrapper">
134
			<section class="yikes-mc-card-top">
135
				<?php echo $gravatar_image; ?>
136
				<h2><?php echo esc_html( $user_email ); ?></h2>
137
				<?php /* translators: the placeholder is a number between 1-5 */ ?>
138
				<span class="member-star-rating-container" title="<?php echo esc_html( sprintf( _n( 'Member Rating: %s star', 'Member Rating: %s stars', esc_attr( $member_rating ), 'yikes-inc-easy-mailchimp-extender' ), esc_attr( $member_rating ) ) ); ?>">
139
					<?php echo $member_rating_stars; ?>
140
				</span>
141
				<span class="member-subscription-date">
142
						<?php
143
							/* translators: the placeholder is a datetime string. */
144
							echo sprintf( esc_html__( 'Subscribed: %1$1s', 'yikes-inc-easy-mailchimp-extender' ), esc_html( gmdate( 'F jS, Y h:i a', $last_changed ) ) );
145
						?>
146
					</span>
147
				<?php if ( isset( $user_data['location'] ) && isset( $user_data['location']['timezone'] ) && ! empty( $user_data['location']['timezone'] ) && isset( $user_data['location']['country_code'] ) && ! empty( $user_data['location']['country_code'] ) ) { ?>
148
					<span class="member-location-data">
149
						<?php
150
							/* translators: the placeholders are a timezone and a country code */
151
							echo sprintf( esc_html__( 'Location: %1$1s, %2$2s', 'yikes-inc-easy-mailchimp-extender' ), esc_html( $user_data['location']['timezone'] ), esc_html( $user_data['location']['country_code'] ) );
152
						?>
153
					</span>
154
				<?php } ?>
155
			</section>
156
157
			<hr class="yikes-mc-subscriber-hr" />
158
159
			<?php
160
			if ( ! isset( $_GET['section'] ) || ( isset( $_GET['section'] ) && filter_var( wp_unslash( $_GET['section'] ), FILTER_SANITIZE_STRING ) === 'subscriber-data' ) ) {
161
				?>
162
			<section class="yikes-mc-card-body merge-variable-section">
163
				<h3><?php esc_html_e( 'Fields:', 'yikes-inc-easy-mailchimp-extender' ); ?></h3>
164
				<?php
165
				if ( ! empty( $merge_variable_fields ) ) {
166
					?>
167
					<?php foreach ( $merge_variable_fields as $field_name => $value ) { ?>
168
						<li>
169
							<label>
170
								<strong class="section-label"><?php echo esc_html( $field_name ); ?></strong>
171
								<p class="section-value"><em><?php echo esc_html( $value ); ?></em></p>
172
							</label>
173
						</li>
174
					<?php }
175
				} else {
176
					?>
177
					<strong><?php esc_html_e( 'No Subscriber Data Found', 'yikes-inc-easy-mailchimp-extender' ); ?></strong>
178
					<?php
179
				}
180
				if ( isset( $user_data['ip_signup'] ) && ! empty( $user_data['ip_signup'] ) ) {
181
					?>
182
					<li>
183
						<label>
184
							<strong class="section-label"><?php esc_html_e( 'Signup IP', 'yikes-inc-easy-mailchimp-extender' ); ?></strong>
185
							<p class="section-value"><em><?php esc_html_e( $user_data['ip_signup'] ); ?></em></p>
186
						</label>
187
					</li>
188
					<?php
189
				}
190
				}
191
				?>
192
			</section>
193
			<?php
194
195
196
			if ( isset( $_GET['section'] ) && $_GET['section'] == 'additional-subscriptions' ) {
197
				?>
198
				<section class="yikes-mc-card-body">
199
					<?php
200
					if ( ! empty( $additional_lists ) ) {
201
						// remove this list from the additional lists list
202
						unset( $additional_lists[ $list_id ] );
203
						if ( ! empty( $additional_lists ) ) {
204
							?>
205
							<h3><?php _e( 'Additional Subscriptions:', 'yikes-inc-easy-mailchimp-extender' ); ?></h3>
206
							<?php foreach ( $additional_lists as $listid => $name ) { ?>
207
								<?php
208
								$user_redirect_url = esc_url_raw( admin_url( 'admin.php?page=yikes-mailchimp-view-list&list-id=' . $listid ) );
209
								?>
210
								<li><a href="<?php echo $user_redirect_url; ?>"><?php echo $name; ?></a></li>
211
							<?php }
212
						}
213
					} else {
214
						?>
215
						<strong><?php _e( 'No Other Subscriptions Found.', 'yikes-inc-easy-mailchimp-extender' ); ?></strong>
216
						<?php
217
					}
218
					?>
219
				</section>
220
				<?php
221
			}
222
			?>
223
224
			<?php
225
			if ( isset( $_GET['section'] ) && $_GET['section'] == 'delete-subscriber' ) {
226
				$unsubscribe_subscriber_url = esc_url_raw( add_query_arg( array(
227
					'action'         => 'yikes-easy-mc-unsubscribe-user',
228
					'mailchimp-list' => $list_id,
229
					'nonce'          => wp_create_nonce( 'unsubscribe-user-' . $email_id ),
230
					'email_id'       => $email_id,
231
				) ) );
232
				?>
233
				<form id="delete_subscriber" method="POST" action="<?php echo $unsubscribe_subscriber_url; ?>">
234
					<p class="description">
235
						<?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>' ); ?>
236
					</p>
237
					<br />
238
					<label>
239
						<input type="checkbox" name="confirm_delete_subscriber" value="1" onclick="toggleDeleteSubscriberButton(jQuery(this));">
240
						<?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>' ); ?>
241
					</label>
242
					<?php submit_button( __( 'Delete Subscriber', 'yikes-inc-easy-mailchimp-extender' ), 'primary', 'delete-mailchimp-subscriber', true, array( 'disabled' => 'disabled' ) ); ?>
243
				</form>
244
				<?php
245
			}
246
			?>
247
248
		</div>
249
250
		<?php
251
		// setup the redirect url for our additional subscriptions
252
		$subscriber_details = esc_url_raw(
253
			add_query_arg(
254
				array(
255
					'section' => 'subscriber-data',
256
				)
257
			)
258
		);
259
		// setup the redirect url for our additional subscriptions
260
		$additional_subscription_url = esc_url_raw(
261
			add_query_arg(
262
				array(
263
					'section' => 'additional-subscriptions',
264
				)
265
			)
266
		);
267
		// setup the redirect url for our delete subscriber
268
		$delete_subscriber_url = esc_url_raw(
269
			add_query_arg(
270
				array(
271
					'section' => 'delete-subscriber',
272
				)
273
			)
274
		);
275
		?>
276
		<!-- Tabs -->
277
		<div id="customer-tab-wrapper">
278
			<ul id="customer-tab-wrapper-list">
279
280
				<?php if ( isset( $_GET['section'] ) && $_GET['section'] != 'subscriber-data' ) { ?>
281
				<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; ?>">
282
					<?php } ?>
283
284
					<li <?php if ( ! isset( $_GET['section'] ) || ( isset( $_GET['section'] ) && $_GET['section'] == 'subscriber-data' ) ) { ?>class="active" <?php } else { ?>class="inactive"<?php } ?>>
285
						<span class="dashicons  dashicons-id"></span></li>
286
287
					<?php if ( isset( $_GET['section'] ) && $_GET['section'] != 'subscriber-data' ) { ?>
288
				</a>
289
			<?php } ?>
290
291 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...
292
				<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; ?>">
293
					<?php } ?>
294
295
					<li <?php if ( isset( $_GET['section'] ) && $_GET['section'] == 'additional-subscriptions' ) { ?>class="active" <?php } else { ?>class="inactive"<?php } ?>>
296
						<span class="dashicons dashicons-portfolio"></span></li>
297
298
					<?php if ( ! isset( $_GET['section'] ) || ( isset( $_GET['section'] ) && $_GET['section'] == 'additional-subscriptions' ) ) { ?>
299
				</a>
300
			<?php } ?>
301
302 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...
303
				<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; ?>">
304
					<?php } ?>
305
306
					<li <?php if ( isset( $_GET['section'] ) && $_GET['section'] == 'delete-subscriber' ) { ?>class="active" <?php } else { ?>class="inactive"<?php } ?>>
307
						<span class="dashicons dashicons-trash"></span></li>
308
309
					<?php if ( ! isset( $_GET['section'] ) || ( isset( $_GET['section'] ) && $_GET['section'] == 'delete-subscriber' ) ) { ?>
310
				</a>
311
			<?php } ?>
312
313
			</ul>
314
		</div>
315
316
	</div>
317