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

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