PluginProbe
Redirection / 3.3
Redirection v3.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 3.3, at redirection-settings.php

210 lines 6.2 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_ADMIN', 2 );
7 define( 'REDIRECTION_API_JSON_RELATIVE', 3 );
8 define( 'REDIRECTION_API_POST', 4 );
9
10 function red_get_plugin_data( $plugin ) {
11 if ( ! function_exists( 'get_plugin_data' ) ) {
12 include_once ABSPATH . '/wp-admin/includes/plugin.php';
13 }
14
15 return get_plugin_data( $plugin );
16 }
17
18 function red_get_post_types( $full = true ) {
19 $types = get_post_types( array( 'public' => true ), 'objects' );
20 $types[] = ( object )array( 'name' => 'trash', 'label' => __( 'Trash' ) );
21
22 $post_types = array();
23 foreach ( $types as $type ) {
24 if ( $type->name === 'attachment' ) {
25 continue;
26 }
27
28 if ( $full ) {
29 $post_types[ $type->name ] = $type->label;
30 } else {
31 $post_types[] = $type->name;
32 }
33 }
34
35 return apply_filters( 'redirection_post_types', $post_types );
36 }
37
38 function red_set_options( array $settings = array() ) {
39 $options = red_get_options();
40 $monitor_types = array();
41
42 if ( isset( $settings['version'] ) ) {
43 $options['version'] = $settings['version'];
44 }
45
46 if ( isset( $settings['rest_api'] ) && in_array( intval( $settings['rest_api'], 10 ), array( 0, 1, 2, 3, 4 ) ) ) {
47 $options['rest_api'] = intval( $settings['rest_api'] );
48 }
49
50 if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) {
51 $allowed = red_get_post_types( false );
52
53 foreach ( $settings['monitor_types'] as $type ) {
54 if ( in_array( $type, $allowed ) ) {
55 $monitor_types[] = $type;
56 }
57 }
58
59 $options['monitor_types'] = $monitor_types;
60 }
61
62 if ( isset( $settings['associated_redirect'] ) ) {
63 $options['associated_redirect'] = '';
64
65 if ( strlen( $settings['associated_redirect'] ) > 0 ) {
66 $sanitizer = new Red_Item_Sanitize();
67 $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) );
68 }
69 }
70
71 if ( isset( $settings['monitor_types'] ) && count( $monitor_types ) === 0 ) {
72 $options['monitor_post'] = 0;
73 $options['associated_redirect'] = '';
74 } elseif ( isset( $settings['monitor_post'] ) ) {
75 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
76
77 if ( ! Red_Group::get( $options['monitor_post'] ) && $options['monitor_post'] !== 0 ) {
78 $groups = Red_Group::get_all();
79 $options['monitor_post'] = $groups[0]['id'];
80 }
81 }
82
83 if ( isset( $settings['auto_target'] ) ) {
84 $options['auto_target'] = $settings['auto_target'];
85 }
86
87 if ( isset( $settings['last_group_id'] ) ) {
88 $options['last_group_id'] = max( 0, intval( $settings['last_group_id'], 10 ) );
89
90 if ( ! Red_Group::get( $options['last_group_id'] ) ) {
91 $groups = Red_Group::get_all();
92 $options['last_group_id'] = $groups[0]['id'];
93 }
94 }
95
96 if ( isset( $settings['support'] ) ) {
97 $options['support'] = $settings['support'] ? true : false;
98 }
99
100 if ( isset( $settings['token'] ) ) {
101 $options['token'] = $settings['token'];
102 }
103
104 if ( isset( $settings['https'] ) ) {
105 $options['https'] = $settings['https'] ? true : false;
106 }
107
108 if ( ! isset( $settings['token'] ) || trim( $options['token'] ) === '' ) {
109 $options['token'] = md5( uniqid() );
110 }
111
112 if ( isset( $settings['newsletter'] ) ) {
113 $options['newsletter'] = $settings['newsletter'] ? true : false;
114 }
115
116 if ( isset( $settings['expire_redirect'] ) ) {
117 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
118 }
119
120 if ( isset( $settings['expire_404'] ) ) {
121 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
122 }
123
124 if ( isset( $settings['ip_logging'] ) ) {
125 $options['ip_logging'] = max( 0, min( 2, intval( $settings['ip_logging'], 10 ) ) );
126 }
127
128 if ( isset( $settings['redirect_cache'] ) ) {
129 $options['redirect_cache'] = intval( $settings['redirect_cache'], 10 );
130
131 if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) {
132 $options['redirect_cache'] = 1;
133 }
134 }
135
136 if ( isset( $settings['location'] ) ) {
137 $module = Red_Module::get( 2 );
138 $options['modules'][2] = $module->update( $settings );
139 }
140
141 if ( ! empty( $options['monitor_post'] ) && count( $options['monitor_types'] ) === 0 ) {
142 // If we have a monitor_post set, but no types, then blank everything
143 $options['monitor_post'] = 0;
144 $options['associated_redirect'] = '';
145 }
146
147 update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) );
148 return $options;
149 }
150
151 function red_get_options() {
152 $options = get_option( REDIRECTION_OPTION );
153 if ( $options === false ) {
154 $options = array();
155 }
156
157 $defaults = apply_filters( 'red_default_options', array(
158 'support' => false,
159 'token' => md5( uniqid() ),
160 'monitor_post' => 0, // Dont monitor posts by default
161 'monitor_types' => array(),
162 'associated_redirect' => '',
163 'auto_target' => '',
164 'expire_redirect' => 7, // Expire in 7 days
165 'expire_404' => 7, // Expire in 7 days
166 'modules' => array(),
167 'newsletter' => false,
168 'redirect_cache' => 1, // 1 hour
169 'ip_logging' => 1, // Full IP logging
170 'last_group_id' => 0,
171 'rest_api' => false,
172 'https' => false,
173 'version' => REDIRECTION_VERSION,
174 ) );
175
176 foreach ( $defaults as $key => $value ) {
177 if ( ! isset( $options[ $key ] ) ) {
178 $options[ $key ] = $value;
179 }
180 }
181
182 // Back-compat. If monitor_post is set without types then it's from an older Redirection
183 if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) {
184 $options['monitor_types'] = array( 'post' );
185 }
186
187 return $options;
188 }
189
190 function red_get_rest_api( $type = false ) {
191 if ( $type === false ) {
192 $options = red_get_options();
193 $type = $options['rest_api'];
194 }
195
196 $url = get_rest_url(); // REDIRECTION_API_JSON
197
198 if ( $type === REDIRECTION_API_JSON_INDEX ) {
199 $url = home_url( '/index.php?rest_route=/' );
200 } elseif ( $type === REDIRECTION_API_ADMIN ) {
201 $url = admin_url( 'admin-ajax.php?action=red_proxy&rest_path=' );
202 } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) {
203 $url = parse_url( $url, PHP_URL_PATH );
204 } elseif ( $type === REDIRECTION_API_POST ) {
205 $url = admin_url( 'tools.php?page=redirection.php&action=red_proxy&rest_path=' );
206 }
207
208 return $url;
209 }
210