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