| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
if ( is_admin() ) { |
| 9 |
add_action( 'wp_ajax_dfrapi_delete_cached_api_data', 'dfrapi_delete_cached_api_data' ); |
| 10 |
add_action( 'wp_ajax_dfrapi_test_api_connection', 'dfrapi_test_api_connection' ); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Delete cached API data. |
| 15 |
*/ |
| 16 |
function dfrapi_delete_cached_api_data() { |
| 17 |
check_ajax_referer( 'dfrapi_ajax_nonce', 'dfrapi_security' ); |
| 18 |
// Only delete if user has API requests remaining. This is because we'll need to make 1 request to rebuild 'dfrapi_account'. |
| 19 |
$status = dfrapi_api_get_status(); |
| 20 |
if ( ! array_key_exists( 'dfrapi_api_error', $status ) ) { |
| 21 |
delete_option( 'dfrapi_account' ); |
| 22 |
$transient_options = get_option( 'dfrapi_transient_whitelist' ); |
| 23 |
if ( ! empty( $transient_options ) ) { |
| 24 |
foreach ( $transient_options as $name ) { |
| 25 |
$use_cache = wp_using_ext_object_cache( false ); |
| 26 |
delete_transient( $name ); |
| 27 |
wp_using_ext_object_cache( $use_cache ); |
| 28 |
} |
| 29 |
} |
| 30 |
// Update account status immediately in case there are not enough API |
| 31 |
// requests remaining in order to do so later. |
| 32 |
$status = dfrapi_api_get_status(); |
| 33 |
update_option( 'dfrapi_account', $status ); |
| 34 |
} |
| 35 |
_e( 'Cached API data deleted successfully.', 'datafeedr-api' ); |
| 36 |
die; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Test API Connection. |
| 41 |
*/ |
| 42 |
function dfrapi_test_api_connection() { |
| 43 |
|
| 44 |
check_ajax_referer( 'dfrapi_ajax_nonce', 'dfrapi_security' ); |
| 45 |
|
| 46 |
$url = "https://api.datafeedr.com"; |
| 47 |
$response = wp_remote_get( $url ); |
| 48 |
$code = wp_remote_retrieve_response_code( $response ); |
| 49 |
$success = $code == '200'; |
| 50 |
$color = $success ? 'green' : 'red'; |
| 51 |
$heading = $success ? __( 'Success!', 'datafeedr-api' ) : __( 'Failed!', 'datafeedr-api' ); |
| 52 |
$message = $success |
| 53 |
? __( 'Your connection to the Datafeedr API is working.', 'datafeedr-api' ) |
| 54 |
: $response->get_error_message(); |
| 55 |
|
| 56 |
printf( '<h2 style="color:%s">%s</h2><hr /><pre>%s</pre>', esc_attr( $color ), esc_html( $heading ), esc_html( $message ) ); |
| 57 |
|
| 58 |
die; |
| 59 |
} |
| 60 |
|