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: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274:
<?php
if (!defined('ABSPATH')) {
exit;
}
if (!class_exists('WP_List_Table')) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class Inbound_API_Keys_Table extends WP_List_Table {
public $per_page = 30;
static $keys;
public function __construct() {
global $status, $page;
parent::__construct(array(
'singular' => __('API Key', 'inbound-pro'),
'plural' => __('API Keys', 'inbound-pro'),
'ajax' => false
));
$this->inline_js();
$this->query();
}
public static function inline_js() {
?>
<script type="text/javascript">
var Inbound_API_Actions = {
init: function () {
this.revoke_api_key();
this.regenerate_api_key();
},
revoke_api_key: function () {
jQuery('body').on('click', '.inbound-revoke-api-keys', function (e) {
return confirm('<?php _e('Are you sure you want to revoke permissions for this API Key?', 'inbound-pro'); ?> ');
});
},
regenerate_api_key: function () {
jQuery('body').on('click', '.inbound-regenerate-api-keys', function (e) {
return confirm('<?php _e('Are you sure you want to regenerate API Keys for this user?', 'inbound-pro'); ?> ');
});
},
};
Inbound_API_Actions.init();
</script>
<?php
}
public function column_default($item, $column_name) {
return $item[$column_name];
}
public function column_user($item) {
$actions = array();
$actions['reissue'] = sprintf(
'<a href="%s" class="inbound-regenerate-api-keys">%s</a>',
esc_url(add_query_arg(array('user_id' => $item['id'], 'inbound_action' => 'regenerate-api-keys'))),
__('Reissue', 'inbound-pro')
);
$actions['revoke'] = sprintf(
'<a href="%s" class="inbound-revoke-api-keys inbound-delete">%s</a>',
esc_url(add_query_arg(array('user_id' => $item['id'], 'inbound_action' => 'revoke-api-keys'))),
__('Revoke', 'inbound-pro')
);
$actions = apply_filters('inbound_api_row_actions', array_filter($actions));
return sprintf('%1$s %2$s', $item['user'], $this->row_actions($actions));
}
public function get_columns() {
$columns = array(
'user' => __('Username', 'inbound-pro'),
'key' => __('Public Key', 'inbound-pro'),
'secret' => __('Secret Key', 'inbound-pro'),
'token' => __('Token', 'inbound-pro')
);
return $columns;
}
public function bulk_actions($which = '') {
}
public function display_controls($which = '') {
static $inbound_api_is_bottom;
if ($inbound_api_is_bottom) {
return;
}
$user = wp_get_current_user();
?>
<form method="post" action="<?php echo admin_url('edit.php?post_type=wp-lead&page=wpleads_global_settings&tab=tabs-wpleads-apikeys'); ?>">
<input type="hidden" name="inbound_action" value="generate-api-keys"/>
<input type='text' name="user_id" placeholder="<?php _e('Enter User ID', 'inbound-pro'); ?>" title="Your Current ID is <?php echo $user->ID; ?> " value="<?php echo $user->ID; ?>">
<?php submit_button(__('Generate New API Keys', 'inbound-pro'), 'secondary', 'submit', false); ?>
<a class='button button-primary' href='http://docs.inboundnow.com/guide/lead-api-documentation-v1/' target='_blank'><?php _e('View Documentation', 'inbound-pro'); ?></a>
</form>
<?php
$inbound_api_is_bottom = true;
}
public function get_paged() {
return isset($_GET['paged']) ? absint($_GET['paged']) : 1;
}
public function query() {
$users = get_users(array(
'meta_key' => 'inbound_user_secret_key',
'number' => $this->per_page,
'offset' => $this->per_page * ($this->get_paged() - 1)
));
$keys = array();
foreach ($users as $user) {
$keys[$user->ID]['id'] = $user->ID;
$keys[$user->ID]['email'] = $user->user_email;
$keys[$user->ID]['user'] = '<a href="' . add_query_arg('user_id', $user->ID, 'user-edit.php') . '"><strong>' . $user->user_login . '</strong></a>';
$keys[$user->ID]['key'] = get_user_meta($user->ID, 'inbound_user_public_key', true);
$keys[$user->ID]['secret'] = get_user_meta($user->ID, 'inbound_user_secret_key', true);
$keys[$user->ID]['token'] = hash('md5', get_user_meta($user->ID, 'inbound_user_secret_key', true) . get_user_meta($user->ID, 'inbound_user_public_key', true));
}
return $keys;
}
public function total_items() {
global $wpdb;
if (!get_transient('inbound_total_api_keys')) {
$total_items = $wpdb->get_var("SELECT count(user_id) FROM $wpdb->usermeta WHERE meta_key='inbound_user_secret_key'");
set_transient('inbound_total_api_keys', $total_items, 60 * 60);
}
return get_transient('inbound_total_api_keys');
}
public function prepare_items() {
$columns = $this->get_columns();
$hidden = array();
$sortable = array();
$this->_column_headers = array($columns, $hidden, $sortable);
$data = $this->query();
$total_items = $this->total_items();
$this->items = $data;
$this->set_pagination_args(array(
'total_items' => $total_items,
'per_page' => $this->per_page,
'total_pages' => ceil($total_items / $this->per_page)
)
);
}
}