-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathredirection.php
More file actions
204 lines (170 loc) · 5.71 KB
/
Copy pathredirection.php
File metadata and controls
204 lines (170 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
Plugin Name: Redirection
Plugin URI: https://redirection.me/
Description: Manage all your 301 redirects and monitor 404 errors
Version: 5.9.0
Author: John Godley
Text Domain: redirection
Requires PHP: 7.4
Requires at least: 6.6
============================================================================================================
For full license details see license.txt
============================================================================================================
*/
define( 'REDIRECTION_DB_VERSION', '4.2' ); // DB schema version. Only change if DB needs changing
define( 'REDIRECTION_FILE', __FILE__ );
define( 'REDIRECTION_VERSION', '5.9.0' );
define( 'REDIRECTION_MIN_WP', '6.6' );
if ( ! defined( 'REDIRECTION_FLYING_SOLO' ) ) {
define( 'REDIRECTION_FLYING_SOLO', apply_filters( 'redirection_flying_solo', true ) );
}
// This file must support PHP < 7.4 so as not to crash
if ( version_compare( PHP_VERSION, '7.4' ) < 0 ) {
add_filter( 'plugin_action_links_' . basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ), 'red_deprecated_php' );
/**
* @param array<string> $links
* @return array<string>
*/
function red_deprecated_php( array $links ): array {
/* translators: 1: server PHP version. 2: required PHP version. */
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>' );
return $links;
}
return;
}
require_once __DIR__ . '/redirection-settings.php';
require_once __DIR__ . '/models/options.php';
require_once __DIR__ . '/models/redirect/redirect.php';
require_once __DIR__ . '/models/url/url.php';
require_once __DIR__ . '/models/regex.php';
require_once __DIR__ . '/models/module.php';
require_once __DIR__ . '/models/log/log.php';
require_once __DIR__ . '/models/flusher.php';
require_once __DIR__ . '/models/match.php';
require_once __DIR__ . '/models/action.php';
require_once __DIR__ . '/models/request.php';
require_once __DIR__ . '/models/header.php';
require_once __DIR__ . '/models/group.php';
/**
* Autoload a namespaced class from a plugin directory.
*
* @param string $requested_class Requested class name.
* @param string $prefix Namespace prefix.
* @param string $base_dir Base directory.
* @return void
*/
function redirection_autoload_namespace( $requested_class, $prefix, $base_dir ) {
if ( strncmp( $prefix, $requested_class, strlen( $prefix ) ) !== 0 ) {
return;
}
$relative_class = substr( $requested_class, strlen( $prefix ) );
if ( $relative_class === '' ) {
return;
}
$normalize = static function ( $value ) {
$value = preg_replace( '/(?<!^)[A-Z]/', '-$0', $value );
if ( ! is_string( $value ) ) {
return '';
}
return str_replace( '_', '-', strtolower( $value ) );
};
$segments = explode( '\\', $relative_class );
$class_name = array_pop( $segments );
if ( ! is_string( $class_name ) || $class_name === '' ) {
return;
}
if ( count( $segments ) > 0 ) {
$base_dir .= implode( '/', array_map( $normalize, $segments ) ) . '/';
}
$path = $base_dir . 'class-' . $normalize( $class_name ) . '.php';
if ( file_exists( $path ) ) {
require_once $path;
}
}
/**
* Autoload namespaced Redirection classes from the includes directory.
*
* @param string $requested_class Requested class name.
* @return void
*/
function redirection_autoload( $requested_class ) {
redirection_autoload_namespace( $requested_class, 'Redirection\\', __DIR__ . '/includes/' );
}
spl_autoload_register( 'redirection_autoload' );
/**
* Clear PHP opcache when plugin is updated. This is to help with mid-update errors.
*
* @param object $upgrader The upgrader object.
* @param array{action: string, type: string, plugins?: string[]} $options The upgrade options.
* @return void
*/
function redirection_clear_opcache_on_upgrade( $upgrader, $options ) {
if ( $options['action'] !== 'update' || $options['type'] !== 'plugin' ) {
return;
}
$plugin_basename = plugin_basename( REDIRECTION_FILE );
$plugins = $options['plugins'] ?? [];
if ( ! in_array( $plugin_basename, $plugins, true ) ) {
return;
}
if ( function_exists( 'opcache_reset' ) ) {
// Suppress warnings if opcache_reset is restricted by server configuration
@opcache_reset(); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
}
}
add_action( 'upgrader_process_complete', 'redirection_clear_opcache_on_upgrade', 10, 2 );
/**
* @return bool
*/
function red_is_wpcli() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return true;
}
return false;
}
/**
* Detect a plain PHP CLI context (e.g., a cron script that loads wp-load.php
* directly). Distinct from red_is_wpcli(), which is only true under WP-CLI.
* Used to skip front-end redirect enforcement that would otherwise call die()
* and silently terminate the CLI process.
*
* @return bool
*/
function red_is_cli() {
return PHP_SAPI === 'cli';
}
/**
* @return bool
*/
function red_is_admin() {
if ( is_admin() ) {
return true;
}
return false;
}
/**
* @return void
*/
function red_start_rest() {
require_once __DIR__ . '/redirection-admin.php';
Redirection\Api\Api::init();
Redirection_Admin::init();
remove_action( 'rest_api_init', 'red_start_rest' );
}
/**
* @return void
*/
function redirection_locale() {
load_plugin_textdomain( 'redirection', false, dirname( plugin_basename( REDIRECTION_FILE ) ) . '/locale/' );
}
if ( red_is_admin() || red_is_wpcli() ) {
require_once __DIR__ . '/redirection-admin.php';
} else {
require_once __DIR__ . '/redirection-front.php';
}
if ( red_is_wpcli() ) {
require_once __DIR__ . '/redirection-cli.php';
}
add_action( 'rest_api_init', 'red_start_rest' );
add_action( 'init', 'redirection_locale' );