Files
wp-mempool-info/mempool-info/mempool-info.php
2025-06-16 10:15:45 +02:00

85 lines
2.9 KiB
PHP

<?php
/*
Plugin Name: Mempool Info
Description: Show current Bitcoin block height and transaction fee estimates from a configurable mempool.space API endpoint.
Version: 0.2
Author: Martien.io
*/
// Settings page
add_action('admin_menu', function() {
add_options_page('Mempool Info Settings', 'Mempool Info', 'manage_options', 'mempool-info', 'mempool_info_settings_page');
});
add_action('admin_init', function() {
register_setting('mempool_info_options', 'mempool_api_url');
});
function mempool_info_settings_page() {
?>
<div class="wrap">
<h1>Mempool Info Settings</h1>
<form method="post" action="options.php">
<?php settings_fields('mempool_info_options'); ?>
<?php do_settings_sections('mempool_info_options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Mempool API Base URL</th>
<td>
<input type="text" name="mempool_api_url" value="<?php echo esc_attr(get_option('mempool_api_url', 'https://mempool.space/api')); ?>" size="40"/>
<p class="description">Example: <code>https://mempool.space/api</code> or your own hosted API endpoint.</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Helper function to get API base url
function mempool_api_url($endpoint = '') {
$base = rtrim(get_option('mempool_api_url', 'https://mempool.space/api'), '/');
return $base . $endpoint;
}
// Block height shortcode
add_shortcode('mempool_block_height', function() {
$block_url = mempool_api_url('/blocks');
$block_response = wp_remote_get($block_url);
if (is_wp_error($block_response)) {
return "Could not reach mempool API.";
}
$block_json = json_decode(wp_remote_retrieve_body($block_response), true);
$current_block = isset($block_json[0]['height']) ? $block_json[0]['height'] : 'Unknown';
return '<span class="mempool-block-height"><strong>Current Bitcoin block:</strong> ' . esc_html($current_block) . '</span>';
});
// Transaction fees shortcode
add_shortcode('mempool_fees', function() {
$fees_url = mempool_api_url('/v1/fees/recommended');
$fees_response = wp_remote_get($fees_url);
if (is_wp_error($fees_response)) {
return "Could not reach mempool API.";
}
$fees_json = json_decode(wp_remote_retrieve_body($fees_response), true);
if (!isset($fees_json['fastestFee'])) {
return "Fee information not available.";
}
ob_start();
?>
<span class="mempool-fees">
<strong>Transaction fees (sat/vB):</strong><br>
Fastest: <?php echo esc_html($fees_json['fastestFee']); ?><br>
Half hour: <?php echo esc_html($fees_json['halfHourFee']); ?><br>
Hour: <?php echo esc_html($fees_json['hourFee']); ?><br>
</span>
<?php
return ob_get_clean();
});