Skip to content

Commit

Permalink
Change path to rewrite-rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bipoza committed Oct 11, 2024
1 parent 8d01a93 commit 837f1f9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 68 deletions.
55 changes: 55 additions & 0 deletions admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php


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

function wp_uptime_settings_page()
{
$url = get_site_url(null, get_option_or_value("wp_uptime_endpoint_path", 'ok') . '/');
?>
<div class="wrap">
<h1>WP Uptime Settings</h1>
<h3>
Editable non-cached monitorization path for Wordpress
</h3>
<p>Monitorization path: <a href="<?php echo $url ?>" target="_blank"><?php echo $url ?></a></p>
<form method="post" action="options.php">
<?php settings_fields('wp_uptime_settings'); ?>
<?php do_settings_sections('wp-uptime-settings'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Endpoint path</th>
<td><input type="text" name="wp_uptime_endpoint_path"
value="<?php echo esc_attr(get_option_or_value('wp_uptime_endpoint_path', 'ok')); ?>"></td>
</tr>
<tr valign="top">
<th scope="row">OK response value</th>
<td>
<?php echo "<textarea name='wp_uptime_response_value'>" . esc_attr(get_option_or_value('wp_uptime_response_value', 'OK')) . '</textarea>'; ?>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}

add_action('admin_init', 'wp_uptime_settings');

function wp_uptime_settings()
{
register_setting('wp_uptime_settings', 'wp_uptime_endpoint_path');
register_setting('wp_uptime_settings', 'wp_uptime_response_value');
}

register_uninstall_hook(__FILE__, 'wp_uptime_uninstall');
function wp_uptime_uninstall()
{
delete_option('wp_uptime_endpoint_path');
delete_option('wp_uptime_response_value');
}
10 changes: 6 additions & 4 deletions check-connection.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php
global $wpdb;
$result = $wpdb->check_connection();


if ($result) {
// Check the connection:
if (!$wpdb->check_connection($allow_bail = false)) {
die(__("No DB connection"));
} else {
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');
}
}
exit;
2 changes: 2 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
# Silence is golden.
9 changes: 9 additions & 0 deletions utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
function get_option_or_value($key, $default_value)
{
$response = get_option($key);
if (!$response) {
$response = $default_value;
}
return $response;
}
83 changes: 19 additions & 64 deletions wp-uptime.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* WP Uptime
*
Expand All @@ -11,89 +12,43 @@
* Plugin Name: WP Uptime
* Plugin URI: https://github.com/codesyntax/wp-uptime
* Description: Editable non-cached monitorization path for Wordpress.
* Version: 3.0.1
* Version: 4.0.0
* Author: CodeSyntax
* Author URI: https://codesyntax.com
* Text Domain: wp-uptime
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

function get_option_or_value($key, $default_value)
{
$response = get_option($key);
if (!$response) {
$response = $default_value;
}
return $response;
}


require_once plugin_dir_path(__FILE__) . 'admin.php';
require_once plugin_dir_path(__FILE__) . 'utils.php';

add_action('init', 'wpUptimeFeed');
function wpUptimeFeed()
function wp_uptime_query_var($public_query_vars)
{
add_feed(get_option_or_value("wp_uptime_endpoint_path", 'ok'), 'wpUptimeResponse');
global $wp_rewrite;
$wp_rewrite->flush_rules();
$public_query_vars[] = "wp_uptime";
return $public_query_vars;
}

function wpUptimeResponse()
function wp_uptime_request($wp)
{
include('check-connection.php');
}

add_action('admin_menu', 'wp_uptime_menu');
function wp_uptime_menu()
{
add_options_page('WP Uptime Settings', 'WP Uptime', 'manage_options', 'wp-uptime-settings', 'wp_uptime_settings_page');
if (isset($wp->query_vars["wp_uptime"]) && "true" === $wp->query_vars["wp_uptime"]) {
include('check-connection.php');
}
}

function wp_uptime_settings_page()
function wp_uptime_endpoint_rewrite()
{
$url = get_site_url(null, get_option_or_value("wp_uptime_endpoint_path", 'ok') . '/');

add_rewrite_rule("^" . get_option_or_value("wp_uptime_endpoint_path", 'ok') . "$", "index.php?wp_uptime=true", "top");

?>
<div class="wrap">
<h1>WP Uptime Settings</h1>
<h3>
Editable non-cached monitorization path for Wordpress
</h3>
<p>Monitorization path: <a href="<?php echo $url ?>" target="_blank"><?php echo $url ?></a></p>
<form method="post" action="options.php">
<?php settings_fields('wp_uptime_settings'); ?>
<?php do_settings_sections('wp-uptime-settings'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Endpoint path</th>
<td><input type="text" name="wp_uptime_endpoint_path"
value="<?php echo esc_attr(get_option_or_value('wp_uptime_endpoint_path', 'ok')); ?>"></td>
</tr>
<tr valign="top">
<th scope="row">OK response value</th>
<td>
<?php echo "<textarea name='wp_uptime_response_value'>" . esc_attr(get_option_or_value('wp_uptime_response_value', 'OK')) . '</textarea>'; ?>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
flush_rewrite_rules();
}

add_action('admin_init', 'wp_uptime_settings');

function wp_uptime_settings()
function run_wp_uptime_endpoint()
{
register_setting('wp_uptime_settings', 'wp_uptime_endpoint_path');
register_setting('wp_uptime_settings', 'wp_uptime_response_value');
add_filter("query_vars", "wp_uptime_query_var", 10, 1);
add_action("parse_request", "wp_uptime_request", 10, 1);
add_action("init", "wp_uptime_endpoint_rewrite", 10);
}

register_uninstall_hook(__FILE__, 'wp_uptime_uninstall');
function wp_uptime_uninstall()
{
delete_option('wp_uptime_endpoint_path');
delete_option('wp_uptime_response_value');
}
run_wp_uptime_endpoint();

0 comments on commit 837f1f9

Please sign in to comment.