PluginProbe
Datafeedr API / 1.1.2
Datafeedr API v1.1.2
1.4.2 1.0.125 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.3 1.0.30 1.0.31 1.0.32 1.0.33 All 181 releases
datafeedr-api / functions / ajax.php

ajax.php in Datafeedr API 1.1.2, at functions/ajax.php

60 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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