PluginProbe
Redirection / 4.7.1
Redirection v4.7.1
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / redirection-front.php

redirection-front.php in Redirection 4.7.1, at redirection-front.php

103 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once __DIR__ . '/modules/wordpress.php';
4 require_once __DIR__ . '/models/canonical.php';
5 require_once __DIR__ . '/database/database-status.php';
6
7 class Redirection {
8 private static $instance = null;
9 private $module;
10
11 public static function init() {
12 if ( is_null( self::$instance ) ) {
13 self::$instance = new Redirection();
14 }
15
16 return self::$instance;
17 }
18
19 public function __construct() {
20 if ( ! $this->can_start() ) {
21 return;
22 }
23
24 $this->module = Red_Module::get( WordPress_Module::MODULE_ID );
25 $this->module->start();
26
27 add_action( Red_Flusher::DELETE_HOOK, array( $this, 'clean_redirection_logs' ) );
28 add_filter( 'redirection_url_target', array( $this, 'replace_special_tags' ) );
29
30 $options = red_get_options();
31 if ( $options['ip_logging'] === 0 ) {
32 add_filter( 'redirection_request_ip', array( $this, 'no_ip_logging' ) );
33 } elseif ( $options['ip_logging'] === 2 ) {
34 add_filter( 'redirection_request_ip', array( $this, 'mask_ip' ) );
35 }
36 }
37
38 public function can_start() {
39 $status = new Red_Database_Status();
40 if ( $status->needs_installing() ) {
41 return false;
42 }
43
44 return true;
45 }
46
47 public function no_ip_logging( $ip ) {
48 return '';
49 }
50
51 public function mask_ip( $ip ) {
52 $ip = trim( $ip );
53
54 if ( strpos( $ip, ':' ) !== false ) {
55 // phpcs:ignore
56 $ip = @inet_pton( trim( $ip ) );
57
58 // phpcs:ignore
59 return @inet_ntop( $ip & pack( 'a16', 'ffff:ffff:ffff:ffff::ff00::0000::0000::0000' ) );
60 }
61
62 $parts = [];
63 if ( strlen( $ip ) > 0 ) {
64 $parts = explode( '.', $ip );
65 }
66
67 if ( count( $parts ) > 0 ) {
68 $parts[ count( $parts ) - 1 ] = 0;
69 }
70
71 return implode( '.', $parts );
72 }
73
74 public function clean_redirection_logs() {
75 $flusher = new Red_Flusher();
76 $flusher->flush();
77 }
78
79 // From the distant Redirection past. Undecided whether to keep
80 public function replace_special_tags( $url ) {
81 if ( is_numeric( $url ) ) {
82 $url = get_permalink( $url );
83 } else {
84 $user = wp_get_current_user();
85
86 if ( ! empty( $user ) ) {
87 $url = str_replace( '%userid%', $user->ID, $url );
88 $url = str_replace( '%userlogin%', isset( $user->user_login ) ? $user->user_login : '', $url );
89 $url = str_replace( '%userurl%', isset( $user->user_url ) ? $user->user_url : '', $url );
90 }
91 }
92
93 return $url;
94 }
95
96 // Used for unit tests
97 public function get_module() {
98 return $this->module;
99 }
100 }
101
102 add_action( 'plugins_loaded', array( 'Redirection', 'init' ) );
103