PluginProbe
Redirection / 4.2
Redirection v4.2
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 4.2, at models/fixer.php

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