WC_Admin_API_Keys

WC_Admin_API_Keys.

Defined (1)

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

/includes/admin/class-wc-admin-api-keys.php  
  1. class WC_Admin_API_Keys { 
  2.  
  3. /** 
  4. * Initialize the API Keys admin actions. 
  5. */ 
  6. public function __construct() { 
  7. add_action( 'admin_init', array( $this, 'actions' ) ); 
  8.  
  9. /** 
  10. * Check if is API Keys settings page. 
  11. * @return bool 
  12. */ 
  13. private function is_api_keys_settings_page() { 
  14. return isset( $_GET['page'] ) 
  15. && 'wc-settings' === $_GET['page'] 
  16. && isset( $_GET['tab'] ) 
  17. && 'api' === $_GET['tab'] 
  18. && isset( $_GET['section'] ) 
  19. && 'keys' === $_GET['section']; 
  20.  
  21. /** 
  22. * Page output. 
  23. */ 
  24. public static function page_output() { 
  25. // Hide the save button 
  26. $GLOBALS['hide_save_button'] = true; 
  27.  
  28. if ( isset( $_GET['create-key'] ) || isset( $_GET['edit-key'] ) ) { 
  29. $key_id = isset( $_GET['edit-key'] ) ? absint( $_GET['edit-key'] ) : 0; 
  30. $key_data = self::get_key_data( $key_id ); 
  31.  
  32. include( 'settings/views/html-keys-edit.php' ); 
  33. } else { 
  34. self::table_list_output(); 
  35.  
  36. /** 
  37. * Table list output. 
  38. */ 
  39. private static function table_list_output() { 
  40. echo '<h2>' . __( 'Keys/Apps', 'woocommerce' ) . ' <a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys&create-key=1' ) ) . '" class="add-new-h2">' . __( 'Add key', 'woocommerce' ) . '</a></h2>'; 
  41.  
  42. $keys_table_list = new WC_Admin_API_Keys_Table_List(); 
  43. $keys_table_list->prepare_items(); 
  44.  
  45. echo '<input type="hidden" name="page" value="wc-settings" />'; 
  46. echo '<input type="hidden" name="tab" value="api" />'; 
  47. echo '<input type="hidden" name="section" value="keys" />'; 
  48.  
  49. $keys_table_list->views(); 
  50. $keys_table_list->search_box( __( 'Search key', 'woocommerce' ), 'key' ); 
  51. $keys_table_list->display(); 
  52.  
  53. /** 
  54. * Get key data. 
  55. * @param int $key_id 
  56. * @return array 
  57. */ 
  58. private static function get_key_data( $key_id ) { 
  59. global $wpdb; 
  60.  
  61. $empty = array( 
  62. 'key_id' => 0,  
  63. 'user_id' => '',  
  64. 'description' => '',  
  65. 'permissions' => '',  
  66. 'truncated_key' => '',  
  67. 'last_access' => '',  
  68. ); 
  69.  
  70. if ( 0 == $key_id ) { 
  71. return $empty; 
  72.  
  73. $key = $wpdb->get_row( $wpdb->prepare( " 
  74. SELECT key_id, user_id, description, permissions, truncated_key, last_access 
  75. FROM {$wpdb->prefix}woocommerce_api_keys 
  76. WHERE key_id = %d 
  77. ", $key_id ), ARRAY_A ); 
  78.  
  79. if ( is_null( $key ) ) { 
  80. return $empty; 
  81.  
  82. return $key; 
  83.  
  84. /** 
  85. * API Keys admin actions. 
  86. */ 
  87. public function actions() { 
  88. if ( $this->is_api_keys_settings_page() ) { 
  89. // Revoke key 
  90. if ( isset( $_GET['revoke-key'] ) ) { 
  91. $this->revoke_key(); 
  92.  
  93. // Bulk actions 
  94. if ( isset( $_GET['action'] ) && isset( $_GET['key'] ) ) { 
  95. $this->bulk_actions(); 
  96.  
  97. /** 
  98. * Notices. 
  99. */ 
  100. public static function notices() { 
  101. if ( isset( $_GET['revoked'] ) && 1 == $_GET['revoked'] ) { 
  102. WC_Admin_Settings::add_message( __( 'API key revoked successfully.', 'woocommerce' ) ); 
  103.  
  104. /** 
  105. * Revoke key. 
  106. */ 
  107. private function revoke_key() { 
  108. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'revoke' ) ) { 
  109. wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); 
  110.  
  111. $key_id = absint( $_GET['revoke-key'] ); 
  112. $this->remove_key( $key_id ); 
  113.  
  114. wp_redirect( esc_url_raw( add_query_arg( array( 'revoked' => 1 ), admin_url( 'admin.php?page=wc-settings&tab=api§ion=keys' ) ) ) ); 
  115. exit(); 
  116.  
  117. /** 
  118. * Bulk actions. 
  119. */ 
  120. private function bulk_actions() { 
  121. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) { 
  122. wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); 
  123.  
  124. $keys = array_map( 'absint', (array) $_GET['key'] ); 
  125.  
  126. if ( 'revoke' == $_GET['action'] ) { 
  127. $this->bulk_revoke_key( $keys ); 
  128.  
  129. /** 
  130. * Bulk revoke key. 
  131. * @param array $keys 
  132. */ 
  133. private function bulk_revoke_key( $keys ) { 
  134. foreach ( $keys as $key_id ) { 
  135. $this->remove_key( $key_id ); 
  136.  
  137. /** 
  138. * Remove key. 
  139. * @param int $key_id 
  140. * @return bool 
  141. */ 
  142. private function remove_key( $key_id ) { 
  143. global $wpdb; 
  144.  
  145. $delete = $wpdb->delete( $wpdb->prefix . 'woocommerce_api_keys', array( 'key_id' => $key_id ), array( '%d' ) ); 
  146.  
  147. return $delete;