-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclass-table-view.php
More file actions
170 lines (146 loc) · 6.27 KB
/
class-table-view.php
File metadata and controls
170 lines (146 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace H5PXAPIKATCHU;
// as suggested by the WordPress community
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Display and handle the settings page
*
* @package H5PXAPIKATCHU
* @since 0.1
*/
class Table_View {
private $class_datatable = 'h5pxapikatchu-data-table';
private $menu_icon;
/**
* Start up
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) );
add_action( 'admin_menu', array( $this, 'add_admin_page' ), 999 );
add_action( 'wp_ajax_nopriv_delete_data', 'H5PXAPIKATCHU\delete_data' );
add_action( 'wp_ajax_delete_data', 'H5PXAPIKATCHU\delete_data' );
}
public function add_scripts( $hook ) {
if ( 'toplevel_page_h5pxapikatchu_options' !== $hook ) {
return;
}
wp_register_script(
'DataTablesScript',
plugins_url( '/DataTables/datatables.min.js', __FILE__ ),
array( 'jquery' ),
H5PXAPIKATCHU_VERSION,
true
);
wp_register_script(
'BuildDataTable',
plugins_url( '/js/build_data_table.js', __FILE__ ),
array(),
H5PXAPIKATCHU_VERSION,
true
);
wp_register_style(
'DataTablesStyle',
plugins_url( '/DataTables/datatables.min.css', __FILE__ ),
array(),
H5PXAPIKATCHU_VERSION
);
wp_enqueue_script( 'DataTablesScript' );
wp_enqueue_script( 'BuildDataTable' );
wp_enqueue_style( 'DataTablesStyle' );
// Used to allow translations for Datatables from within WordPress translations
$language_datatables = array(
'info' => esc_html__( 'Showing _START_ to _END_ of _TOTAL_ entries', 'h5pxapikatchu' ),
'infoEmpty' => esc_html__( 'Showing 0 to 0 of 0 entries', 'h5pxapikatchu' ),
'infoFiltered' => esc_html__( 'filtered from _MAX_ total entries', 'h5pxapikatchu' ),
'lengthMenu' => esc_html__( 'Show _MENU_ entries', 'h5pxapikatchu' ),
'loadingRecords' => esc_html__( 'Loading...', 'h5pxapikatchu' ),
'processing' => esc_html__( 'Processing...', 'h5pxapikatchu' ),
'search' => esc_html__( 'Search', 'h5pxapikatchu' ),
'zeroRecords' => esc_html__( 'No matching records found', 'h5pxapikatchu' ),
'paginate' => array(
'first' => esc_html__( 'First', 'h5pxapikatchu' ),
'last' => esc_html__( 'Last', 'h5pxapikatchu' ),
'next' => esc_html__( 'Next', 'h5pxapikatchu' ),
'previous' => esc_html__( 'Previous', 'h5pxapikatchu' ),
),
);
// pass variables to JavaScript
wp_localize_script(
'BuildDataTable',
'h5pxapikatchuDataTable',
array(
'classDataTable' => $this->class_datatable,
'columnsHidden' => Options::get_columns_hidden(),
'buttonLabelDownload' => esc_html__( 'Download', 'h5pxapikatchu' ),
'buttonLabelColumnVisibility' => esc_html__( 'Show/hide columns', 'h5pxapikatchu' ),
'userCanDownloadResults' => current_user_can( 'download_h5pxapikatchu_results' ) ? '1' : '0',
'userCanDeleteResults' => current_user_can( 'delete_h5pxapikatchu_results' ) ? '1' : '0',
'languageData' => $language_datatables,
'buttonLabelDelete' => esc_html__( 'Delete', 'h5pxapikatchu' ),
'dialogTextDelete' => esc_html__( 'Do you really want to delete all the data?', 'h5pxapikatchu' ),
'errorMessage' => esc_html__( 'Sorry, something went wrong with deleting the data.', 'h5pxapikatchu' ),
'wpAJAXurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'h5pxapikatchu_nonce_delete_data' ),
)
);
}
public function add_admin_page() {
$this->menu_icon = 'data:image/svg+xml;base64,' . base64_encode( '<svg width="20" height="20" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path fill="black" d="M896 768q237 0 443-43t325-127v170q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-170q119 84 325 127t443 43zm0 768q237 0 443-43t325-127v170q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-170q119 84 325 127t443 43zm0-384q237 0 443-43t325-127v170q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-170q119 84 325 127t443 43zm0-1152q208 0 385 34.5t280 93.5 103 128v128q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-128q0-69 103-128t280-93.5 385-34.5z"/></svg>' );
add_menu_page( 'h5pxapikatchu_options', 'H5PxAPIkatchu', 'view_h5pxapikatchu_results', 'h5pxapikatchu_options', array( $this, 'add_plugin_page' ), $this->menu_icon );
}
public function add_plugin_page() {
if ( ! current_user_can( 'view_h5pxapikatchu_results' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'h5pxapikatchu' ) );
}
global $wpdb;
// Users with appropriate capability are allowed to see all entries with wildcard %
$wp_user_id = current_user_can( 'view_others_h5pxapikatchu_results' ) ? '%' : get_current_user_id();
$complete_table = Database::get_complete_table( $wp_user_id );
$column_titles = Database::get_column_titles();
echo '<div class="wrap">';
echo '<h2>' . esc_html__( 'H5PxAPIkatchu', 'h5pxapikatchu' ) . '</h2>';
if ( ! $complete_table ) {
echo esc_html__( 'There is no xAPI information stored.', 'h5pxapikatchu' );
wp_die();
}
// Use Datatable to make the table pretty.
echo '<div><table id="' . esc_attr( $this->class_datatable ) . '" class="table-striped table-bordered">';
// Table Head and Footer
echo '<thead><tr>';
for ( $i = 0; $i < sizeof( (array) $complete_table[0] ); $i++ ) {
echo '<th>' . esc_html(
isset( Database::$column_title_names[ $column_titles[ $i ] ] )
? Database::$column_title_names[ $column_titles[ $i ] ]
: ''
) . '</th>';
}
echo '</tr></thead>';
echo '<tfoot><tr>';
for ( $i = 0; $i < sizeof( (array) $complete_table[0] ); $i++ ) {
echo '<th>' . esc_html(
isset( Database::$column_title_names[ $column_titles[ $i ] ] )
? Database::$column_title_names[ $column_titles[ $i ] ]
: ''
) . '</th>';
}
echo '</tr></tfoot>';
// Table Body
echo '<tbody>';
foreach ( $complete_table as $fields ) {
$values = array_map(
function( $field ) {
return '\'' . $field . '\'';
},
(array) $fields
);
echo '<tr>';
foreach ( $fields as $key => $value ) {
echo '<td>' . esc_html( $value ) . '</td>';
}
echo '</tr>';
}
echo '</tbody>';
echo '</table></div>';
echo '</div>';
}
}