monitor = new Red_Monitor( red_get_options() ); $this->run_hacks(); } // These are only called on the single standard site, or in the network admin of the multisite - they run across all available sites public static function plugin_activated() { Red_Database::apply_to_sites( function() { Red_Flusher::clear(); red_set_options(); } ); } // These are only called on the single standard site, or in the network admin of the multisite - they run across all available sites public static function plugin_deactivated() { Red_Database::apply_to_sites( function() { Red_Flusher::clear(); } ); } // These are only called on the single standard site, or in the network admin of the multisite - they run across all available sites public static function plugin_uninstall() { $database = Red_Database::get_latest_database(); Red_Database::apply_to_sites( function() use ( $database ) { $database->remove(); } ); } public function update_nag() { if ( ! $this->user_has_access() ) { return; } $status = new Red_Database_Status(); $message = false; if ( $status->needs_installing() ) { /* translators: 1: URL to plugin page */ $message = sprintf( __( 'Please complete your Redirection setup to activate the plugin.' ), 'tools.php?page=' . basename( REDIRECTION_FILE ) ); } elseif ( $status->needs_updating() ) { /* translators: 1: URL to plugin page, 2: current version, 3: target version */ $message = sprintf( __( 'Redirection\'s database needs to be updated - click to update.' ), 'tools.php?page=' . basename( REDIRECTION_FILE ) ); } if ( ! $message || strpos( Redirection_Request::get_request_url(), 'page=redirection.php' ) !== false ) { return; } // Contains HTML echo '
' . $message . '
'; } // 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 ) { if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], '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; } function set_per_page( $status, $option, $value ) { if ( $option === 'redirection_log_per_page' ) { return max( 1, min( intval( $value, 10 ), RED_MAX_PER_PAGE ) ); } return $status; } function plugin_settings( $links ) { $status = new Red_Database_Status(); if ( $status->needs_updating() ) { array_unshift( $links, '' . __( 'Upgrade Database', 'redirection' ) . '' ); } array_unshift( $links, '' . __( 'Settings', 'redirection' ) . '' ); return $links; } function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) { if ( $plugin_file === basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ) ) { $plugin_data['Description'] .= '

' . __( 'Please upgrade your database', 'redirection' ) . '

'; } return $plugin_meta; } function redirection_head() { global $wp_version; if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'wp_rest' ) ) { if ( $_REQUEST['action'] === 'fixit' ) { $this->run_fixit(); } elseif ( $_REQUEST['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, 'WordPress: ' . $wp_version . ' (' . ( is_multisite() ? 'multi' : 'single' ) . ')', 'PHP: ' . phpversion(), 'Browser: ' . Redirection_Request::get_user_agent(), 'JavaScript: ' . plugin_dir_url( REDIRECTION_FILE ) . 'redirection.js', 'REST API: ' . red_get_rest_api(), ); $this->inject(); if ( in_array( $this->get_menu_page(), array( 'redirects', 'log', '404s', 'groups' ) ) ) { 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 ); $status = new Red_Database_Status(); $status->check_tables_exist(); 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' ), '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' => admin_url( 'tools.php?page=redirection.php' ), 'per_page' => $this->get_per_page(), 'locale' => $this->get_i18n_data(), 'localeSlug' => get_locale(), 'settings' => $options, 'preload' => $preload, 'versions' => implode( "\n", $versions ), 'version' => REDIRECTION_VERSION, 'database' => $status->get_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 ( $this->user_has_access() ) { include_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() { if ( $this->get_menu_page() === 'support' ) { include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php'; $fixer = new Red_Fixer(); return array( 'pluginStatus' => $fixer->get_json(), ); } return array(); } private function add_help_tab() { /* translators: URL */ $content = sprintf( __( 'You can find full documentation about using Redirection on the redirection.me support site.', 'redirection' ), 'https://redirection.me/support/?utm_source=redirection&utm_medium=plugin&utm_campaign=context-help' ); $title = __( 'Redirection Support', 'redirection' ); $current_screen = get_current_screen(); $current_screen->add_help_tab( array( 'id' => 'redirection', 'title' => 'Redirection', 'content' => "

$title

$content

", ) ); } private function get_per_page() { $per_page = intval( get_user_meta( get_current_user_id(), 'redirection_log_per_page', true ), 10 ); return $per_page > 0 ? max( 5, min( $per_page, RED_MAX_PER_PAGE ) ) : RED_DEFAULT_PER_PAGE; } private function get_i18n_data() { $locale = get_locale(); // WP 4.7 if ( function_exists( 'get_user_locale' ) ) { $locale = get_user_locale(); } $i18n_json = dirname( REDIRECTION_FILE ) . '/locale/json/redirection-' . $locale . '.json'; if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { $locale_data = @file_get_contents( $i18n_json ); if ( $locale_data ) { return json_decode( $locale_data ); } } // Return empty if we have nothing to return so it doesn't fail when parsed in JS return array(); } public function admin_menu() { $hook = add_management_page( 'Redirection', 'Redirection', $this->get_access_role(), basename( REDIRECTION_FILE ), [ $this, 'admin_screen' ] ); add_action( 'load-' . $hook, [ $this, 'redirection_head' ] ); } public function get_access_role() { return apply_filters( 'redirection_role', 'manage_options' ); } private function check_minimum_wp() { $wp_version = get_bloginfo( 'version' ); if ( version_compare( $wp_version, REDIRECTION_MIN_WP, '<' ) ) { return false; } return true; } public function set_default_group( $id, $redirect ) { red_set_options( array( 'last_group_id' => $redirect->get_group_id() ) ); } public function admin_screen() { if ( ! $this->user_has_access() ) { die( 'You do not have sufficient permissions to access this page.' ); } if ( $this->check_minimum_wp() === false ) { return $this->show_minimum_wordpress(); } if ( $this->fixit_failed ) { $this->show_fixit_failed(); } Red_Flusher::schedule(); $this->show_main(); } private function show_fixit_failed() { ?>

fixit_failed->get_error_message() ); ?>

fixit_failed->get_error_data() ); ?>

show_load_fail(); ?>
get_access_role() ); } private function inject() { if ( isset( $_GET['page'] ) && $this->get_menu_page() !== 'redirects' && $_GET['page'] === 'redirection.php' ) { $this->try_export_logs(); $this->try_export_redirects(); $this->try_export_rss(); } } private function get_menu_page() { if ( isset( $_GET['sub'] ) && in_array( $_GET['sub'], array( 'group', '404s', 'log', 'io', 'options', 'support', true ) ) ) { return $_GET['sub']; } return 'redirects'; } public function try_export_rss() { if ( isset( $_GET['token'] ) && $this->get_menu_page() === 'rss' ) { $options = red_get_options(); if ( $_GET['token'] === $options['token'] && ! empty( $options['token'] ) ) { $items = Red_Item::get_all_for_module( intval( $_GET['module'] ) ); $exporter = Red_FileIO::create( 'rss' ); $exporter->force_download(); echo $exporter->get_data( $items, array() ); die(); } } } private function try_export_logs() { if ( $this->user_has_access() && isset( $_POST['export-csv'] ) && check_admin_referer( 'wp_rest' ) ) { if ( $this->get_menu_page() === 'log' ) { RE_Log::export_to_csv(); } else { RE_404::export_to_csv(); } die(); } } private function try_export_redirects() { if ( $this->user_has_access() && $_GET['sub'] === 'io' && isset( $_GET['exporter'] ) && isset( $_GET['export'] ) && check_admin_referer( 'wp_rest' ) ) { $export = Red_FileIO::export( $_GET['export'], $_GET['exporter'] ); if ( $export !== false ) { $export['exporter']->force_download(); echo $export['data']; die(); } } } } register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) ); add_action( 'init', array( 'Redirection_Admin', 'init' ) ); // This is causing a lot of problems with the REST API - disable qTranslate add_filter( 'qtranslate_language_detect_redirect', function( $lang, $url ) { $url = Redirection_Request::get_request_url(); if ( strpos( $url, '/wp-json/' ) !== false || strpos( $url, 'index.php?rest_route' ) !== false ) { return false; } return $lang; }, 10, 2 );