WC_Admin_API_Keys::bulk_revoke_key()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * WooCommerce Admin API Keys Class
4
 *
5
 * @author   WooThemes
6
 * @category Admin
7
 * @package  WooCommerce/Admin
8
 * @version  2.4.0
9
 */
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * WC_Admin_API_Keys.
17
 */
18
class WC_Admin_API_Keys {
19
20
	/**
21
	 * Initialize the API Keys admin actions.
22
	 */
23
	public function __construct() {
24
		add_action( 'admin_init', array( $this, 'actions' ) );
25
	}
26
27
	/**
28
	 * Check if is API Keys settings page.
29
	 *
30
	 * @return bool
31
	 */
32 View Code Duplication
	private function is_api_keys_settings_page() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
		return isset( $_GET['page'] )
34
			&& 'wc-settings' == $_GET['page']
35
			&& isset( $_GET['tab'] )
36
			&& 'api' == $_GET['tab']
37
			&& isset( $_GET['section'] )
38
			&& 'keys' == isset( $_GET['section'] );
39
	}
40
41
	/**
42
	 * Page output.
43
	 */
44
	public static function page_output() {
45
		// Hide the save button
46
		$GLOBALS['hide_save_button'] = true;
47
48
		if ( isset( $_GET['create-key'] ) || isset( $_GET['edit-key'] ) ) {
49
			$key_id   = isset( $_GET['edit-key'] ) ? absint( $_GET['edit-key'] ) : 0;
50
			$key_data = self::get_key_data( $key_id );
51
52
			include( 'settings/views/html-keys-edit.php' );
53
		} else {
54
			self::table_list_output();
55
		}
56
	}
57
58
	/**
59
	 * Table list output.
60
	 */
61 View Code Duplication
	private static function table_list_output() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
		echo '<h2>' . __( 'Keys/Apps', 'woocommerce' ) . ' <a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=api&section=keys&create-key=1' ) ) . '" class="add-new-h2">' . __( 'Add Key', 'woocommerce' ) . '</a></h2>';
63
64
		$keys_table_list = new WC_Admin_API_Keys_Table_List();
65
		$keys_table_list->prepare_items();
66
67
		echo '<input type="hidden" name="page" value="wc-settings" />';
68
		echo '<input type="hidden" name="tab" value="api" />';
69
		echo '<input type="hidden" name="section" value="keys" />';
70
71
		$keys_table_list->views();
72
		$keys_table_list->search_box( __( 'Search Key', 'woocommerce' ), 'key' );
73
		$keys_table_list->display();
74
	}
75
76
	/**
77
	 * Get key data.
78
	 *
79
	 * @param  int $key_id
80
	 * @return array
81
	 */
82
	private static function get_key_data( $key_id ) {
83
		global $wpdb;
84
85
		$empty = array(
86
			'key_id'        => 0,
87
			'user_id'       => '',
88
			'description'   => '',
89
			'permissions'   => '',
90
			'truncated_key' => '',
91
			'last_access'   => ''
92
		);
93
94
		if ( 0 == $key_id ) {
95
			return $empty;
96
		}
97
98
		$key = $wpdb->get_row( $wpdb->prepare( "
99
			SELECT key_id, user_id, description, permissions, truncated_key, last_access
100
			FROM {$wpdb->prefix}woocommerce_api_keys
101
			WHERE key_id = %d
102
		", $key_id ), ARRAY_A );
103
104
		if ( is_null( $key ) ) {
105
			return $empty;
106
		}
107
108
		return $key;
109
	}
110
111
	/**
112
	 * API Keys admin actions.
113
	 */
114
	public function actions() {
115
		if ( $this->is_api_keys_settings_page() ) {
116
			// Revoke key
117
			if ( isset( $_GET['revoke-key'] ) ) {
118
				$this->revoke_key();
119
			}
120
121
			// Bulk actions
122
			if ( isset( $_GET['action'] ) && isset( $_GET['key'] ) ) {
123
				$this->bulk_actions();
124
			}
125
		}
126
	}
127
128
	/**
129
	 * Notices.
130
	 */
131
	public static function notices() {
132
		if ( isset( $_GET['revoked'] ) && 1 == $_GET['revoked'] ) {
133
			WC_Admin_Settings::add_message( __( 'API Key revoked successfully.', 'woocommerce' ) );
134
		}
135
	}
136
137
	/**
138
	 * Revoke key.
139
	 */
140
	private function revoke_key() {
141
		if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'revoke' ) ) {
142
			wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
143
		}
144
145
		$key_id = absint( $_GET['revoke-key'] );
146
		$this->remove_key( $key_id );
147
148
		wp_redirect( esc_url_raw( add_query_arg( array( 'revoked' => 1 ), admin_url( 'admin.php?page=wc-settings&tab=api&section=keys' ) ) ) );
149
		exit();
150
	}
151
152
	/**
153
	 * Bulk actions.
154
	 */
155
	private function bulk_actions() {
156 View Code Duplication
		if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
			wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
158
		}
159
160
		$keys = array_map( 'absint', (array) $_GET['key'] );
161
162
		if ( 'revoke' == $_GET['action'] ) {
163
			$this->bulk_revoke_key( $keys );
164
		}
165
	}
166
167
	/**
168
	 * Bulk revoke key.
169
	 *
170
	 * @param array $keys
171
	 */
172
	private function bulk_revoke_key( $keys ) {
173
		foreach ( $keys as $key_id ) {
174
			$this->remove_key( $key_id );
175
		}
176
	}
177
178
	/**
179
	 * Remove key.
180
	 *
181
	 * @param  int $key_id
182
	 * @return bool
183
	 */
184
	private function remove_key( $key_id ) {
185
		global $wpdb;
186
187
		$delete = $wpdb->delete( $wpdb->prefix . 'woocommerce_api_keys', array( 'key_id' => $key_id ), array( '%d' ) );
188
189
		return $delete;
190
	}
191
}
192
193
new WC_Admin_API_Keys();
194