From 1d1b381b7392ff69b493fd6b1b7518f3d857a0c2 Mon Sep 17 00:00:00 2001 From: Chris Portscheller Date: Fri, 27 Nov 2026 10:09:27 -0600 Subject: [PATCH] Offer Connect in a dismissible notice after activation --- includes/class-webdecoy-cloud-connect.php | 15 ++ includes/class-webdecoy-connect-invite.php | 194 +++++++++++++++++++++ includes/class-webdecoy-review-ask.php | 4 + tests/ConnectInviteTest.php | 66 +++++++ uninstall.php | 6 + webdecoy.php | 5 + 6 files changed, 290 insertions(+) create mode 100644 includes/class-webdecoy-connect-invite.php create mode 100644 tests/ConnectInviteTest.php diff --git a/includes/class-webdecoy-cloud-connect.php b/includes/class-webdecoy-cloud-connect.php index 1168410..0ec1352 100644 --- a/includes/class-webdecoy-cloud-connect.php +++ b/includes/class-webdecoy-cloud-connect.php @@ -123,6 +123,11 @@ public function handle_connect(): void // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- boolean presence only; nonce already verified above $digest = !empty($_POST['digest']) ? '1' : '0'; + // Which control started the flow, so the app can count connects by + // entry point. An allowlisted label, never free text. + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- allowlisted below; nonce already verified above + $source = self::connect_source(isset($_POST['source']) ? sanitize_key(wp_unslash($_POST['source'])) : ''); + // Build the query with http_build_query so every value is encoded // exactly once (WP's add_query_arg does not encode appended args). $query = http_build_query([ @@ -130,6 +135,7 @@ public function handle_connect(): void 'site_name' => get_bloginfo('name'), 'nonce' => $nonce, 'digest' => $digest, + 'source' => $source, 'return_url' => admin_url('admin.php?page=webdecoy&tab=cloud&wd_connect=1'), ]); $url = self::CONNECT_URL . '?' . $query; @@ -403,6 +409,15 @@ public static function get_entitlements(): array return self::normalize_entitlements(is_array($cached) ? $cached : [], time()); } + /** + * The entry point label sent with a connect: 'invite' for the activation + * notice, 'settings' for everything else. + */ + public static function connect_source(string $posted): string + { + return $posted === 'invite' ? 'invite' : 'settings'; + } + /** * Whether this site is connected to WebDecoy Cloud (an API key is stored). */ diff --git a/includes/class-webdecoy-connect-invite.php b/includes/class-webdecoy-connect-invite.php new file mode 100644 index 0000000..1948198 --- /dev/null +++ b/includes/class-webdecoy-connect-invite.php @@ -0,0 +1,194 @@ +' (snoozed until then). + * + * @package WebDecoy + */ + +// Prevent direct access. +if (!defined('ABSPATH')) { + exit; +} + +/** + * WebDecoy connect invite controller. + */ +class WebDecoy_Connect_Invite +{ + public const STATE_OPTION = 'webdecoy_connect_invite'; + + /** "Not now" snoozes for 14 days (literal to keep the class loadable sans WordPress). */ + public const SNOOZE = 1209600; + + /** Where the Connect click came from, sent to the app so it can count invite connects. */ + public const SOURCE = 'invite'; + + private const ACTION = 'webdecoy_connect_invite'; + + /** Set once this request has rendered the invite, so the review ask can stand aside. */ + private static bool $rendered = false; + + public function register(): void + { + if (!is_admin() || WebDecoy_Runtime_Config::hide_admin_ui()) { + return; + } + // Ahead of the review ask (priority 10), which checks rendered(). + add_action('admin_notices', [$this, 'render'], 5); + add_action('admin_post_' . self::ACTION, [$this, 'handle']); + } + + /** Whether the invite was shown on this page view. */ + public static function rendered(): bool + { + return self::$rendered; + } + + public function render(): void + { + if (!current_user_can('manage_options') || !self::on_relevant_screen()) { + return; + } + $connected = class_exists('WebDecoy_Cloud_Connect') && WebDecoy_Cloud_Connect::is_connected(); + if (!self::is_due($connected, (string) get_option(self::STATE_OPTION, ''), time())) { + return; + } + self::$rendered = true; + + $visits = class_exists('WebDecoy_Crawler_Visits') ? WebDecoy_Crawler_Visits::summary(168) : ['total' => 0, 'groups' => ['ai' => 0]]; + $total = (int) ($visits['total'] ?? 0); + $ai = (int) ($visits['groups']['ai'] ?? 0); + + $link = static function (string $choice): string { + return wp_nonce_url( + admin_url('admin-post.php?action=' . self::ACTION . '&choice=' . $choice), + self::ACTION + ); + }; + ?> +
+

