Example #1
0
function M_repair_tables()
{
    global $wpdb;
    $tables = M_build_database_structure();
    foreach ($tables as $name => $fields) {
        echo "<p>" . __('Checking table : ', 'membership') . $name . " - ";
        $sql = "SHOW TABLES LIKE '{$name}';";
        $t = $wpdb->get_var($sql);
        if ($t == $name) {
            echo "<span style='color: green;'>" . __('Ok', 'membership') . "</span>";
            echo "</p>";
            echo "<p>" . __('Checking fields in table : ', 'membership') . $name . " - ";
            $sql = "SHOW COLUMNS FROM {$name};";
            $t = $wpdb->get_results($sql);
            $pfield = '';
            foreach ($fields as $fieldname => $type) {
                $found = false;
                echo "<br/>" . $fieldname . " - ";
                foreach ($t as $dbf) {
                    //print_r($dbf);
                    if ($dbf->Field == $fieldname && $dbf->Type == $type) {
                        $found = true;
                        break;
                    }
                }
                if ($found) {
                    echo "<span style='color: green;'>" . __('Ok', 'membership') . "</span>";
                } else {
                    M_add_possible_missing_fields($name, $fieldname, $type, $pfield);
                    echo "<span style='color: red;'>" . __('Fixed', 'membership') . "</span>";
                }
                $pfield = $fieldname;
            }
            echo "</p>";
        } else {
            M_Create_single_table($name);
            echo "<span style='color: red;'>" . __('Fixed', 'membership') . "</span>";
            echo "</p>";
        }
    }
}
Example #2
0
function M_repair_tables($print = true)
{
    global $wpdb;
    $tables = M_build_database_structure();
    $html = '';
    foreach ($tables as $name => $fields) {
        $html .= "<p>" . __('Checking table : ', 'membership') . $name . " - ";
        $sql = "SHOW TABLES LIKE '{$name}';";
        $t = $wpdb->get_var($sql);
        if ($t == $name) {
            $html .= "<span style='color: green;'>" . __('Ok', 'membership') . "</span>";
            $html .= "</p>";
            $html .= "<p>" . __('Checking fields in table : ', 'membership') . $name . " - ";
            $sql = "SHOW COLUMNS FROM {$name};";
            $t = $wpdb->get_results($sql);
            $pfield = '';
            foreach ($fields as $fieldname => $type) {
                $found = false;
                $incorrect_type = false;
                $html .= "<br/>" . $fieldname . " - ";
                foreach ($t as $dbf) {
                    //print_r($dbf);
                    if ($dbf->Field == $fieldname && $dbf->Type == $type) {
                        $found = true;
                        break;
                    }
                    //Column Field found, but with incorrect data type
                    if ($dbf->Field == $fieldname && $dbf->Type != $type) {
                        $incorrect_type = true;
                        break;
                    }
                }
                if ($found) {
                    $html .= "<span style='color: green;'>" . __('Ok', 'membership') . "</span>";
                } else {
                    if ($incorrect_type) {
                        M_repair_field_type($name, $fieldname, $type);
                    } else {
                        M_add_possible_missing_fields($name, $fieldname, $type, $pfield);
                    }
                    $html .= "<span style='color: red;'>" . __('Fixed', 'membership') . "</span>";
                }
                $pfield = $fieldname;
            }
            $html .= "</p>";
        } else {
            M_Create_single_table($name);
            $html .= "<span style='color: red;'>" . __('Fixed', 'membership') . "</span>";
            $html .= "</p>";
        }
    }
    // $html .= "<p>" . __('Cleaning up empty subscription/level relationships : ', 'membership');
    // $wpdb->delete( MEMBERSHIP_TABLE_RELATIONS, array( 'sub_id' => 0 ), array( '%d' ) );
    // $wpdb->delete( MEMBERSHIP_TABLE_RELATIONS, array( 'level_id' => 0 ), array( '%d' ) );
    // $html .= "<span style='color: green;'>" . __('Ok', 'membership') . "</span></p>";
    if ($print) {
        echo $html;
    }
}