EDD_API_Keys_Table

EDD_API_Keys_Table Class.

Defined (1)

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

/includes/admin/class-api-keys-table.php  
  1. class EDD_API_Keys_Table extends WP_List_Table { 
  2.  
  3. /** 
  4. * @var int Number of items per page 
  5. * @since 2.0 
  6. */ 
  7. public $per_page = 30; 
  8.  
  9. /** 
  10. * @var object Query results 
  11. * @since 2.0 
  12. */ 
  13. private $keys; 
  14.  
  15. /** 
  16. * Get things started 
  17. * @since 1.5 
  18. * @see WP_List_Table::__construct() 
  19. */ 
  20. public function __construct() { 
  21. global $status, $page; 
  22.  
  23. // Set parent defaults 
  24. parent::__construct( array( 
  25. 'singular' => __( 'API Key', 'easy-digital-downloads' ),  
  26. 'plural' => __( 'API Keys', 'easy-digital-downloads' ),  
  27. 'ajax' => false,  
  28. ) ); 
  29.  
  30. $this->query(); 
  31.  
  32. /** 
  33. * Gets the name of the primary column. 
  34. * @since 2.5 
  35. * @access protected 
  36. * @return string Name of the primary column. 
  37. */ 
  38. protected function get_primary_column_name() { 
  39. return 'user'; 
  40.  
  41. /** 
  42. * This function renders most of the columns in the list table. 
  43. * @access public 
  44. * @since 2.0 
  45. * @param array $item Contains all the data of the keys 
  46. * @param string $column_name The name of the column 
  47. * @return string Column Name 
  48. */ 
  49. public function column_default( $item, $column_name ) { 
  50. return $item[ $column_name ]; 
  51.  
  52. /** 
  53. * Displays the public key rows 
  54. * @access public 
  55. * @since 2.4 
  56. * @param array $item Contains all the data of the keys 
  57. * @param string $column_name The name of the column 
  58. * @return string Column Name 
  59. */ 
  60. public function column_key( $item ) { 
  61. return '<input readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item[ 'key' ] ) . '"/>'; 
  62.  
  63. /** 
  64. * Displays the token rows 
  65. * @access public 
  66. * @since 2.4 
  67. * @param array $item Contains all the data of the keys 
  68. * @param string $column_name The name of the column 
  69. * @return string Column Name 
  70. */ 
  71. public function column_token( $item ) { 
  72. return '<input readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item[ 'token' ] ) . '"/>'; 
  73.  
  74. /** 
  75. * Displays the secret key rows 
  76. * @access public 
  77. * @since 2.4 
  78. * @param array $item Contains all the data of the keys 
  79. * @param string $column_name The name of the column 
  80. * @return string Column Name 
  81. */ 
  82. public function column_secret( $item ) { 
  83. return '<input readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item[ 'secret' ] ) . '"/>'; 
  84.  
  85. /** 
  86. * Renders the column for the user field 
  87. * @access public 
  88. * @since 2.0 
  89. * @return void 
  90. */ 
  91. public function column_user( $item ) { 
  92.  
  93. $actions = array(); 
  94.  
  95. if( apply_filters( 'edd_api_log_requests', true ) ) { 
  96. $actions['view'] = sprintf( 
  97. '<a href="%s">%s</a>',  
  98. esc_url( add_query_arg( array( 'view' => 'api_requests', 'post_type' => 'download', 'page' => 'edd-reports', 'tab' => 'logs', 's' => $item['email'] ), 'edit.php' ) ),  
  99. __( 'View API Log', 'easy-digital-downloads' ) 
  100. ); 
  101.  
  102. $actions['reissue'] = sprintf( 
  103. '<a href="%s" class="edd-regenerate-api-key">%s</a>',  
  104. esc_url( wp_nonce_url( add_query_arg( array( 'user_id' => $item['id'], 'edd_action' => 'process_api_key', 'edd_api_process' => 'regenerate' ) ), 'edd-api-nonce' ) ),  
  105. __( 'Reissue', 'easy-digital-downloads' ) 
  106. ); 
  107. $actions['revoke'] = sprintf( 
  108. '<a href="%s" class="edd-revoke-api-key edd-delete">%s</a>',  
  109. esc_url( wp_nonce_url( add_query_arg( array( 'user_id' => $item['id'], 'edd_action' => 'process_api_key', 'edd_api_process' => 'revoke' ) ), 'edd-api-nonce' ) ),  
  110. __( 'Revoke', 'easy-digital-downloads' ) 
  111. ); 
  112.  
  113. $actions = apply_filters( 'edd_api_row_actions', array_filter( $actions ) ); 
  114.  
  115. return sprintf('%1$s %2$s', $item['user'], $this->row_actions( $actions ) ); 
  116.  
  117. /** 
  118. * Retrieve the table columns 
  119. * @access public 
  120. * @since 2.0 
  121. * @return array $columns Array of all the list table columns 
  122. */ 
  123. public function get_columns() { 
  124. $columns = array( 
  125. 'user' => __( 'Username', 'easy-digital-downloads' ),  
  126. 'key' => __( 'Public Key', 'easy-digital-downloads' ),  
  127. 'token' => __( 'Token', 'easy-digital-downloads' ),  
  128. 'secret' => __( 'Secret Key', 'easy-digital-downloads' ),  
  129. ); 
  130.  
  131. return $columns; 
  132.  
  133. /** 
  134. * Display the key generation form 
  135. * @access public 
  136. * @since 1.5 
  137. * @return void 
  138. */ 
  139. public function bulk_actions( $which = '' ) { 
  140. // These aren't really bulk actions but this outputs the markup in the right place 
  141. static $edd_api_is_bottom; 
  142.  
  143. if( $edd_api_is_bottom ) { 
  144. return; 
  145. ?> 
  146. <form id="api-key-generate-form" method="post" action="<?php echo admin_url( 'edit.php?post_type=download&page=edd-tools&tab=api_keys' ); ?>"> 
  147. <input type="hidden" name="edd_action" value="process_api_key" /> 
  148. <input type="hidden" name="edd_api_process" value="generate" /> 
  149. <?php wp_nonce_field( 'edd-api-nonce' ); ?> 
  150. <?php echo EDD()->html->ajax_user_search(); ?> 
  151. <?php submit_button( __( 'Generate New API Keys', 'easy-digital-downloads' ), 'secondary', 'submit', false ); ?> 
  152. </form> 
  153. <?php 
  154. $edd_api_is_bottom = true; 
  155.  
  156. /** 
  157. * Generate the table navigation above or below the table 
  158. * @since 3.1.0 
  159. * @access protected 
  160. * @param string $which 
  161. */ 
  162. protected function display_tablenav( $which ) { 
  163. if ( 'top' === $which ) { 
  164. wp_nonce_field( 'bulk-' . $this->_args['plural'] ); 
  165. ?> 
  166. <div class="tablenav <?php echo esc_attr( $which ); ?>"> 
  167.  
  168. <div class="alignleft actions bulkactions"> 
  169. <?php $this->bulk_actions( $which ); ?> 
  170. </div> 
  171. <?php 
  172. $this->extra_tablenav( $which ); 
  173. $this->pagination( $which ); 
  174. ?> 
  175.  
  176. <br class="clear" /> 
  177. </div> 
  178. <?php 
  179.  
  180. /** 
  181. * Retrieve the current page number 
  182. * @access public 
  183. * @since 2.0 
  184. * @return int Current page number 
  185. */ 
  186. public function get_paged() { 
  187. return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; 
  188.  
  189. /** 
  190. * Performs the key query 
  191. * @access public 
  192. * @since 2.0 
  193. * @return void 
  194. */ 
  195. public function query() { 
  196. $users = get_users( array( 
  197. 'meta_value' => 'edd_user_secret_key',  
  198. 'number' => $this->per_page,  
  199. 'offset' => $this->per_page * ( $this->get_paged() - 1 ),  
  200. ) ); 
  201. $keys = array(); 
  202.  
  203. foreach( $users as $user ) { 
  204. $keys[$user->ID]['id'] = $user->ID; 
  205. $keys[$user->ID]['email'] = $user->user_email; 
  206. $keys[$user->ID]['user'] = '<a href="' . add_query_arg( 'user_id', $user->ID, 'user-edit.php' ) . '"><strong>' . $user->user_login . '</strong></a>'; 
  207.  
  208. $keys[$user->ID]['key'] = EDD()->api->get_user_public_key( $user->ID ); 
  209. $keys[$user->ID]['secret'] = EDD()->api->get_user_secret_key( $user->ID ); 
  210. $keys[$user->ID]['token'] = EDD()->api->get_token( $user->ID ); 
  211.  
  212. return $keys; 
  213.  
  214.  
  215.  
  216. /** 
  217. * Retrieve count of total users with keys 
  218. * @access public 
  219. * @since 2.0 
  220. * @return int 
  221. */ 
  222. public function total_items() { 
  223. global $wpdb; 
  224.  
  225. if( ! get_transient( 'edd_total_api_keys' ) ) { 
  226. $total_items = $wpdb->get_var( "SELECT count(user_id) FROM $wpdb->usermeta WHERE meta_value='edd_user_secret_key'" ); 
  227.  
  228. set_transient( 'edd_total_api_keys', $total_items, 60 * 60 ); 
  229.  
  230. return get_transient( 'edd_total_api_keys' ); 
  231.  
  232. /** 
  233. * Setup the final data for the table 
  234. * @access public 
  235. * @since 2.0 
  236. * @return void 
  237. */ 
  238. public function prepare_items() { 
  239. $columns = $this->get_columns(); 
  240.  
  241. $hidden = array(); // No hidden columns 
  242. $sortable = array(); // Not sortable... for now 
  243.  
  244. $this->_column_headers = array( $columns, $hidden, $sortable, 'user' ); 
  245.  
  246. $data = $this->query(); 
  247.  
  248. $total_items = $this->total_items(); 
  249.  
  250. $this->items = $data; 
  251.  
  252. $this->set_pagination_args( array( 
  253. 'total_items' => $total_items,  
  254. 'per_page' => $this->per_page,  
  255. 'total_pages' => ceil( $total_items / $this->per_page ),  
  256. );