Completed
Pull Request — master (#11616)
by
unknown
07:51
created

WC_Admin_API_Keys::get_key_data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 22
rs 9.2
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
	 * @return bool
30
	 */
31
	private function is_api_keys_settings_page() {
32
		return isset( $_GET['page'] )
33
			&& 'wc-settings' === $_GET['page']
34
			&& isset( $_GET['tab'] )
35
			&& 'api' === $_GET['tab']
36
			&& isset( $_GET['section'] )
37
			&& 'keys' === $_GET['section'];
38
	}
39
40
	/**
41
	 * Page output.
42
	 */
43
	public static function page_output() {
44
		// Hide the save button
45
		$GLOBALS['hide_save_button'] = true;
46
47
		if ( isset( $_GET['create-key'] ) || isset( $_GET['edit-key'] ) ) {
48
			$key_id   = isset( $_GET['edit-key'] ) ? absint( $_GET['edit-key'] ) : 0;
49
			$key_data = self::get_key_data( $key_id );
50
51
			include( 'settings/views/html-keys-edit.php' );
52
		} else {
53
			self::table_list_output();
54
		}
55
	}
56
57
	/**
58
	 * Table list output.
59
	 */
60 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...
61
		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>';
62
63
		$keys_table_list = new WC_Admin_API_Keys_Table_List();
64
		$keys_table_list->prepare_items();
65
66
		echo '<input type="hidden" name="page" value="wc-settings" />';
67
		echo '<input type="hidden" name="tab" value="api" />';
68
		echo '<input type="hidden" name="section" value="keys" />';
69
70
		$keys_table_list->views();
71
		$keys_table_list->search_box( __( 'Search Key', 'woocommerce' ), 'key' );
72
		$keys_table_list->display();
73
	}
74
75
	/**
76
	 * Get key data.
77
	 *
78
	 * @param  int $key_id
79
	 * @return array
80
	 */
81
	private static function get_key_data( $key_id ) {
82
		$empty = array(
83
			'key_id'        => 0,
84
			'user_id'       => '',
85
			'description'   => '',
86
			'permissions'   => '',
87
			'truncated_key' => '',
88
			'last_access'   => ''
89
		);
90
91
		if ( 0 == $key_id ) {
92
			return $empty;
93
		}
94
95
		$key = WC_Auth::get_api_key_data( $key_id );
96
97
		if ( ! $key ) {
98
			return $empty;
99
		}
100
101
		return (array)$key;
102
	}
103
104
	/**
105
	 * API Keys admin actions.
106
	 */
107
	public function actions() {
108
		if ( $this->is_api_keys_settings_page() ) {
109
			// Revoke key
110
			if ( isset( $_GET['revoke-key'] ) ) {
111
				$this->revoke_key();
112
			}
113
114
			// Bulk actions
115
			if ( isset( $_GET['action'] ) && isset( $_GET['key'] ) ) {
116
				$this->bulk_actions();
117
			}
118
		}
119
	}
120
121
	/**
122
	 * Notices.
123
	 */
124
	public static function notices() {
125
		if ( isset( $_GET['revoked'] ) && 1 == $_GET['revoked'] ) {
126
			WC_Admin_Settings::add_message( __( 'API Key revoked successfully.', 'woocommerce' ) );
127
		}
128
	}
129
130
	/**
131
	 * Revoke key.
132
	 */
133
	private function revoke_key() {
134
		if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'revoke' ) ) {
135
			wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
136
		}
137
138
		$key_id = absint( $_GET['revoke-key'] );
139
		$this->remove_key( $key_id );
140
141
		wp_redirect( esc_url_raw( add_query_arg( array( 'revoked' => 1 ), admin_url( 'admin.php?page=wc-settings&tab=api&section=keys' ) ) ) );
142
		exit();
143
	}
144
145
	/**
146
	 * Bulk actions.
147
	 */
148
	private function bulk_actions() {
149 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...
150
			wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
151
		}
152
153
		$keys = array_map( 'absint', (array) $_GET['key'] );
154
155
		if ( 'revoke' == $_GET['action'] ) {
156
			$this->bulk_revoke_key( $keys );
157
		}
158
	}
159
160
	/**
161
	 * Bulk revoke key.
162
	 *
163
	 * @param array $keys
164
	 */
165
	private function bulk_revoke_key( $keys ) {
166
		foreach ( $keys as $key_id ) {
167
			$this->remove_key( $key_id );
168
		}
169
	}
170
171
	/**
172
	 * Remove key.
173
	 *
174
	 * @param  int $key_id
175
	 * @return bool
176
	 */
177
	private function remove_key( $key_id ) {
178
		return WC_Auth::delete_api_key( $key_id );
179
	}
180
}
181
182
new WC_Admin_API_Keys();
183