, which the browser * runtime in packages/fix-engine looks up so a site running both carries one stylesheet. */ function equalsites_enqueue_fixes_css() { $rules = equalsites_request_rules(); if ( empty( $rules ) ) { return; } $built = EqualSites_Fix_Applicator::css_body( $rules ); if ( '' === $built['body'] ) { return; } wp_register_style( 'ableguard-fixes', false, array(), EQUALSITES_VERSION ); wp_enqueue_style( 'ableguard-fixes' ); wp_add_inline_style( 'ableguard-fixes', $built['body'] ); } // Last in line, so the sheet prints after the theme's stylesheets and the Customizer's // additional CSS, as near to where the fixes always sat (just before ) as the API allows. // Embed templates fire enqueue_embed_scripts instead of wp_enqueue_scripts. add_action( 'wp_enqueue_scripts', 'equalsites_enqueue_fixes_css', PHP_INT_MAX ); add_action( 'enqueue_embed_scripts', 'equalsites_enqueue_fixes_css', PHP_INT_MAX ); // The stored-data migration of 0.4.0 runs on the first request after the update. It takes // the site-wide lock and skips quietly when another request holds it. add_action( 'init', array( 'EqualSites_Connect_Store', 'maybe_migrate' ) ); /** * The buffer callback, with the response headers passed in rather than read. * * `headers_list()` is the one thing here a test cannot arrange — the CLI SAPI * sends no headers and always reports none — so it is read at the call site and * this stays a plain function of its arguments. */ function equalsites_filter_document( $html, $rules, $headers ) { if ( ! EqualSites_Response_Kind::is_html_document( $html, $headers ) ) { return $html; } $result = EqualSites_Fix_Applicator::apply( $html, $rules ); return $result['html']; } /** * The owner's "Apply approved fixes to my pages" setting — the plugin's kill * switch, and the one thing that decides whether any of this reaches a visitor. * * One function because there are three callers and they must never disagree: the * output buffer above, which is where the setting takes effect; the environment * report in EqualSites_Api_Client, which is how the SaaS learns about it; and that * class's alt-text write-back, because alt text saved into the media library * reaches visitors wherever WordPress renders the attachment and is a fix like any * other. * * The first two used to be one `get_option()` call and no call at all — the report * simply did not mention the setting — so an owner could untick this box and their * published accessibility statement went on telling readers that AbleGuard was * correcting their HTML on the server, including for visitors with no JavaScript, * because the twice-daily sync kept reporting in and nothing in the report said * otherwise. The write-back made the same mistake from the other side: it never * asked, so a paused site went on having alt text saved into its media library. * * Defaults to true, matching the checkbox: an install that has never touched the * setting is applying fixes. */ function equalsites_fixes_enabled() { return (bool) ableguard_option( 'ableguard_enabled', 'equalsites_enabled', true ); } /** * Whether this request is one AbleGuard must keep its hands off entirely. * * This used to include `wp_is_json_request()`, which was the wrong question * asked in the wrong place. That function is a *content negotiation* helper: it * reads `$_SERVER['HTTP_ACCEPT']` and answers "would a JSON response be * acceptable to this caller", so any visitor who sent `Accept: application/json` * on an ordinary page URL — an auditor, a checker, a preview bot, a curl — got * that page back with every approved fix silently withheld, no authentication * involved. Worse than a one-off: a page cache or CDN keying on the URL without * varying on `Accept` stores the unfixed copy and then serves it to everyone, * which turns the product off site-wide without a single error anywhere. And * a customer whose published accessibility statement says corrections are in * the served markup is the one holding the claim when an auditor sends that * header. * * What we actually need to know is what is *being served*, not what the caller * would have accepted — the two coincide only on a real API route, and on a real * API route WordPress tells us so itself. `REST_REQUEST` is defined by * `rest_api_loaded()` before the REST server serves, and `wp_doing_ajax()` is * set for `admin-ajax.php`; both are decided by the route, not by the client. * (Neither actually reaches `template_redirect` in core today — REST and * admin-ajax both terminate first — so these are the belt to the braces below. * Cheap, and they keep the intent legible.) * * The rest of the answer cannot be known here at all: a front-end template is * free to emit JSON, and it has not done so yet when this runs. That half is * decided at flush time, from the response's own `Content-Type` — see * `EqualSites_Response_Kind`. */ function equalsites_skip_request() { if ( is_admin() || is_feed() || is_robots() ) { return true; } if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return true; } return wp_doing_ajax(); } /** * Serve this page unfixed if the request carries a valid, unspent baseline * value from AbleGuard, and tell AbleGuard we did. Returns true when the * caller must leave the page alone. * * Everything that is not a valid value — absent, malformed, expired, replayed, * minted for another site or another path — takes the same path as a normal * visit: no header, no notice, no log line. An attacker cannot learn from a * response whether they were close. */ function equalsites_serve_baseline() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- the value IS the authentication, and a nonce is bound to a WordPress user session the verifier does not have. if ( isset( $_GET[ EqualSites_Baseline_Bypass::PARAM ] ) ) { $param = EqualSites_Baseline_Bypass::PARAM; } elseif ( isset( $_GET[ EqualSites_Baseline_Bypass::LEGACY_PARAM ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- see above. $param = EqualSites_Baseline_Bypass::LEGACY_PARAM; } else { return false; } $method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : ''; if ( 'GET' !== $method ) { return false; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- see above; $param names a key that was just found to be set. $value = sanitize_text_field( wp_unslash( $_GET[ $param ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- the signature covers the exact path as sent, so it must not be altered first; it is only parsed and compared, never output or stored. $request = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; $path = (string) wp_parse_url( $request, PHP_URL_PATH ); // The credentials come from the same snapshot the buffer reads, so a verification run // keeps getting its unfixed baseline through the migration. A site with no connection has // no token, and no value is honoured. $page = EqualSites_Connect_Store::page(); $signature = EqualSites_Baseline_Bypass::authorize( $value, (string) $page['token'], (string) $page['site_id'], $path, time() ); if ( null === $signature ) { return false; } header( EqualSites_Baseline_Bypass::HEADER . ': 1' ); // An unfixed copy must never end up cached or indexed in place of the real // page. DONOTCACHEPAGE is the de facto opt-out honoured by the page caches. header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0', true ); header( 'X-Robots-Tag: noindex, nofollow', true ); if ( ! defined( 'DONOTCACHEPAGE' ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- DONOTCACHEPAGE is the exact name page-cache plugins look for, so it cannot carry a prefix. define( 'DONOTCACHEPAGE', true ); } ob_start( function ( $html ) use ( $signature ) { // Spend the value only once it has actually produced a document: a // canonical redirect emits no body, and must not burn the one use. if ( false !== stripos( $html, '' . esc_html__( 'AbleGuard: your published accessibility statement is hidden right now — you have no approved fixes and we could not refresh it. It returns automatically once either changes; press "Sync now" in AbleGuard settings to retry immediately.', 'ableguard-accessibility-toolkit' ) . '
' : ''; } $html = $page['statement_html']; } return wp_kses_post( $html ); } /** What a site manager sees where the statement would be, and a visitor never does. */ function equalsites_statement_missing_hint() { return current_user_can( 'manage_options' ) ? '' . esc_html__( 'AbleGuard: no accessibility statement yet — connect the plugin and run a scan first.', 'ableguard-accessibility-toolkit' ) . '
' : ''; } add_shortcode( 'ableguard_statement', 'equalsites_statement_shortcode' ); add_shortcode( 'equalsites_statement', 'equalsites_statement_shortcode' ); /** Twice-daily rule sync from the AbleGuard API. */ function equalsites_schedule_sync() { if ( ! wp_next_scheduled( 'equalsites_sync_rules' ) ) { wp_schedule_event( time(), 'twicedaily', 'equalsites_sync_rules' ); } } register_activation_hook( __FILE__, 'equalsites_schedule_sync' ); add_action( 'equalsites_sync_rules', array( 'EqualSites_Api_Client', 'sync_rules' ) ); function equalsites_deactivate() { wp_clear_scheduled_hook( 'equalsites_sync_rules' ); } register_deactivation_hook( __FILE__, 'equalsites_deactivate' );