PluginProbe
Database Addon For WPForms ( wpforms entries ) – WPFormsDB / trunk
Database Addon For WPForms ( wpforms entries ) – WPFormsDB vtrunk
trunk 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.8.1 1.0.9 1.1.0
database-for-wpforms / inc / class-sub-page.php

class-sub-page.php in Database Addon For WPForms ( wpforms entries ) – WPFormsDB trunk, at inc/class-sub-page.php

464 lines 14.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WPFormsDB Admin subpage
5 */
6
7 if (!defined( 'ABSPATH')) exit;
8
9 /**
10 * WPFormsDB_Wp_List_Table class will create the page to load the table
11 */
12 class WPFormsDB_Wp_Sub_Page
13 {
14 private $form_post_id;
15
16 /**
17 * Constructor start subpage
18 */
19 public function __construct()
20 {
21 $this->form_post_id = (int) $_GET['fid'];
22 $this->list_table_page();
23
24 }
25 /**
26 * Display the list table page
27 *
28 * @return Void
29 */
30 public function list_table_page()
31 {
32 $ListTable = new WPFormsDB_List_Table();
33 $ListTable->prepare_items();
34 ?>
35 <div class="wrap">
36 <div id="icon-users" class="icon32"></div>
37 <h2><?php echo esc_html( get_the_title( $this->form_post_id ) ); ?></h2>
38 <form method="post" action="">
39
40 <?php $ListTable->search_box('Search', 'search'); ?>
41 <?php $ListTable->display(); ?>
42 </form>
43 </div>
44 <?php
45 }
46
47 }
48 // WP_List_Table is not loaded automatically so we need to load it in our application
49 if( ! class_exists( 'WP_List_Table' ) ) {
50 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
51 }
52 /**
53 * Create a new table class that will extend the WP_List_Table
54 */
55 class WPFormsDB_List_Table extends WP_List_Table
56 {
57 private $form_post_id;
58 private $column_titles;
59 private $search;
60
61 public function __construct() {
62
63 parent::__construct(
64 array(
65 'singular' => 'contact_form',
66 'plural' => 'contact_forms',
67 'ajax' => false
68 )
69 );
70
71 }
72
73 /**
74 * Prepare the items for the table to process
75 *
76 * @return Void
77 */
78 public function prepare_items()
79 {
80
81 $this->form_post_id = (int) $_GET['fid'];
82 $search = empty( $_REQUEST['s'] ) ? false : esc_sql( $_POST['s'] );
83 echo esc_html( $this->search );
84 $form_post_id = $this->form_post_id;
85
86 global $wpdb;
87
88 $this->process_bulk_action();
89
90 $cfdb = apply_filters( 'WPFormsDB_database', $wpdb );
91 $table_name = $cfdb->prefix.'wpforms_db';
92 $columns = $this->get_columns();
93 $hidden = $this->get_hidden_columns();
94 $sortable = $this->get_sortable_columns();
95 $data = $this->table_data();
96
97 //usort( $data, array( &$this, 'sort_data' ) );
98
99 $perPage = 100;
100 $currentPage = $this->get_pagenum();
101 if ( ! empty($search) ) {
102
103 $totalItems = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_value LIKE '%$search%' AND form_post_id = '$form_post_id' ");
104 }else{
105
106 $totalItems = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_post_id = '$form_post_id'");
107 }
108
109 $this->set_pagination_args( array(
110 'total_items' => $totalItems,
111 'per_page' => $perPage
112 ) );
113 $this->_column_headers = array($columns, $hidden ,$sortable);
114 $this->items = $data;
115 }
116 /**
117 * Override the parent columns method. Defines the columns to use in your listing table
118 *
119 * @return Array
120 */
121 public function get_columns()
122 {
123 $form_post_id = $this->form_post_id;
124
125 global $wpdb;
126 $cfdb = apply_filters( 'WPFormsDB_database', $wpdb );
127 $table_name = $cfdb->prefix.'wpforms_db';
128
129 $results = $cfdb->get_results( "SELECT * FROM $table_name
130 WHERE form_post_id = $form_post_id ORDER BY form_id DESC LIMIT 1", OBJECT );
131
132 $first_row = isset($results[0]) ? unserialize( $results[0]->form_value ): 0 ;
133 $columns = array();
134
135 if( !empty($first_row) ){
136 //$columns['form_id'] = $results[0]->form_id;
137 $columns['cb'] = '<input type="checkbox" />';
138 foreach ($first_row as $key => $value) {
139
140 if ( $key == 'WPFormsDB_status' ) continue;
141
142 $key_val = str_replace( array('your-', 'WPFormsDB_file'), '', $key);
143 $columns[$key] = ucfirst( $key_val );
144
145 $this->column_titles[] = $key_val;
146
147 if ( count($columns) > 4) break;
148 }
149 $columns['form-date'] = 'Date';
150 }
151
152
153 return $columns;
154 }
155 /**
156 * Define check box for bulk action (each row)
157 * @param $item
158 * @return checkbox
159 */
160 public function column_cb($item){
161 return sprintf(
162 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
163 $this->_args['singular'],
164 $item['form_id']
165 );
166 }
167 /**
168 * Define which columns are hidden
169 *
170 * @return Array
171 */
172 public function get_hidden_columns()
173 {
174 return array('form_id');
175 }
176 /**
177 * Define the sortable columns
178 *
179 * @return Array
180 */
181 public function get_sortable_columns()
182 {
183 return array('form-date' => array('form-date', true));
184 }
185 /**
186 * Define bulk action
187 * @return Array
188 */
189 public function get_bulk_actions() {
190
191 return array(
192 'read' => __( 'Read', 'database-for-wpforms' ),
193 'unread' => __( 'Unread', 'database-for-wpforms' ),
194 'delete' => __( 'Delete', 'database-for-wpforms' )
195 );
196
197 }
198 /**
199 * Get the table data
200 *
201 * @return Array
202 */
203 private function table_data()
204 {
205 $data = array();
206 global $wpdb;
207 $cfdb = apply_filters( 'WPFormsDB_database', $wpdb );
208 $search = empty( $_REQUEST['s'] ) ? false : esc_sql( $_POST['s'] );
209 $table_name = $cfdb->prefix.'wpforms_db';
210 $page = $this->get_pagenum();
211 $page = $page - 1;
212 $start = $page * 100;
213 $form_post_id = $this->form_post_id;
214
215 $orderby = isset($_GET['orderby']) ? 'form_date' : 'form_id';
216 $order = isset($_GET['order']) && $_GET['order'] == 'asc' ? 'ASC' : 'DESC';
217
218 if ( ! empty($search) ) {
219
220 $results = $cfdb->get_results( "SELECT * FROM $table_name WHERE form_value LIKE '%$search%'
221 AND form_post_id = '$form_post_id'
222 ORDER BY $orderby $order
223 LIMIT $start,100", OBJECT );
224 }else{
225
226 $results = $cfdb->get_results( "SELECT * FROM $table_name WHERE form_post_id = $form_post_id
227 ORDER BY $orderby $order
228 LIMIT $start,100", OBJECT );
229 }
230
231 foreach ( $results as $result ) {
232
233 $form_value = unserialize( $result->form_value );
234
235 $link = "<b><a href=admin.php?page=wp-forms-db-list.php&fid=%s&ufid=%s>%s</a></b>";
236 if(isset($form_value['WPFormsDB_status']) && ( $form_value['WPFormsDB_status'] === 'read' ) )
237 $link = "<a href=admin.php?page=wp-forms-db-list.php&fid=%s&ufid=%s>%s</a>";
238
239
240
241 $fid = $result->form_post_id;
242 $form_values['form_id'] = $result->form_id;
243
244 foreach ( $this->column_titles as $col_title) {
245 $form_value[ $col_title ] = isset( $form_value[ $col_title ] ) ?
246 $form_value[ $col_title ] : '';
247 }
248
249 foreach ($form_value as $k => $value) {
250
251 $ktmp = $k;
252
253 $can_foreach = is_array($value) || is_object($value);
254
255 if ( $can_foreach ) {
256
257 foreach ($value as $k_val => $val):
258 $val = esc_html( $val );
259 $form_values[$ktmp] = ( strlen($val) > 150 ) ? substr($val, 0, 150).'...': $val;
260 $form_values[$ktmp] = sprintf($link, $fid, $result->form_id, $form_values[$ktmp]);
261
262 endforeach;
263 }else{
264 $value = esc_html( $value );
265 $form_values[$ktmp] = ( strlen($value) > 150 ) ? substr($value, 0, 150).'...': $value;
266 $form_values[$ktmp] = sprintf($link, $fid, $result->form_id, $form_values[$ktmp]);
267 }
268
269 }
270 $form_values['form-date'] = sprintf($link, $fid, $result->form_id, $result->form_date );
271 $data[] = $form_values;
272 }
273
274 return $data;
275 }
276 /**
277 * Define bulk action
278 *
279 */
280 public function process_bulk_action(){
281
282 global $wpdb;
283 $cfdb = apply_filters( 'WPFormsDB_database', $wpdb );
284 $table_name = $cfdb->prefix.'wpforms_db';
285 $action = $this->current_action();
286
287 if ( isset( $_POST['_wpnonce'] ) && ! empty( $_POST['_wpnonce'] ) ) {
288
289 $nonce = sanitize_text_field( $_POST['_wpnonce'] );
290 $nonce_action = 'bulk-' . $this->_args['plural'];
291
292 if ( !wp_verify_nonce( $nonce, $nonce_action ) ){
293
294 wp_die( 'Not valid..!!' );
295 }
296 }
297
298 $form_ids = isset( $_POST['contact_form'] ) ? $_POST['contact_form'] : array();
299
300
301 if( 'delete' === $action ) {
302
303 foreach ($form_ids as $form_id):
304
305 $form_id = (int) $form_id;
306 $results = $cfdb->get_results( "SELECT * FROM $table_name WHERE form_id = $form_id LIMIT 1", OBJECT );
307 $result_value = $results[0]->form_value;
308 $result_values = unserialize($result_value);
309 $upload_dir = wp_upload_dir();
310 $WPFormsDB_dirname = $upload_dir['basedir'].'/WPFormsDB_uploads';
311
312 foreach ($result_values as $key => $result) {
313
314 if ( ( strpos($key, 'WPFormsDB_file') !== false ) &&
315 file_exists($WPFormsDB_dirname.'/'.$result) ) {
316
317 unlink($WPFormsDB_dirname.'/'.$result);
318 }
319
320 }
321
322 $cfdb->delete(
323 $table_name ,
324 array( 'form_id' => $form_id ),
325 array( '%d' )
326 );
327 endforeach;
328
329 }else if( 'read' === $action ){
330
331 foreach ($form_ids as $form_id):
332
333 $form_id = (int) $form_id;
334 $results = $cfdb->get_results( "SELECT * FROM $table_name WHERE form_id = '$form_id' LIMIT 1", OBJECT );
335 $result_value = $results[0]->form_value;
336 $result_values = unserialize( $result_value );
337 $result_values['WPFormsDB_status'] = 'read';
338 $form_data = serialize( $result_values );
339
340 $sql = $cfdb->prepare(
341 "UPDATE {$table_name} SET form_value = %s WHERE form_id = %d",
342 $form_data,
343 $form_id
344 );
345 $cfdb->query( $sql );
346
347 endforeach;
348
349 }else if( 'unread' === $action ){
350
351 foreach ($form_ids as $form_id):
352
353 $form_id = (int) $form_id;
354 $results = $cfdb->get_results( "SELECT * FROM $table_name WHERE form_id = '$form_id' LIMIT 1", OBJECT );
355 $result_value = $results[0]->form_value;
356 $result_values = unserialize( $result_value );
357 $result_values['WPFormsDB_status'] = 'unread';
358 $form_data = serialize( $result_values );
359
360 $sql = $cfdb->prepare(
361 "UPDATE {$table_name} SET form_value = %s WHERE form_id = %d",
362 $form_data,
363 $form_id
364 );
365 $cfdb->query( $sql );
366 endforeach;
367 }
368
369 }
370 /**
371 * Define what data to show on each column of the table
372 *
373 * @param Array $item Data
374 * @param String $column_name - Current column name
375 *
376 * @return Mixed
377 */
378 public function column_default( $item, $column_name )
379 {
380 return $item[ $column_name ];
381
382 }
383 /**
384 * Allows you to sort the data by the variables set in the $_GET
385 *
386 * @return Mixed
387 */
388 private function sort_data( $a, $b )
389 {
390 // Set defaults
391 $orderby = 'form_date';
392 $order = 'asc';
393 // If orderby is set, use this as the sort column
394 if(!empty($_GET['orderby']))
395 {
396 $orderby = $_GET['orderby'] === 'form_id' ? 'form_id' : 'form_date';
397 }
398 // If order is set use this as the order
399 if(!empty($_GET['order']))
400 {
401 $order = $_GET['order'] === 'asc' ? 'ASC' : 'DESC';
402 }
403 $result = strcmp( $a[$orderby], $b[$orderby] );
404 if($order === 'asc')
405 {
406 return $result;
407 }
408 return -$result;
409 }
410 /**
411 * Display the bulk actions dropdown.
412 *
413 * @since 3.1.0
414 * @access protected
415 *
416 * @param string $which The location of the bulk actions: 'top' or 'bottom'.
417 * This is designated as optional for backward compatibility.
418 */
419 protected function bulk_actions( $which = '' ) {
420 if ( is_null( $this->_actions ) ) {
421 $this->_actions = $this->get_bulk_actions();
422 /**
423 * Filters the list table Bulk Actions drop-down.
424 *
425 * The dynamic portion of the hook name, `$this->screen->id`, refers
426 * to the ID of the current screen, usually a string.
427 *
428 * This filter can currently only be used to remove bulk actions.
429 *
430 * @since 3.5.0
431 *
432 * @param array $actions An array of the available bulk actions.
433 */
434 $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions );
435 $two = '';
436 } else {
437 $two = '2';
438 }
439
440 if ( empty( $this->_actions ) )
441 return;
442
443 echo '<label for="bulk-action-selector-' . esc_attr( $which ) . '" class="screen-reader-text">' . esc_html__( 'Select bulk action', 'database-for-wpforms' ) . '</label>';
444 echo '<select name="action' . esc_attr( $two ) . '" id="bulk-action-selector-' . esc_attr( $which ) . "\">\n";
445 echo '<option value="-1">' . esc_html__( 'Bulk Actions', 'database-for-wpforms' ) . "</option>\n";
446
447 foreach ( $this->_actions as $name => $title ) {
448 $class = 'edit' === $name ? ' class="hide-if-no-js"' : '';
449
450 echo "\t" . '<option value="' . esc_attr( $name ) . '"' . esc_attr( $class ) . '>' . esc_html( $title ) . "</option>\n";
451 }
452
453 echo "</select>\n";
454
455 submit_button( __( 'Apply', 'database-for-wpforms' ), 'action', '', false, array( 'id' => "doaction$two" ) );
456 echo "\n";
457 $nonce = wp_create_nonce( 'dnonce' );
458 echo "<a href='". esc_url( $_SERVER['REQUEST_URI'] )."&wpforms-csv=true&nonce=".$nonce."' style='float:right; margin:0;' class='button'>";
459 esc_html_e( 'Export CSV', 'database-for-wpforms' );
460 echo '</a>';
461 do_action('WPFormsDB_after_export_button');
462 }
463 }
464