Skip to content

Commit ce0d332

Browse files
feat: Show API key usage information (#13)
1 parent 3fc2e6f commit ce0d332

3 files changed

Lines changed: 78 additions & 24 deletions

File tree

plugin/README.txt

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Contributors: andreaswittig,michaelwittig
33
Tags: malware,virus,antivirus,malware scanner,security
44
Requires at least: 6.0
5-
Tested up to: 6.8
6-
Stable tag: 1.5
5+
Tested up to: 6.9
6+
Stable tag: 1.7
77
License: GPLv2
88
License URI: http://www.gnu.org/licenses/gpl-2.0.html
99

@@ -37,7 +37,11 @@ Go to [attachmentAV for WordPress Setup Guide](https://attachmentav.com/help/set
3737

3838
= Which file types are supported? =
3939

40-
attachmentAV scans all file types. The maximum file size is 10 MB.
40+
attachmentAV scans all file types.
41+
42+
= What's the maximum supported file size? =
43+
44+
The maximum file size is 10 MB.
4145

4246
= Which upload methods are covered? =
4347

@@ -57,11 +61,18 @@ attachmentAV scans all files uploaded via:
5761

5862
== Changelog ==
5963

60-
= 1.5.2 =
64+
= 1.7.0 =
65+
* Show API key usage information
66+
* Support Wordpress 6.9
67+
68+
= 1.6.0 =
6169
* Minor improvements
6270

71+
= 1.5.2 =
72+
* Bug fixes
73+
6374
= 1.5.1 =
64-
* Small fixes
75+
* Bug fixes
6576

6677
= 1.5.0 =
6778
* Support for plugin Drag and Drop Multiple File Upload for Contact Form 7 added
@@ -73,16 +84,16 @@ attachmentAV scans all files uploaded via:
7384
* Support Wordpress 6.8
7485

7586
= 1.2.1 =
76-
* Minor fixes
87+
* Bug fixes
7788

7889
= 1.2.0 =
7990
* Support for plugin Formidable Forms added
8091

8192
= 1.1.2 =
82-
* Small fixes
93+
* Bug fixes
8394

8495
= 1.1.1 =
85-
* Small fixes
96+
* Bug fixes
8697

8798
= 1.1.0 =
8899
* Support for plugin WPForms added
@@ -92,16 +103,16 @@ attachmentAV scans all files uploaded via:
92103
* Adding scan results to metadata
93104

94105
= 1.0.4 =
95-
* Small fixes
106+
* Bug fixes
96107

97108
= 1.0.3 =
98-
* Small fixes
109+
* Bug fixes
99110

100111
= 1.0.2 =
101-
* Small fixes
112+
* Bug fixes
102113

103114
= 1.0.1 =
104-
* Small fixes
115+
* Bug fixes
105116

106117
= 1.0 =
107118
* Initial Release

plugin/admin/class-attachmentav-admin.php

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function create_admin_page() {
8282
<form method="post" action="options.php">
8383
<?php
8484
// This prints out all hidden setting fields
85+
settings_errors( 'attachmentav' );
8586
settings_fields( 'attachmentav' );
8687
do_settings_sections( 'attachmentav' );
8788
submit_button();
@@ -98,7 +99,7 @@ public function page_init() {
9899
register_setting(
99100
'attachmentav', // Option group
100101
'attachmentav_api_key', // Option name
101-
array( $this, 'sanitize_text' ) // Sanitize
102+
array( $this, 'sanitize_api_key' ) // Sanitize
102103
);
103104

104105
register_setting(
@@ -203,15 +204,36 @@ public function page_init() {
203204
);
204205
}
205206

206-
/**
207-
* Sanitize each setting field as needed
208-
*
209-
* @param array $input Contains all settings fields as array keys
210-
*/
211207
public function sanitize_text( $input ) {
212208
return sanitize_text_field($input);
213209
}
214210

211+
public function sanitize_api_key( $input ) {
212+
$api_key = sanitize_text_field($input);
213+
$response = wp_remote_get('https://eu.developer.attachmentav.com/v1/test', array(
214+
'timeout' => 30,
215+
'headers' => array(
216+
'x-api-key' => $api_key,
217+
'x-wordpress-site-url' => get_site_url(),
218+
),
219+
));
220+
if (is_wp_error($response)) {
221+
$error_messages = implode(',', $response->get_error_messages());
222+
add_settings_error('attachmentav_api_key', 'attachmentav_api_key', "Failure ({$error_messages}", 'error');
223+
return get_option( 'attachmentav_api_key' );
224+
} else {
225+
if ($response['response']['code'] == 204) {
226+
return $api_key;
227+
} else if ($response['response']['code'] == 401) {
228+
add_settings_error('attachmentav_api_key', 'attachmentav_api_key', 'License key is invalid', 'error');
229+
return get_option( 'attachmentav_api_key' );
230+
} else {
231+
add_settings_error('attachmentav_api_key', 'attachmentav_api_key', "Unknown error ({$response['response']['code']}", 'error');
232+
return get_option( 'attachmentav_api_key' );
233+
}
234+
}
235+
}
236+
215237
public function print_subscription_section()
216238
{
217239
print '<a target="_blank" href="https://attachmentav.com/subscribe/wordpress/">Subscribe to attachmentAV</a> and enter the API key below.';
@@ -233,10 +255,31 @@ public function print_plugins_section()
233255
}
234256

235257
public function print_api_key() {
236-
printf(
237-
'<input type="text" name="attachmentav_api_key" value="%s" />', esc_attr(get_option('attachmentav_api_key'))
238-
);
239-
258+
printf(
259+
'<input type="text" name="attachmentav_api_key" value="%s" />', esc_attr(get_option('attachmentav_api_key'))
260+
);
261+
$response = wp_remote_get('https://eu.developer.attachmentav.com/v1/usage', array(
262+
'timeout' => 30,
263+
'headers' => array(
264+
'x-api-key' => get_option('attachmentav_api_key'),
265+
'x-wordpress-site-url' => get_site_url(),
266+
),
267+
));
268+
if (is_wp_error($response)) {
269+
$error_messages = implode(',', $response->get_error_messages());
270+
printf("Failed to get usage ({$error_messages}). Please try again later or contact hello@attachmentav.com for help.");
271+
} else {
272+
if ($response['response']['code'] == 200) {
273+
$body = json_decode($response['body'], true);
274+
$usage = $body['quota']['limit']-$body['credits'];
275+
$period = strtolower($body['quota']['period']);
276+
printf("You used {$usage} of your {$body['quota']['limit']} scans this {$period}.");
277+
} else if ($response['response']['code'] == 401) {
278+
printf("Could not get usage as license key is missing or invalid. Add a valid license key.");
279+
} else {
280+
printf("Failed to get usage due to unknown error ({$response['response']['code']}). Please try again later or contact hello@attachmentav.com for help.");
281+
}
282+
}
240283
}
241284

242285
public function print_block_unscannable() {

plugin/attachmentav.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Plugin Name: attachmentAV
1717
* Plugin URI: https://attachmentav.com/solution/malware-protection-for-wordpress/
1818
* Description: Protect your blog from malware. Scan attachments for viruses, worms, and trojans by sending them to the attachmentAV API powered by Sophos. To get started, please go to your <a href="/wp-admin/admin.php?page=attachmentav">attachmentAV Settings page</a> to set up your API key.
19-
* Version: 1.5.1
19+
* Version: 1.7.0
2020
* Author: widdix GmbH
2121
* License: GPL-2.0+
2222
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
@@ -34,7 +34,7 @@
3434
* Start at version 1.0.0 and use SemVer - https://semver.org
3535
* Rename this for your plugin and update it as you release new versions.
3636
*/
37-
define( 'ATTACHMENTAV_VERSION', '1.5.1' );
37+
define( 'ATTACHMENTAV_VERSION', '1.7.0' );
3838

3939
/**
4040
* The code that runs during plugin activation.

0 commit comments

Comments
 (0)