Completed
Pull Request — master (#1085)
by Devin
19:12
created

Give_API_Keys_Table::display_tablenav()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * API Key Table Class
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Tools/APIKeys
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php 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;
0 ignored issues
show
Unused Code introduced by
The property $keys is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
43
	/**
44
	 * Get things started
45
	 *
46
	 * @since 1.1
47
	 * @see   WP_List_Table::__construct()
48
	 */
49
	public function __construct() {
50
		global $status, $page;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
51
52
		// Set parent defaults
53
		parent::__construct( array(
54
			'singular' => esc_html__( 'API Key', 'give' ),     // Singular name of the listed records
55
			'plural'   => esc_html__( 'API Keys', 'give' ),    // Plural name of the listed records
56
			'ajax'     => false                       // Does this table support ajax?
57
		) );
58
59
		$this->query();
60
	}
61
62
	/**
63
	 * This function renders most of the columns in the list table.
64
	 *
65
	 * @access public
66
	 * @since  1.1
67
	 *
68
	 * @param array $item Contains all the data of the keys
69
	 * @param string $column_name The name of the column
70
	 *
71
	 * @return string Column Name
72
	 */
73
	public function column_default( $item, $column_name ) {
74
		return $item[ $column_name ];
75
	}
76
77
	/**
78
	 * Displays the public key rows
79
	 *
80
	 * @access public
81
	 * @since  1.1
82
	 *
83
	 * @param array $item Contains all the data of the keys
84
	 * @param string $column_name The name of the column
0 ignored issues
show
Bug introduced by
There is no parameter named $column_name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
85
	 *
86
	 * @return string Column Name
87
	 */
88
	public function column_key( $item ) {
89
		return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['key'] ) . '"/>';
90
	}
91
92
	/**
93
	 * Displays the token rows
94
	 *
95
	 * @access public
96
	 * @since  1.1
97
	 *
98
	 * @param array $item Contains all the data of the keys
99
	 * @param string $column_name The name of the column
0 ignored issues
show
Bug introduced by
There is no parameter named $column_name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
100
	 *
101
	 * @return string Column Name
102
	 */
103
	public function column_token( $item ) {
104
		return '<input onClick="this.setSelectionRange(0, this.value.length)" readonly="readonly" type="text" class="large-text" value="' . esc_attr( $item['token'] ) . '"/>';
105
	}
106
107
	/**
108
	 * Displays the secret key rows
109
	 *
110
	 * @access public
111
	 * @since  1.1
112
	 *
113
	 * @param array $item Contains all the data of the keys
114
	 * @param string $column_name The name of the column
0 ignored issues
show
Bug introduced by
There is no parameter named $column_name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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 void
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
					'view'      => 'api_requests',
138
					'post_type' => 'give_forms',
139
					'page'      => 'give-reports',
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
	 * Retrieve the table columns
173
	 *
174
	 * @access public
175
	 * @since  1.1
176
	 * @return array $columns Array of all the list table columns
177
	 */
178
	public function get_columns() {
179
		$columns = array(
180
			'user'   => esc_html__( 'Username', 'give' ),
181
			'key'    => esc_html__( 'Public Key', 'give' ),
182
			'token'  => esc_html__( 'Token', 'give' ),
183
			'secret' => esc_html__( 'Secret Key', 'give' )
184
		);
185
186
		return $columns;
187
	}
188
189
	/**
190
	 * Generate the table navigation above or below the table
191
	 *
192
	 * @since 3.1.0
193
	 * @access protected
194
	 * @param string $which
195
	 */
196
	protected function display_tablenav( $which ) {
197
		if ( 'top' === $which ) {
198
			wp_nonce_field( 'bulk-' . $this->_args['plural'] );
199
		}
200
		?>
201
		<div class="tablenav <?php echo esc_attr( $which ); ?>">
202
203
			<div class="alignleft actions bulkactions">
204
				<?php $this->bulk_actions( $which ); ?>
205
			</div>
206
207
			<?php
208
			$this->extra_tablenav( $which );
209
			$this->pagination( $which );
210
			?>
211
212
			<br class="clear" />
213
		</div>
214
		<?php
215
	}
