Skip to content

Commit

Permalink
Improvement in the route and disable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
bipoza committed May 29, 2023
1 parent 148000f commit 271146c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
12 changes: 12 additions & 0 deletions check-connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
global $wpdb;
$result = $wpdb->check_connection();


if ($result) {
header('Content-Type: text/plain; charset=utf-8');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
echo get_option_or_value("wp_uptime_response_value", 'OK');
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Just install the plugin and activate it.
By default, the API is at this address and if mysql is running it will return OK.

```
https://your-site.com/wp-json/wp-uptime/ok
https://your-site.com/ok/
```

## Edition panel
Expand Down
30 changes: 11 additions & 19 deletions wp-uptime.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,30 @@ function get_option_or_value($key, $default_value)
return $response;
}

add_action('rest_api_init', function () {
register_rest_route(
'wp-uptime',
'/' . get_option_or_value("wp_uptime_endpoint_path", 'ok') . '/',
array(
'methods' => 'GET',
'callback' => 'wp_uptime_route_handler',
)
);
});


function wp_uptime_route_handler($request)
add_action('init', 'wpUptimeFeed');
function wpUptimeFeed()
{
global $wpdb;
$result = $wpdb->check_connection();

add_feed(get_option_or_value("wp_uptime_endpoint_path", 'ok'), 'wpUptimeResponse');
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

if ($result) {
echo get_option_or_value("wp_uptime_response_value", 'OK');
}
function wpUptimeResponse()
{
include('check-connection.php');
}

add_action('admin_menu', 'wp_uptime_menu');
function wp_uptime_menu()
{
add_menu_page('WP Uptime Settings', 'WP Uptime', 'manage_options', 'wp-uptime-settings', 'wp_uptime_settings_page');
add_options_page('WP Uptime Settings', 'WP Uptime', 'manage_options', 'wp-uptime-settings', 'wp_uptime_settings_page');
}

function wp_uptime_settings_page()
{
$url = get_rest_url(null, 'wp-uptime/' . get_option_or_value("wp_uptime_endpoint_path", 'ok') . '/');
$url = get_site_url(null, get_option_or_value("wp_uptime_endpoint_path", 'ok') . '/');


?>
Expand Down

0 comments on commit 271146c

Please sign in to comment.