PluginProbe
Datafeedr API / 1.4.3
Datafeedr API v1.4.3
1.4.3 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 All 182 releases
datafeedr-api / hooks / admin / ajax.php

ajax.php in Datafeedr API 1.4.3, at hooks/admin/ajax.php

94 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || exit;
4
5 /**
6 * Returns the capability required to use the Datafeedr API Tools AJAX actions.
7 *
8 * @since 1.4.3
9 *
10 * @return string
11 */
12 function dfrapi_tools_capability(): string {
13
14 /**
15 * Filters the capability required to delete cached API data and test the API connection.
16 *
17 * @since 1.4.3
18 *
19 * @param string $capability Default 'manage_options'.
20 */
21 return (string) apply_filters( 'dfrapi_tools_capability', 'manage_options' );
22 }
23
24 /**
25 * Delete cached API data.
26 *
27 * We can only delete if user has API requests remaining.
28 * This is because we'll need to make 1 request to rebuild 'dfrapi_account'.
29 */
30 function dfrapi_delete_cached_api_data() {
31
32 check_ajax_referer( 'dfrapi_ajax_nonce', 'dfrapi_security' );
33
34 if ( ! current_user_can( dfrapi_tools_capability() ) ) {
35 _e( 'You do not have permission to perform this action.', 'datafeedr-api' );
36 die;
37 }
38
39 $status = dfrapi_api_get_status();
40
41 if ( ! array_key_exists( 'dfrapi_api_error', $status ) ) {
42
43 delete_option( 'dfrapi_account' );
44 $transient_options = get_option( 'dfrapi_transient_whitelist' );
45
46 if ( ! empty( $transient_options ) ) {
47 foreach ( $transient_options as $name ) {
48 $use_cache = wp_using_ext_object_cache( false );
49 delete_transient( $name );
50 wp_using_ext_object_cache( $use_cache );
51 }
52 }
53
54 // Update account status immediately in case there are not enough API
55 // requests remaining in order to do so later.
56 $status = dfrapi_api_get_status();
57 update_option( 'dfrapi_account', $status );
58 }
59
60 _e( 'Cached API data deleted successfully.', 'datafeedr-api' );
61 die;
62 }
63
64 add_action( 'wp_ajax_dfrapi_delete_cached_api_data', 'dfrapi_delete_cached_api_data' );
65
66 /**
67 * Test API Connection.
68 */
69 function dfrapi_test_api_connection() {
70
71 check_ajax_referer( 'dfrapi_ajax_nonce', 'dfrapi_security' );
72
73 if ( ! current_user_can( dfrapi_tools_capability() ) ) {
74 _e( 'You do not have permission to perform this action.', 'datafeedr-api' );
75 die;
76 }
77
78 $url = "https://api.datafeedr.com";
79 $response = wp_remote_get( $url );
80 $code = wp_remote_retrieve_response_code( $response );
81 $success = $code === 200;
82 $color = $success ? 'green' : 'red';
83 $heading = $success ? __( 'Success!', 'datafeedr-api' ) : __( 'Failed!', 'datafeedr-api' );
84 $message = $success
85 ? __( 'Your connection to the Datafeedr API is working.', 'datafeedr-api' )
86 : $response->get_error_message();
87
88 printf( '<h2 style="color:%s">%s</h2><hr /><pre>%s</pre>', esc_attr( $color ), esc_html( $heading ), esc_html( $message ) );
89
90 die;
91 }
92
93 add_action( 'wp_ajax_dfrapi_test_api_connection', 'dfrapi_test_api_connection' );
94