'', 'company_slug' => '', 'site_id' => '', 'widget_enabled' => false, // Whether the matched site actually licenses the widget, recorded at // connect from /account's `isPro` / `isTrial`. Without it the widget // switch was a silent no-op on an unlicensed site: the admin ticked // it, the script loaded on every public page and rendered nothing, // and nothing in wp-admin said why. 'widget_licensed' => false, // Whether the matched site's domain is verified in AccessiBit. Scans // need it, so the screens have to be able to say so rather than // showing a raw `site_not_verified` from the API. 'site_verified' => false, // The address this install had when it connected, as // `Accessibit_Admin::sanitize_domain` spells it. See // `is_binding_stale()`. 'bound_domain' => '', // The site's statement and whether any language of it has been // published, as last seen by the REST proxy. The shortcode and the // block embed it by id, and print nothing before it is published: // the public address answers a plain-text "not found" until then. 'statement_id' => '', 'statement_published' => false, // Whether AccessiBit serves the Tracker to this plugin: `/account`'s // `tracker` capability, as last read (ACC-412). The Tracker menu is // registered from it, on every admin page, without asking. 'tracker_available' => false, ); $stored = get_option( self::OPTION, array() ); return is_array( $stored ) ? array_merge( $defaults, $stored ) : $defaults; } /** * The stored settings as they are now, not as this request first read them. * * The option is autoloaded, so `get_option()` answers from a copy taken * when the request began. A request that waited on AccessiBit — a statement * generation, up to the proxy's write timeout — would otherwise act on * settings a Disconnect or a Reconnect has since replaced (ACC-411). * * @return array */ public static function reload(): array { wp_cache_delete( 'alloptions', 'options' ); wp_cache_delete( self::OPTION, 'options' ); return self::get(); } /** * Merge a patch into the stored settings and persist the result. * * Into the settings as they are now (`reload()`): merging into the copy * this request began with wrote it back over whatever changed meanwhile — * a slow statement answer restored a key that Disconnect had just cleared. * * @param array $patch The keys to overwrite; everything else is kept. */ public static function update( array $patch ): void { update_option( self::OPTION, array_merge( self::reload(), $patch ) ); } /** * Return the stored API key (empty string if unset). * * @return string */ public static function get_api_key(): string { return (string) self::get()['api_key']; } /** * Return the stored site ID (empty string if unset). * * @return string */ public static function get_site_id(): string { return (string) self::get()['site_id']; } /** * Return the stored company slug (empty string if unset). * * @return string */ public static function get_company_slug(): string { return (string) self::get()['company_slug']; } /** * True once this site holds a key and knows which company it belongs to. * * @return bool */ public static function is_connected(): bool { return self::get_api_key() !== '' && self::get_company_slug() !== ''; } /** * True once this WordPress site is also matched to an AccessiBit site. * Everything site-scoped — scans, the summary, the dashboard deep links — * needs this, so the views and the admin asset enqueue share one answer * rather than each re-deriving it. * * @return bool */ public static function is_site_linked(): bool { return self::is_connected() && self::get_site_id() !== ''; } /** * True when the matched site licenses the widget. * * The widget switch does nothing without this: the script loads on every * public page and the bundle declines to mount, with nothing in wp-admin to * explain it. * * @return bool */ public static function is_widget_licensed(): bool { return (bool) self::get()['widget_licensed']; } /** * True when the matched site's domain is verified in AccessiBit. * * Scans — free and paid — are refused without it, so the screens check this * rather than waiting for the API to answer `site_not_verified`. * * @return bool */ public static function is_site_verified(): bool { return (bool) self::get()['site_verified']; } /** * True when AccessiBit's last `/account` offered the Tracker (ACC-412). * * Behind the server's `TRACKER_IN_PLUGIN`: until it is on, `/account` * lists no `tracker` capability and the Tracker menu stays hidden. * * @return bool */ public static function is_tracker_available(): bool { return (bool) self::get()['tracker_available']; } /** * The `tracker_available` flag from an `/account` (or connect) body. * * Pure, for the unit suite. An answer without `capabilities` is an API * older than the Tracker, which offers none. * * @param array $data The decoded body. * @return array{tracker_available: bool} */ public static function capability_flags( array $data ): array { $capabilities = isset( $data['capabilities'] ) && is_array( $data['capabilities'] ) ? $data['capabilities'] : array(); return array( 'tracker_available' => in_array( 'tracker', $capabilities, true ) ); } /** * The site's statement id, or '' when the plugin has not seen one yet. * * @return string */ public static function get_statement_id(): string { return (string) self::get()['statement_id']; } /** * True once the statement has a published language to embed. * * @return bool */ public static function is_statement_published(): bool { return (bool) self::get()['statement_published']; } /** * Persist API credentials obtained after a successful connect flow. * * `$licensed` and `$verified` come from the site the connect exchange * reported, or from the matched entry of `/account`'s `sites` on a Check * again. They were previously read and thrown away, which is what made the * widget switch a silent no-op on an unlicensed site and left the scan * buttons with no way to explain a refusal. * * @param string $api_key The key AccessiBit issued for this site. * @param string $slug The company slug the key resolved to. * @param string $site_id The AccessiBit site this install matched, or '' for none. * @param bool $licensed Whether that site licenses the widget. * @param bool $verified Whether that site's domain is verified. */ public static function set_account( string $api_key, string $slug, string $site_id, bool $licensed = false, bool $verified = false ): void { $patch = array( 'api_key' => $api_key, 'company_slug' => $slug, 'site_id' => $site_id, 'widget_licensed' => $licensed, 'site_verified' => $verified, 'bound_domain' => Accessibit_Admin::sanitize_domain( home_url() ), ); // A statement belongs to its site: binding to another one forgets it. if ( self::get_site_id() !== $site_id ) { $patch['statement_id'] = ''; $patch['statement_published'] = false; } self::update( $patch ); } /** * True when this install's address is no longer the one it connected as. * * The usual cause is a staging or development copy made by cloning the * database: the options table comes with it, so the clone inherits the * production account, the production site id and the production API key. * Nothing re-checked, so the clone showed production's scan * results and its public pages served the widget under the production * account — which also counted the clone's traffic as the real site's. * * Deliberately a HINT the screens show rather than a silent disconnect: a * site legitimately changing domain is the same signal, and throwing away * their key for it would be worse than telling them. The front-end widget * does stop, because that one has a visitor-facing cost and no way to ask. * * An install that connected before this field existed has no `bound_domain` * and is left alone — there is nothing to compare, and warning on an unknown * would fire on every upgrade. A DISCONNECTED install is left alone for the * same reason: there is no binding to be stale, and the notice told such a * site to disconnect something it had already disconnected. * * @return bool */ public static function is_binding_stale(): bool { // Nothing is bound if nothing is connected, so there is nothing to be // stale. Belt and braces with `handle_disconnect` clearing the field: // this also covers an install that disconnected on an earlier build, // which still carries the old `bound_domain` in its options row. if ( ! self::is_connected() ) { return false; } $bound = (string) self::get()['bound_domain']; if ( '' === $bound ) { return false; } return Accessibit_Admin::sanitize_domain( home_url() ) !== $bound; } /** * The address this install connected as, for the notice that reports a stale * binding. * * @return string */ public static function get_bound_domain(): string { return (string) self::get()['bound_domain']; } }