| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Utilities { |
| 4 |
|
| 5 |
use WPDataAccess\WPDA; |
| 6 |
|
| 7 |
class WPDA_Restore_Repository { |
| 8 |
|
| 9 |
const BACKUP_TABLE_EXTENSION = '_BACKUP_'; |
| 10 |
|
| 11 |
protected $repository_tables = array(); |
| 12 |
|
| 13 |
public function restore( $restore_date ) { |
| 14 |
global $wpdb; |
| 15 |
$this->repository_tables = array(); |
| 16 |
|
| 17 |
$suppress = $wpdb->suppress_errors( true ); |
| 18 |
foreach ( WPDA_Repository::CREATE_TABLE as $key => $value ) { |
| 19 |
if ( isset( $_REQUEST[ $key ] ) ) { |
| 20 |
if ( 'replace' === $_REQUEST[ $key ] || 'add' === $_REQUEST[ $key ] ) { |
| 21 |
$this->restore_table( |
| 22 |
$wpdb->prefix . $key, |
| 23 |
$wpdb->prefix . $key . self::BACKUP_TABLE_EXTENSION . $restore_date, |
| 24 |
sanitize_text_field( wp_unslash( $_REQUEST[ $key ] ) ) |
| 25 |
); |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
$wpdb->suppress_errors( $suppress ); |
| 30 |
|
| 31 |
return $this->repository_tables; |
| 32 |
} |
| 33 |
|
| 34 |
protected function restore_table( $arg_table_name, $arg_bck_table_name, $action ) { |
| 35 |
global $wpdb; |
| 36 |
|
| 37 |
// Use backticks to prevent SQL injection |
| 38 |
$table_name = WPDA::remove_backticks( $arg_table_name ); |
| 39 |
$bck_table_name = WPDA::remove_backticks( $arg_bck_table_name ); |
| 40 |
|
| 41 |
$same_cols = $wpdb->get_results( |
| 42 |
$wpdb->prepare( |
| 43 |
' |
| 44 |
select c1.column_name as column_name |
| 45 |
from information_schema.columns c1 |
| 46 |
where c1.table_schema = %s |
| 47 |
and c1.table_name = %s |
| 48 |
and c1.column_name in ( |
| 49 |
select c2.column_name |
| 50 |
from information_schema.columns c2 |
| 51 |
where c2.table_schema = %s |
| 52 |
and c2.table_name = %s |
| 53 |
) |
| 54 |
', |
| 55 |
array( |
| 56 |
$wpdb->dbname, |
| 57 |
$table_name, |
| 58 |
$wpdb->dbname, |
| 59 |
$bck_table_name, |
| 60 |
) |
| 61 |
), |
| 62 |
'ARRAY_A' |
| 63 |
); |
| 64 |
|
| 65 |
// Get selected columns |
| 66 |
$selected_columns = ''; |
| 67 |
foreach ( $same_cols as $same_col ) { |
| 68 |
$selected_columns .= $same_col['column_name'] . ','; |
| 69 |
} |
| 70 |
$selected_columns = substr( $selected_columns, 0, strlen( $selected_columns ) - 1 ); |
| 71 |
|
| 72 |
if ( 'replace' === $action ) { |
| 73 |
// Truncate table before restoring backup data |
| 74 |
$wpdb->query( |
| 75 |
$wpdb->prepare( |
| 76 |
'truncate table `%1s`', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 77 |
array( |
| 78 |
WPDA::remove_backticks( $table_name ), |
| 79 |
) |
| 80 |
) |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
// Restore data |
| 85 |
$result = $wpdb->query( |
| 86 |
$wpdb->prepare( |
| 87 |
'insert into `%1s` (%1s) select %1s from `%1s`', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 88 |
array( |
| 89 |
WPDA::remove_backticks( $table_name ), |
| 90 |
$selected_columns, |
| 91 |
$selected_columns, |
| 92 |
WPDA::remove_backticks( $bck_table_name ), |
| 93 |
) |
| 94 |
) |
| 95 |
); |
| 96 |
|
| 97 |
$this->repository_tables[ $table_name ] = array( |
| 98 |
'rows' => $result, |
| 99 |
'errors' => $wpdb->last_error, |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|