PluginProbe
Database Addon For WPForms ( wpforms entries ) – WPFormsDB / 1.0.4
Database Addon For WPForms ( wpforms entries ) – WPFormsDB v1.0.4
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 1.0.4, at inc/class-sub-page.php

456 lines 14.5 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 private $search;
16
17 /**
18 * Constructor start subpage
19 */
20 public function __construct()
21 {
22 $this->form_post_id = (int) $_GET['fid'];
23 $this->list_table_page();
24
25 }
26 /**
27 * Display the list table page
28 *
29 * @return Void
30 */
31 public function list_table_page()
32 {
33 $ListTable = new WPFormsDB_List_Table();
34 $ListTable->prepare_items();
35 ?>
36 <div class="wrap">
37 <div id="icon-users" class="icon32"></div>
38 <h2><?php echo get_the_title( $this->form_post_id ); ?></h2>
39 <form method="post" action="">
40
41 <?php $ListTable->search_box('Search', 'search'); ?>
42 <?php $ListTable->display(); ?>
43 </form>
44 </div>
45 <?php
46 }
47
48 }
49 // WP_List_Table is not loaded automatically so we need to load it in our application
50 if( ! class_exists( 'WP_List_Table' ) ) {
51 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
52 }
53 /**
54 * Create a new table class that will extend the WP_List_Table
55 */
56 class WPFormsDB_List_Table extends WP_List_Table
57 {
58 private $form_post_id;
59 private $column_titles;
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 ( sizeof($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', 'contact-form-WPFormsDB' ),
193 'unread' => __( 'Unread', 'contact-form-WPFormsDB' ),
194 'delete' => __( 'Delete', 'contact-form-WPFormsDB' )
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 = filter_input( INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING );
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 $cfdb->query(
340 "UPDATE $table_name SET form_value = '$form_data' WHERE form_id = '$form_id'"
341 );
342
343 endforeach;
344
345 }else if( 'unread' === $action ){
346
347 foreach ($form_ids as $form_id):
348
349 $form_id = (int) $form_id;
350 $results = $cfdb->get_results( "SELECT * FROM $table_name WHERE form_id = '$form_id' LIMIT 1", OBJECT );
351 $result_value = $results[0]->form_value;
352 $result_values = unserialize( $result_value );
353 $result_values['WPFormsDB_status'] = 'unread';
354 $form_data = serialize( $result_values );
355 $cfdb->query(
356 "UPDATE $table_name SET form_value = '$form_data' WHERE form_id = '$form_id'"
357 );
358 endforeach;
359 }
360
361 }
362 /**
363 * Define what data to show on each column of the table
364 *
365 * @param Array $item Data
366 * @param String $column_name - Current column name
367 *
368 * @return Mixed
369 */
370 public function column_default( $item, $column_name )
371 {
372 return $item[ $column_name ];
373
374 }
375 /**
376 * Allows you to sort the data by the variables set in the $_GET
377 *
378 * @return Mixed
379 */
380 private function sort_data( $a, $b )
381 {
382 // Set defaults
383 $orderby = 'form_date';
384 $order = 'asc';
385 // If orderby is set, use this as the sort column
386 if(!empty($_GET['orderby']))
387 {
388 $orderby = $_GET['orderby'] === 'form_id' ? 'form_id' : 'form_date';
389 }
390 // If order is set use this as the order
391 if(!empty($_GET['order']))
392 {
393 $order = $_GET['order'] === 'asc' ? 'ASC' : 'DESC';
394 }
395 $result = strcmp( $a[$orderby], $b[$orderby] );
396 if($order === 'asc')
397 {
398 return $result;
399 }
400 return -$result;
401 }
402 /**
403 * Display the bulk actions dropdown.
404 *
405 * @since 3.1.0
406 * @access protected
407 *
408 * @param string $which The location of the bulk actions: 'top' or 'bottom'.
409 * This is designated as optional for backward compatibility.
410 */
411 protected function bulk_actions( $which = '' ) {
412 if ( is_null( $this->_actions ) ) {
413 $this->_actions = $this->get_bulk_actions();
414 /**
415 * Filters the list table Bulk Actions drop-down.
416 *
417 * The dynamic portion of the hook name, `$this->screen->id`, refers
418 * to the ID of the current screen, usually a string.
419 *
420 * This filter can currently only be used to remove bulk actions.
421 *
422 * @since 3.5.0
423 *
424 * @param array $actions An array of the available bulk actions.
425 */
426 $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions );
427 $two = '';
428 } else {
429 $two = '2';
430 }
431
432 if ( empty( $this->_actions ) )
433 return;
434
435 echo '<label for="bulk-action-selector-' . esc_attr( $which ) . '" class="screen-reader-text">' . __( 'Select bulk action', 'contact-form-WPFormsDB' ) . '</label>';
436 echo '<select name="action' . esc_attr( $two ) . '" id="bulk-action-selector-' . esc_attr( $which ) . "\">\n";
437 echo '<option value="-1">' . __( 'Bulk Actions', 'contact-form-WPFormsDB' ) . "</option>\n";
438
439 foreach ( $this->_actions as $name => $title ) {
440 $class = 'edit' === $name ? ' class="hide-if-no-js"' : '';
441
442 echo "\t" . '<option value="' . esc_attr( $name ) . '"' . esc_attr( $class ) . '>' . esc_html( $title ) . "</option>\n";
443 }
444
445 echo "</select>\n";
446
447 submit_button( __( 'Apply', 'contact-form-WPFormsDB' ), 'action', '', false, array( 'id' => "doaction$two" ) );
448 echo "\n";
449 $nonce = wp_create_nonce( 'dnonce' );
450 echo "<a href='".$_SERVER['REQUEST_URI']."&wpforms-csv=true&nonce=".$nonce."' style='float:right; margin:0;' class='button'>";
451 _e( 'Export CSV', 'contact-form-WPFormsDB' );
452 echo '</a>';
453 do_action('WPFormsDB_after_export_button');
454 }
455 }
456