| 1244 | * Handle SQL/DB error. |
| 1245 | * |
| 1246 | * This function adds the possibility to react to specific database errors |
| 1247 | * Initial problem had to do with accidentally removed WP tables (causes: bad backup, user fault, etc) |
| 1248 | * This allows for a check to verify all core tables are in place |
| 1249 | * |
| 1250 | * @since 4.2.0 |
| 1251 | * @access public |
| 1252 | * |
| 1253 | */ |
| 1254 | public function handle_error() { |
| 1255 | if ( $this->suppress_errors || 0 == $this->last_error_no ) { |
| 1256 | return; |
| 1257 | } |
| 1258 | |
| 1259 | // Make sure the error is logged and optionally printed. |
| 1260 | $this->print_error(); |
| 1261 | |
| 1262 | |
| 1263 | /* |
| 1264 | * Check for missing tables; only once per execution. |
| 1265 | * If this passes, some custom table is missing and we can't really do anything about that. |
| 1266 | */ |
| 1267 | static $wp_tables_verified = false; |
| 1268 | |
| 1269 | /** |
| 1270 | * For all MySQL Error codes see: |
| 1271 | * http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html |
| 1272 | * |
| 1273 | * Implemented |
| 1274 | * Error: 1146 SQLSTATE: 42S02 (ER_NO_SUCH_TABLE) |
| 1275 | * |
| 1276 | */ |
| 1277 | if ( false === $wp_tables_verified && 1146 == $this->last_error_no ) { |
| 1278 | $suppress = $this->suppress_errors(); |
| 1279 | |
| 1280 | /* |
| 1281 | * Loop over the WP tables. |
| 1282 | * If one or more are missing, suggest table repair. |
| 1283 | * |
| 1284 | * Because we got this far, the global is_blog_installed() passed, so options table should be there. |
| 1285 | */ |
| 1286 | $wp_tables = $this->tables(); |
| 1287 | $rebuild_tables = false; |
| 1288 | |
| 1289 | foreach ( $wp_tables as $table ) { |
| 1290 | // The existence of custom user tables shouldn't suggest an insane state or prevent a clean install. |
| 1291 | if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table ) |
| 1292 | continue; |
| 1293 | if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE == $table ) |
| 1294 | continue; |
| 1295 | |
| 1296 | // If we have results, the table exists |
| 1297 | $sql_check = $this->prepare( "SHOW TABLES LIKE %s", $table ); |
| 1298 | $table_exists = ( $this->get_var( $sql_check ) != null ); |
| 1299 | if ( ! $table_exists ) { |
| 1300 | // There is a missing table we should be able to fix |
| 1301 | $rebuild_tables = true; |
| 1302 | // We're done here. |
| 1303 | break; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | // Recreate missing tables. |
| 1308 | if ( $rebuild_tables ) { |
| 1309 | // Get dependencies to be able to execute dbDelta |
| 1310 | require( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 1311 | |
| 1312 | // Make sure we have the latest (and all) tables |
| 1313 | dbDelta(); |
| 1314 | } |
| 1315 | |
| 1316 | $this->suppress_errors( $suppress ); |
| 1317 | |
| 1318 | $wp_tables_verified = true; |
| 1319 | } |
| 1320 | |
| 1321 | } |
| 1322 | |
| 1323 | /** |