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'] ) ? array_merge( $args, array( 'return' => $_REQUEST['action'] ) ) : $args; |
44
|
|
|
|
45
|
|
|
return add_query_arg( $args, $url ); |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns whether not the remote file exists. |
53
|
|
|
* Checking via GET because HEAD requests are blocked on some server configurations. |
54
|
|
|
* @param string $url |
55
|
|
|
* @param boolean $sslverify whether we care if the SSL certificate for the requested site is setup properly |
|
|
|
|
56
|
|
|
* @return boolean |
57
|
|
|
*/ |
58
|
|
|
public static function remote_file_exists( $url, $args = array() ){ |
59
|
|
|
$results = wp_remote_request($url,array_merge( array( |
60
|
|
|
'method'=>'GET', |
61
|
|
|
'redirection'=>1 |
62
|
|
|
), $args ) ); |
63
|
|
|
if( ! $results instanceof WP_Error && |
|
|
|
|
64
|
|
|
isset($results['response']) && |
65
|
|
|
isset($results['response']['code']) && |
66
|
|
|
$results['response']['code'] == '200'){ |
67
|
|
|
return true; |
68
|
|
|
}else{ |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* refactor_url |
77
|
|
|
* primarily used for removing the query string from a URL |
78
|
|
|
* |
79
|
|
|
* @param string $url |
80
|
|
|
* @param bool $remove_query - TRUE (default) will strip off any URL params, ie: ?this=1&that=2 |
81
|
|
|
* @param bool $base_url_only - TRUE will only return the scheme and host with no other parameters |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public static function refactor_url( $url = '', $remove_query = TRUE, $base_url_only = FALSE ) { |
85
|
|
|
// break apart incoming URL |
86
|
|
|
$url_bits = parse_url( $url ); |
87
|
|
|
// HTTP or HTTPS ? |
88
|
|
|
$scheme = isset( $url_bits[ 'scheme' ] ) ? $url_bits[ 'scheme' ] . '://' : 'http://'; |
89
|
|
|
// domain |
90
|
|
|
$host = isset( $url_bits[ 'host' ] ) ? $url_bits[ 'host' ] : ''; |
91
|
|
|
// if only the base URL is requested, then return that now |
92
|
|
|
if ( $base_url_only ) { |
93
|
|
|
return $scheme . $host; |
94
|
|
|
} |
95
|
|
|
$port = isset( $url_bits[ 'port' ] ) ? ':' . $url_bits[ 'port' ] : ''; |
96
|
|
|
$user = isset( $url_bits[ 'user' ] ) ? $url_bits[ 'user' ] : ''; |
97
|
|
|
$pass = isset( $url_bits[ 'pass' ] ) ? ':' . $url_bits[ 'pass' ] : ''; |
98
|
|
|
$pass = ( $user || $pass ) ? $pass . '@' : ''; |
99
|
|
|
$path = isset( $url_bits[ 'path' ] ) ? $url_bits[ 'path' ] : ''; |
100
|
|
|
// if the query string is not required, then return what we have so far |
101
|
|
|
if ( $remove_query ) { |
102
|
|
|
return $scheme . $user . $pass . $host . $port . $path; |
103
|
|
|
} |
104
|
|
|
$query = isset( $url_bits[ 'query' ] ) ? '?' . $url_bits[ 'query' ] : ''; |
105
|
|
|
$fragment = isset( $url_bits[ 'fragment' ] ) ? '#' . $url_bits[ 'fragment' ] : ''; |
106
|
|
|
return $scheme . $user . $pass . $host . $port . $path . $query . $fragment; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* get_query_string |
113
|
|
|
* returns just the query string from a URL, formatted by default into an array of key value pairs |
114
|
|
|
* |
115
|
|
|
* @param string $url |
116
|
|
|
* @param bool $as_array TRUE (default) will return query params as an array of key value pairs, FALSE will simply return the query string |
117
|
|
|
* @return string|array |
118
|
|
|
*/ |
119
|
|
|
public static function get_query_string( $url = '', $as_array = TRUE ) { |
120
|
|
|
// break apart incoming URL |
121
|
|
|
$url_bits = parse_url( $url ); |
122
|
|
|
// grab query string from URL |
123
|
|
|
$query = isset( $url_bits[ 'query' ] ) ? $url_bits[ 'query' ] : ''; |
124
|
|
|
// if we don't want the query string formatted into an array of key => value pairs, then just return it as is |
125
|
|
|
if ( ! $as_array ) { |
126
|
|
|
return $query; |
127
|
|
|
} |
128
|
|
|
// if no query string exists then just return an empty array now |
129
|
|
|
if ( empty( $query )) { |
130
|
|
|
return array(); |
131
|
|
|
} |
132
|
|
|
// empty array to hold results |
133
|
|
|
$query_params = array(); |
134
|
|
|
// now break apart the query string into separate params |
135
|
|
|
$query = explode( '&', $query ); |
136
|
|
|
// loop thru our query params |
137
|
|
|
foreach ( $query as $query_args ) { |
138
|
|
|
// break apart the key value pairs |
139
|
|
|
$query_args = explode( '=', $query_args ); |
140
|
|
|
// and add to our results array |
141
|
|
|
$query_params[ $query_args[0] ] = $query_args[1]; |
142
|
|
|
} |
143
|
|
|
return $query_params; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* prevent_prefetching |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
public static function prevent_prefetching(){ |
153
|
|
|
// prevent browsers from prefetching of the rel='next' link, because it may contain content that interferes with the registration process |
154
|
|
|
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* This generates a unique site-specific string. |
162
|
|
|
* An example usage for this string would be to save as a unique identifier for a record in the db for usage in urls. |
163
|
|
|
* |
164
|
|
|
* @param string $prefix Use this to prefix the string with something. |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public static function generate_unique_token( $prefix = '' ) { |
168
|
|
|
$token = md5( uniqid() . mt_rand() ); |
169
|
|
|
return $prefix ? $prefix . '_' . $token : $token; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* add_nocache_headers |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
public static function add_nocache_headers(){ |
179
|
|
|
// add no cache headers |
180
|
|
|
// add_action( 'wp_head' , array( 'EED_Single_Page_Checkout', 'nocache_headers' ), 10 ); |
181
|
|
|
// plus a little extra for nginx |
182
|
|
|
// add_filter( 'nocache_headers' , array( 'EED_Single_Page_Checkout', 'nocache_headers_nginx' ), 10, 1 ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* filter_input_server_url |
189
|
|
|
* uses filter_input() to sanitize one of the INPUT_SERVER URL values |
190
|
|
|
* but adds a backup in case filter_input() returns nothing, which can erringly happen on some servers |
191
|
|
|
* |
192
|
|
|
* @param string $server_variable |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
public static function filter_input_server_url( $server_variable = 'REQUEST_URI' ){ |
196
|
|
|
$URL = ''; |
197
|
|
|
$server_variables = array( |
198
|
|
|
'REQUEST_URI' => 1, |
199
|
|
|
'HTTP_HOST' => 1, |
200
|
|
|
'PHP_SELF' => 1, |
201
|
|
|
); |
202
|
|
|
$server_variable = strtoupper( $server_variable ); |
203
|
|
|
// whitelist INPUT_SERVER var |
204
|
|
|
if ( isset( $server_variables[ $server_variable ] ) ) { |
205
|
|
|
$URL = filter_input( INPUT_SERVER, $server_variable, FILTER_SANITIZE_URL, FILTER_NULL_ON_FAILURE ); |
206
|
|
|
if ( empty( $URL ) ) { |
207
|
|
|
$URL = esc_url( $_SERVER[ $server_variable ] ); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
return $URL; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
// End of file EEH_URL.helper.php |
217
|
|
|
|
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.