+ + +

+

+
+ + + +

+ +

+

+ + + + + + + +

+
+
+ id; + return $id === 'dashboard' || $id === 'plugins' || strpos($id, 'webdecoy') !== false; + } + + // --------------------------------------------------------------------- + // Pure helpers (no WordPress calls) — unit-tested in tests/ConnectInviteTest.php. + // --------------------------------------------------------------------- + + /** Whether to show the invite: not connected, not dismissed, not snoozed past now. */ + public static function is_due(bool $connected, string $state, int $now): bool + { + if ($connected) { + return false; + } + if ($state === '') { + return true; + } + if (strpos($state, 'later:') === 0) { + return $now >= (int) substr($state, 6); + } + return false; + } + + /** The stored state for a choice. Anything unrecognised snoozes rather than silencing it for good. */ + public static function state_for(string $choice, int $now): string + { + return $choice === 'never' ? 'never' : 'later:' . ($now + self::SNOOZE); + } + + /** What the plugin is doing on this site, from this week's counts. */ + public static function message(int $total, int $ai): string + { + if ($total <= 0) { + return __('WebDecoy is now counting bot and AI crawler visits on this site. Everything it shows works without an account.', 'webdecoy'); + } + if ($ai > 0) { + return sprintf( + /* translators: 1: bot visits counted in 7 days, 2: of which AI crawlers and agents */ + __('In the last 7 days WebDecoy counted %1$s bot visits on this site, %2$s of them from AI crawlers and agents. Everything it shows works without an account.', 'webdecoy'), + number_format($total), + number_format($ai) + ); + } + return sprintf( + /* translators: %s: bot visits counted in 7 days */ + __('In the last 7 days WebDecoy counted %s bot visits on this site. Everything it shows works without an account.', 'webdecoy'), + number_format($total) + ); + } + + /** Why connect, in what a free connected site actually gets. */ + public static function offer(): string + { + return __('Connect free to see each bot\'s history beside your detections and this site in the WebDecoy dashboard, with an optional monthly report. Nothing leaves your site until you click Connect.', 'webdecoy'); + } +} diff --git a/includes/class-webdecoy-review-ask.php b/includes/class-webdecoy-review-ask.php index c305ed9..8fbaeed 100644 --- a/includes/class-webdecoy-review-ask.php +++ b/includes/class-webdecoy-review-ask.php @@ -61,6 +61,10 @@ public function render(): void if (!current_user_can('manage_options') || !self::on_relevant_screen()) { return; } + // One ask per page view: the connect invite goes first when it shows. + if (class_exists('WebDecoy_Connect_Invite') && WebDecoy_Connect_Invite::rendered()) { + return; + } $now = time(); $first_seen = (int) get_option(self::FIRST_SEEN_OPTION, 0); diff --git a/tests/ConnectInviteTest.php b/tests/ConnectInviteTest.php new file mode 100644 index 0000000..9388c7d --- /dev/null +++ b/tests/ConnectInviteTest.php @@ -0,0 +1,66 @@ + shown'); +}); + +$t('never on a connected site, whatever the state', function () use ($true, $now) { + $true(WebDecoy_Connect_Invite::is_due(true, '', $now) === false, 'connected -> hidden'); + $true(WebDecoy_Connect_Invite::is_due(true, 'later:1', $now) === false, 'connected after a snooze -> hidden'); +}); + +$t("don't show again ends it; a snooze holds until it ends", function () use ($true, $now) { + $true(WebDecoy_Connect_Invite::is_due(false, 'never', $now) === false, 'never -> hidden'); + $true(WebDecoy_Connect_Invite::is_due(false, 'later:' . ($now + 1), $now) === false, 'inside the snooze -> hidden'); + $true(WebDecoy_Connect_Invite::is_due(false, 'later:' . $now, $now) === true, 'snooze over -> shown'); +}); + +echo "\nConnect invite: choices\n"; + +$t('not now snoozes 14 days; anything unrecognised snoozes too', function () use ($same, $now) { + $same('later:' . ($now + WebDecoy_Connect_Invite::SNOOZE), WebDecoy_Connect_Invite::state_for('later', $now)); + $same('later:' . ($now + WebDecoy_Connect_Invite::SNOOZE), WebDecoy_Connect_Invite::state_for('bogus', $now)); + $same('never', WebDecoy_Connect_Invite::state_for('never', $now)); +}); + +$t('the connect source is an allowlisted label', function () use ($same) { + $same('invite', WebDecoy_Cloud_Connect::connect_source('invite')); + $same('settings', WebDecoy_Cloud_Connect::connect_source('')); + $same('settings', WebDecoy_Cloud_Connect::connect_source('