PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.5.12
GiveWP – Donation Plugin and Fundraising Platform v2.5.12
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 6 years ago donors 6 years ago emails 6 years ago forms 6 years ago payments 6 years ago reports 7 years ago settings 6 years ago shortcodes 6 years ago tools 6 years ago upgrades 6 years ago views 6 years ago abstract-admin-settings-page.php 6 years ago admin-actions.php 6 years ago admin-filters.php 7 years ago admin-footer.php 6 years ago admin-pages.php 6 years ago class-addon-activation-banner.php 6 years ago class-admin-settings.php 6 years ago class-api-keys-table.php 7 years ago class-blank-slate.php 6 years ago class-give-admin.php 6 years ago class-give-html-elements.php 7 years ago class-give-welcome.php 6 years ago class-i18n-module.php 6 years ago dashboard-widgets.php 6 years ago give-metabox-functions.php 6 years ago import-functions.php 7 years ago misc-functions.php 6 years ago plugins.php 6 years ago setting-page-functions.php 6 years ago welcome.php 6 years ago
class-api-keys-table.php
362 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( array(
57 'singular' => esc_html__( 'API Key', 'give' ), // Singular name of the listed records
58 'plural' => esc_html__( 'API Keys', 'give' ), // Plural name of the listed records
59 'ajax' => false, // Does this table support ajax?
60 ) );
61
62 $this->query();
63 }
64
65 /**
66 * This function renders most of the columns in the list table.
67 *
68 * @access public
69 * @since 1.1
70 *
71 * @param array $item Contains all the data of the keys
72 * @param string $column_name The name of the column
73 *
74 * @return string Column Name
75 */
76 public function column_default( $item, $column_name ) {
77 return $item[ $column_name ];
78 }
79
80 /**
81 * Displays the public key rows
82 *
83 * @access public
84 * @since 1.1
85 *
86 * @param array $item Contains all the data of the keys
87 *
88 * @return string Column Name
89 */
90 public function column_key( $item ) {
91 return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['key'] ) . '"/>';
92 }
93
94 /**
95 * Displays the token rows
96 *
97 * @access public
98 * @since 1.1
99 *
100 * @param array $item Contains all the data of the keys
101 *
102 * @return string Column Name
103 */
104 public function column_token( $item ) {
105 return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['token'] ) . '"/>';
106 }
107
108 /**
109 * Displays the secret key rows
110 *
111 * @access public
112 * @since 1.1
113 *
114 * @param array $item Contains all the data of the keys
115 *
116 * @return string Column Name
117 */
118 public function column_secret( $item ) {
119 return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['secret'] ) . '"/>';
120 }
121
122 /**
123 * Renders the column for the user field
124 *
125 * @access public
126 * @since 1.1
127 * @return string
128 */
129 public function column_user( $item ) {
130
131 $actions = array();
132
133 if ( apply_filters( 'give_api_log_requests', true ) ) {
134 $actions['view'] = sprintf(
135 '<a href="%s">%s</a>',
136 esc_url( add_query_arg( array(
137 'section' => 'api_requests',
138 'post_type' => 'give_forms',
139 'page' => 'give-tools',
140 'tab' => 'logs',
141 's' => $item['email'],
142 ), 'edit.php' ) ),
143 esc_html__( 'View API Log', 'give' )
144 );
145 }
146
147 $actions['reissue'] = sprintf(
148 '<a href="%s" class="give-regenerate-api-key">%s</a>',
149 esc_url( wp_nonce_url( add_query_arg( array(
150 'user_id' => $item['id'],
151 'give_action' => 'process_api_key',
152 'give_api_process' => 'regenerate',
153 ) ), 'give-api-nonce' ) ),
154 esc_html__( 'Reissue', 'give' )
155 );
156 $actions['revoke'] = sprintf(
157 '<a href="%s" class="give-revoke-api-key give-delete">%s</a>',
158 esc_url( wp_nonce_url( add_query_arg( array(
159 'user_id' => $item['id'],
160 'give_action' => 'process_api_key',
161 'give_api_process' => 'revoke',
162 ) ), 'give-api-nonce' ) ),
163 esc_html__( 'Revoke', 'give' )
164 );
165
166 $actions = apply_filters( 'give_api_row_actions', array_filter( $actions ) );
167
168 return sprintf( '%1$s %2$s', $item['user'], $this->row_actions( $actions ) );
169 }
170
171 /**
172 * Gets the name of the primary column.
173 *
174 * @since 1.5
175 * @access protected
176 *
177 * @return string Name of the primary column.
178 */
179 protected function get_primary_column_name() {
180 return 'user';
181 }
182
183 /**
184 * Retrieve the table columns
185 *
186 * @access public
187 * @since 1.1
188 * @return array $columns Array of all the list table columns
189 */
190 public function get_columns() {
191 $columns = array(
192 'user' => esc_html__( 'Username', 'give' ),
193 'key' => esc_html__( 'Public Key', 'give' ),
194 'token' => esc_html__( 'Token', 'give' ),
195 'secret' => esc_html__( 'Secret Key', 'give' ),
196 );
197
198 return $columns;
199 }
200
201 /**
202 * Generate the table navigation above or below the table
203 *
204 * @since 3.1.0
205 * @access protected
206 *
207 * @param string $which
208 */
209 protected function display_tablenav( $which ) {
210 if ( 'top' === $which ) {
211 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
212 }
213 ?>
214 <div class="tablenav <?php echo esc_attr( $which ); ?>">
215
216 <div class="alignleft actions bulkactions">
217 <?php $this->bulk_actions( $which ); ?>
218 </div>
219
220 <?php
221 $this->extra_tablenav( $which );
222 $this->pagination( $which );
223 ?>
224
225 <br class="clear"/>
226 </div>
227 <?php
228 }
229
230 /**
231 * Display the key generation form
232 *
233 * @access public
234 * @since 1.1
235 *
236 * @param string $which
237 *
238 * @return void
239 */
240 function bulk_actions( $which = '' ) {
241 // These aren't really bulk actions but this outputs the markup in the right place.
242 static $give_api_is_bottom;
243
244 if ( $give_api_is_bottom ) {
245 return;
246 }
247 ?>
248 <input type="hidden" name="give_action" value="process_api_key"/>
249 <input type="hidden" name="give_api_process" value="generate"/>
250 <?php
251 wp_nonce_field( 'give-api-nonce' );
252 /**
253 * API Key user search.
254 */
255 $args = array(
256 'id' => 'give-api-user-search',
257 'name' => 'user_id',
258 );
259
260 echo Give()->html->ajax_user_search( $args );
261
262 submit_button( esc_html__( 'Generate New API Keys', 'give' ), 'secondary', 'submit', false );
263
264 $give_api_is_bottom = true;
265 }
266
267 /**
268 * Retrieve the current page number
269 *
270 * @access public
271 * @since 1.1
272 * @return int Current page number
273 */
274 public function get_paged() {
275 return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
276 }
277
278 /**
279 * Performs the key query
280 *
281 * @access public
282 * @since 1.1
283 * @return array
284 */
285 public function query() {
286 $users = get_users( array(
287 'meta_value' => 'give_user_secret_key',
288 'number' => $this->per_page,
289 'offset' => $this->per_page * ( $this->get_paged() - 1 ),
290 ) );
291 $keys = array();
292
293 foreach ( $users as $user ) {
294 $keys[ $user->ID ]['id'] = $user->ID;
295 $keys[ $user->ID ]['email'] = $user->user_email;
296 $keys[ $user->ID ]['user'] = '<a href="' . add_query_arg( 'user_id', $user->ID, 'user-edit.php' ) . '"><strong>' . $user->user_login . '</strong></a>';
297
298 $keys[ $user->ID ]['key'] = Give()->api->get_user_public_key( $user->ID );
299 $keys[ $user->ID ]['secret'] = Give()->api->get_user_secret_key( $user->ID );
300 $keys[ $user->ID ]['token'] = Give()->api->get_token( $user->ID );
301 }
302
303 return $keys;
304 }
305
306
307 /**
308 * Retrieve count of total users with keys
309 *
310 * @access public
311 * @since 1.1
312 * @return int
313 */
314 public function total_items() {
315 global $wpdb;
316
317 $total_items = Give_Cache::get( 'give_total_api_keys', true );
318
319 if ( ! $total_items ) {
320 $total_items = $wpdb->get_var(
321 $wpdb->prepare(
322 "SELECT count(user_id)
323 FROM {$wpdb->usermeta} WHERE meta_value=%s",
324 'give_user_secret_key'
325 )
326 );
327
328 Give_Cache::set( 'give_total_api_keys', $total_items, HOUR_IN_SECONDS, true );
329 }
330
331 return $total_items;
332 }
333
334 /**
335 * Setup the final data for the table
336 *
337 * @access public
338 * @since 1.1
339 * @return void
340 */
341 public function prepare_items() {
342 $columns = $this->get_columns();
343
344 $hidden = array(); // No hidden columns
345 $sortable = array(); // Not sortable... for now
346
347 $this->_column_headers = array( $columns, $hidden, $sortable );
348
349 $data = $this->query();
350
351 $total_items = $this->total_items();
352
353 $this->items = $data;
354
355 $this->set_pagination_args( array(
356 'total_items' => $total_items,
357 'per_page' => $this->per_page,
358 'total_pages' => ceil( $total_items / $this->per_page ),
359 ) );
360 }
361 }
362