PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.4
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.4
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / WPDataAccess / Utilities / WPDA_Restore_Repository.php

WPDA_Restore_Repository.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.4, at WPDataAccess/Utilities/WPDA_Restore_Repository.php

106 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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