' . $message . '
';
}
/**
* Perform an automatic DB upgrade
*
* @return void
*/
private function automatic_upgrade() {
$loop = 0;
$status = new Red_Database_Status();
$database = new Red_Database();
// Loop until the DB is upgraded, or until a max is exceeded (just in case)
while ( $loop < 20 ) {
if ( ! $status->needs_updating() ) {
break;
}
$database->apply_upgrade( $status );
if ( $status->is_error() ) {
// If an error occurs then switch to 'prompt' mode and let the user deal with it.
red_set_options( [ 'plugin_update' => 'prompt' ] );
return;
}
$loop++;
}
}
// So it finally came to this... some plugins include their JS in all pages, whether they are needed or not. If there is an error
// then this can prevent Redirection running and it's a little sensitive about that. We use the nuclear option here to disable
// all other JS while viewing Redirection
public function flying_solo( $src, $handle ) {
$request = Redirection_Request::get_request_url();
if ( strpos( $request, 'page=redirection.php' ) !== false ) {
if ( substr( $src, 0, 4 ) === 'http' && $handle !== 'redirection' && strpos( $src, 'plugins' ) !== false ) {
if ( $this->ignore_this_plugin( $src ) ) {
return false;
}
}
}
return $src;
}
private function ignore_this_plugin( $src ) {
$ignore = array(
'mootools',
'wp-seo-',
'authenticate',
'wordpress-seo',
'yikes',
);
foreach ( $ignore as $text ) {
if ( strpos( $src, $text ) !== false ) {
return true;
}
}
return false;
}
public function flush_schedule( $options ) {
Red_Flusher::schedule();
return $options;
}
public function set_per_page( $status, $option, $value ) {
if ( $option === 'redirection_log_per_page' ) {
$value = max( 1, min( intval( $value, 10 ), RED_MAX_PER_PAGE ) );
return $value;
}
return $status;
}
public function plugin_settings( $links ) {
$status = new Red_Database_Status();
if ( $status->needs_updating() ) {
array_unshift( $links, '' . __( 'Please upgrade your database', 'redirection' ) . '
';
}
return $plugin_meta;
}
private function get_plugin_url() {
return admin_url( 'tools.php?page=' . basename( REDIRECTION_FILE ) );
}
private function get_first_available_page_url() {
$pages = Redirection_Capabilities::get_available_pages();
if ( count( $pages ) > 0 ) {
return $this->get_plugin_url() . ( $pages[0] === 'redirect' ? '' : '&sub=' . rawurlencode( $pages[0] ) );
}
return admin_url();
}
private function get_query( $name ) {
if ( isset( $_GET[ $name ] ) ) {
return sanitize_text_field( $_GET[ $name ] );
}
return null;
}
public function redirection_head() {
global $wp_version;
// Does user have access to this page?
if ( $this->get_current_page() === false ) {
// Redirect to root plugin page
wp_safe_redirect( $this->get_first_available_page_url() );
die();
}
if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['_wpnonce'] ) && is_string( $_REQUEST['action'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'wp_rest' ) ) {
$action = sanitize_text_field( $_REQUEST['action'] );
if ( $action === 'fixit' ) {
$this->run_fixit();
} elseif ( $action === 'rest_api' ) {
$this->set_rest_api( intval( $_REQUEST['rest_api'], 10 ) );
}
}
$build = REDIRECTION_VERSION . '-' . REDIRECTION_BUILD;
$preload = $this->get_preload_data();
$options = red_get_options();
$versions = array(
'Plugin: ' . REDIRECTION_VERSION . ' ' . REDIRECTION_DB_VERSION,
'WordPress: ' . $wp_version . ' (' . ( is_multisite() ? 'multi' : 'single' ) . ')',
'PHP: ' . phpversion(),
'Browser: ' . Redirection_Request::get_user_agent(),
'JavaScript: ' . plugin_dir_url( REDIRECTION_FILE ) . 'redirection.js?ver=' . $build,
'REST API: ' . red_get_rest_api(),
);
$this->inject();
// Add contextual help to some pages
if ( in_array( $this->get_current_page(), [ 'redirect', 'log', '404s', 'groups' ], true ) ) {
add_screen_option( 'per_page', array(
/* translators: maximum number of log entries */
'label' => sprintf( __( 'Log entries (%d max)', 'redirection' ), RED_MAX_PER_PAGE ),
'default' => RED_DEFAULT_PER_PAGE,
'option' => 'redirection_log_per_page',
) );
}
if ( defined( 'REDIRECTION_DEV_MODE' ) && REDIRECTION_DEV_MODE ) {
wp_enqueue_script( 'redirection', 'http://localhost:3312/redirection.js', array(), $build, true );
} else {
wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ) . 'redirection.js', array(), $build, true );
}
wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ) . 'redirection.css', array(), $build );
$is_new = false;
$major_version = implode( '.', array_slice( explode( '.', REDIRECTION_VERSION ), 0, 2 ) );
if ( $this->get_query( 'page' ) === 'redirection.php' && strpos( REDIRECTION_VERSION, '-beta' ) === false ) {
$is_new = version_compare( $options['update_notice'], $major_version ) < 0;
}
$status = new Red_Database_Status();
$status->check_tables_exist();
// Fix some sites having a version set to +OK - not sure why
if ( isset( $options['database'] ) && $options['database'] === '+OK' ) {
red_set_options( [ 'database' => REDIRECTION_DB_VERSION ] );
$status->stop_update();
}
$translations = $this->get_i18n_data();
wp_localize_script( 'redirection', 'Redirectioni10n', array(
'api' => [
'WP_API_root' => esc_url_raw( red_get_rest_api() ),
'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
'site_health' => admin_url( 'site-health.php' ),
'current' => $options['rest_api'],
'routes' => [
REDIRECTION_API_JSON => red_get_rest_api( REDIRECTION_API_JSON ),
REDIRECTION_API_JSON_INDEX => red_get_rest_api( REDIRECTION_API_JSON_INDEX ),
REDIRECTION_API_JSON_RELATIVE => red_get_rest_api( REDIRECTION_API_JSON_RELATIVE ),
],
],
'pluginBaseUrl' => plugins_url( '', REDIRECTION_FILE ),
'pluginRoot' => $this->get_plugin_url(),
'per_page' => $this->get_per_page(),
'locale' => implode( '-', array_slice( explode( '-', str_replace( '_', '-', get_locale() ) ), 0, 2 ) ),
'settings' => $options,
'preload' => $preload,
'versions' => implode( "\n", $versions ),
'version' => REDIRECTION_VERSION,
'database' => $status->get_json(),
'caps' => [
'pages' => Redirection_Capabilities::get_available_pages(),
'capabilities' => Redirection_Capabilities::get_all_capabilities(),
],
'update_notice' => $is_new ? $major_version : false,
) );
wp_set_script_translations( 'redirection', 'redirection', plugin_dir_path( __FILE__ ) . 'locale/json/' );
$this->add_help_tab();
}
// Some plugins misbehave, so this attempts to 'fix' them so Redirection can get on with it's work
private function run_hacks() {
add_filter( 'ip-geo-block-admin', array( $this, 'ip_geo_block' ) );
}
/*
* This works around the IP Geo Block plugin being very aggressive and breaking Redirection
*/
public function ip_geo_block( $validate ) {
$url = Redirection_Request::get_request_url();
$override = array(
'tools.php?page=redirection.php',
'action=red_proxy&rest_path=redirection',
);
foreach ( $override as $path ) {
if ( strpos( $url, $path ) !== false ) {
return array(
'result' => 'passed',
'auth' => false,
'asn' => false,
'code' => false,
'ip' => false,
);
}
}
return $validate;
}
private function run_fixit() {
if ( Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_SUPPORT_MANAGE ) ) {
require_once dirname( REDIRECTION_FILE ) . '/models/fixer.php';
$fixer = new Red_Fixer();
$result = $fixer->fix( $fixer->get_status() );
if ( is_wp_error( $result ) ) {
$this->fixit_failed = $result;
}
}
}
private function set_rest_api( $api ) {
if ( $api >= 0 && $api <= REDIRECTION_API_JSON_RELATIVE ) {
red_set_options( array( 'rest_api' => intval( $api, 10 ) ) );
}
}
private function get_preload_data() {
$status = new Red_Database_Status();
if ( $status->needs_installing() ) {
include_once __DIR__ . '/models/importer.php';
return [
'importers' => Red_Plugin_Importer::get_plugins(),
];
}
if ( $this->get_current_page() === 'support' ) {
require_once dirname( REDIRECTION_FILE ) . '/models/fixer.php';
$fixer = new Red_Fixer();
return array(
'pluginStatus' => $fixer->get_json(),
);
}
return [];
}
private function add_help_tab() {
/* translators: URL */
$content = sprintf( __( 'You can find full documentation about using Redirection on the
v
redirection.js:', 'redirection' ); ?>
list of common problems.', 'redirection' ); ?>
Redirectioni10n is not defined. This usually means another plugin is blocking Redirection from loading. Please disable all plugins and try again.', 'redirection' ); ?>