| 1 |
<?php |
| 2 |
|
| 3 |
const REDIRECTION_OPTION = 'redirection_options'; |
| 4 |
|
| 5 |
function red_set_options( array $settings = array() ) { |
| 6 |
$options = red_get_options(); |
| 7 |
$monitor_types = array(); |
| 8 |
$have_monitor = false; |
| 9 |
|
| 10 |
if ( isset( $settings['monitor_type_post'] ) ) { |
| 11 |
$monitor_types[] = $settings['monitor_type_post'] ? 'post' : false; |
| 12 |
$have_monitor = true; |
| 13 |
} |
| 14 |
|
| 15 |
if ( isset( $settings['monitor_type_page'] ) ) { |
| 16 |
$monitor_types[] = $settings['monitor_type_page'] ? 'page' : false; |
| 17 |
$have_monitor = true; |
| 18 |
} |
| 19 |
|
| 20 |
if ( isset( $settings['monitor_type_trash'] ) ) { |
| 21 |
$monitor_types[] = $settings['monitor_type_trash'] ? 'trash' : false; |
| 22 |
$have_monitor = true; |
| 23 |
} |
| 24 |
|
| 25 |
if ( isset( $settings['associated_redirect'] ) ) { |
| 26 |
$options['associated_redirect'] = ''; |
| 27 |
|
| 28 |
if ( strlen( $settings['associated_redirect'] ) > 0 ) { |
| 29 |
$sanitizer = new Red_Item_Sanitize(); |
| 30 |
$options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
$monitor_types = array_values( array_filter( $monitor_types ) ); |
| 35 |
|
| 36 |
if ( count( $monitor_types ) === 0 ) { |
| 37 |
$options['monitor_post'] = 0; |
| 38 |
$options['associated_redirect'] = ''; |
| 39 |
} elseif ( isset( $settings['monitor_post'] ) ) { |
| 40 |
$options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) ); |
| 41 |
|
| 42 |
if ( ! Red_Group::get( $options['monitor_post'] ) ) { |
| 43 |
$groups = Red_Group::get_all(); |
| 44 |
$options['monitor_post'] = $groups[ 0 ]['id']; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if ( isset( $settings['auto_target'] ) ) { |
| 49 |
$options['auto_target'] = stripslashes( $settings['auto_target'] ); |
| 50 |
} |
| 51 |
|
| 52 |
if ( isset( $settings['support'] ) ) { |
| 53 |
$options['support'] = $settings['support'] ? true : false; |
| 54 |
} |
| 55 |
|
| 56 |
if ( isset( $settings['token'] ) ) { |
| 57 |
$options['token'] = stripslashes( $settings['token'] ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( !isset( $settings['token'] ) || trim( $options['token'] ) === '' ) { |
| 61 |
$options['token'] = md5( uniqid() ); |
| 62 |
} |
| 63 |
|
| 64 |
if ( isset( $settings['newsletter'] ) ) { |
| 65 |
$options['newsletter'] = $settings['newsletter'] ? true : false; |
| 66 |
} |
| 67 |
|
| 68 |
if ( isset( $settings['expire_redirect'] ) ) { |
| 69 |
$options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( isset( $settings['expire_404'] ) ) { |
| 73 |
$options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) ); |
| 74 |
} |
| 75 |
|
| 76 |
if ( isset( $settings['redirect_cache'] ) ) { |
| 77 |
$options['redirect_cache'] = intval( $settings['redirect_cache'], 10 ); |
| 78 |
|
| 79 |
if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) { |
| 80 |
$options['redirect_cache'] = 1; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if ( isset( $settings['location'] ) ) { |
| 85 |
$module = Red_Module::get( 2 ); |
| 86 |
$options['modules'][2] = $module->update( $settings ); |
| 87 |
} |
| 88 |
|
| 89 |
if ( $have_monitor ) { |
| 90 |
$options['monitor_types'] = $monitor_types; |
| 91 |
} |
| 92 |
|
| 93 |
update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) ); |
| 94 |
return $options; |
| 95 |
} |
| 96 |
|
| 97 |
function red_get_options() { |
| 98 |
$options = get_option( REDIRECTION_OPTION ); |
| 99 |
if ( $options === false ) { |
| 100 |
$options = array(); |
| 101 |
} |
| 102 |
|
| 103 |
$defaults = apply_filters( 'red_default_options', array( |
| 104 |
'support' => false, |
| 105 |
'token' => md5( uniqid() ), |
| 106 |
'monitor_post' => 0, // Dont monitor posts by default |
| 107 |
'monitor_types' => array(), |
| 108 |
'associated_redirect' => '', |
| 109 |
'auto_target' => '', |
| 110 |
'expire_redirect' => 7, // Expire in 7 days |
| 111 |
'expire_404' => 7, // Expire in 7 days |
| 112 |
'modules' => array(), |
| 113 |
'newsletter' => false, |
| 114 |
'redirect_cache' => 1, // 1 hour |
| 115 |
) ); |
| 116 |
|
| 117 |
foreach ( $defaults as $key => $value ) { |
| 118 |
if ( ! isset( $options[ $key ] ) ) { |
| 119 |
$options[ $key ] = $value; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// Back-compat. If monitor_post is set without types then it's from an older Redirection |
| 124 |
if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) { |
| 125 |
$options['monitor_types'] = array( 'post' ); |
| 126 |
} |
| 127 |
|
| 128 |
return $options; |
| 129 |
} |
| 130 |
|