| 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 |
|
| 17 |
$result = array( |
| 18 |
array_merge( array( 'id' => 'db', 'name' => __( 'Database tables', 'redirection' ) ), $db->get_status() ), |
| 19 |
array( |
| 20 |
'name' => __( 'Valid groups', 'redirection' ), |
| 21 |
'id' => 'groups', |
| 22 |
'message' => $groups === 0 ? __( 'No valid groups, so you will not be able to create any redirects', 'redirection' ) : __( 'Valid groups detected', 'redirection' ), |
| 23 |
'status' => $groups === 0 ? 'problem' : 'good', |
| 24 |
), |
| 25 |
array( |
| 26 |
'name' => __( 'Valid redirect group', 'redirection' ), |
| 27 |
'id' => 'redirect_groups', |
| 28 |
'message' => count( $bad_group ) > 0 ? __( 'Redirects with invalid groups detected', 'redirection' ) : __( 'All redirects have a valid group', 'redirection' ), |
| 29 |
'status' => count( $bad_group ) > 0 ? 'problem' : 'good', |
| 30 |
), |
| 31 |
array( |
| 32 |
'name' => __( 'Post monitor group', 'redirection' ), |
| 33 |
'id' => 'monitor', |
| 34 |
'message' => $valid_monitor === false ? __( 'Post monitor group is invalid', 'redirection' ) : __( 'Post monitor group is valid' ), |
| 35 |
'status' => $valid_monitor === false ? 'problem' : 'good', |
| 36 |
) |
| 37 |
); |
| 38 |
|
| 39 |
return $result; |
| 40 |
} |
| 41 |
|
| 42 |
public function fix( $status ) { |
| 43 |
foreach ( $status as $item ) { |
| 44 |
if ( $item['status'] !== 'good' ) { |
| 45 |
$fixer = 'fix_'.$item['id']; |
| 46 |
$result = $this->$fixer(); |
| 47 |
|
| 48 |
if ( is_wp_error( $result ) ) { |
| 49 |
return $result; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return $this->get_status(); |
| 55 |
} |
| 56 |
|
| 57 |
private function get_missing() { |
| 58 |
global $wpdb; |
| 59 |
|
| 60 |
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" ); |
| 61 |
} |
| 62 |
|
| 63 |
private function fix_db() { |
| 64 |
$db = new RE_Database(); |
| 65 |
|
| 66 |
try { |
| 67 |
$db->create_tables(); |
| 68 |
} catch ( Exception $e ) { |
| 69 |
return new WP_Error( __( 'Failed to fix database tables', 'redirection' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
private function fix_groups() { |
| 76 |
if ( Red_Group::create( 'new group', 1 ) === false ) { |
| 77 |
return new WP_Error( __( 'Unable to create group', 'redirection' ) ); |
| 78 |
} |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
private function fix_redirect_groups() { |
| 84 |
global $wpdb; |
| 85 |
|
| 86 |
$missing = $this->get_missing(); |
| 87 |
|
| 88 |
foreach ( $missing as $row ) { |
| 89 |
$wpdb->update( $wpdb->prefix.'redirection_items', array( 'group_id' => $this->get_valid_group() ), array( 'id' => $row->id ) ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
private function fix_monitor() { |
| 94 |
red_set_options( array( 'monitor_post' => $this->get_valid_group() ) ); |
| 95 |
} |
| 96 |
|
| 97 |
private function get_valid_group() { |
| 98 |
$groups = Red_Group::get_all(); |
| 99 |
|
| 100 |
return $groups[ 0 ]['id']; |
| 101 |
} |
| 102 |
} |
| 103 |
|