|
1
|
|
|
<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
|
2
|
|
|
/** |
|
3
|
|
|
* EEH_URL helper |
|
4
|
|
|
* Helper class for URL-related PHP functions |
|
5
|
|
|
* |
|
6
|
|
|
* @package Event Espresso |
|
7
|
|
|
* @subpackage /helper/EEH_URL.helper.php |
|
8
|
|
|
* @author Brent Christensen, Michael Nelson |
|
9
|
|
|
* |
|
10
|
|
|
* ------------------------------------------------------------------------ |
|
11
|
|
|
*/ |
|
12
|
|
|
class EEH_URL{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* _add_query_arg |
|
16
|
|
|
* adds nonce to array of arguments then calls WP add_query_arg function |
|
17
|
|
|
* |
|
18
|
|
|
* @access public |
|
19
|
|
|
* @param array $args |
|
20
|
|
|
* @param string $url |
|
21
|
|
|
* @param bool $exclude_nonce If true then the nonce will be excluded from the generated url. |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function add_query_args_and_nonce( $args = array(), $url = '', $exclude_nonce = false ) { |
|
25
|
|
View Code Duplication |
if ( empty( $url ) ) { |
|
26
|
|
|
$user_msg = __('An error occurred. A URL is a required parameter for the add_query_args_and_nonce method.', 'event_espresso' ); |
|
27
|
|
|
$dev_msg = $user_msg . "\n" . sprintf( |
|
28
|
|
|
__('In order to dynamically generate nonces for your actions, you need to supply a valid URL as a second parameter for the %s::add_query_args_and_nonce method.', 'event_espresso' ), |
|
29
|
|
|
__CLASS__ |
|
30
|
|
|
); |
|
31
|
|
|
EE_Error::add_error( $user_msg . '||' . $dev_msg, __FILE__, __FUNCTION__, __LINE__ ); |
|
32
|
|
|
} |
|
33
|
|
|
// check that an action exists and add nonce |
|
34
|
|
|
if ( ! $exclude_nonce ) { |
|
35
|
|
|
if ( isset( $args['action'] ) && ! empty( $args['action'] ) ) { |
|
36
|
|
|
$args = array_merge( $args, array( $args['action'] . '_nonce' => wp_create_nonce( $args['action'] . '_nonce' ) ) ); |
|
37
|
|
|
} else { |
|
38
|
|
|
$args = array_merge( $args, array( 'action' => 'default', 'default_nonce' => wp_create_nonce( 'default_nonce' ) ) ); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
//finally, let's always add a return address (if present) :) |
|
43
|
|
|
$args = ! empty( $_REQUEST['action'] ) && ! isset( $_REQUEST[ 'return' ] ) |
|
44
|
|
|
? array_merge( $args, array( 'return' => $_REQUEST['action'] ) ) |
|
45
|
|
|
: $args; |
|
46
|
|
|
|
|
47
|
|
|
return add_query_arg( $args, $url ); |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns whether not the remote file exists. |
|
55
|
|
|
* Checking via GET because HEAD requests are blocked on some server configurations. |
|
56
|
|
|
* @param string $url |
|
57
|
|
|
* @param boolean $sslverify whether we care if the SSL certificate for the requested site is setup properly |
|
|
|
|
|
|
58
|
|
|
* @return boolean |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function remote_file_exists( $url, $args = array() ){ |
|
61
|
|
|
$results = wp_remote_request($url,array_merge( array( |
|
62
|
|
|
'method'=>'GET', |
|
63
|
|
|
'redirection'=>1 |
|
64
|
|
|
), $args ) ); |
|
65
|
|
|
if( ! $results instanceof WP_Error && |
|
|
|
|
|
|
66
|
|
|
isset($results['response']) && |
|
67
|
|
|
isset($results['response']['code']) && |
|
68
|
|
|
$results['response']['code'] == '200'){ |
|
69
|
|
|
return true; |
|
70
|
|
|
}else{ |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* refactor_url |
|
79
|
|
|
* primarily used for removing the query string from a URL |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $url |
|
82
|
|
|
* @param bool $remove_query - TRUE (default) will strip off any URL params, ie: ?this=1&that=2 |
|
83
|
|
|
* @param bool $base_url_only - TRUE will only return the scheme and host with no other parameters |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function refactor_url( $url = '', $remove_query = TRUE, $base_url_only = FALSE ) { |
|
87
|
|
|
// break apart incoming URL |
|
88
|
|
|
$url_bits = parse_url( $url ); |
|
89
|
|
|
// HTTP or HTTPS ? |
|
90
|
|
|
$scheme = isset( $url_bits[ 'scheme' ] ) ? $url_bits[ 'scheme' ] . '://' : 'http://'; |
|
91
|
|
|
// domain |
|
92
|
|
|
$host = isset( $url_bits[ 'host' ] ) ? $url_bits[ 'host' ] : ''; |
|
93
|
|
|
// if only the base URL is requested, then return that now |
|
94
|
|
|
if ( $base_url_only ) { |
|
95
|
|
|
return $scheme . $host; |
|
96
|
|
|
} |
|
97
|
|
|
$port = isset( $url_bits[ 'port' ] ) ? ':' . $url_bits[ 'port' ] : ''; |
|
98
|
|
|
$user = isset( $url_bits[ 'user' ] ) ? $url_bits[ 'user' ] : ''; |
|
99
|
|
|
$pass = isset( $url_bits[ 'pass' ] ) ? ':' . $url_bits[ 'pass' ] : ''; |
|
100
|
|
|
$pass = ( $user || $pass ) ? $pass . '@' : ''; |
|
101
|
|
|
$path = isset( $url_bits[ 'path' ] ) ? $url_bits[ 'path' ] : ''; |
|
102
|
|
|
// if the query string is not required, then return what we have so far |
|
103
|
|
|
if ( $remove_query ) { |
|
104
|
|
|
return $scheme . $user . $pass . $host . $port . $path; |
|
105
|
|
|
} |
|
106
|
|
|
$query = isset( $url_bits[ 'query' ] ) ? '?' . $url_bits[ 'query' ] : ''; |
|
107
|
|
|
$fragment = isset( $url_bits[ 'fragment' ] ) ? '#' . $url_bits[ 'fragment' ] : ''; |
|
108
|
|
|
return $scheme . $user . $pass . $host . $port . $path . $query . $fragment; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* get_query_string |
|
115
|
|
|
* returns just the query string from a URL, formatted by default into an array of key value pairs |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $url |
|
118
|
|
|
* @param bool $as_array TRUE (default) will return query params as an array of key value pairs, FALSE will simply return the query string |
|
119
|
|
|
* @return string|array |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function get_query_string( $url = '', $as_array = TRUE ) { |
|
122
|
|
|
// break apart incoming URL |
|
123
|
|
|
$url_bits = parse_url( $url ); |
|
124
|
|
|
// grab query string from URL |
|
125
|
|
|
$query = isset( $url_bits[ 'query' ] ) ? $url_bits[ 'query' ] : ''; |
|
126
|
|
|
// if we don't want the query string formatted into an array of key => value pairs, then just return it as is |
|
127
|
|
|
if ( ! $as_array ) { |
|
128
|
|
|
return $query; |
|
129
|
|
|
} |
|
130
|
|
|
// if no query string exists then just return an empty array now |
|
131
|
|
|
if ( empty( $query )) { |
|
132
|
|
|
return array(); |
|
133
|
|
|
} |
|
134
|
|
|
// empty array to hold results |
|
135
|
|
|
$query_params = array(); |
|
136
|
|
|
// now break apart the query string into separate params |
|
137
|
|
|
$query = explode( '&', $query ); |
|
138
|
|
|
// loop thru our query params |
|
139
|
|
|
foreach ( $query as $query_args ) { |
|
140
|
|
|
// break apart the key value pairs |
|
141
|
|
|
$query_args = explode( '=', $query_args ); |
|
142
|
|
|
// and add to our results array |
|
143
|
|
|
$query_params[ $query_args[0] ] = $query_args[1]; |
|
144
|
|
|
} |
|
145
|
|
|
return $query_params; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* prevent_prefetching |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
public static function prevent_prefetching(){ |
|
155
|
|
|
// prevent browsers from prefetching of the rel='next' link, because it may contain content that interferes with the registration process |
|
156
|
|
|
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* This generates a unique site-specific string. |
|
164
|
|
|
* An example usage for this string would be to save as a unique identifier for a record in the db for usage in urls. |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $prefix Use this to prefix the string with something. |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public static function generate_unique_token( $prefix = '' ) { |
|
170
|
|
|
$token = md5( uniqid() . mt_rand() ); |
|
171
|
|
|
return $prefix ? $prefix . '_' . $token : $token; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* add_nocache_headers |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
|
|
public static function add_nocache_headers(){ |
|
181
|
|
|
// add no cache headers |
|
182
|
|
|
// add_action( 'wp_head' , array( 'EED_Single_Page_Checkout', 'nocache_headers' ), 10 ); |
|
183
|
|
|
// plus a little extra for nginx |
|
184
|
|
|
// add_filter( 'nocache_headers' , array( 'EED_Single_Page_Checkout', 'nocache_headers_nginx' ), 10, 1 ); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* filter_input_server_url |
|
191
|
|
|
* uses filter_input() to sanitize one of the INPUT_SERVER URL values |
|
192
|
|
|
* but adds a backup in case filter_input() returns nothing, which can erringly happen on some servers |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $server_variable |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
|
|
public static function filter_input_server_url( $server_variable = 'REQUEST_URI' ){ |
|
198
|
|
|
$URL = ''; |
|
199
|
|
|
$server_variables = array( |
|
200
|
|
|
'REQUEST_URI' => 1, |
|
201
|
|
|
'HTTP_HOST' => 1, |
|
202
|
|
|
'PHP_SELF' => 1, |
|
203
|
|
|
); |
|
204
|
|
|
$server_variable = strtoupper( $server_variable ); |
|
205
|
|
|
// whitelist INPUT_SERVER var |
|
206
|
|
|
if ( isset( $server_variables[ $server_variable ] ) ) { |
|
207
|
|
|
$URL = filter_input( INPUT_SERVER, $server_variable, FILTER_SANITIZE_URL, FILTER_NULL_ON_FAILURE ); |
|
208
|
|
|
if ( empty( $URL ) ) { |
|
209
|
|
|
$URL = esc_url( $_SERVER[ $server_variable ] ); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
return $URL; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Gets the current page's full URL |
|
219
|
|
|
* @return string |
|
220
|
|
|
*/ |
|
221
|
|
View Code Duplication |
public static function current_url() { |
|
|
|
|
|
|
222
|
|
|
if ( isset( $_SERVER[ 'HTTP_HOST' ], $_SERVER[ 'REQUEST_URI' ] ) ) { |
|
223
|
|
|
$url = is_ssl() ? 'https://' : 'http://'; |
|
224
|
|
|
$url .= \EEH_URL::filter_input_server_url( 'HTTP_HOST' ); |
|
225
|
|
|
$url .= \EEH_URL::filter_input_server_url( 'REQUEST_URI' ); |
|
226
|
|
|
} else { |
|
227
|
|
|
$url = 'unknown'; |
|
228
|
|
|
} |
|
229
|
|
|
return $url; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
// End of file EEH_URL.helper.php |
|
236
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.