monitor = new Red_Monitor( Red_Options::get() ); $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 * @return void */ 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 * @return void */ 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 * @return void */ public static function plugin_uninstall() { $database = Red_Database::get_latest_database(); Red_Database::apply_to_sites( function () use ( $database ) { $database->remove(); } ); } /** * Show the database upgrade nag * * @return void */ public function update_nag() { $options = Red_Options::get(); // Is the site configured to upgrade automatically? if ( $options['plugin_update'] === 'admin' ) { $this->automatic_upgrade(); return; } // Can the user perform a manual database upgrade? if ( ! Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_OPTION_MANAGE ) ) { return; } // Default manual update, with nag $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.', 'redirection' ), esc_url( $this->get_plugin_url() ) ); } 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.', 'redirection' ), esc_url( $this->get_plugin_url() ) ); } if ( $message === false || strpos( Redirection_Request::get_request_url(), 'page=redirection.php' ) !== false ) { return; } // Known HTML and so isn't escaped // phpcs:ignore echo '
' . __( 'Please upgrade your database', 'redirection' ) . '
'; } return $plugin_meta; } /** * @return string */ private function get_plugin_url() { return admin_url( 'tools.php?page=' . basename( REDIRECTION_FILE ) ); } /** * @return string */ 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(); } /** * @param mixed $name * @return string|null */ private function get_query( $name ) { if ( isset( $_GET[ $name ] ) ) { return sanitize_text_field( $_GET[ $name ] ); } return null; } /** * @return void */ 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' ) !== false ) { $action = sanitize_text_field( $_REQUEST['action'] ); if ( $action === 'fixit' ) { $this->run_fixit(); } elseif ( $action === 'rest_api' && isset( $_REQUEST['rest_api'] ) && is_string( $_REQUEST['rest_api'] ) ) { $this->set_rest_api( intval( $_REQUEST['rest_api'], 10 ) ); } } $build = REDIRECTION_VERSION . '-' . REDIRECTION_BUILD; $preload = $this->get_preload_data(); $options = Red_Options::get(); $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 ) . 'build/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_Log::MAX_PER_PAGE ), 'default' => Red_Log::DEFAULT_PER_PAGE, 'option' => 'redirection_log_per_page', ) ); } $assets = include plugin_dir_path( REDIRECTION_FILE ) . 'build/redirection.asset.php'; $dependencies = $assets['dependencies']; $version = $assets['version']; wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ) . 'build/redirection.js', $dependencies, $version, true ); wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ) . 'build/redirection.css', [], $version ); $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( (string) $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 ( $options['database'] === '+OK' ) { red_set_options( [ 'database' => REDIRECTION_DB_VERSION ] ); $status->stop_update(); } 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' => [ Red_Options::API_JSON => red_get_rest_api( Red_Options::API_JSON ), Red_Options::API_JSON_INDEX => red_get_rest_api( Red_Options::API_JSON_INDEX ), Red_Options::API_JSON_RELATIVE => red_get_rest_api( Red_Options::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' ); $this->add_help_tab(); } /** * Some plugins misbehave, so this attempts to 'fix' them so Redirection can get on with it's work * @return void */ 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 * * @param array$content
", ) ); } /** * @return int */ 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_Log::MAX_PER_PAGE ) ) : Red_Log::DEFAULT_PER_PAGE; } /** * @return void */ public function admin_menu() { $hook = add_management_page( 'Redirection', 'Redirection', Redirection_Capabilities::get_plugin_access(), basename( REDIRECTION_FILE ), [ $this, 'admin_screen' ] ); add_action( 'load-' . $hook, [ $this, 'redirection_head' ] ); } /** * @return bool */ private function check_minimum_wp() { $wp_version = get_bloginfo( 'version' ); if ( version_compare( $wp_version, REDIRECTION_MIN_WP, '<' ) ) { return false; } return true; } /** * Update the cache key when updating or creating a redirect * * @return void */ public function clear_cache() { $settings = Red_Options::get(); if ( $settings['cache_key'] > 0 ) { red_set_options( [ 'cache_key' => time() ] ); } } /** * @param mixed $id * @param mixed $redirect * @return void */ public function set_default_group( $id, $redirect ) { red_set_options( array( 'last_group_id' => $redirect->get_group_id() ) ); } /** * @return void */ public function admin_screen() { if ( count( Redirection_Capabilities::get_all_capabilities() ) === 0 ) { die( 'You do not have sufficient permissions to access this page.' ); } if ( $this->check_minimum_wp() === false ) { $this->show_minimum_wordpress(); return; } if ( $this->fixit_failed instanceof WP_Error ) { $this->show_fixit_failed(); } Red_Flusher::schedule(); $this->show_main(); } /** * @return void */ private function show_fixit_failed() { if ( $this->fixit_failed === false ) { return; } ?>fixit_failed->get_error_data() ); ?>