index.php ➔ wsl_component_authtest()   F
last analyzed

Complexity

Conditions 39
Paths > 20000

Size

Total Lines 356

Duplication

Lines 45
Ratio 12.64 %

Importance

Changes 0
Metric Value
cc 39
nc 10096142
nop 0
dl 45
loc 356
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*!
3
* WordPress Social Login
4
*
5
* https://miled.github.io/wordpress-social-login/ | https://github.com/miled/wordpress-social-login
6
*   (c) 2011-2020 Mohamed Mrassi and contributors | https://wordpress.org/plugins/wordpress-social-login/
7
*/
8
9
/**
10
* Authentication Playground
11
*/
12
13
// Exit if accessed directly
14
if ( !defined( 'ABSPATH' ) ) exit;
15
16
/*!
17
	Important
18
19
	Direct access to providers apis is newly introduced into WSL and we are still experimenting, so they may change in future releases.
20
*/
21
22
// --------------------------------------------------------------------
23
24
function wsl_component_authtest()
25
{
26
	// HOOKABLE:
27
	do_action( "wsl_component_authtest_start" );
28
29
	$adapter         = null;
30
	$provider_id     = isset( $_REQUEST["provider"] ) ? $_REQUEST["provider"] : null;
31
	$user_profile    = null;
32
	$assets_base_url = '';
33
34
	if ( ! empty( $provider_id ) )
35
	{
36
		$assets_base_url = WORDPRESS_SOCIAL_LOGIN_PLUGIN_URL . 'assets/img/';
37
38
		try
39
		{
40
			$adapter = wsl_process_login_get_provider_adapter( $provider_id );
41
42
			// make as few call as possible
43 View Code Duplication
			if( ! ( isset( $_SESSION['wsl::userprofile'] ) && $_SESSION['wsl::userprofile'] && $user_profile = json_decode( $_SESSION['wsl::userprofile'] ) ) )
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...
44
			{
45
				$user_profile = $adapter->getUserProfile();
46
				$_SESSION['wsl::userprofile'] = json_encode( $user_profile );
47
			}
48
49
		}
50
		catch( Exception $e )
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
51
		{
52
		}
53
	}
54
55
	$ha_profile_fields = array(
56
		array( 'field' => 'identifier'  , 'label' => _wsl__( "Provider user ID" , 'wordpress-social-login') ),
57
		array( 'field' => 'profileURL'  , 'label' => _wsl__( "Profile URL"      , 'wordpress-social-login') ),
58
		array( 'field' => 'webSiteURL'  , 'label' => _wsl__( "Website URL"      , 'wordpress-social-login') ),
59
		array( 'field' => 'photoURL'    , 'label' => _wsl__( "Photo URL"        , 'wordpress-social-login') ),
60
		array( 'field' => 'displayName' , 'label' => _wsl__( "Display name"     , 'wordpress-social-login') ),
61
		array( 'field' => 'description' , 'label' => _wsl__( "Description"      , 'wordpress-social-login') ),
62
		array( 'field' => 'firstName'   , 'label' => _wsl__( "First name"       , 'wordpress-social-login') ),
63
		array( 'field' => 'lastName'    , 'label' => _wsl__( "Last name"        , 'wordpress-social-login') ),
64
		array( 'field' => 'gender'      , 'label' => _wsl__( "Gender"           , 'wordpress-social-login') ),
65
		array( 'field' => 'language'    , 'label' => _wsl__( "Language"         , 'wordpress-social-login') ),
66
		array( 'field' => 'age'         , 'label' => _wsl__( "Age"              , 'wordpress-social-login') ),
67
		array( 'field' => 'birthDay'    , 'label' => _wsl__( "Birth day"        , 'wordpress-social-login') ),
68
		array( 'field' => 'birthMonth'  , 'label' => _wsl__( "Birth month"      , 'wordpress-social-login') ),
69
		array( 'field' => 'birthYear'   , 'label' => _wsl__( "Birth year"       , 'wordpress-social-login') ),
70
		array( 'field' => 'email'       , 'label' => _wsl__( "Email"            , 'wordpress-social-login') ),
71
		array( 'field' => 'phone'       , 'label' => _wsl__( "Phone"            , 'wordpress-social-login') ),
72
		array( 'field' => 'address'     , 'label' => _wsl__( "Address"          , 'wordpress-social-login') ),
73
		array( 'field' => 'country'     , 'label' => _wsl__( "Country"          , 'wordpress-social-login') ),
74
		array( 'field' => 'region'      , 'label' => _wsl__( "Region"           , 'wordpress-social-login') ),
75
		array( 'field' => 'city'        , 'label' => _wsl__( "City"             , 'wordpress-social-login') ),
76
		array( 'field' => 'zip'         , 'label' => _wsl__( "Zip"              , 'wordpress-social-login') ),
77
	);
78
?>
79
<style>
80
	.widefat td, .widefat th { border: 1px solid #DDDDDD; }
81
	.widefat th label { font-weight: bold; }
82
83
	.wp-social-login-provider-list { padding: 10px; }
84
	.wp-social-login-provider-list a {text-decoration: none; }
85
	.wp-social-login-provider-list img{ border: 0 none; }
86
</style>
87
88
<div class="metabox-holder columns-2" id="post-body">
89
	<table width="100%">
90
		<tr valign="top">
91
			<td>
92
				<?php if( ! $adapter ): ?>
93
					<div style="padding: 15px; margin-bottom: 8px; border: 1px solid #ddd; background-color: #fff;box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);">
94
						<p><?php _wsl_e("Connect with a provider to get started", 'wordpress-social-login') ?>.</p>
95
					</div>
96
				<?php else: ?>
97
					<div class="stuffbox">
98
						<h3>
99
							<label><?php _wsl_e("Connected adapter specs", 'wordpress-social-login') ?></label>
100
						</h3>
101
						<div class="inside">
102
							<table class="wp-list-table widefat">
103
								<tr>
104
									<th width="200"><label><?php _wsl_e("Provider", 'wordpress-social-login') ?></label></th>
105
									<td><?php echo $provider_id; ?></td>
106
								</tr>
107
108
								<?php if( isset( $adapter->openidIdentifier ) ): ?>
109
									<tr>
110
										<th width="200"><label><?php _wsl_e("OpenID Identifier", 'wordpress-social-login') ?></label></th>
111
										<td><?php echo $adapter->openidIdentifier; ?></td>
112
									</tr>
113
								<?php endif; ?>
114
115
								<?php if( isset( $adapter->scope ) ): ?>
116
									<tr>
117
										<th width="200"><label><?php _wsl_e("Scope", 'wordpress-social-login') ?></label></th>
118
										<td><?php echo $adapter->scope; ?></td>
119
									</tr>
120
								<?php endif; ?>
121
122
								<?php if( isset( $adapter->config['keys'] ) ): ?>
123
									<tr>
124
										<th width="200"><label><?php _wsl_e("Application keys", 'wordpress-social-login') ?></label></th>
125
										<td><div style="max-width:650px"><?php echo json_encode( $adapter->config['keys'] ); ?></div></td>
126
									</tr>
127
								<?php endif; ?>
128
129
								<?php 
130
									$accessToken = $adapter->getAccessToken();
131
								?>
132
								
133 View Code Duplication
								<?php if( isset($accessToken["access_token"]) ): ?>
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...
134
									<tr>
135
										<th width="200"><label><?php _wsl_e("Access token", 'wordpress-social-login') ?></label></th>
136
										<td><div style="max-width:650px"><?php echo $adapter->getAccessToken()["access_token"]; ?></div></td>
137
									</tr>
138
								<?php endif; ?>
139
140 View Code Duplication
								<?php if( isset($accessToken["access_token_secret"]) ): ?>
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...
141
									<tr>
142
										<th width="200"><label><?php _wsl_e("Access token secret", 'wordpress-social-login') ?></label></th>
143
										<td><?php echo $adapter->getAccessToken()["access_token_secret"]; ?></td>
144
									</tr>
145
								<?php endif; ?>
146
147 View Code Duplication
								<?php if( isset($accessToken["expires_in"]) ): ?>
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...
148
									<tr>
149
										<th width="200"><label><?php _wsl_e("Access token expires in", 'wordpress-social-login') ?></label></th>
150
										<td><?php echo (int) $adapter->getAccessToken()["expires_at"] - time(); ?> <?php _wsl_e("second(s)", 'wordpress-social-login') ?></td>
151
									</tr>
152
								<?php endif; ?>
153
154 View Code Duplication
								<?php if( isset($accessToken["expires_at"]) ): ?>
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...
155
									<tr>
156
										<th width="200"><label><?php _wsl_e("Access token expires at", 'wordpress-social-login') ?></label></th>
157
										<td><?php echo date( DATE_W3C, $adapter->getAccessToken()["expires_at"] ); ?></td>
158
									</tr>
159
								<?php endif; ?>
160
							</table>
161
						</div>
162
					</div>
163
164
					<?php
165
						$console = false;
166
167
						if( ! isset( $adapter->openidIdentifier ) ):
168
					?>
169
						<div class="stuffbox">
170
							<h3>
171
								<label><?php _wsl_e("Connected adapter console", 'wordpress-social-login') ?></label>
172
							</h3>
173
							<div class="inside">
174
								<?php
175
									$path   = isset( $adapter->api->api_base_url ) ? $adapter->api->api_base_url : '';
176
									$path   = isset( $_REQUEST['console-path']   ) ? $_REQUEST['console-path']   : $path;
177
									$method = isset( $_REQUEST['console-method'] ) ? $_REQUEST['console-method'] : '';
178
									$query  = isset( $_REQUEST['console-query']  ) ? $_REQUEST['console-query']  : '';
179
180
									$response = '';
181
182
									if( $path && in_array( $method, array( 'GET', 'POST' ) ) )
183
									{
184
										$console = true;
185
186
										try
187
										{
188
											if( $method == 'GET' )
189
											{
190
												$response = $adapter->apiRequest( $path . ( $query ? '?' . $query : '' ) );
191
											}
192
											else
193
											{
194
												$response = $adapter->apiRequest( $path, $query );
195
											}
196
197
											$response = $response ? $response : Hybrid_Error::getApiError();
198
										}
199
										catch( Exception $e )
200
										{
201
											$response = "ERROR: " . $e->getMessage();
202
										}
203
									}
204
								?>
205
								<form action="" method="post"/>
206
									<table class="wp-list-table widefat">
207
										<tr>
208
											<th width="200"><label><?php _wsl_e("Path", 'wordpress-social-login') ?></label></th>
209
											<td><input type="text" style="width:96%" name="console-path" value="<?php echo htmlentities( $path ); ?>"><a href="https://apigee.com/providers" target="_blank"><img src="<?php echo $assets_base_url . 'question.png' ?>" style="vertical-align: text-top;" /></a></td>
210
										</tr>
211
										<tr>
212
											<th width="200"><label><?php _wsl_e("Method", 'wordpress-social-login') ?></label></th>
213
											<td><select style="width:100px" name="console-method"><option value="GET" <?php if( $method == 'GET' ) echo 'selected'; ?>>GET</option><!-- <option value="POST" <?php if( $method == 'POST' ) echo 'selected'; ?>>POST</option>--></select></td>
214
										</tr>
215
										<tr>
216
											<th width="200"><label><?php _wsl_e("Query", 'wordpress-social-login') ?></label></th>
217
											<td><textarea style="width:100%;height:60px;margin-top:6px;" name="console-query"><?php echo htmlentities( $query ); ?></textarea></td>
218
										</tr>
219
									</table>
220
221
									<br />
222
223
									<input type="submit" value="<?php _wsl_e("Submit", 'wordpress-social-login') ?>" class="button">
224
								</form>
225
							</div>
226
						</div>
227
228
						<?php if( $console ): ?>
229
							<div class="stuffbox">
230
								<h3>
231
									<label><?php _wsl_e("API Response", 'wordpress-social-login') ?></label>
232
								</h3>
233
								<div class="inside">
234
									<textarea rows="25" cols="70" wrap="off" style="width:100%;height:400px;margin-bottom:15px;font-family: monospace;font-size: 12px;"><?php echo htmlentities( print_r( $response, true ) ); ?></textarea>
235
								</div>
236
							</div>
237
						<?php if( 0 ): ?>
238
							<div class="stuffbox">
239
								<h3>
240
									<label><?php _wsl_e("Code PHP", 'wordpress-social-login') ?></label>
241
								</h3>
242
								<div class="inside">
243
<textarea rows="25" cols="70" wrap="off" style="width:100%;height:210px;margin-bottom:15px;font-family: monospace;font-size: 12px;"
244
>
245
246
/*!
247
	Important
248
249
	Direct access to providers apis is newly introduced into WSL and we are still experimenting, so they may change in future releases.
250
*/
251
252
try
253
{
254
    $<?php echo strtolower( $adapter->providerId ); ?> = $adapter = wsl_process_login_get_provider_adapter( '<?php echo htmlentities( $provider_id ); ?>' );
255
256
<?php if( $method == 'GET' ): ?>
257
    $response = $<?php echo strtolower( $adapter->providerId ); ?>->api()->get( '<?php echo htmlentities( $path . ( $query ? '?' . $query : '' ) ); ?>' );
258
<?php else: ?>
259
    $response = $<?php echo strtolower( $adapter->providerId ); ?>->api()->post( '<?php echo htmlentities( $path ); ?>', (array) $query );
260
<?php endif; ?>
261
}
262
catch( Exception $e )
263
{
264
    echo "Ooophs, we got an error: " . $e->getMessage();
265
}</textarea>
266
								</div>
267
							</div>
268
							<div class="stuffbox">
269
								<h3>
270
									<label><?php _wsl_e("Connected adapter debug", 'wordpress-social-login') ?></label>
271
								</h3>
272
								<div class="inside">
273
									<textarea rows="25" cols="70" wrap="off" style="width:100%;height:400px;margin-bottom:15px;font-family: monospace;font-size: 12px;"><?php echo htmlentities( print_r( $adapter, true ) ); ?></textarea>
274
								</div>
275
							</div>
276
							<div class="stuffbox">
277
								<h3>
278
									<label><?php _wsl_e("PHP Session", 'wordpress-social-login') ?></label>
279
								</h3>
280
								<div class="inside">
281
									<textarea rows="25" cols="70" wrap="off" style="width:100%;height:350px;margin-bottom:15px;font-family: monospace;font-size: 12px;"><?php echo htmlentities( print_r( $_SESSION, true ) ); ?></textarea>
282
								</div>
283
							</div>
284
						<?php endif; ?>
285
						<?php endif; ?>
286
					<?php endif; ?>
287
288
					<?php if( ! $console ): ?>
289
						<div class="stuffbox">
290
							<h3>
291
								<label><?php _wsl_e("Connected user social profile", 'wordpress-social-login') ?></label>
292
							</h3>
293
							<div class="inside">
294
								<table class="wp-list-table widefat">
295
									<?php
296
										$user_profile = (array) $user_profile;
297
298
										foreach( $ha_profile_fields as $item )
299
										{
300
											$item['field'] = $item['field'];
301
										?>
302
											<tr>
303
												<th width="200">
304
													<label><?php echo $item['label']; ?></label>
305
												</th>
306
												<td>
307
													<?php
308
														if( isset( $user_profile[ $item['field'] ] ) && $user_profile[ $item['field'] ] )
309
														{
310
															$field_value = $user_profile[ $item['field'] ];
311
312 View Code Duplication
															if( in_array( strtolower( $item['field'] ), array( 'profileurl', 'websiteurl', 'email' ) ) )
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...
313
															{
314
																?>
315
																	<a href="<?php if( $item['field'] == 'email' ) echo 'mailto:'; echo $field_value; ?>" target="_blank"><?php echo $field_value; ?></a>
316
																<?php
317
															}
318
															elseif( strtolower( $item['field'] ) == 'photourl' )
319
															{
320
																?>
321
																	<a href="<?php echo $field_value; ?>" target="_blank"><img width="36" height="36" align="left" src="<?php echo $field_value; ?>" style="margin-right: 5px;" > <?php echo $field_value; ?></a>
322
																<?php
323
															}
324
															else
325
															{
326
																echo $field_value;
327
															}
328
														}
329
													?>
330
												</td>
331
											</tr>
332
										<?php
333
										}
334
									?>
335
								</table>
336
							</div>
337
						</div>
338
					<?php endif; ?>
339
				<?php endif; ?>
340
			</td>
341
			<td width="10"></td>
342
			<td width="400">
343
				<div class="postbox">
344
					<div class="inside">
345
						<h3><?php _wsl_e("Authentication Playground", 'wordpress-social-login') ?></h3>
346
347
						<div style="padding:0 20px;">
348
							<p>
349
								<?php _wsl_e('Authentication Playground will let you authenticate with the enabled social networks without creating any new user account', 'wordpress-social-login') ?>.
350
							</p>
351
							<p>
352
								<?php _wsl_e('This tool will also give you a direct access to social networks apis via a lightweight console', 'wordpress-social-login') ?>.
353
							</p>
354
						</div>
355
					</div>
356
				</div>
357
358
				</style>
359
				<div class="postbox">
360
					<div class="inside">
361
						<div style="padding:0 20px;">
362
							<p>
363
								<?php _wsl_e("Connect with", 'wordpress-social-login') ?>:
364
							</p>
365
366
							<div style="width: 380px; padding: 10px; border: 1px solid #ddd; background-color: #fff;">
367
								<?php do_action( 'wordpress_social_login', array( 'mode' => 'test', 'caption' => '' ) ); ?>
368
							</div>
369
						</div>
370
					</div>
371
				</div>
372
			</td>
373
		</tr>
374
	</table>
375
</div>
376
<?php
377
	// HOOKABLE:
378
	do_action( "wsl_component_authtest_end" );
379
}
380
381
wsl_component_authtest();
382
383
// --------------------------------------------------------------------
384