Skip to content

Commit

Permalink
MFN WP plugin, fix subscription health check from customer point of v…
Browse files Browse the repository at this point in the history
…iew: https://gitlab.modfin.sh/modfin/tickets/-/issues/11230 (#42)

* MFN WP plugin, fix subscription health check from customer point of view: https://gitlab.modfin.sh/modfin/tickets/-/issues/11230

* Flipped logic in hub subscription verification, check for success and fallthrough to fail instead
  • Loading branch information
MFMarkus authored Sep 19, 2024
1 parent 92b929e commit 6adf1f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
26 changes: 25 additions & 1 deletion admin/partials/sections/mfn-wp-plugin-admin-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,31 @@
$subscription_id = $subscription['subscription_id'] ?? "";
$subscription_msg = $subscription_id;

if ($subscription_id == "") {
function mfn_verify_hub_subscription($subscription_id)
{
$hub_url = mfn_fetch_hub_url();

if (mfn_starts_with($hub_url, "http")) {

$response = wp_remote_get($hub_url . '/verify/' . $subscription_id . "/status");

if ( !is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) == 200 ) {
$content = wp_remote_retrieve_body($response);

if (strstr($content, 'subscription verified')) {
return "success";
}
}

echo '<span class="mfn-status-error"><strong>Could not validate server side subscription, try to resubscribe</strong></span>';
return "error";
}
die("fail, not a valid url.");
}

$server_verified_subscription = mfn_verify_hub_subscription($subscription_id);

if ($subscription_id == "" || $server_verified_subscription == "error") {
$subscription_msg = '<strong>' . $subscription_id . '</strong>';
$subscription_msg .= ' <span class="mfn-status-error"><strong>' . mfn_get_text('status_not_subscribed') . '</strong></span>';
} else {
Expand Down
7 changes: 5 additions & 2 deletions posthook.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
}
$mode = $queries["hub_mode"];

if ($mode != 'subscribe' && $mode != 'unsubscribe') {
if ($mode != 'subscribe' && $mode != 'unsubscribe' && $mode != 'status') {
http_response_code(400);
die("mode must be subscribe or unsubscribe");
}
Expand All @@ -67,7 +67,10 @@
}
$topic = $queries["hub_topic"];

mfn_update_challenge_by_plugin_url($subscriptions, $plugin_url, $challenge);
if ($mode != 'status') {
// status should not write anything, just a healthcheck
mfn_update_challenge_by_plugin_url($subscriptions, $plugin_url, $challenge);
}

http_response_code(200);
echo $challenge;
Expand Down

0 comments on commit 6adf1f8

Please sign in to comment.