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