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

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