PluginProbe
Redirection / 3.1
Redirection v3.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 / models / fixer.php

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

215 lines 6.4 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 ).'/models/database.php';
4
5 class Red_Fixer {
6 public function get_status() {
7 global $wpdb;
8
9 $options = red_get_options();
10
11 $db = new RE_Database();
12 $groups = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ), 10 );
13 $bad_group = $this->get_missing();
14 $monitor_group = $options['monitor_post'];
15 $valid_monitor = Red_Group::get( $monitor_group ) || $monitor_group === 0;
16 $rest_status = $this->get_rest_status();
17
18 $result = array(
19 $rest_status,
20 $this->get_rest_route_status( $rest_status ),
21 array_merge( array( 'id' => 'db', 'name' => __( 'Database tables', 'redirection' ) ), $db->get_status() ),
22 array(
23 'name' => __( 'Valid groups', 'redirection' ),
24 'id' => 'groups',
25 'message' => $groups === 0 ? __( 'No valid groups, so you will not be able to create any redirects', 'redirection' ) : __( 'Valid groups detected', 'redirection' ),
26 'status' => $groups === 0 ? 'problem' : 'good',
27 ),
28 array(
29 'name' => __( 'Valid redirect group', 'redirection' ),
30 'id' => 'redirect_groups',
31 'message' => count( $bad_group ) > 0 ? __( 'Redirects with invalid groups detected', 'redirection' ) : __( 'All redirects have a valid group', 'redirection' ),
32 'status' => count( $bad_group ) > 0 ? 'problem' : 'good',
33 ),
34 array(
35 'name' => __( 'Post monitor group', 'redirection' ),
36 'id' => 'monitor',
37 'message' => $valid_monitor === false ? __( 'Post monitor group is invalid', 'redirection' ) : __( 'Post monitor group is valid' ),
38 'status' => $valid_monitor === false ? 'problem' : 'good',
39 ),
40 );
41
42 return $result;
43 }
44
45 private function get_rest_route_status( $status ) {
46 $result = array(
47 'name' => __( 'Redirection routes', 'redirection' ),
48 'id' => 'routes',
49 'status' => 'problem',
50 );
51
52 if ( $status['status'] === 'good' ) {
53 $rest_api = red_get_rest_api();
54
55 $result['message'] = __( 'Redirection does not appear in your REST API routes. Have you disabled it with a plugin?', 'redirection' );
56
57 if ( strpos( $rest_api, 'admin-ajax.php' ) !== false ) {
58 $result['message'] = __( 'Redirection routes are working', 'redirection' );
59 $result['status'] = 'good';
60 } else {
61 $response = wp_remote_get( $rest_api, array( 'cookies' => $_COOKIE ) );
62
63 if ( $response && is_array( $response ) && isset( $response['body'] ) ) {
64 $json = @json_decode( $response['body'], true );
65
66 if ( isset( $json['routes']['/redirection/v1'] ) ) {
67 $result['message'] = __( 'Redirection routes are working', 'redirection' );
68 $result['status'] = 'good';
69 }
70 }
71 }
72 } else {
73 $result['message'] = __( 'REST API is not working so routes not checked', 'redirection' );
74 }
75
76 return $result;
77 }
78
79 public function get_rest_status() {
80 $status = array(
81 'name' => __( 'WordPress REST API', 'redirection' ),
82 'id' => 'rest',
83 'status' => 'good',
84 'message' => sprintf( __( 'WordPress REST API is working at %s', 'redirection' ), red_get_rest_api() ),
85 );
86
87 $result = $this->check_api( red_get_rest_api() );
88
89 if ( is_wp_error( $result ) ) {
90 $status['status'] = 'problem';
91 $status['message'] = $result->get_error_message();
92 }
93
94 return $status;
95 }
96
97 public function fix( $status ) {
98 foreach ( $status as $item ) {
99 if ( $item['status'] !== 'good' ) {
100 $fixer = 'fix_'.$item['id'];
101 $result = $this->$fixer();
102
103 if ( is_wp_error( $result ) ) {
104 return $result;
105 }
106 }
107 }
108
109 return $this->get_status();
110 }
111
112 private function get_missing() {
113 global $wpdb;
114
115 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" );
116 }
117
118 public function fix_routes() {
119 }
120
121 public function fix_rest() {
122 // First check the default REST API
123 $result = $this->check_api( get_rest_url() );
124
125 if ( is_wp_error( $result ) ) {
126 // Try directly at index.php?rest_route
127 $rest_api = home_url( '/index.php?rest_route=/' );
128 $result = $this->check_api( $rest_api );
129
130 if ( is_wp_error( $result ) ) {
131 $rest_api = admin_url( 'admin-ajax.php' );
132 $response = wp_remote_get( $rest_api );
133
134 if ( is_array( $response ) && isset( $response['body'] ) && $response['body'] === '0' ) {
135 red_set_options( array( 'rest_api' => 2 ) );
136 return true;
137 }
138
139 red_set_options( array( 'rest_api' => 0 ) );
140 return false;
141 }
142
143 // It worked! Save the URL
144 red_set_options( array( 'rest_api' => 1 ) );
145 return true;
146 }
147
148 // Working
149 red_set_options( array( 'rest_api' => 0 ) );
150 return true;
151 }
152
153 private function check_api( $url ) {
154 $response = wp_remote_get( $url, array( 'cookies' => $_COOKIE ) );
155 $http_code = wp_remote_retrieve_response_code( $response );
156
157 $specific = 'REST API returns an error code';
158 if ( $http_code === 200 ) {
159 $json = @json_decode( $response['body'], true );
160
161 if ( $json || $response['body'] === '0' ) {
162 return true;
163 } else {
164 $specific = 'REST API returned invalid JSON data. This is probably an error page of some kind and indicates it has been disabled';
165 }
166 } elseif ( $http_code === 301 || $http_code === 302 ) {
167 $specific = 'REST API is being redirected. This indicates it has been disabled.';
168 } elseif ( $http_code === 404 ) {
169 $specific = 'REST API is returning 404 error. This indicates it has been disabled.';
170 }
171
172 return new WP_Error( 'redirection', $specific.' ('.( $http_code ? $http_code : '40x' ) .' - '.$url.')' );
173 }
174
175 private function fix_db() {
176 $db = new RE_Database();
177
178 try {
179 $db->create_tables();
180 } catch ( Exception $e ) {
181 return new WP_Error( __( 'Failed to fix database tables', 'redirection' ) );
182 }
183
184 return true;
185 }
186
187 private function fix_groups() {
188 if ( Red_Group::create( 'new group', 1 ) === false ) {
189 return new WP_Error( __( 'Unable to create group', 'redirection' ) );
190 }
191
192 return true;
193 }
194
195 private function fix_redirect_groups() {
196 global $wpdb;
197
198 $missing = $this->get_missing();
199
200 foreach ( $missing as $row ) {
201 $wpdb->update( $wpdb->prefix.'redirection_items', array( 'group_id' => $this->get_valid_group() ), array( 'id' => $row->id ) );
202 }
203 }
204
205 private function fix_monitor() {
206 red_set_options( array( 'monitor_post' => $this->get_valid_group() ) );
207 }
208
209 private function get_valid_group() {
210 $groups = Red_Group::get_all();
211
212 return $groups[ 0 ]['id'];
213 }
214 }
215