1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15
16 if ( ! class_exists( 'WP_List_Table' ) ) {
17 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
18 }
19
20 21 22 23 24 25 26
27 class EDD_API_Request_Log_Table extends WP_List_Table {
28 29 30 31 32 33
34 public $per_page = 30;
35
36 37 38 39 40 41 42 43
44 public function __construct() {
45 global $status, $page;
46
47
48 parent::__construct( array(
49 'singular' => edd_get_label_singular(),
50 'plural' => edd_get_label_plural(),
51 'ajax' => false
52 ) );
53 }
54
55 56 57 58 59 60 61 62 63 64 65
66 public function search_box( $text, $input_id ) {
67 $input_id = $input_id . '-search-input';
68
69 if ( ! empty( $_REQUEST['orderby'] ) )
70 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
71 if ( ! empty( $_REQUEST['order'] ) )
72 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
73 ?>
74 <p class="search-box">
75 <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
76 <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
77 <?php submit_button( $text, 'button', false, false, array('ID' => 'search-submit') ); ?>
78 </p>
79 <?php
80 }
81
82 83 84 85 86 87 88
89 public function get_columns() {
90 $columns = array(
91 'ID' => __( 'Log ID', 'edd' ),
92 'details' => __( 'Request Details', 'edd' ),
93 'ip' => __( 'Request IP', 'edd' ),
94 'date' => __( 'Date', 'edd' )
95 );
96
97 return $columns;
98 }
99
100 101 102 103 104 105 106 107 108 109 110
111 public function column_default( $item, $column_name ) {
112 switch( $column_name ){
113 default:
114 return $item[ $column_name ];
115 }
116 }
117
118 119 120 121 122 123 124 125
126 public function column_details( $item ) {
127 ?>
128 <a href="#TB_inline?width=640&inlineId=log-details-<?php echo $item['ID']; ?>" class="thickbox" title="<?php _e( 'View Request Details', 'edd' ); ?> "><?php _e( 'View Request', 'edd' ); ?></a>
129 <div id="log-details-<?php echo $item['ID']; ?>" style="display:none;">
130 <?php
131
132 $request = get_post_field( 'post_excerpt', $item['ID'] );
133 $error = get_post_field( 'post_content', $item['ID'] );
134 echo '<p><strong>' . __( 'API Request:', 'edd' ) . '</strong></p>';
135 echo '<div>' . $request . '</div>';
136 if( ! empty( $error ) ) {
137 echo '<p><strong>' . __( 'Error', 'edd' ) . '</strong></p>';
138 echo '<div>' . esc_html( $error ) . '</div>';
139 }
140 echo '<p><strong>' . __( 'API User:', 'edd' ) . '</strong></p>';
141 echo '<div>' . get_post_meta( $item['ID'], '_edd_log_user', true ) . '</div>';
142 echo '<p><strong>' . __( 'API Key:', 'edd' ) . '</strong></p>';
143 echo '<div>' . get_post_meta( $item['ID'], '_edd_log_api_key', true ) . '</div>';
144 echo '<p><strong>' . __( 'Request Date:', 'edd' ) . '</strong></p>';
145 echo '<div>' . get_post_field( 'post_date', $item['ID'] ) . '</div>';
146 ?>
147 </div>
148 <?php
149 }
150
151 152 153 154 155 156 157
158 public function get_search() {
159 return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false;
160 }
161
162 163 164 165 166 167 168 169 170
171 function get_meta_query() {
172 $meta_query = array();
173
174 $search = $this->get_search();
175
176 if ( $search ) {
177 if ( filter_var( $search, FILTER_VALIDATE_IP ) ) {
178
179 $key = '_edd_log_request_ip';
180 } else if ( is_email( $search ) ) {
181
182 $key = '_edd_log_user';
183 } else {
184
185 $key = '_edd_log_api_key';
186 }
187
188
189 $meta_query[] = array(
190 'key' => $key,
191 'value' => $search,
192 'compare' => '='
193 );
194 }
195
196 return $meta_query;
197 }
198
199 200 201 202 203 204 205
206 public function get_paged() {
207 return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
208 }
209
210 211 212 213 214 215 216
217 function bulk_actions() {
218
219 edd_log_views();
220 }
221
222 223 224 225 226 227 228 229
230 public function get_logs() {
231 global $edd_logs;
232
233 $logs_data = array();
234 $paged = $this->get_paged();
235 $log_query = array(
236 'log_type' => 'api_requests',
237 'paged' => $paged,
238 'meta_query' => $this->get_meta_query()
239 );
240
241 $logs = $edd_logs->get_connected_logs( $log_query );
242
243 if ( $logs ) {
244 foreach ( $logs as $log ) {
245
246 $logs_data[] = array(
247 'ID' => $log->ID,
248 'ip' => get_post_meta( $log->ID, '_edd_log_request_ip', true ),
249 'date' => $log->post_date
250 );
251 }
252 }
253
254 return $logs_data;
255 }
256
257 258 259 260 261 262 263 264 265 266 267 268 269
270 public function prepare_items() {
271 global $edd_logs;
272
273 $columns = $this->get_columns();
274 $hidden = array();
275 $sortable = $this->get_sortable_columns();
276 $this->_column_headers = array( $columns, $hidden, $sortable );
277 $current_page = $this->get_pagenum();
278 $this->items = $this->get_logs();
279 $total_items = $edd_logs->get_log_count( 0, 'api_requests' );
280
281 $this->set_pagination_args( array(
282 'total_items' => $total_items,
283 'per_page' => $this->per_page,
284 'total_pages' => ceil( $total_items / $this->per_page )
285 )
286 );
287 }
288 }