PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.84
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.84
5.5.84 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 All 160 releases
wp-data-access / WPDataAccess / Plugin_Table_Models / WPDA_CSV_Uploads_Model.php

WPDA_CSV_Uploads_Model.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.84, at WPDataAccess/Plugin_Table_Models/WPDA_CSV_Uploads_Model.php

215 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDataAccess\Plugin_Table_Models {
4
5 use WPDataAccess\CSV_Files\WPDA_CSV_Import;
6 use WPDataAccess\WPDA;
7
8 class WPDA_CSV_Uploads_Model extends WPDA_Plugin_Table_Base_Model {
9
10 const BASE_TABLE_NAME = 'wpda_csv_uploads';
11
12 public static function query( $csv_id ) {
13 global $wpdb;
14 return $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table
15 $wpdb->prepare(
16 'select * from `%1s` where csv_id = %d', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders
17 array(
18 WPDA::remove_backticks( static::get_base_table_name() ),
19 $csv_id,
20 )
21 )
22 );
23 }
24
25 public static function insert( $csv_name, $real_file_name, $orig_file_name, $csv_encoding = null ) {
26 global $wpdb;
27 if ( 1 === $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- plugin table
28 static::get_base_table_name(),
29 array(
30 'csv_name' => $csv_name,
31 'csv_real_file_name' => $real_file_name,
32 'csv_orig_file_name' => $orig_file_name,
33 'csv_timestamp' => gmdate( 'Y-m-d H:i:s' ),
34 'csv_encoding' => $csv_encoding,
35 )
36 )
37 ) {
38 return $wpdb->insert_id;
39 } else {
40 return false;
41 }
42 }
43
44 public static function update( $csv_id, $real_file_name, $orig_file_name, $csv_encoding = null ) {
45 global $wpdb;
46 return ( 1 === $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table
47 static::get_base_table_name(),
48 array(
49 'csv_real_file_name' => $real_file_name,
50 'csv_orig_file_name' => $orig_file_name,
51 'csv_timestamp' => gmdate( 'Y-m-d H:i:s' ),
52 'csv_encoding' => $csv_encoding,
53 ),
54 array(
55 'csv_id' => $csv_id,
56 )
57 )
58 );
59 }
60
61 public static function save_mapping() {
62 header( 'Content-Type: text/plain; charset=utf-8' );
63 if (
64 ! isset( $_REQUEST['csv_id'] ) ||
65 ! isset( $_REQUEST['csv_mapping'] ) ||
66 ! isset( $_REQUEST['wpnonce'] )
67 ) {
68 echo 'INV-Wrong arguments';
69 return;
70 }
71
72 $csv_id = sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ); // input var okay.
73
74 // Check if actions is allowed
75 $wp_nonce = isset( $_REQUEST['wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpnonce'] ) ) : ''; // input var okay.
76 if ( ! wp_verify_nonce( $wp_nonce, "wpda-csv-mapping-{$csv_id}" ) ) {
77 echo 'INV-Not authorized';
78 return;
79 }
80
81 $csv_mapping = json_encode(
82 WPDA::sanitize_text_field_array( $_REQUEST['csv_mapping'], ['delimiter'] ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
83 );
84
85 global $wpdb;
86 $wpdb->suppress_errors( true );
87 $rows_update = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table
88 $wpdb->prepare(
89 'update `%1s` set csv_mapping = %s where csv_id = %d', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders
90 array(
91 WPDA::remove_backticks( static::get_base_table_name() ),
92 $csv_mapping,
93 $csv_id,
94 )
95 )
96 );
97
98 echo '' === $wpdb->last_error ? 'UPD-' . esc_attr( $rows_update ) : 'ERR-' . esc_attr( $wpdb->last_error );
99 }
100
101 public static function preview_mapping() {
102 header( 'Content-Type: text/plain; charset=utf-8' );
103 if (
104 ! isset( $_REQUEST['csv_id'] ) ||
105 ! isset( $_REQUEST['page_number'] ) ||
106 ! isset( $_REQUEST['page_length'] ) ||
107 ! isset( $_REQUEST['wpnonce'] )
108 ) {
109 echo 'INV-Wrong arguments';
110 return;
111 }
112
113 $csv_id = sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ); // input var okay.
114
115 // Check if actions is allowed
116 $wp_nonce = isset( $_REQUEST['wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpnonce'] ) ) : ''; // input var okay.
117 if ( ! wp_verify_nonce( $wp_nonce, "wpda-csv-preview-mapping-{$csv_id}" ) ) {
118 echo 'INV-Not authorized';
119 return;
120 }
121
122 $page_number = sanitize_text_field( wp_unslash( $_REQUEST['page_number'] ) ); // input var okay.
123 $page_length = sanitize_text_field( wp_unslash( $_REQUEST['page_length'] ) ); // input var okay.
124
125 $dbrow = self::query( $csv_id );
126
127 global $wpdb;
128 if ( '' !== $wpdb->last_error ) {
129 echo 'ERR-' . esc_attr( $wpdb->last_error );
130 }
131 if ( 1 === $wpdb->num_rows ) {
132 if ( ! isset( $dbrow[0]->csv_real_file_name ) ) {
133 echo 'ERR-No file';
134 } else {
135 $upload_dir = WPDA::get_plugin_upload_dir();
136 $file_name = $upload_dir . $dbrow[0]->csv_real_file_name;
137
138 @ini_set( 'auto_detect_line_endings', true ); // phpcs:ignore
139 if ( false !== ( $fp = fopen( $file_name, 'rb' ) ) ) { // phpcs:ignore
140 $mapping = isset( $dbrow[0]->csv_mapping ) ? json_decode( $dbrow[0]->csv_mapping, true ) : array();
141 $delimiter = isset( $mapping['settings']['delimiter'] ) ? $mapping['settings']['delimiter'] : ',';
142 $has_header_columns = isset( $mapping['settings']['has_header_columns'] ) ? $mapping['settings']['has_header_columns'] : true;
143
144 if ( '\\t' == $delimiter ) {
145 $delimiter = "\t";
146 }
147
148 $start = ( $page_number - 1 ) * $page_length;
149 if ( $start === 0 ) {
150 $end = $page_length;
151 } else {
152 $end = $page_number * $page_length;
153 }
154 $next_page = $page_number + 1;
155 $prev_page = $page_number - 1;
156 if ( $prev_page < 1 ) {
157 $prev_page = 1;
158 }
159
160 echo '<div style="text-align: right; margin-bottom: 10px;">';
161 echo '<a href="javascript:void(0)" class="button" onclick="preview(' . esc_attr( $prev_page ) . ', ' . esc_attr( $page_length ) . ')">&lt;</a>';
162 echo '&nbsp;';
163 echo '<a href="javascript:void(0)" class="button" onclick="preview(' . esc_attr( $next_page ) . ', ' . esc_attr( $page_length ) . ')">&gt;</a>';
164 echo '</div>';
165 echo '<table class="wp-list-table widefat fixed striped rows">';
166
167 $number_of_columns = 1;
168 if ( 'false' !== $has_header_columns ) {
169 echo '<thead>';
170 if ( false !== ( $data = fgetcsv( $fp, 0, $delimiter, '"' ) ) ) {
171 $number_of_columns = count( $data ); // phpcs:ignore -- 8.1 proof
172 echo '<tr>';
173 for ( $column = 0; $column < count( $data ); $column++ ) { // phpcs:ignore -- 8.1 proof
174 echo '<th>' . esc_attr( $data[$column] ) . '</th>';
175 }
176 echo '</tr>';
177 }
178 echo '</thead>';
179 }
180
181 $row = 0;
182 $fnd = false;
183 echo '<tbody>';
184 while ( false !== ( $data = fgetcsv( $fp, 0, $delimiter, '"' ) ) ) {
185 if ( $row >= $start && $row < $end ) {
186 echo '<tr>';
187 for ( $column = 0; $column < count( $data ); $column++ ) { // phpcs:ignore -- 8.1 proof
188 echo '<td>' . esc_attr( $data[$column] ) . '</td>';
189 }
190 echo '</tr>';
191 $fnd = true;
192 }
193
194 $row++;
195 }
196 if ( ! $fnd ) {
197 echo '<tr colspan="' . esc_attr( $number_of_columns ) . '"><td>' . esc_attr__( 'No data found', 'wp-data-access' ) . '</td></tr>';
198 }
199 echo '</tbody>';
200 echo '</table>';
201
202 fclose( $fp ); // phpcs:ignore
203 } else {
204 echo 'ERR-File not found';
205 }
206 }
207 } else {
208 echo 'ERR-No data found';
209 }
210 }
211
212 }
213
214 }
215