PluginProbe
Redirection / 5.0
Redirection v5.0
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / redirection-settings.php

redirection-settings.php in Redirection 5.0, at redirection-settings.php

379 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 define( 'REDIRECTION_OPTION', 'redirection_options' );
4 define( 'REDIRECTION_API_JSON', 0 );
5 define( 'REDIRECTION_API_JSON_INDEX', 1 );
6 define( 'REDIRECTION_API_JSON_RELATIVE', 3 );
7
8 function red_get_plugin_data( $plugin ) {
9 if ( ! function_exists( 'get_plugin_data' ) ) {
10 include_once ABSPATH . '/wp-admin/includes/plugin.php';
11 }
12
13 return get_plugin_data( $plugin );
14 }
15
16 function red_get_post_types( $full = true ) {
17 $types = get_post_types( array( 'public' => true ), 'objects' );
18 $types[] = (object) array(
19 'name' => 'trash',
20 'label' => __( 'Trash', 'default' ),
21 );
22
23 $post_types = array();
24 foreach ( $types as $type ) {
25 if ( $type->name === 'attachment' ) {
26 continue;
27 }
28
29 if ( $full && strlen( $type->label ) > 0 ) {
30 $post_types[ $type->name ] = $type->label;
31 } else {
32 $post_types[] = $type->name;
33 }
34 }
35
36 return apply_filters( 'redirection_post_types', $post_types );
37 }
38
39 /**
40 * Get default options. Contains all valid options
41 *
42 * @return array
43 */
44 function red_get_default_options() {
45 $flags = new Red_Source_Flags();
46 $defaults = [
47 'support' => false,
48 'token' => md5( uniqid() ),
49 'monitor_post' => 0, // Dont monitor posts by default
50 'monitor_types' => [],
51 'associated_redirect' => '',
52 'auto_target' => '',
53 'expire_redirect' => 7, // Expire in 7 days
54 'expire_404' => 7, // Expire in 7 days
55 'log_external' => false,
56 'log_header' => false,
57 'track_hits' => true,
58 'modules' => [],
59 'newsletter' => false,
60 'redirect_cache' => 1, // 1 hour
61 'ip_logging' => 1, // Full IP logging
62 'last_group_id' => 0,
63 'rest_api' => REDIRECTION_API_JSON,
64 'https' => false,
65 'headers' => [],
66 'database' => '',
67 'relocate' => '',
68 'preferred_domain' => '',
69 'aliases' => [],
70 'permalinks' => [],
71 'cache_key' => 0,
72 'plugin_update' => 'prompt',
73 'update_notice' => 0,
74 ];
75 $defaults = array_merge( $defaults, $flags->get_json() );
76
77 return apply_filters( 'red_default_options', $defaults );
78 }
79
80 /**
81 * Set options
82 *
83 * @param array $settings Partial settings.
84 * @return array
85 */
86 function red_set_options( array $settings = array() ) {
87 $options = red_get_options();
88 $monitor_types = array();
89
90 if ( isset( $settings['database'] ) ) {
91 $options['database'] = $settings['database'];
92 }
93
94 if ( isset( $settings['rest_api'] ) && in_array( intval( $settings['rest_api'], 10 ), array( 0, 1, 2, 3, 4 ), true ) ) {
95 $options['rest_api'] = intval( $settings['rest_api'], 10 );
96 }
97
98 if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) {
99 $allowed = red_get_post_types( false );
100
101 foreach ( $settings['monitor_types'] as $type ) {
102 if ( in_array( $type, $allowed, true ) ) {
103 $monitor_types[] = $type;
104 }
105 }
106
107 $options['monitor_types'] = $monitor_types;
108 }
109
110 if ( isset( $settings['associated_redirect'] ) ) {
111 $options['associated_redirect'] = '';
112
113 if ( strlen( $settings['associated_redirect'] ) > 0 ) {
114 $sanitizer = new Red_Item_Sanitize();
115 $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) );
116 }
117 }
118
119 if ( isset( $settings['monitor_types'] ) && count( $monitor_types ) === 0 ) {
120 $options['monitor_post'] = 0;
121 $options['associated_redirect'] = '';
122 } elseif ( isset( $settings['monitor_post'] ) ) {
123 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
124
125 if ( ! Red_Group::get( $options['monitor_post'] ) && $options['monitor_post'] !== 0 ) {
126 $groups = Red_Group::get_all();
127
128 if ( count( $groups ) > 0 ) {
129 $options['monitor_post'] = $groups[0]['id'];
130 }
131 }
132 }
133
134 if ( isset( $settings['auto_target'] ) ) {
135 $options['auto_target'] = $settings['auto_target'];
136 }
137
138 if ( isset( $settings['last_group_id'] ) ) {
139 $options['last_group_id'] = max( 0, intval( $settings['last_group_id'], 10 ) );
140
141 if ( ! Red_Group::get( $options['last_group_id'] ) ) {
142 $groups = Red_Group::get_all();
143 $options['last_group_id'] = $groups[0]['id'];
144 }
145 }
146
147 if ( isset( $settings['token'] ) ) {
148 $options['token'] = $settings['token'];
149 }
150
151 if ( isset( $settings['token'] ) && trim( $options['token'] ) === '' ) {
152 $options['token'] = md5( uniqid() );
153 }
154
155 // Boolean settings
156 foreach ( [ 'support', 'https', 'newsletter', 'log_external', 'log_header', 'track_hits' ] as $name ) {
157 if ( isset( $settings[ $name ] ) ) {
158 $options[ $name ] = $settings[ $name ] ? true : false;
159 }
160 }
161
162 if ( isset( $settings['expire_redirect'] ) ) {
163 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
164 }
165
166 if ( isset( $settings['expire_404'] ) ) {
167 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
168 }
169
170 if ( isset( $settings['ip_logging'] ) ) {
171 $options['ip_logging'] = max( 0, min( 2, intval( $settings['ip_logging'], 10 ) ) );
172 }
173
174 if ( isset( $settings['redirect_cache'] ) ) {
175 $options['redirect_cache'] = intval( $settings['redirect_cache'], 10 );
176
177 if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) {
178 $options['redirect_cache'] = 1;
179 }
180 }
181
182 if ( isset( $settings['location'] ) && ( ! isset( $options['location'] ) || $options['location'] !== $settings['location'] ) ) {
183 $module = Red_Module::get( 2 );
184 $options['modules'][2] = $module->update( $settings );
185 }
186
187 if ( ! empty( $options['monitor_post'] ) && count( $options['monitor_types'] ) === 0 ) {
188 // If we have a monitor_post set, but no types, then blank everything
189 $options['monitor_post'] = 0;
190 $options['associated_redirect'] = '';
191 }
192
193 if ( isset( $settings['plugin_update'] ) && in_array( $settings['plugin_update'], [ 'prompt', 'admin' ], true ) ) {
194 $options['plugin_update'] = $settings['plugin_update'];
195 }
196
197 $flags = new Red_Source_Flags();
198 $flags_present = [];
199
200 foreach ( array_keys( $flags->get_json() ) as $flag ) {
201 if ( isset( $settings[ $flag ] ) ) {
202 $flags_present[ $flag ] = $settings[ $flag ];
203 }
204 }
205
206 if ( count( $flags_present ) > 0 ) {
207 $flags->set_flags( $flags_present );
208 $options = array_merge( $options, $flags->get_json() );
209 }
210
211 if ( isset( $settings['headers'] ) ) {
212 $headers = new Red_Http_Headers( $settings['headers'] );
213 $options['headers'] = $headers->get_json();
214 }
215
216 if ( isset( $settings['aliases'] ) && is_array( $settings['aliases'] ) ) {
217 $options['aliases'] = array_values( array_filter( array_map( 'red_parse_domain_only', $settings['aliases'] ) ) );
218 $options['aliases'] = array_slice( $options['aliases'], 0, 20 ); // Max 20
219 }
220
221 if ( isset( $settings['permalinks'] ) && is_array( $settings['permalinks'] ) ) {
222 $options['permalinks'] = array_values( array_filter( array_map( 'trim', $settings['permalinks'] ) ) );
223 $options['permalinks'] = array_slice( $options['permalinks'], 0, 10 ); // Max 10
224 }
225
226 if ( isset( $settings['preferred_domain'] ) && in_array( $settings['preferred_domain'], [ '', 'www', 'nowww' ], true ) ) {
227 $options['preferred_domain'] = $settings['preferred_domain'];
228 }
229
230 if ( isset( $settings['relocate'] ) ) {
231 $options['relocate'] = red_parse_domain_path( $settings['relocate'] );
232
233 if ( strlen( $options['relocate'] ) > 0 ) {
234 $options['preferred_domain'] = '';
235 $options['aliases'] = [];
236 $options['https'] = false;
237 }
238 }
239
240 if ( isset( $settings['cache_key'] ) ) {
241 $key = intval( $settings['cache_key'], 10 );
242
243 if ( $settings['cache_key'] === true ) {
244 $key = time();
245 } elseif ( $settings['cache_key'] === false ) {
246 $key = 0;
247 }
248
249 $options['cache_key'] = $key;
250 }
251
252 if ( isset( $settings['update_notice'] ) ) {
253 $major_version = explode( '-', REDIRECTION_VERSION )[0]; // Remove any beta suffix
254 $major_version = implode( '.', array_slice( explode( '.', REDIRECTION_VERSION ), 0, 2 ) );
255 $options['update_notice'] = $major_version;
256 }
257
258 update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) );
259 return $options;
260 }
261
262 function red_parse_url( $url ) {
263 $domain = filter_var( $url, FILTER_SANITIZE_URL );
264 if ( substr( $domain, 0, 5 ) !== 'http:' && substr( $domain, 0, 6 ) !== 'https:' ) {
265 $domain = ( is_ssl() ? 'https://' : 'http://' ) . $domain;
266 }
267
268 return wp_parse_url( $domain );
269 }
270
271 function red_parse_domain_only( $domain ) {
272 $parsed = red_parse_url( $domain );
273
274 if ( $parsed && isset( $parsed['host'] ) ) {
275 return $parsed['host'];
276 }
277
278 return '';
279 }
280
281 function red_parse_domain_path( $domain ) {
282 $parsed = red_parse_url( $domain );
283
284 if ( $parsed && isset( $parsed['host'] ) ) {
285 return $parsed['scheme'] . '://' . $parsed['host'] . ( isset( $parsed['path'] ) ? $parsed['path'] : '' );
286 }
287
288 return '';
289 }
290
291 /**
292 * Have redirects been disabled?
293 *
294 * @return boolean
295 */
296 function red_is_disabled() {
297 return ( defined( 'REDIRECTION_DISABLE' ) && REDIRECTION_DISABLE ) || file_exists( __DIR__ . '/redirection-disable.txt' );
298 }
299
300 /**
301 * Get Redirection options
302 *
303 * @return array
304 */
305 function red_get_options() {
306 $options = get_option( REDIRECTION_OPTION );
307
308 if ( is_array( $options ) && red_is_disabled() ) {
309 $options['https'] = false;
310 }
311
312 if ( $options === false ) {
313 // Default flags for new installs - ignore case and trailing slashes
314 $options = [
315 'flags_case' => true,
316 'flags_trailing' => true,
317 ];
318 }
319
320 $defaults = red_get_default_options();
321
322 foreach ( $defaults as $key => $value ) {
323 if ( ! isset( $options[ $key ] ) ) {
324 $options[ $key ] = $value;
325 }
326 }
327
328 // Back-compat. If monitor_post is set without types then it's from an older Redirection
329 if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) {
330 $options['monitor_types'] = [ 'post' ];
331 }
332
333 // Remove old options not in red_get_default_options()
334 foreach ( $options as $key => $value ) {
335 if ( ! isset( $defaults[ $key ] ) ) {
336 unset( $options[ $key ] );
337 }
338 }
339
340 // Back-compat fix
341 if ( $options['rest_api'] === false || ! in_array( $options['rest_api'], [ REDIRECTION_API_JSON, REDIRECTION_API_JSON_INDEX, REDIRECTION_API_JSON_RELATIVE ], true ) ) {
342 $options['rest_api'] = REDIRECTION_API_JSON;
343 }
344
345 return $options;
346 }
347
348 /**
349 * Get the current REST API
350 *
351 * @param boolean $type Override with a specific API type.
352 * @return string
353 */
354 function red_get_rest_api( $type = false ) {
355 if ( $type === false ) {
356 $options = red_get_options();
357 $type = $options['rest_api'];
358 }
359
360 $url = get_rest_url(); // REDIRECTION_API_JSON
361
362 if ( $type === REDIRECTION_API_JSON_INDEX ) {
363 $url = home_url( '/?rest_route=/' );
364 } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) {
365 $relative = wp_parse_url( $url, PHP_URL_PATH );
366
367 if ( $relative ) {
368 $url = $relative;
369 }
370
371 if ( $url === '/index.php' ) {
372 // No permalinks. Default to normal REST API
373 $url = home_url( '/?rest_route=/' );
374 }
375 }
376
377 return $url;
378 }
379