wsl.utilities.php ➔ wsl_get_current_url()   F
last analyzed

Complexity

Conditions 13
Paths 768

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 768
nop 0
dl 0
loc 43
rs 2.7722
c 0
b 0
f 0

How to fix   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
* Few utilities and functions
11
*/
12
13
// Exit if accessed directly
14
if ( !defined( 'ABSPATH' ) ) exit;
15
16
// --------------------------------------------------------------------
17
18
/**
19
* Return the current used WSL version
20
*/
21
function wsl_get_version()
22
{
23
	global $WORDPRESS_SOCIAL_LOGIN_VERSION;
24
25
	return $WORDPRESS_SOCIAL_LOGIN_VERSION;
26
}
27
28
// --------------------------------------------------------------------
29
30
/**
31
* Check if the current connection is being made over https
32
*
33
* Borrowed from http://wordpress.org/extend/plugins/oa-social-login/
34
*/
35
function wsl_is_https_on()
36
{
37
	if( ! empty ( $_SERVER ['SERVER_PORT'] ) )
38
	{
39
		if(trim ( $_SERVER ['SERVER_PORT'] ) == '443')
40
		{
41
			return true;
42
		}
43
	}
44
45
	if ( ! empty ( $_SERVER ['HTTP_X_FORWARDED_PROTO'] ) )
46
	{
47
		if(strtolower (trim ($_SERVER ['HTTP_X_FORWARDED_PROTO'])) == 'https')
48
		{
49
			return true;
50
		}
51
	}
52
53
	if( ! empty ( $_SERVER ['HTTPS'] ) )
54
	{
55
		if ( strtolower( trim($_SERVER ['HTTPS'] ) ) == 'on' OR trim ($_SERVER ['HTTPS']) == '1')
56
		{
57
			return true;
58
		}
59
	}
60
61
	return false;
62
}
63
64
// --------------------------------------------------------------------
65
66
/**
67
* Return the current url
68
*
69
* Borrowed from http://wordpress.org/extend/plugins/oa-social-login/
70
*/
71
function wsl_get_current_url()
72
{
73
	//Extract parts
74
	$request_uri = (isset ($_SERVER ['REQUEST_URI']) ? $_SERVER ['REQUEST_URI'] : $_SERVER ['PHP_SELF']);
75
	$request_protocol = (wsl_is_https_on () ? 'https' : 'http');
76
	$request_host = (isset ($_SERVER ['HTTP_X_FORWARDED_HOST']) ? $_SERVER ['HTTP_X_FORWARDED_HOST'] : (isset ($_SERVER ['HTTP_HOST']) ? $_SERVER ['HTTP_HOST'] : $_SERVER ['SERVER_NAME']));
77
78
	//Port of this request
79
	$request_port = '';
80
81
	//We are using a proxy
82
	if( isset( $_SERVER ['HTTP_X_FORWARDED_PORT'] ) )
83
	{
84
		// SERVER_PORT is usually wrong on proxies, don't use it!
85
		$request_port = intval($_SERVER ['HTTP_X_FORWARDED_PORT']);
86
	}
87
	//Does not seem like a proxy
88
	elseif( isset( $_SERVER ['SERVER_PORT'] ) )
89
	{
90
		$request_port = intval($_SERVER ['SERVER_PORT']);
91
	}
92
93
	//Remove standard ports
94
	$request_port = (!in_array($request_port, array (80, 443)) ? $request_port : '');
95
96
	//Ignore $request_port if $request_host already contains it
97
	$request_port = ( substr_compare( $request_host, ":$request_port", -strlen( ":$request_port" ) ) === 0 ? '' : $request_port );
98
99
	//Build url
100
	$current_url = $request_protocol . '://' . $request_host . ( ! empty ($request_port) ? (':'.$request_port) : '') . $request_uri;
101
102
	// overwrite all the above if ajax
103
	if( strpos( $current_url, 'admin-ajax.php') && isset( $_SERVER ['HTTP_REFERER'] ) && $_SERVER ['HTTP_REFERER'] )
104
	{
105
		$current_url = $_SERVER ['HTTP_REFERER'];
106
	}
107
108
	// HOOKABLE:
109
	$current_url = apply_filters( 'wsl_hook_alter_current_url', $current_url );
110
111
	//Done
112
	return $current_url;
113
}
114
115
// --------------------------------------------------------------------
116
117
/**
118
* Display a debugging area.
119
*
120
* This function is highly inspired by the Query Monitor.
121
* https://wordpress.org/plugins/query-monitor/
122
*
123
* Note: in order for this function to display the sql queries, 'SAVEQUERIES' should be defined as true in 'wp-config.php'
124
*/
125
function wsl_display_dev_mode_debugging_area( $keyword = 'wsl_' )
126
{
127
	global $wpdb, $wp_actions , $wp_filter;
128
?>
129
<style>
130
	.wsl-dev-nonselectsql {
131
		color: #a0a !important;
132
	}
133
	.wsl-dev-expensivesql {
134
		color: #f44 !important;
135
	}
136
	.wsl-dev-optionfunc {
137
		color: #4a4 !important;
138
	}
139
	.wsl-dev-wslfunc {
140
		color: #1468fa !important;
141
	}
142
	.wsl-dev-nonwslfunc {
143
		color: #a0a !important;
144
	}
145
	.wsl-dev-usedhook, .wsl-dev-usedhook a {
146
		color: #1468fa;
147
	}
148
	.wsl-dev-usedwslhook {
149
		color: #a0a !important;
150
	}
151
	.wsl-dev-unusedhook, .wsl-dev-unusedhook a{
152
		color: #a3a3a3 !important;
153
	}
154
	.wsl-dev-hookcallback, .wsl-dev-hookcallback a {
155
		color: #4a4 !important;
156
	}
157
	.wsl-dev-table {
158
		width:100%;
159
		border: 1px solid #e5e5e5;
160
		box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
161
		border-spacing: 0;
162
		clear: both;
163
		margin: 0;
164
		width: 100%;
165
	}
166
	.wsl-dev-table td, .wsl-dev-table th {
167
		border: 1px solid #dddddd;
168
		padding: 8px 10px;
169
		background-color: #fff;
170
		text-align: left;
171
	}
172
</style>
173
174
<?php
175
	if( class_exists( 'Hybrid_Error', false ) && Hybrid_Error::getApiError() )
176
	{
177
		?>
178
			<h4>Provider API Error</h4>
179
			<table class="wsl-dev-table">
180
				<tr>
181
					<td>
182
						<?php echo Hybrid_Error::getApiError(); ?>
183
					</td>
184
				</tr>
185
			</table>
186
		<?php
187
	}
188
?>
189
190
	<h4>SQL Queries</h4>
191
	<table class="wsl-dev-table">
192
		<tr>
193
			<td colspan="3">
194
				1. SAVEQUERIES should be defined and set to TRUE in order for the queries to show up (https://wordpress.org/support/article/editing-wp-config-php/#save-queries-for-analysis)
195
				<br />
196
				2. Calls for get_option() don't necessarily result on a query to the database. WP use both cache and wp_load_alloptions() to load all options at once. Hence, it won't be shown here.
197
			</td>
198
		</tr>
199
		<?php
200
			$queries = $wpdb->queries;
201
202
			$total_wsl_queries = 0;
203
			$total_wsl_queries_time = 0;
204
205
			if( $queries )
206
			{
207
				foreach( $queries as $item )
208
				{
209
					$sql    = trim( $item[0] );
210
					$time   = $item[1];
211
					$stack  = $item[2];
212
213
					$sql = str_ireplace( array( ' FROM ', ' WHERE ' , ' LIMIT ' , ' GROUP BY ' , ' ORDER BY ' , ' SET ' ), ARRAY( "\n" . 'FROM ', "\n" . 'WHERE ', "\n" . 'LIMIT ', "\n" . 'GROUP BY ', "\n" . 'ORDER BY ', "\n" . 'SET ' ), $sql );
214
215
					# https://wordpress.org/plugins/query-monitor/
216
					$callers   = explode( ',', $stack );
217
					$caller    = trim( end( $callers ) );
218
219
					if ( false !== strpos( $caller, '(' ) )
220
						$caller_name = substr( $caller, 0, strpos( $caller, '(' ) ) . '()';
221
					else
222
						$caller_name = $caller;
223
224
					if( stristr( $caller_name, $keyword ) || stristr( $sql, $keyword ) || stristr( $stack,$keyword ) )
225
					{
226
						?>
227
							<tr>
228
								<td valign="top" width="450">
229
									<?php if( stristr( $caller_name, $keyword ) ): ?>
230
										<a href="https://github.com/hybridauth/WordPress-Social-Login/search?q=<?php echo $caller_name ; ?>" target="_blank" class="wsl-dev-wslfunc"><?php echo $caller_name; ?></a>
231
									<?php else: ?>
232
										<a href="https://developer.wordpress.org/?s=<?php echo $caller_name ; ?>" target="_blank" class="wsl-dev-nonwslfunc<?php if( stristr( $caller_name, '_option' ) ) echo "- wsl-dev-optionfunc"; ?>"><?php echo $caller_name; ?></a>
233
									<?php endif; ?>
234
235
									<p style="font-size:11px; margin-left:10px">
236
										<?php
237
											if(  count( $callers ) )
238
											{
239
												# God damn it
240
												for( $i = count( $callers ) - 1; $i > 0; $i-- )
241
												{
242
													if( ! stristr( $callers[$i], '.php' ) && ! stristr( $callers[$i],  'call_user_func_' ) )
243
													{
244
														echo "#$i &nbsp; " . $callers[$i] . '<br />';
245
													}
246
												}
247
											}
248
										?>
249
									</p>
250
								</td>
251
								<td valign="top" class="<?php if( ! stristr( '#' . $sql, '#select ' ) ) echo 'wsl-dev-nonselectsql'; ?>"><?php echo nl2br( $sql ); ?></td>
252
								<td valign="top" width="50" nowrap class="<?php if( $time > 0.05 ) echo 'wsl-dev-expensivesql'; ?>"><?php echo number_format( $time, 4, '.', '' ); ?></td>
253
							</tr>
254
						<?php
255
256
						$total_wsl_queries++;
257
						$total_wsl_queries_time += $time;
258
					}
259
				}
260
			}
261
		?>
262
		<tr>
263
			<td colspan="2">Total SQL Queries by WSL : <?php echo $total_wsl_queries; ?></td>
264
			<td width="50" nowrap><?php echo number_format( $total_wsl_queries_time, 4, '.', '' ); ?></td>
265
		</tr>
266
	</table>
267
268
	<h4>Hooks</h4>
269
	<table class="wsl-dev-table">
270
		<?php
271
			if( $wp_actions )
272
			{
273
				foreach( $wp_actions as $name => $count )
274
				{
275
					if ( isset( $wp_filter[$name] ) )
276
					{
277
						$action = $wp_filter[$name];
278
279
						if( $action )
280
						{
281
							foreach( $action as $priority => $callbacks )
282
							{
283
								foreach( $callbacks as $callback )
284
								{
285
									if( isset( $callback['function'] ) && is_string( $callback['function'] ) )
286
									{
287
										if( stristr( $callback['function'], $keyword ) || stristr( $name, $keyword ) )
288
										{
289
											?>
290
												<tr>
291
													<td valign="top" width="270" nowrap class="wsl-dev-usedhook">
292
														<?php
293
															if( stristr( $name, $keyword ) )
294
															{
295
																?>
296
																	<a class="wsl-dev-usedwslhook" href="https://github.com/hybridauth/WordPress-Social-Login/search?q=<?php echo $name ; ?>" target="_blank"><?php echo $name ; ?></a>
297
																<?php
298
															}
299
															else
300
															{
301
																echo $name ;
302
															}
303
														?>
304
													</td>
305
													<td valign="top" class="wsl-dev-hookcallback">
306
														<?php
307
															if( stristr( $callback['function'], $keyword ) )
308
															{
309
																?>
310
																	<a href="https://github.com/hybridauth/WordPress-Social-Login/search?q=<?php echo $callback['function'] ; ?>" target="_blank"><?php echo $callback['function'] ; ?></a>
311
																<?php
312
															}
313
															else
314
															{
315
																echo $callback['function'] ;
316
															}  // I hit a record
317
														?>
318
													</td>
319
													<td valign="top" width="50">
320
														<?php echo $priority; ?>
321
													</td>
322
													<td valign="top" width="50">
323
														<?php echo $callback['accepted_args'] ; ?>
324
													</td>
325
												</tr>
326
											<?php
327
										}
328
									}
329
								}
330
							}
331
						}
332
					}
333
					elseif( stristr( $name, $keyword )  )
334
					{
335
						?>
336
							<tr>
337
								<td valign="top" width="270" nowrap class="wsl-dev-unusedhook">
338
									<a href="https://github.com/hybridauth/WordPress-Social-Login/search?q=<?php echo $name ; ?>" target="_blank"><?php echo $name ; ?></a>
339
								</td>
340
								<td></td>
341
								<td></td>
342
								<td></td>
343
							</tr>
344
						<?php
345
					}
346
				}
347
			}
348
		?>
349
	</table>
350
351
	<h4>PHP Session</h4>
352
	<table class="wsl-dev-table">
353
		<?php
354
			foreach( $_SESSION as $k => $v )
355
			{
356
		?>
357
			<tr><th width="270"><label><?php echo $k; ?></label></th><td><?php print_r( $v ); ?></td></tr>
358
		<?php
359
			}
360
		?>
361
		</tbody>
362
	</table>
363
364
	<h4>Wordpress</h4>
365
	<table class="wsl-dev-table">
366
		<tbody>
367
			<tr><th width="270"><label>Version</label></th><td><?php echo get_bloginfo( 'version' ); ?></td></tr>
368
			<tr><th><label>Multi-site</label></th><td><?php echo is_multisite() ? 'Yes' . "\n" : 'No'; ?></td></tr>
369
			<tr><th><label>Site url</label></th><td><?php echo site_url(); ?></td></tr>
370
			<tr><th><label>Home url</label></th><td><?php echo home_url(); ?></td></tr>
371
			<tr><th><label>Plugins url</label></th><td><?php echo plugins_url(); ?></td></tr>
372
		</tbody>
373
	</table>
374
375
	<h4>WSL</h4>
376
	<table class="wsl-dev-table">
377
		<tbody>
378
			<tr><th width="270"><label>Version</label></th><td><?php echo wsl_get_version(); ?></td></tr>
379
			<tr><th><label>Plugin path</label></th><td><?php echo WORDPRESS_SOCIAL_LOGIN_ABS_PATH; ?></td></tr>
380
			<tr><th><label>Plugin url</label></th><td><?php echo WORDPRESS_SOCIAL_LOGIN_PLUGIN_URL; ?></td></tr>
381
			<tr><th><label>HA endpoint</label></th><td><?php echo WORDPRESS_SOCIAL_LOGIN_HYBRIDAUTH_ENDPOINT_URL; ?></td></tr>
382
		</tbody>
383
	</table>
384
385
	<h4>Website</h4>
386
	<table class="wsl-dev-table">
387
		<tbody>
388
			<tr><th width="270"><label>IP</label></th><td><?php echo $_SERVER['SERVER_ADDR']; ?></td></tr>
389
			<tr><th><label>Domain</label></th><td><?php echo $_SERVER['HTTP_HOST']; ?></td></tr>
390
			<tr><th><label>Port</label></th><td><?php echo isset( $_SERVER['SERVER_PORT'] ) ? 'On (' . $_SERVER['SERVER_PORT'] . ')' : 'N/A'; ?></td></tr>
391
			<tr><th><label>X Forward</label></th><td><?php echo isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ? 'On (' . $_SERVER['HTTP_X_FORWARDED_PROTO'] . ')' : 'N/A';; ?></td></tr>
392
		</tbody>
393
	</table>
394
395
	<h4>Software</h4>
396
	<table class="wsl-dev-table">
397
		<tbody>
398
			<tr><th width="270"><label>Server</label></th><td><?php echo $_SERVER['SERVER_SOFTWARE']; ?></td></tr>
399
			<tr><th><label>PHP</label></th><td><?php echo PHP_VERSION; ?></td></tr>
400
			<tr><th><label>MySQL</label></th><td><?php echo $wpdb->db_version(); ?></td></tr>
401
			<tr><th><label>Time</label></th><td><?php echo date( DATE_ATOM, time() ); ?> / <?php echo time(); ?></td></tr>
402
		</tbody>
403
	</table>
404
405
	<h4>MySQL</h4>
406
	<table class="wsl-dev-table">
407
		<tbody>
408
			<tr><th width="270"><label>Host</label></th><td><?php echo $wpdb->dbhost; ?></td></tr>
409
			<tr><th><label>User</label></th><td><?php echo $wpdb->dbuser; ?></td></tr>
410
			<tr><th><label>Database</label></th><td><?php echo $wpdb->dbname; ?></td></tr>
411
			<tr><th><label>Prefix</label></th><td><?php echo $wpdb->prefix; ?></td></tr>
412
			<tr><th><label>Base_prefix</label></th><td><?php echo $wpdb->prefix; ?></td></tr>
413
			<tr><th><label>Num_queries</label></th><td><?php echo$wpdb->num_queries; ?></td></tr>
414
		</tbody>
415
	</table>
416
<?php
417
}
418
419
// --------------------------------------------------------------------
420
421
/*
422
* http://php.net/manual/en/function.debug-backtrace.php#112238
423
*/
424
function wsl_generate_backtrace()
425
{
426
    $e = new Exception();
427
    $trace = explode( "\n", $e->getTraceAsString() );
428
429
    array_pop($trace);
430
    $length = count($trace);
431
    $result = array();
432
433
    for ( $i = 0; $i < $length; $i++ )
434
    {
435
        $result[] = ( $i + 1 )  . ')' . str_ireplace( array( realpath( WORDPRESS_SOCIAL_LOGIN_ABS_PATH ), realpath( WP_PLUGIN_DIR . '/' ), realpath( ABSPATH . '/' ) ) , '', substr( $trace[$i], strpos( $trace[$i], ' ' ) ) );
436
    }
437
438
    return "\n\t" . implode( "\n\t", $result );
439
}
440
441
// --------------------------------------------------------------------
442