216
217
	/**
218
	 * Display the key generation form
219
	 *
220
	 * @access public
221
	 * @since  1.1
222
	 * @param  string $which
223
	 * @return void
224
	 */
225
	function bulk_actions( $which = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
226
		// These aren't really bulk actions but this outputs the markup in the right place
227
		static $give_api_is_bottom;
228
229
		if ( $give_api_is_bottom ) {
230
			return;
231
		}
232
		?>
233
		<input type="hidden" name="give_action" value="process_api_key"/>
234
		<input type="hidden" name="give_api_process" value="generate"/>
235
		<?php wp_nonce_field( 'give-api-nonce' ); ?>
236
		<?php echo Give()->html->ajax_user_search(); ?>
237
		<?php submit_button( esc_html__( 'Generate New API Keys', 'give' ), 'secondary', 'submit', false ); ?>
238
		<?php
239
		$give_api_is_bottom = true;
240
	}
241
242
	/**
243
	 * Retrieve the current page number
244
	 *
245
	 * @access public
246
	 * @since  1.1
247
	 * @return int Current page number
248
	 */
249
	public function get_paged() {
250
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
251
	}
252
253
	/**
254
	 * Performs the key query
255
	 *
256
	 * @access public
257
	 * @since  1.1
258
	 * @return array
259
	 */
260
	public function query() {
261
		$users = get_users( array(
262
			'meta_value' => 'give_user_secret_key',
263
			'number'     => $this->per_page,
264
			'offset'     => $this->per_page * ( $this->get_paged() - 1 )
265
		) );
266
		$keys  = array();
267
268
		foreach ( $users as $user ) {
269
			$keys[ $user->ID ]['id']    = $user->ID;
270
			$keys[ $user->ID ]['email'] = $user->user_email;
271
			$keys[ $user->ID ]['user']  = '<a href="' . add_query_arg( 'user_id', $user->ID, 'user-edit.php' ) . '"><strong>' . $user->user_login . '</strong></a>';
272
273
			$keys[ $user->ID ]['key']    = Give()->api->get_user_public_key( $user->ID );
274
			$keys[ $user->ID ]['secret'] = Give()->api->get_user_secret_key( $user->ID );
275
			$keys[ $user->ID ]['token']  = Give()->api->get_token( $user->ID );
276
		}
277
278
		return $keys;
279
	}
280
281
282
	/**
283
	 * Retrieve count of total users with keys
284
	 *
285
	 * @access public
286
	 * @since  1.1
287
	 * @return int
288
	 */
289
	public function total_items() {
290
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
291
292
		if ( ! get_transient( 'give_total_api_keys' ) ) {
293
			$total_items = $wpdb->get_var( "SELECT count(user_id) FROM $wpdb->usermeta WHERE meta_value='give_user_secret_key'" );
294
295
			set_transient( 'give_total_api_keys', $total_items, 60 * 60 );
296
		}
297
298
		return get_transient( 'give_total_api_keys' );
299
	}
300
301
	/**
302
	 * Setup the final data for the table
303
	 *
304
	 * @access public
305
	 * @since  1.1
306
	 * @return void
307
	 */
308
	public function prepare_items() {
309
		$columns = $this->get_columns();
310
311
		$hidden   = array(); // No hidden columns
312
		$sortable = array(); // Not sortable... for now
313
314
		$this->_column_headers = array( $columns, $hidden, $sortable, 'id' );
315
316
		$data = $this->query();
317
318
		$total_items = $this->total_items();
319
320
		$this->items = $data;
321
322
		$this->set_pagination_args( array(
323
				'total_items' => $total_items,
324
				'per_page'    => $this->per_page,
325
				'total_pages' => ceil( $total_items / $this->per_page )
326
			)
327
		);
328
	}
329
}
330