EDD_API_Request_Log_Table

EDD_API_Request_Log_Table List Table Class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/reporting/class-api-requests-logs-list-table.php  
  1. class EDD_API_Request_Log_Table extends WP_List_Table { 
  2. /** 
  3. * Number of items per page 
  4. * @var int 
  5. * @since 1.5 
  6. */ 
  7. public $per_page = 30; 
  8.  
  9. /** 
  10. * Get things started 
  11. * @since 1.5 
  12. * @see WP_List_Table::__construct() 
  13. */ 
  14. public function __construct() { 
  15. global $status, $page; 
  16.  
  17. // Set parent defaults 
  18. parent::__construct( array( 
  19. 'singular' => edd_get_label_singular(),  
  20. 'plural' => edd_get_label_plural(),  
  21. 'ajax' => false,  
  22. ) ); 
  23.  
  24. /** 
  25. * Show the search field 
  26. * @since 1.5 
  27. * @access public 
  28. * @param string $text Label for the search box 
  29. * @param string $input_id ID of the search box 
  30. * @return void 
  31. */ 
  32. public function search_box( $text, $input_id ) { 
  33. $input_id = $input_id . '-search-input'; 
  34.  
  35. if ( ! empty( $_REQUEST['orderby'] ) ) 
  36. echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />'; 
  37. if ( ! empty( $_REQUEST['order'] ) ) 
  38. echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />'; 
  39. ?> 
  40. <p class="search-box"> 
  41. <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label> 
  42. <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" /> 
  43. <?php submit_button( $text, 'button', false, false, array('ID' => 'search-submit') ); ?> 
  44. </p> 
  45. <?php 
  46.  
  47. /** 
  48. * Retrieve the table columns 
  49. * @access public 
  50. * @since 1.5 
  51. * @return array $columns Array of all the list table columns 
  52. */ 
  53. public function get_columns() { 
  54. $columns = array( 
  55. 'ID' => __( 'Log ID', 'easy-digital-downloads' ),  
  56. 'details' => __( 'Request Details', 'easy-digital-downloads' ),  
  57. 'version' => __( 'API Version', 'easy-digital-downloads' ),  
  58. 'ip' => __( 'Request IP', 'easy-digital-downloads' ),  
  59. 'speed' => __( 'Request Speed', 'easy-digital-downloads' ),  
  60. 'date' => __( 'Date', 'easy-digital-downloads' ),  
  61. ); 
  62.  
  63. return $columns; 
  64.  
  65. /** 
  66. * Gets the name of the primary column. 
  67. * @since 2.5 
  68. * @access protected 
  69. * @return string Name of the primary column. 
  70. */ 
  71. protected function get_primary_column_name() { 
  72. return 'ID'; 
  73.  
  74. /** 
  75. * This function renders most of the columns in the list table. 
  76. * @access public 
  77. * @since 1.5 
  78. * @param array $item Contains all the data of the api request 
  79. * @param string $column_name The name of the column 
  80. * @return string Column Name 
  81. */ 
  82. public function column_default( $item, $column_name ) { 
  83. return $item[ $column_name ]; 
  84.  
  85. /** 
  86. * Output Error Message column 
  87. * @access public 
  88. * @since 1.5 
  89. * @param array $item Contains all the data of the log 
  90. * @return void 
  91. */ 
  92. public function column_details( $item ) { 
  93. ?> 
  94. <a href="#TB_inline?width=640&inlineId=log-details-<?php echo $item['ID']; ?>" class="thickbox"><?php _e( 'View Request', 'easy-digital-downloads' ); ?></a> 
  95. <div id="log-details-<?php echo $item['ID']; ?>" style="display:none;"> 
  96. <?php 
  97.  
  98. $request = get_post_field( 'post_excerpt', $item['ID'] ); 
  99. $error = get_post_field( 'post_content', $item['ID'] ); 
  100. echo '<p><strong>' . __( 'API Request:', 'easy-digital-downloads' ) . '</strong></p>'; 
  101. echo '<div>' . $request . '</div>'; 
  102. if( ! empty( $error ) ) { 
  103. echo '<p><strong>' . __( 'Error', 'easy-digital-downloads' ) . '</strong></p>'; 
  104. echo '<div>' . esc_html( $error ) . '</div>'; 
  105. echo '<p><strong>' . __( 'API User:', 'easy-digital-downloads' ) . '</strong></p>'; 
  106. echo '<div>' . get_post_meta( $item['ID'], '_edd_log_user', true ) . '</div>'; 
  107. echo '<p><strong>' . __( 'API Key:', 'easy-digital-downloads' ) . '</strong></p>'; 
  108. echo '<div>' . get_post_meta( $item['ID'], '_edd_log_key', true ) . '</div>'; 
  109. echo '<p><strong>' . __( 'Request Date:', 'easy-digital-downloads' ) . '</strong></p>'; 
  110. echo '<div>' . get_post_field( 'post_date', $item['ID'] ) . '</div>'; 
  111. ?> 
  112. </div> 
  113. <?php 
  114.  
  115. /** 
  116. * Retrieves the search query string 
  117. * @access public 
  118. * @since 1.5 
  119. * @return string|false String if search is present, false otherwise 
  120. */ 
  121. public function get_search() { 
  122. return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false; 
  123.  
  124. /** 
  125. * Gets the meta query for the log query 
  126. * This is used to return log entries that match our search query 
  127. * @access public 
  128. * @since 1.5 
  129. * @return array $meta_query 
  130. */ 
  131. function get_meta_query() { 
  132. $meta_query = array(); 
  133.  
  134. $search = $this->get_search(); 
  135.  
  136. if ( $search ) { 
  137. if ( filter_var( $search, FILTER_VALIDATE_IP ) ) { 
  138. // This is an IP address search 
  139. $key = '_edd_log_request_ip'; 
  140. } else if ( is_email( $search ) ) { 
  141. // This is an email search 
  142. $userdata = get_user_by( 'email', $search ); 
  143.  
  144. if( $userdata ) { 
  145. $search = $userdata->ID; 
  146.  
  147. $key = '_edd_log_user'; 
  148. } elseif( strlen( $search ) == 32 ) { 
  149. // Look for an API key 
  150. $key = '_edd_log_key'; 
  151. } elseif( stristr( $search, 'token:' ) ) { 
  152. // Look for an API token 
  153. $search = str_ireplace( 'token:', '', $search ); 
  154. $key = '_edd_log_token'; 
  155. } else { 
  156. // This is (probably) a user ID search 
  157. $userdata = get_userdata( $search ); 
  158.  
  159. if( $userdata ) { 
  160. $search = $userdata->ID; 
  161.  
  162. $key = '_edd_log_user'; 
  163.  
  164. // Setup the meta query 
  165. $meta_query[] = array( 
  166. 'key' => $key,  
  167. 'value' => $search,  
  168. 'compare' => '=',  
  169. ); 
  170.  
  171. return $meta_query; 
  172.  
  173. /** 
  174. * Retrieve the current page number 
  175. * @access public 
  176. * @since 1.5 
  177. * @return int Current page number 
  178. */ 
  179. public function get_paged() { 
  180. return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; 
  181.  
  182. /** 
  183. * Outputs the log views 
  184. * @access public 
  185. * @since 1.5 
  186. * @return void 
  187. */ 
  188. function bulk_actions( $which='' ) { 
  189. // These aren't really bulk actions but this outputs the markup in the right place 
  190. edd_log_views(); 
  191.  
  192. /** 
  193. * Gets the log entries for the current view 
  194. * @access public 
  195. * @since 1.5 
  196. * @global object $edd_logs EDD Logs Object 
  197. * @return array $logs_data Array of all the Log entires 
  198. */ 
  199. public function get_logs() { 
  200. global $edd_logs; 
  201.  
  202. $logs_data = array(); 
  203. $paged = $this->get_paged(); 
  204. $log_query = array( 
  205. 'log_type' => 'api_request',  
  206. 'paged' => $paged,  
  207. 'meta_query' => $this->get_meta_query(),  
  208. ); 
  209.  
  210. $logs = $edd_logs->get_connected_logs( $log_query ); 
  211.  
  212. if ( $logs ) { 
  213. foreach ( $logs as $log ) { 
  214.  
  215. $logs_data[] = array( 
  216. 'ID' => $log->ID,  
  217. 'version' => get_post_meta( $log->ID, '_edd_log_version', true ),  
  218. 'speed' => get_post_meta( $log->ID, '_edd_log_time', true ),  
  219. 'ip' => get_post_meta( $log->ID, '_edd_log_request_ip', true ),  
  220. 'date' => $log->post_date,  
  221. ); 
  222.  
  223. return $logs_data; 
  224.  
  225. /** 
  226. * Setup the final data for the table 
  227. * @access public 
  228. * @since 1.5 
  229. * @global object $edd_logs EDD Logs Object 
  230. * @uses EDD_API_Request_Log_Table::get_columns() 
  231. * @uses WP_List_Table::get_sortable_columns() 
  232. * @uses EDD_API_Request_Log_Table::get_pagenum() 
  233. * @uses EDD_API_Request_Log_Table::get_logs() 
  234. * @uses EDD_API_Request_Log_Table::get_log_count() 
  235. * @return void 
  236. */ 
  237. public function prepare_items() { 
  238. global $edd_logs; 
  239.  
  240. $columns = $this->get_columns(); 
  241. $hidden = array(); // No hidden columns 
  242. $sortable = $this->get_sortable_columns(); 
  243. $this->_column_headers = array( $columns, $hidden, $sortable, 'ID' ); 
  244. $this->items = $this->get_logs(); 
  245. $total_items = $edd_logs->get_log_count( 0, 'api_requests' ); 
  246.  
  247. $this->set_pagination_args( array( 
  248. 'total_items' => $total_items,  
  249. 'per_page' => $this->per_page,  
  250. 'total_pages' => ceil( $total_items / $this->per_page ),  
  251. ); 
  252.  
  253. /** 
  254. * Since our "bulk actions" are navigational, we want them to always show, not just when there's items 
  255. * @access public 
  256. * @since 2.5 
  257. * @return bool 
  258. */ 
  259. public function has_items() { 
  260. return true;