tag, so it goes through wp_enqueue_script() plus a * script_loader_tag filter rather than a hand-printed tag in wp_footer. */ class Accessibit_Widget { const HANDLE = 'accessibit-widget'; /** * Attributes that ask the caching/optimization plugins to leave this tag alone. * * The CDN bundle is a loader that reads the account off its own tag via * `document.currentScript`. An optimizer that concatenates our script into a * combined file breaks that: LiteSpeed Cache, with the default "Combine * External and Inline JS", downloads the remote loader, inlines it into * `wp-content/litespeed/js/.js` and drops our tag — so `currentScript` * is the combined file, which carries no `data-account`, and the widget * silently never licenses. Measured on LSCWP 7.9.1 / WP 6.6 (ACC-481). * * One attribute per vendor, because none of them reads another's: * `data-no-optimize` (LiteSpeed Cache), `data-noptimize` (Autoptimize), * `data-no-minify` (WP Rocket) and `data-cfasync="false"`, which is both * Cloudflare Rocket Loader's opt-out and a second one LiteSpeed honours. */ const OPT_OUT_ATTRIBUTES = 'data-no-optimize="1" data-noptimize="1" data-no-minify="1" data-cfasync="false"'; /** * The query parameter that asks for the widget's preview mode, carrying a * nonce for `PREVIEW_ACTION` (ACC-404). */ const PREVIEW_PARAM = 'accessibit_preview'; const PREVIEW_ACTION = 'accessibit_widget_preview'; /** * The widget's preview contract, as `@accessibit/types` spells it * (`WIDGET_PREVIEW_FLAG` and the three messages). Mirrored here because the * relay below is printed by PHP; the e2e preview case breaks if they drift. */ const PREVIEW_FLAG = '__ACCESSIBIT_PREVIEW__'; const PREVIEW_READY = 'accessibit-preview:ready'; const PREVIEW_SETTINGS = 'accessibit-preview:settings'; const PREVIEW_RESET = 'accessibit-preview:reset'; /** * Register the front-end hooks. */ public static function init(): void { add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue' ) ); add_filter( 'script_loader_tag', array( __CLASS__, 'filter_tag' ), 10, 2 ); add_action( 'wp', array( __CLASS__, 'prepare_preview' ) ); } /** * The homepage in preview mode, for the Widget screen's Tools tab. * * @return string */ public static function preview_url(): string { return add_query_arg( self::PREVIEW_PARAM, wp_create_nonce( self::PREVIEW_ACTION ), home_url( '/' ) ); } /** * Whether this request is the Widget screen's preview frame. * * An administrator with the nonce the screen handed out, on an install whose * binding is current: the preview runs the widget unlicensed, so it is not * something a link should be able to switch on for anyone else. * * @return bool */ public static function is_preview_request(): bool { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- this IS the nonce check. $nonce = isset( $_GET[ self::PREVIEW_PARAM ] ) ? sanitize_text_field( wp_unslash( $_GET[ self::PREVIEW_PARAM ] ) ) : ''; if ( '' === $nonce || ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( $nonce, self::PREVIEW_ACTION ) ) { return false; } return Accessibit_Settings::is_site_linked() && ! Accessibit_Settings::is_binding_stale(); } /** * `wp`: a preview frame is nobody's page view. No admin bar over the site * being previewed, and nothing a page cache should keep. */ public static function prepare_preview(): void { if ( ! self::is_preview_request() ) { return; } add_filter( 'show_admin_bar', '__return_false' ); nocache_headers(); // The page caches' shared opt-out (WP Rocket, W3TC, LiteSpeed and // others read it). Not ours to prefix: it is their name. if ( ! defined( 'DONOTCACHEPAGE' ) ) { define( 'DONOTCACHEPAGE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound } } /** * The script printed before the widget in a preview frame. * * Sets the preview flag before the loader runs: without it the widget, in * a same-origin frame, mounts into `window.parent.document` — wp-admin — * and asks for the saved settings and the licence. With it the widget * renders what it is posted, counts no page view, and talks only to its own * window, so this relays between that window and the admin page: settings * and reset down, "ready" up, each checked for origin and sender. * * Links and forms are stopped: a page reached from here would carry no * preview parameter, and the plain widget there would mount into wp-admin. * * The admin page is spoken to on ITS origin, which need not be the * frame's: the frame is the homepage, and the Site Address may differ from * the WordPress Address in host or scheme (www vs apex, admin on a * subdomain, an https admin over an http front end). The widget's own * window keeps the frame's origin (PR #1162 review). * * Pure, for the unit suite. * * @param string|null $admin_origin wp-admin's origin; `admin_url()`'s by default. * @return string */ public static function preview_relay_script( ?string $admin_origin = null ): string { $admin = wp_json_encode( $admin_origin ?? Accessibit_Admin::origin_of( admin_url() ) ); $contract = wp_json_encode( array( 'flag' => self::PREVIEW_FLAG, 'ready' => self::PREVIEW_READY, 'settings' => self::PREVIEW_SETTINGS, 'reset' => self::PREVIEW_RESET, ) ); return '(function (c) {' . 'window[c.flag] = true;' . 'var origin = window.location.origin, admin = ' . $admin . ', parent = window.parent;' . 'if (parent === window) return;' . 'window.addEventListener("message", function (e) {' . 'if (!e.data || typeof e.data.type !== "string") return;' . 'if (e.source === parent && e.origin === admin && (e.data.type === c.settings || e.data.type === c.reset)) window.postMessage(e.data, origin);' . 'else if (e.source === window && e.origin === origin && e.data.type === c.ready) parent.postMessage(e.data, admin);' . '});' . 'document.addEventListener("click", function (e) {' . 'var a = e.target && e.target.closest ? e.target.closest("a[href]") : null;' . 'if (a) e.preventDefault();' . '}, true);' . 'document.addEventListener("submit", function (e) { e.preventDefault(); }, true);' . '})(' . $contract . ');'; } /** * Loading args for wp_enqueue_script()'s 5th parameter. * * 6.3 turned that parameter into an options array and added `strategy`; * before then it was the `$in_footer` boolean, so old installs get the * footer and new ones get `defer` as well. * * @param string $wp_version The running WordPress version, as `get_bloginfo( 'version' )` reports it. * @return array|bool */ public static function script_args( string $wp_version ) { return version_compare( $wp_version, '6.3', '>=' ) ? array( 'in_footer' => true, 'strategy' => 'defer', ) : true; } /** * Add `data-account` plus the optimizer opt-outs to our own tag, leaving every * other script alone. * * @param string $tag The `