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