PluginProbe
Redirection / 4.9.2
Redirection v4.9.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.9.2, at models/fixer.php

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