PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.19.2
GiveWP – Donation Plugin and Fundraising Platform v2.19.2
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / admin / class-api-keys-table.php
give / includes / admin Last commit date
add-ons 4 years ago donors 5 years ago emails 4 years ago forms 4 years ago payments 4 years ago reports 5 years ago settings 4 years ago shortcodes 5 years ago tools 4 years ago upgrades 5 years ago views 4 years ago abstract-admin-settings-page.php 6 years ago admin-actions.php 4 years ago admin-filters.php 6 years ago admin-footer.php 5 years ago admin-pages.php 4 years ago class-addon-activation-banner.php 6 years ago class-admin-settings.php 4 years ago class-api-keys-table.php 6 years ago class-blank-slate.php 6 years ago class-give-admin.php 5 years ago class-give-html-elements.php 6 years ago class-i18n-module.php 6 years ago dashboard-widgets.php 6 years ago give-metabox-functions.php 4 years ago import-functions.php 5 years ago misc-functions.php 5 years ago plugins.php 5 years ago setting-page-functions.php 6 years ago
class-api-keys-table.php
387 lines
1 <?php
2 /**
3 * API Key Table Class
4 *
5 * @package Give
6 * @subpackage Admin/Tools/APIKeys
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.1
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 // Load WP_List_Table if not loaded
18 if ( ! class_exists( 'WP_List_Table' ) ) {
19 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
20 }
21
22 /**
23 * Give_API_Keys_Table Class
24 *
25 * Renders the API Keys table
26 *
27 * @since 1.1
28 */
29 class Give_API_Keys_Table extends WP_List_Table {
30
31 /**
32 * @var int Number of items per page
33 * @since 1.1
34 */
35 public $per_page = 30;
36
37 /**
38 * @var object Query results
39 * @since 1.1
40 */
41 private $keys;
42
43 /**
44 * Get things started
45 *
46 * @since 1.1
47 * @see WP_List_Table::__construct()
48 *
49 * @global $status
50 * @global $page
51 */
52 public function __construct() {
53 global $status, $page;
54
55 // Set parent defaults
56 parent::__construct(
57 array(
58 'singular' => esc_html__( 'API Key', 'give' ), // Singular name of the listed records
59 'plural' => esc_html__( 'API Keys', 'give' ), // Plural name of the listed records
60 'ajax' => false, // Does this table support ajax?
61 )
62 );
63
64 $this->query();
65 }
66
67 /**
68 * This function renders most of the columns in the list table.
69 *
70 * @access public
71 * @since 1.1
72 *
73 * @param array $item Contains all the data of the keys
74 * @param string $column_name The name of the column
75 *
76 * @return string Column Name
77 */
78 public function column_default( $item, $column_name ) {
79 return $item[ $column_name ];
80 }
81
82 /**
83 * Displays the public key rows
84 *
85 * @access public
86 * @since 1.1
87 *
88 * @param array $item Contains all the data of the keys
89 *
90 * @return string Column Name
91 */
92 public function column_key( $item ) {
93 return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['key'] ) . '"/>';
94 }
95
96 /**
97 * Displays the token rows
98 *
99 * @access public
100 * @since 1.1
101 *
102 * @param array $item Contains all the data of the keys
103 *
104 * @return string Column Name
105 */
106 public function column_token( $item ) {
107 return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['token'] ) . '"/>';
108 }
109
110 /**
111 * Displays the secret key rows
112 *
113 * @access public
114 * @since 1.1
115 *
116 * @param array $item Contains all the data of the keys
117 *
118 * @return string Column Name
119 */
120 public function column_secret( $item ) {
121 return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['secret'] ) . '"/>';
122 }
123
124 /**
125 * Renders the column for the user field
126 *
127 * @access public
128 * @since 1.1
129 * @return string
130 */
131 public function column_user( $item ) {
132
133 $actions = array();
134
135 if ( apply_filters( 'give_api_log_requests', true ) ) {
136 $actions['view'] = sprintf(
137 '<a href="%s">%s</a>',
138 esc_url(
139 add_query_arg(
140 array(
141 'section' => 'api_requests',
142 'post_type' => 'give_forms',
143 'page' => 'give-tools',
144 'tab' => 'logs',
145 's' => $item['email'],
146 ),
147 'edit.php'
148 )
149 ),
150 esc_html__( 'View API Log', 'give' )
151 );
152 }
153
154 $actions['reissue'] = sprintf(
155 '<a href="%s" class="give-regenerate-api-key">%s</a>',
156 esc_url(
157 wp_nonce_url(
158 add_query_arg(
159 array(
160 'user_id' => $item['id'],
161 'give_action' => 'process_api_key',
162 'give_api_process' => 'regenerate',
163 )
164 ),
165 'give-api-nonce'
166 )
167 ),
168 esc_html__( 'Reissue', 'give' )
169 );
170 $actions['revoke'] = sprintf(
171 '<a href="%s" class="give-revoke-api-key give-delete">%s</a>',
172 esc_url(
173 wp_nonce_url(
174 add_query_arg(
175 array(
176 'user_id' => $item['id'],
177 'give_action' => 'process_api_key',
178 'give_api_process' => 'revoke',
179 )
180 ),
181 'give-api-nonce'
182 )
183 ),
184 esc_html__( 'Revoke', 'give' )
185 );
186
187 $actions = apply_filters( 'give_api_row_actions', array_filter( $actions ) );
188
189 return sprintf( '%1$s %2$s', $item['user'], $this->row_actions( $actions ) );
190 }
191
192 /**
193 * Gets the name of the primary column.
194 *
195 * @since 1.5
196 * @access protected
197 *
198 * @return string Name of the primary column.
199 */
200 protected function get_primary_column_name() {
201 return 'user';
202 }
203
204 /**
205 * Retrieve the table columns
206 *
207 * @access public
208 * @since 1.1
209 * @return array $columns Array of all the list table columns
210 */
211 public function get_columns() {
212 $columns = array(
213 'user' => esc_html__( 'Username', 'give' ),
214 'key' => esc_html__( 'Public Key', 'give' ),
215 'token' => esc_html__( 'Token', 'give' ),
216 'secret' => esc_html__( 'Secret Key', 'give' ),
217 );
218
219 return $columns;
220 }
221
222 /**
223 * Generate the table navigation above or below the table
224 *
225 * @since 3.1.0
226 * @access protected
227 *
228 * @param string $which
229 */
230 protected function display_tablenav( $which ) {
231 if ( 'top' === $which ) {
232 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
233 }
234 ?>
235 <div class="tablenav <?php echo esc_attr( $which ); ?>">
236
237 <div class="alignleft actions bulkactions">
238 <?php $this->bulk_actions( $which ); ?>
239 </div>
240
241 <?php
242 $this->extra_tablenav( $which );
243 $this->pagination( $which );
244 ?>
245
246 <br class="clear"/>
247 </div>
248 <?php
249 }
250
251 /**
252 * Display the key generation form
253 *
254 * @access public
255 * @since 1.1
256 *
257 * @param string $which
258 *
259 * @return void
260 */
261 function bulk_actions( $which = '' ) {
262 // These aren't really bulk actions but this outputs the markup in the right place.
263 static $give_api_is_bottom;
264
265 if ( $give_api_is_bottom ) {
266 return;
267 }
268 ?>
269 <input type="hidden" name="give_action" value="process_api_key"/>
270 <input type="hidden" name="give_api_process" value="generate"/>
271 <?php
272 wp_nonce_field( 'give-api-nonce' );
273 /**
274 * API Key user search.
275 */
276 $args = array(
277 'id' => 'give-api-user-search',
278 'name' => 'user_id',
279 );
280
281 echo Give()->html->ajax_user_search( $args );
282
283 submit_button( esc_html__( 'Generate New API Keys', 'give' ), 'secondary', 'submit', false );
284
285 $give_api_is_bottom = true;
286 }
287
288 /**
289 * Retrieve the current page number
290 *
291 * @access public
292 * @since 1.1
293 * @return int Current page number
294 */
295 public function get_paged() {
296 return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
297 }
298
299 /**
300 * Performs the key query
301 *
302 * @access public
303 * @since 1.1
304 * @return array
305 */
306 public function query() {
307 $users = get_users(
308 array(
309 'meta_value' => 'give_user_secret_key',
310 'number' => $this->per_page,
311 'offset' => $this->per_page * ( $this->get_paged() - 1 ),
312 )
313 );
314 $keys = array();
315
316 foreach ( $users as $user ) {
317 $keys[ $user->ID ]['id'] = $user->ID;
318 $keys[ $user->ID ]['email'] = $user->user_email;
319 $keys[ $user->ID ]['user'] = '<a href="' . add_query_arg( 'user_id', $user->ID, 'user-edit.php' ) . '"><strong>' . $user->user_login . '</strong></a>';
320
321 $keys[ $user->ID ]['key'] = Give()->api->get_user_public_key( $user->ID );
322 $keys[ $user->ID ]['secret'] = Give()->api->get_user_secret_key( $user->ID );
323 $keys[ $user->ID ]['token'] = Give()->api->get_token( $user->ID );
324 }
325
326 return $keys;
327 }
328
329
330 /**
331 * Retrieve count of total users with keys
332 *
333 * @access public
334 * @since 1.1
335 * @return int
336 */
337 public function total_items() {
338 global $wpdb;
339
340 $total_items = Give_Cache::get( 'give_total_api_keys', true );
341
342 if ( ! $total_items ) {
343 $total_items = $wpdb->get_var(
344 $wpdb->prepare(
345 "SELECT count(user_id)
346 FROM {$wpdb->usermeta} WHERE meta_value=%s",
347 'give_user_secret_key'
348 )
349 );
350
351 Give_Cache::set( 'give_total_api_keys', $total_items, HOUR_IN_SECONDS, true );
352 }
353
354 return $total_items;
355 }
356
357 /**
358 * Setup the final data for the table
359 *
360 * @access public
361 * @since 1.1
362 * @return void
363 */
364 public function prepare_items() {
365 $columns = $this->get_columns();
366
367 $hidden = array(); // No hidden columns
368 $sortable = array(); // Not sortable... for now
369
370 $this->_column_headers = array( $columns, $hidden, $sortable );
371
372 $data = $this->query();
373
374 $total_items = $this->total_items();
375
376 $this->items = $data;
377
378 $this->set_pagination_args(
379 array(
380 'total_items' => $total_items,
381 'per_page' => $this->per_page,
382 'total_pages' => ceil( $total_items / $this->per_page ),
383 )
384 );
385 }
386 }
387