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() { if ( is_network_admin() ) { foreach ( get_sites() as $site ) { switch_to_blog( $site->blog_id ); Redirection_Admin::update(); Red_Flusher::schedule(); red_set_options(); restore_current_blog(); } } else { Redirection_Admin::update(); Red_Flusher::schedule(); 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() { if ( is_network_admin() ) { foreach ( get_sites() as $site ) { switch_to_blog( $site->blog_id ); Red_Flusher::clear(); restore_current_blog(); } } else { 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() { include_once dirname( REDIRECTION_FILE ) . '/models/database.php'; $database = new RE_Database(); if ( is_network_admin() ) { foreach ( get_sites() as $site ) { switch_to_blog( $site->blog_id ); $database->remove( REDIRECTION_FILE ); restore_current_blog(); } } else { $database->remove( REDIRECTION_FILE ); } } private static function update() { $version = get_option( 'redirection_version' ); Red_Flusher::schedule(); if ( $version !== REDIRECTION_DB_VERSION || ( defined( 'REDIRECTION_FORCE_UPDATE' ) && REDIRECTION_FORCE_UPDATE ) ) { include_once dirname( REDIRECTION_FILE ) . '/models/database.php'; $database = new RE_Database(); if ( $version === false ) { $database->install(); } $database->upgrade( $version, REDIRECTION_DB_VERSION ); } } // 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, 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', '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 ) { $settings_link = '' . __( 'Settings', 'redirection' ) . ''; array_unshift( $links, $settings_link ); return $links; } function redirection_head() { global $wp_version; $this->check_rest_api(); if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'wp_rest' ) ) { if ( $_REQUEST['action'] === 'fixit' ) { $this->run_fixit(); } else if ( $_REQUEST['action'] === 'rest_api' ) { $this->set_rest_api( intval( $_REQUEST['rest_api'], 10 ) ); } else if ( $_REQUEST['action'] === 'red_proxy' ) { // Hack to get around clash with WP page param if ( isset( $_GET['page'] ) && $_GET['page'] === 'redirection.php' ) { unset( $_GET['page'] ); } if ( isset( $_GET['ppage'] ) ) { $_GET['page'] = $_GET['ppage']; } $this->red_proxy(); } } $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 ( ! isset( $_GET['sub'] ) || ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) ) ) { add_screen_option( 'per_page', array( '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 ); wp_localize_script( 'redirection', 'Redirectioni10n', array( 'WP_API_root' => esc_url_raw( red_get_rest_api() ), 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), '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(), 'token' => $options['token'], 'autoGenerate' => $options['auto_target'], 'preload' => $preload, 'versions' => implode( "\n", $versions ), 'version' => REDIRECTION_VERSION, 'api_setting' => $options['rest_api'], ) ); $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; } public function check_rest_api() { $options = red_get_options(); if ( $options['version'] !== REDIRECTION_VERSION || $options['rest_api'] === false || ( defined( 'REDIRECTION_FORCE_UPDATE' ) && REDIRECTION_FORCE_UPDATE ) ) { include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php'; $fixer = new Red_Fixer(); $status = $fixer->get_rest_status(); if ( $status['status'] === 'problem' ) { $fixer->fix_rest(); } elseif ( $options['rest_api'] === false ) { red_set_options( array( 'rest_api' => 0 ) ); } red_set_options( array( 'version' => REDIRECTION_VERSION ) ); } } private function run_fixit() { if ( current_user_can( apply_filters( 'redirection_role', 'manage_options' ) ) ) { include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php'; $fixer = new Red_Fixer(); $fixer->fix( $fixer->get_status() ); } } private function set_rest_api( $api ) { if ( $api >= 0 && $api <= REDIRECTION_API_POST ) { red_set_options( array( 'rest_api' => intval( $api, 10 ) ) ); } } private function get_preload_data() { $page = ''; if ( isset( $_GET['sub'] ) && in_array( $_GET['sub'], array( 'group', '404s', 'log', 'io', 'options', 'support' ) ) ) { $page = $_GET['sub']; } if ( $page === 'support' ) { $api = new Redirection_Api_Plugin( REDIRECTION_API_NAMESPACE ); return array( 'pluginStatus' => $api->route_status( new WP_REST_Request() ), ); } return array(); } private function add_help_tab() { $title = __( 'Redirection Support', 'redirection' ); $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' ); $current_screen = get_current_screen(); $current_screen->add_help_tab( array( 'id' => 'redirection', 'title' => 'Redirection', 'content' => "
$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() { $i18n_json = dirname( REDIRECTION_FILE ) . '/locale/json/redirection-' . get_user_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(); } function admin_menu() { $hook = add_management_page( 'Redirection', 'Redirection', apply_filters( 'redirection_role', 'manage_options' ), basename( REDIRECTION_FILE ), array( &$this, 'admin_screen' ) ); add_action( 'load-' . $hook, array( $this, 'redirection_head' ) ); } private function check_minimum_wp() { $wp_version = get_bloginfo( 'version' ); if ( version_compare( $wp_version, REDIRECTION_MIN_WP, '<' ) ) { ?>support page for more details.', 'redirection' ), 'tools.php?page=redirection.php&sub=support' ); ?>