PluginProbe
Redirection / 4.6.2
Redirection v4.6.2
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 4.6.2, at redirection-settings.php

267 lines 7.5 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 ) {
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 function red_get_default_options() {
40 $flags = new Red_Source_Flags();
41 $defaults = [
42 'support' => false,
43 'token' => md5( uniqid() ),
44 'monitor_post' => 0, // Dont monitor posts by default
45 'monitor_types' => [],
46 'associated_redirect' => '',
47 'auto_target' => '',
48 'expire_redirect' => 7, // Expire in 7 days
49 'expire_404' => 7, // Expire in 7 days
50 'modules' => [],
51 'newsletter' => false,
52 'redirect_cache' => 1, // 1 hour
53 'ip_logging' => 1, // Full IP logging
54 'last_group_id' => 0,
55 'rest_api' => REDIRECTION_API_JSON,
56 'https' => false,
57 'headers' => [],
58 'database' => '',
59 ];
60 $defaults = array_merge( $defaults, $flags->get_json() );
61
62 return apply_filters( 'red_default_options', $defaults );
63 }
64
65 function red_set_options( array $settings = array() ) {
66 $options = red_get_options();
67 $monitor_types = array();
68
69 if ( isset( $settings['database'] ) ) {
70 $options['database'] = $settings['database'];
71 }
72
73 if ( isset( $settings['rest_api'] ) && in_array( intval( $settings['rest_api'], 10 ), array( 0, 1, 2, 3, 4 ), true ) ) {
74 $options['rest_api'] = intval( $settings['rest_api'], 10 );
75 }
76
77 if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) {
78 $allowed = red_get_post_types( false );
79
80 foreach ( $settings['monitor_types'] as $type ) {
81 if ( in_array( $type, $allowed, true ) ) {
82 $monitor_types[] = $type;
83 }
84 }
85
86 $options['monitor_types'] = $monitor_types;
87 }
88
89 if ( isset( $settings['associated_redirect'] ) ) {
90 $options['associated_redirect'] = '';
91
92 if ( strlen( $settings['associated_redirect'] ) > 0 ) {
93 $sanitizer = new Red_Item_Sanitize();
94 $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) );
95 }
96 }
97
98 if ( isset( $settings['monitor_types'] ) && count( $monitor_types ) === 0 ) {
99 $options['monitor_post'] = 0;
100 $options['associated_redirect'] = '';
101 } elseif ( isset( $settings['monitor_post'] ) ) {
102 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
103
104 if ( ! Red_Group::get( $options['monitor_post'] ) && $options['monitor_post'] !== 0 ) {
105 $groups = Red_Group::get_all();
106
107 if ( count( $groups ) > 0 ) {
108 $options['monitor_post'] = $groups[0]['id'];
109 }
110 }
111 }
112
113 if ( isset( $settings['auto_target'] ) ) {
114 $options['auto_target'] = $settings['auto_target'];
115 }
116
117 if ( isset( $settings['last_group_id'] ) ) {
118 $options['last_group_id'] = max( 0, intval( $settings['last_group_id'], 10 ) );
119
120 if ( ! Red_Group::get( $options['last_group_id'] ) ) {
121 $groups = Red_Group::get_all();
122 $options['last_group_id'] = $groups[0]['id'];
123 }
124 }
125
126 if ( isset( $settings['support'] ) ) {
127 $options['support'] = $settings['support'] ? true : false;
128 }
129
130 if ( isset( $settings['token'] ) ) {
131 $options['token'] = $settings['token'];
132 }
133
134 if ( isset( $settings['https'] ) ) {
135 $options['https'] = $settings['https'] ? true : false;
136 }
137
138 if ( isset( $settings['token'] ) && trim( $options['token'] ) === '' ) {
139 $options['token'] = md5( uniqid() );
140 }
141
142 if ( isset( $settings['newsletter'] ) ) {
143 $options['newsletter'] = $settings['newsletter'] ? true : false;
144 }
145
146 if ( isset( $settings['expire_redirect'] ) ) {
147 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
148 }
149
150 if ( isset( $settings['expire_404'] ) ) {
151 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
152 }
153
154 if ( isset( $settings['ip_logging'] ) ) {
155 $options['ip_logging'] = max( 0, min( 2, intval( $settings['ip_logging'], 10 ) ) );
156 }
157
158 if ( isset( $settings['redirect_cache'] ) ) {
159 $options['redirect_cache'] = intval( $settings['redirect_cache'], 10 );
160
161 if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) {
162 $options['redirect_cache'] = 1;
163 }
164 }
165
166 if ( isset( $settings['location'] ) && strlen( $settings['location'] ) > 0 ) {
167 $module = Red_Module::get( 2 );
168 $options['modules'][2] = $module->update( $settings );
169 }
170
171 if ( ! empty( $options['monitor_post'] ) && count( $options['monitor_types'] ) === 0 ) {
172 // If we have a monitor_post set, but no types, then blank everything
173 $options['monitor_post'] = 0;
174 $options['associated_redirect'] = '';
175 }
176
177 $flags = new Red_Source_Flags();
178 $flags_present = [];
179
180 foreach ( array_keys( $flags->get_json() ) as $flag ) {
181 if ( isset( $settings[ $flag ] ) ) {
182 $flags_present[ $flag ] = $settings[ $flag ];
183 }
184 }
185
186 if ( count( $flags_present ) > 0 ) {
187 $flags->set_flags( $flags_present );
188 $options = array_merge( $options, $flags->get_json() );
189 }
190
191 if ( isset( $settings['headers'] ) ) {
192 $headers = new Red_Http_Headers( $settings['headers'] );
193 $options['headers'] = $headers->get_json();
194 }
195
196 update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) );
197 return $options;
198 }
199
200 function red_is_disabled() {
201 return ( defined( 'REDIRECTION_DISABLE' ) && REDIRECTION_DISABLE ) || file_exists( __DIR__ . '/redirection-disable.txt' );
202 }
203
204 function red_get_options() {
205 $options = get_option( REDIRECTION_OPTION );
206
207 if ( is_array( $options ) && red_is_disabled() ) {
208 $options['https'] = false;
209 }
210
211 if ( $options === false ) {
212 // Default flags for new installs - ignore case and trailing slashes
213 $options = [
214 'flags_case' => true,
215 'flags_trailing' => true,
216 ];
217 }
218
219 $defaults = red_get_default_options();
220
221 foreach ( $defaults as $key => $value ) {
222 if ( ! isset( $options[ $key ] ) ) {
223 $options[ $key ] = $value;
224 }
225 }
226
227 // Back-compat. If monitor_post is set without types then it's from an older Redirection
228 if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) {
229 $options['monitor_types'] = [ 'post' ];
230 }
231
232 // Remove old options not in red_get_default_options()
233 foreach ( $options as $key => $value ) {
234 if ( ! isset( $defaults[ $key ] ) ) {
235 unset( $options[ $key ] );
236 }
237 }
238
239 // Back-compat fix
240 if ( $options['rest_api'] === false || ! in_array( $options['rest_api'], [ REDIRECTION_API_JSON, REDIRECTION_API_JSON_INDEX, REDIRECTION_API_JSON_RELATIVE ], true ) ) {
241 $options['rest_api'] = REDIRECTION_API_JSON;
242 }
243
244 return $options;
245 }
246
247 function red_get_rest_api( $type = false ) {
248 if ( $type === false ) {
249 $options = red_get_options();
250 $type = $options['rest_api'];
251 }
252
253 $url = get_rest_url(); // REDIRECTION_API_JSON
254
255 if ( $type === REDIRECTION_API_JSON_INDEX ) {
256 $url = home_url( '/index.php?rest_route=/' );
257 } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) {
258 $relative = wp_parse_url( $url, PHP_URL_PATH );
259
260 if ( $relative ) {
261 $url = $relative;
262 }
263 }
264
265 return $url;
266 }
267