PluginProbe
Redirection / 5.3.7
Redirection v5.3.7
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 / models / fixer.php

fixer.php in Redirection 5.3.7, at models/fixer.php

184 lines 5.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 dirname( REDIRECTION_FILE ) . '/database/database.php';
4
5 class Red_Fixer {
6 const REGEX_LIMIT = 200;
7
8 public function get_json() {
9 return [
10 'status' => $this->get_status(),
11 'debug' => $this->get_debug(),
12 ];
13 }
14
15 public function get_debug() {
16 $status = new Red_Database_Status();
17 $ip = [];
18
19 foreach ( Redirection_Request::get_ip_headers() as $var ) {
20 $ip[ $var ] = isset( $_SERVER[ $var ] ) ? sanitize_text_field( $_SERVER[ $var ] ) : false;
21 }
22
23 return [
24 'database' => [
25 'current' => $status->get_current_version(),
26 'latest' => REDIRECTION_DB_VERSION,
27 ],
28 'ip_header' => $ip,
29 ];
30 }
31
32 public function save_debug( $name, $value ) {
33 if ( $name === 'database' ) {
34 $database = new Red_Database();
35 $status = new Red_Database_Status();
36
37 foreach ( $database->get_upgrades() as $upgrade ) {
38 if ( $value === $upgrade['version'] ) {
39 $status->finish();
40 $status->save_db_version( $value );
41
42 // Switch to prompt mode
43 red_set_options( [ 'plugin_update' => 'prompt' ] );
44 break;
45 }
46 }
47 }
48 }
49
50 public function get_status() {
51 global $wpdb;
52
53 $options = red_get_options();
54
55 $groups = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ), 10 );
56 $bad_group = $this->get_missing();
57 $monitor_group = $options['monitor_post'];
58 $valid_monitor = Red_Group::get( $monitor_group ) || $monitor_group === 0;
59
60 $status = [
61 array_merge( [
62 'id' => 'db',
63 'name' => __( 'Database tables', 'redirection' ),
64 ], $this->get_database_status( Red_Database::get_latest_database() ) ),
65 [
66 'name' => __( 'Valid groups', 'redirection' ),
67 'id' => 'groups',
68 'message' => $groups === 0 ? __( 'No valid groups, so you will not be able to create any redirects', 'redirection' ) : __( 'Valid groups detected', 'redirection' ),
69 'status' => $groups === 0 ? 'problem' : 'good',
70 ],
71 [
72 'name' => __( 'Valid redirect group', 'redirection' ),
73 'id' => 'redirect_groups',
74 'message' => count( $bad_group ) > 0 ? __( 'Redirects with invalid groups detected', 'redirection' ) : __( 'All redirects have a valid group', 'redirection' ),
75 'status' => count( $bad_group ) > 0 ? 'problem' : 'good',
76 ],
77 [
78 'name' => __( 'Post monitor group', 'redirection' ),
79 'id' => 'monitor',
80 'message' => $valid_monitor === false ? __( 'Post monitor group is invalid', 'redirection' ) : __( 'Post monitor group is valid', 'redirection' ),
81 'status' => $valid_monitor === false ? 'problem' : 'good',
82 ],
83 $this->get_http_settings(),
84 ];
85
86 $regex_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items WHERE regex=1" );
87 if ( $regex_count > self::REGEX_LIMIT ) {
88 $status[] = [
89 'name' => __( 'Regular Expressions', 'redirection' ),
90 'id' => 'regex',
91 'message' => __( 'Too many regular expressions may impact site performance', 'redirection' ),
92 'status' => 'problem',
93 ];
94 }
95
96 return $status;
97 }
98
99 private function get_database_status( $database ) {
100 $missing = $database->get_missing_tables();
101
102 return array(
103 'status' => count( $missing ) === 0 ? 'good' : 'error',
104 'message' => count( $missing ) === 0 ? __( 'All tables present', 'redirection' ) : __( 'The following tables are missing:', 'redirection' ) . ' ' . join( ',', $missing ),
105 );
106 }
107
108 private function get_http_settings() {
109 $site = wp_parse_url( get_site_url(), PHP_URL_SCHEME );
110 $home = wp_parse_url( get_home_url(), PHP_URL_SCHEME );
111
112 $message = __( 'Site and home are consistent', 'redirection' );
113 if ( $site !== $home ) {
114 /* translators: 1: Site URL, 2: Home URL */
115 $message = sprintf( __( 'Site and home URL are inconsistent. Please correct from your Settings > General page: %1$1s is not %2$2s', 'redirection' ), get_site_url(), get_home_url() );
116 }
117
118 return array(
119 'name' => __( 'Site and home protocol', 'redirection' ),
120 'id' => 'redirect_url',
121 'message' => $message,
122 'status' => $site === $home ? 'good' : 'problem',
123 );
124 }
125
126 public function fix( $status ) {
127 foreach ( $status as $item ) {
128 if ( $item['status'] !== 'good' ) {
129 $fixer = 'fix_' . $item['id'];
130
131 $result = true;
132 if ( method_exists( $this, $fixer ) ) {
133 $result = $this->$fixer();
134 }
135
136 if ( is_wp_error( $result ) ) {
137 return $result;
138 }
139 }
140 }
141
142 return $this->get_status();
143 }
144
145 private function get_missing() {
146 global $wpdb;
147
148 return $wpdb->get_results( "SELECT {$wpdb->prefix}redirection_items.id FROM {$wpdb->prefix}redirection_items LEFT JOIN {$wpdb->prefix}redirection_groups ON {$wpdb->prefix}redirection_items.group_id = {$wpdb->prefix}redirection_groups.id WHERE {$wpdb->prefix}redirection_groups.id IS NULL" );
149 }
150
151 private function fix_db() {
152 $database = Red_Database::get_latest_database();
153 return $database->install();
154 }
155
156 private function fix_groups() {
157 if ( Red_Group::create( 'new group', 1 ) === false ) {
158 return new WP_Error( 'Unable to create group' );
159 }
160
161 return true;
162 }
163
164 private function fix_redirect_groups() {
165 global $wpdb;
166
167 $missing = $this->get_missing();
168
169 foreach ( $missing as $row ) {
170 $wpdb->update( $wpdb->prefix . 'redirection_items', array( 'group_id' => $this->get_valid_group() ), array( 'id' => $row->id ) );
171 }
172 }
173
174 private function fix_monitor() {
175 red_set_options( array( 'monitor_post' => $this->get_valid_group() ) );
176 }
177
178 private function get_valid_group() {
179 $groups = Red_Group::get_all();
180
181 return $groups[0]['id'];
182 }
183 }
184