| 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 = $this->normalize_url( red_get_rest_api().'redirection/v1/' ); |
| 54 |
$rest_api = add_query_arg( '_wpnonce', wp_create_nonce( 'wp_rest' ), $rest_api ); |
| 55 |
|
| 56 |
$result['message'] = __( 'Redirection does not appear in your REST API routes. Have you disabled it with a plugin?', 'redirection' ); |
| 57 |
|
| 58 |
if ( strpos( $rest_api, 'admin-ajax.php' ) !== false ) { |
| 59 |
$result['message'] = __( 'Redirection routes are working', 'redirection' ); |
| 60 |
$result['status'] = 'good'; |
| 61 |
} else { |
| 62 |
$response = wp_remote_get( $rest_api, array( 'cookies' => $_COOKIE, 'redirection' => 0 ) ); |
| 63 |
|
| 64 |
if ( $response && is_array( $response ) && isset( $response['body'] ) ) { |
| 65 |
$json = @json_decode( $response['body'], true ); |
| 66 |
|
| 67 |
if ( isset( $json['routes']['/redirection/v1'] ) ) { |
| 68 |
$result['message'] = __( 'Redirection routes are working', 'redirection' ); |
| 69 |
$result['status'] = 'good'; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} else { |
| 74 |
$result['message'] = __( 'REST API is not working so routes not checked', 'redirection' ); |
| 75 |
} |
| 76 |
|
| 77 |
return $result; |
| 78 |
} |
| 79 |
|
| 80 |
public function get_rest_status() { |
| 81 |
$status = array( |
| 82 |
'name' => __( 'WordPress REST API', 'redirection' ), |
| 83 |
'id' => 'rest', |
| 84 |
'status' => 'good', |
| 85 |
'message' => sprintf( __( 'WordPress REST API is working at %s', 'redirection' ), red_get_rest_api() ), |
| 86 |
); |
| 87 |
|
| 88 |
$result = $this->check_api( red_get_rest_api() ); |
| 89 |
|
| 90 |
if ( is_wp_error( $result ) ) { |
| 91 |
$status['status'] = 'problem'; |
| 92 |
$status['message'] = $result->get_error_message(); |
| 93 |
} |
| 94 |
|
| 95 |
return $status; |
| 96 |
} |
| 97 |
|
| 98 |
public function fix( $status ) { |
| 99 |
foreach ( $status as $item ) { |
| 100 |
if ( $item['status'] !== 'good' ) { |
| 101 |
$fixer = 'fix_'.$item['id']; |
| 102 |
$result = $this->$fixer(); |
| 103 |
|
| 104 |
if ( is_wp_error( $result ) ) { |
| 105 |
return $result; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $this->get_status(); |
| 111 |
} |
| 112 |
|
| 113 |
private function get_missing() { |
| 114 |
global $wpdb; |
| 115 |
|
| 116 |
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" ); |
| 117 |
} |
| 118 |
|
| 119 |
public function fix_routes() { |
| 120 |
} |
| 121 |
|
| 122 |
public function fix_rest() { |
| 123 |
// First check the default REST API |
| 124 |
$result = $this->check_api( get_rest_url() ); |
| 125 |
|
| 126 |
if ( is_wp_error( $result ) ) { |
| 127 |
// Try directly at index.php?rest_route |
| 128 |
$rest_api = home_url( '/index.php?rest_route=/' ); |
| 129 |
$result = $this->check_api( $rest_api ); |
| 130 |
|
| 131 |
if ( is_wp_error( $result ) ) { |
| 132 |
$rest_api = admin_url( 'admin-ajax.php' ); |
| 133 |
$response = wp_remote_get( $rest_api ); |
| 134 |
|
| 135 |
if ( is_array( $response ) && isset( $response['body'] ) && $response['body'] === '0' ) { |
| 136 |
red_set_options( array( 'rest_api' => 2 ) ); |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
red_set_options( array( 'rest_api' => 0 ) ); |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
// It worked! Save the URL |
| 145 |
red_set_options( array( 'rest_api' => 1 ) ); |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
// Working |
| 150 |
red_set_options( array( 'rest_api' => 0 ) ); |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
private function normalize_url( $url ) { |
| 155 |
if ( substr( $url, 0, 4 ) !== 'http' ) { |
| 156 |
$parts = parse_url( get_site_url() ); |
| 157 |
$url = ( isset( $parts['scheme'] ) ? $parts['scheme'] : 'http' ).'://'.$parts['host'].$url; |
| 158 |
} |
| 159 |
|
| 160 |
return $url; |
| 161 |
} |
| 162 |
|
| 163 |
private function check_api( $url ) { |
| 164 |
$url = $this->normalize_url( $url.'redirection/v1/' ); |
| 165 |
$request_url = add_query_arg( '_wpnonce', wp_create_nonce( 'wp_rest' ), $url ); |
| 166 |
|
| 167 |
$response = wp_remote_get( $request_url, array( 'cookies' => $_COOKIE, 'redirection' => 0 ) ); |
| 168 |
$http_code = wp_remote_retrieve_response_code( $response ); |
| 169 |
|
| 170 |
$specific = 'REST API returns an error code'; |
| 171 |
if ( $http_code === 200 ) { |
| 172 |
$json = @json_decode( $response['body'], true ); |
| 173 |
|
| 174 |
if ( $json || $response['body'] === '0' ) { |
| 175 |
return true; |
| 176 |
} else { |
| 177 |
$specific = 'REST API returned invalid JSON data. This is probably an error page of some kind and indicates it has been disabled'; |
| 178 |
} |
| 179 |
} elseif ( $http_code === 301 || $http_code === 302 ) { |
| 180 |
$specific = 'REST API is being redirected. This indicates it has been disabled or you have a trailing slash redirect.'; |
| 181 |
} elseif ( $http_code === 404 ) { |
| 182 |
$specific = 'REST API is returning 404 error. This indicates it has been disabled.'; |
| 183 |
} |
| 184 |
|
| 185 |
return new WP_Error( 'redirection', $specific.' ('.( $http_code ? $http_code : '40x' ) .' - '.$url.')' ); |
| 186 |
} |
| 187 |
|
| 188 |
private function fix_db() { |
| 189 |
$db = new RE_Database(); |
| 190 |
|
| 191 |
try { |
| 192 |
$db->create_tables(); |
| 193 |
} catch ( Exception $e ) { |
| 194 |
return new WP_Error( __( 'Failed to fix database tables', 'redirection' ) ); |
| 195 |
} |
| 196 |
|
| 197 |
return true; |
| 198 |
} |
| 199 |
|
| 200 |
private function fix_groups() { |
| 201 |
if ( Red_Group::create( 'new group', 1 ) === false ) { |
| 202 |
return new WP_Error( __( 'Unable to create group', 'redirection' ) ); |
| 203 |
} |
| 204 |
|
| 205 |
return true; |
| 206 |
} |
| 207 |
|
| 208 |
private function fix_redirect_groups() { |
| 209 |
global $wpdb; |
| 210 |
|
| 211 |
$missing = $this->get_missing(); |
| 212 |
|
| 213 |
foreach ( $missing as $row ) { |
| 214 |
$wpdb->update( $wpdb->prefix.'redirection_items', array( 'group_id' => $this->get_valid_group() ), array( 'id' => $row->id ) ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
private function fix_monitor() { |
| 219 |
red_set_options( array( 'monitor_post' => $this->get_valid_group() ) ); |
| 220 |
} |
| 221 |
|
| 222 |
private function get_valid_group() { |
| 223 |
$groups = Red_Group::get_all(); |
| 224 |
|
| 225 |
return $groups[ 0 ]['id']; |
| 226 |
} |
| 227 |
} |
| 228 |
|