'Security check failed.' ] );
}
$raw = strtoupper( sanitize_text_field( $_POST['postcode'] ?? '' ) );
$postcode = preg_replace( '/\s+/', '', $raw );
// Validate UK postcode format
if ( ! preg_match( '/^[A-Z]{1,2}[0-9][0-9A-Z]?[0-9][A-Z]{2}$/', $postcode ) ) {
wp_send_json_error( [ 'message' => 'Please enter a valid UK postcode.' ] );
}
// Transient cache — avoid hammering the API (cache for 24 hours)
$cache_key = 'kindly_cov_' . $postcode;
$cached = get_transient( $cache_key );
if ( $cached !== false ) {
wp_send_json_success( $cached );
}
// Call Ofcom API server-side (key never exposed to browser)
$url = "https://api-proxy.ofcom.org.uk/mobile/coverage/{$postcode}";
$response = wp_remote_get( $url, [
'headers' => [ 'Ocp-Apim-Subscription-Key' => KINDLY_OFCOM_KEY ],
'timeout' => 10,
] );
if ( is_wp_error( $response ) ) {
wp_send_json_error( [ 'message' => 'Could not fetch coverage data. Please try again.' ] );
}
$code = wp_remote_retrieve_response_code( $response );
if ( $code !== 200 ) {
wp_send_json_error( [ 'message' => "Ofcom API returned status {$code}." ] );
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
// Average signal values across all addresses in the postcode
$addrs = $body[0]['Availability'] ?? [];
$avg = function( $key ) use ( $addrs ) {
$vals = array_filter( array_column( $addrs, $key ), fn($v) => $v > 0 );
return $vals ? (int) round( array_sum( $vals ) / count( $vals ) ) : 0;
};
$data = [
'o2' => [
'voiceOutdoor' => $avg('TFVoiceOutdoor'),
'voiceIndoor' => $avg('TFVoiceIndoor'),
'data4gOutdoor' => $avg('TFData4GOutdoor'),
'data4gIndoor' => $avg('TFData4GIndoor'),
'data5gOutdoor' => $avg('TFData5GOutdoor'),
'data5gIndoor' => $avg('TFData5GIndoor'),
],
'plan' => [
'voiceOutdoor' => $avg('EEVoiceOutdoor'),
'voiceIndoor' => $avg('EEVoiceIndoor'),
'data4gOutdoor' => $avg('EEData4GOutdoor'),
'data4gIndoor' => $avg('EEData4GIndoor'),
'data5gOutdoor' => $avg('EEData5GOutdoor'),
'data5gIndoor' => $avg('EEData5GIndoor'),
],
'postcode' => $raw,
];
set_transient( $cache_key, $data, DAY_IN_SECONDS );
wp_send_json_success( $data );
}
// ── Shortcode [kindly_coverage] ──────────────────────────────────────────────
add_shortcode( 'kindly_coverage', 'kindly_coverage_shortcode' );
function kindly_coverage_shortcode() {
$nonce = wp_create_nonce( 'kindly_coverage_nonce' );
$ajax_url = admin_url( 'admin-ajax.php' );
ob_start(); ?>
Coverage for
Data sourced from Ofcom Connected Nations
O₂
Kindly on O2
O2 SIM · Powered by O2
Kindly
Plan
Plan
Kindly Plan
Plan SIM · UK's leading network
No signal
Unreliable
Limited
Good
Enhanced
Coverage predictions are provided by Ofcom and are indicative only. Actual signal may vary due to local geography, buildings, and device type.