| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Redirection |
| 4 |
Plugin URI: https://redirection.me/ |
| 5 |
Description: Manage all your 301 redirects and monitor 404 errors |
| 6 |
Version: 5.10.0 |
| 7 |
Author: John Godley |
| 8 |
Text Domain: redirection |
| 9 |
Requires PHP: 7.4 |
| 10 |
Requires at least: 6.7 |
| 11 |
============================================================================================================ |
| 12 |
For full license details see license.txt |
| 13 |
============================================================================================================ |
| 14 |
*/ |
| 15 |
|
| 16 |
define( 'REDIRECTION_DB_VERSION', '4.2' ); // DB schema version. Only change if DB needs changing |
| 17 |
define( 'REDIRECTION_FILE', __FILE__ ); |
| 18 |
define( 'REDIRECTION_VERSION', '5.10.0' ); |
| 19 |
define( 'REDIRECTION_MIN_WP', '6.6' ); |
| 20 |
|
| 21 |
if ( ! defined( 'REDIRECTION_FLYING_SOLO' ) ) { |
| 22 |
define( 'REDIRECTION_FLYING_SOLO', apply_filters( 'redirection_flying_solo', true ) ); |
| 23 |
} |
| 24 |
|
| 25 |
// This file must support PHP < 7.4 so as not to crash |
| 26 |
if ( version_compare( phpversion(), '7.4' ) < 0 ) { |
| 27 |
add_filter( 'plugin_action_links_' . basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ), 'red_deprecated_php' ); |
| 28 |
|
| 29 |
/** |
| 30 |
* @param array<string> $links |
| 31 |
* @return array<string> |
| 32 |
*/ |
| 33 |
function red_deprecated_php( array $links ): array { |
| 34 |
/* translators: 1: server PHP version. 2: required PHP version. */ |
| 35 |
array_unshift( $links, '<a href="https://redirection.me/support/problems/php-version/" style="color: red; text-decoration: underline">' . sprintf( __( 'Disabled! Detected PHP %1$s, need PHP %2$s+', 'redirection' ), phpversion(), '7.4' ) . '</a>' ); |
| 36 |
return $links; |
| 37 |
} |
| 38 |
|
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Autoload a namespaced class from a plugin directory. |
| 44 |
* |
| 45 |
* @param string $requested_class Requested class name. |
| 46 |
* @param string $prefix Namespace prefix. |
| 47 |
* @param string $base_dir Base directory. |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
function redirection_autoload_namespace( $requested_class, $prefix, $base_dir ) { |
| 51 |
if ( strncmp( $prefix, $requested_class, strlen( $prefix ) ) !== 0 ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$relative_class = substr( $requested_class, strlen( $prefix ) ); |
| 56 |
if ( $relative_class === '' ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$normalize = static function ( $value ) { |
| 61 |
$value = preg_replace( '/(?<!^)[A-Z]/', '-$0', $value ); |
| 62 |
|
| 63 |
if ( ! is_string( $value ) ) { |
| 64 |
return ''; |
| 65 |
} |
| 66 |
|
| 67 |
return str_replace( '_', '-', strtolower( $value ) ); |
| 68 |
}; |
| 69 |
|
| 70 |
$segments = explode( '\\', $relative_class ); |
| 71 |
$class_name = array_pop( $segments ); |
| 72 |
if ( ! is_string( $class_name ) || $class_name === '' ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if ( count( $segments ) > 0 ) { |
| 77 |
$base_dir .= implode( '/', array_map( $normalize, $segments ) ) . '/'; |
| 78 |
} |
| 79 |
|
| 80 |
$path = $base_dir . 'class-' . $normalize( $class_name ) . '.php'; |
| 81 |
|
| 82 |
if ( file_exists( $path ) ) { |
| 83 |
require_once $path; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Autoload namespaced Redirection classes from the includes directory. |
| 89 |
* |
| 90 |
* @param string $requested_class Requested class name. |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
function redirection_autoload( $requested_class ) { |
| 94 |
redirection_autoload_namespace( $requested_class, 'Redirection\\', __DIR__ . '/includes/' ); |
| 95 |
} |
| 96 |
|
| 97 |
spl_autoload_register( 'redirection_autoload' ); |
| 98 |
|
| 99 |
/** |
| 100 |
* Set REDIRECTION_REFACTOR to true to enable the refactor mode, using autoloading for all classes. |
| 101 |
* This is implemented as a dual-mode plugin to allow for a smooth(er) transition to the new codebase. |
| 102 |
* |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
function red_is_refactor_enabled() { |
| 106 |
// @phpstan-ignore booleanAnd.rightAlwaysTrue |
| 107 |
return defined( 'REDIRECTION_REFACTOR' ) && REDIRECTION_REFACTOR; |
| 108 |
} |
| 109 |
|
| 110 |
if ( red_is_refactor_enabled() ) { |
| 111 |
require_once __DIR__ . '/redirection-refactor.php'; |
| 112 |
} else { |
| 113 |
require_once __DIR__ . '/redirection-settings.php'; |
| 114 |
require_once __DIR__ . '/models/options.php'; |
| 115 |
require_once __DIR__ . '/models/redirect/redirect.php'; |
| 116 |
require_once __DIR__ . '/models/url/url.php'; |
| 117 |
require_once __DIR__ . '/models/regex.php'; |
| 118 |
require_once __DIR__ . '/models/module.php'; |
| 119 |
require_once __DIR__ . '/models/log/log.php'; |
| 120 |
require_once __DIR__ . '/models/flusher.php'; |
| 121 |
require_once __DIR__ . '/models/match.php'; |
| 122 |
require_once __DIR__ . '/models/action.php'; |
| 123 |
require_once __DIR__ . '/models/request.php'; |
| 124 |
require_once __DIR__ . '/models/header.php'; |
| 125 |
require_once __DIR__ . '/models/group.php'; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Clear PHP opcache when plugin is updated. This is to help with mid-update errors. |
| 130 |
* |
| 131 |
* @param object $upgrader The upgrader object. |
| 132 |
* @param array{action: string, type: string, plugins?: string[]} $options The upgrade options. |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
function redirection_clear_opcache_on_upgrade( $upgrader, $options ) { |
| 136 |
if ( $options['action'] !== 'update' || $options['type'] !== 'plugin' ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
$plugin_basename = plugin_basename( REDIRECTION_FILE ); |
| 141 |
$plugins = $options['plugins'] ?? []; |
| 142 |
|
| 143 |
if ( ! in_array( $plugin_basename, $plugins, true ) ) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
if ( function_exists( 'opcache_reset' ) ) { |
| 148 |
// Suppress warnings if opcache_reset is restricted by server configuration |
| 149 |
@opcache_reset(); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
add_action( 'upgrader_process_complete', 'redirection_clear_opcache_on_upgrade', 10, 2 ); |
| 154 |
|
| 155 |
/** |
| 156 |
* @return bool |
| 157 |
*/ |
| 158 |
function red_is_wpcli() { |
| 159 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Detect a plain PHP CLI context (e.g., a cron script that loads wp-load.php |
| 168 |
* directly). Distinct from red_is_wpcli(), which is only true under WP-CLI. |
| 169 |
* Used to skip front-end redirect enforcement that would otherwise call die() |
| 170 |
* and silently terminate the CLI process. |
| 171 |
* |
| 172 |
* @return bool |
| 173 |
*/ |
| 174 |
function red_is_cli() { |
| 175 |
return PHP_SAPI === 'cli'; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @return bool |
| 180 |
*/ |
| 181 |
function red_is_admin() { |
| 182 |
if ( is_admin() ) { |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
function red_start_rest() { |
| 193 |
if ( red_is_refactor_enabled() ) { |
| 194 |
Redirection\Api\Api::init(); |
| 195 |
Redirection\Plugin\Admin::init(); |
| 196 |
remove_action( 'rest_api_init', 'red_start_rest' ); |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
require_once __DIR__ . '/redirection-admin.php'; |
| 201 |
Redirection\Api\Api::init(); |
| 202 |
Redirection_Admin::init(); |
| 203 |
|
| 204 |
remove_action( 'rest_api_init', 'red_start_rest' ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
function redirection_locale() { |
| 211 |
load_plugin_textdomain( 'redirection', false, dirname( plugin_basename( REDIRECTION_FILE ) ) . '/locale/' ); |
| 212 |
} |
| 213 |
|
| 214 |
if ( red_is_refactor_enabled() ) { |
| 215 |
if ( red_is_admin() || red_is_wpcli() ) { |
| 216 |
register_activation_hook( REDIRECTION_FILE, [ Redirection\Plugin\Admin::class, 'plugin_activated' ] ); |
| 217 |
|
| 218 |
// @phpstan-ignore return.void |
| 219 |
add_action( 'init', [ Redirection\Plugin\Admin::class, 'init' ] ); |
| 220 |
} else { |
| 221 |
// @phpstan-ignore return.void |
| 222 |
add_action( 'plugins_loaded', [ Redirection\Plugin\Front::class, 'init' ] ); |
| 223 |
} |
| 224 |
|
| 225 |
if ( red_is_wpcli() ) { |
| 226 |
WP_CLI::add_command( 'redirection', Redirection\Plugin\Cli::class ); |
| 227 |
|
| 228 |
add_action( |
| 229 |
Redirection\Plugin\Flusher::DELETE_HOOK, |
| 230 |
function () { |
| 231 |
$flusher = new Redirection\Plugin\Flusher(); |
| 232 |
$flusher->flush(); |
| 233 |
} |
| 234 |
); |
| 235 |
} |
| 236 |
} elseif ( red_is_admin() || red_is_wpcli() ) { |
| 237 |
require_once __DIR__ . '/redirection-admin.php'; |
| 238 |
} else { |
| 239 |
require_once __DIR__ . '/redirection-front.php'; |
| 240 |
} |
| 241 |
|
| 242 |
if ( ! red_is_refactor_enabled() && red_is_wpcli() ) { |
| 243 |
require_once __DIR__ . '/redirection-cli.php'; |
| 244 |
} |
| 245 |
|
| 246 |
add_action( 'rest_api_init', 'red_start_rest' ); |
| 247 |
add_action( 'init', 'redirection_locale' ); |
| 248 |
|