\n"; } } /** The fixes' CSS and the badge's CSS, as inline styles of their own handles (they have no files). */ public static function enqueue_styles() { $css = self::enabled() ? alexibility_fixes_fix_css( Alexibility_Plan::approved() ) : ''; if ( '' !== $css ) { self::inline_style( 'alexibility-fixes', $css ); } if ( self::badge_enabled() ) { self::inline_style( 'alexibility-fixes-badge', self::badge_css() ); } } /** Registers a style with no file, enqueues it and attaches the CSS. */ public static function inline_style( $handle, $css ) { wp_register_style( $handle, false, array(), ALEXIBILITY_FIXES_VERSION ); wp_enqueue_style( $handle ); wp_add_inline_style( $handle, $css ); } public static function start_buffer() { if ( ! self::enabled() || is_admin() || wp_doing_ajax() || is_feed() || is_embed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { return; } ob_start( array( __CLASS__, 'rewrite' ) ); } /** Output-buffer callback: edits the finished page HTML. */ public static function rewrite( $html ) { if ( ! is_string( $html ) || false === stripos( $html, 'next_tag() ) { $tag = $p->get_tag(); if ( 'HTML' === $tag && $lang && null === $p->get_attribute( 'lang' ) ) { $p->set_attribute( 'lang', $lang[0]['lang'] ); } elseif ( 'IMG' === $tag && $alts ) { $stem = alexibility_fixes_wp_stem( alexibility_fixes_file_name_of( (string) $p->get_attribute( 'src' ) ) ); if ( array_key_exists( $stem, $alts ) && alexibility_fixes_alt_missing( $p->get_attribute( 'alt' ), in_array( $stem, $replace, true ) ) ) { $p->set_attribute( 'alt', $alts[ $stem ] ); } } elseif ( 'MAIN' === $tag && null === $main_id ) { $id = $p->get_attribute( 'id' ); if ( is_string( $id ) && '' !== $id ) { $main_id = $id; } else { $main_id = 'alexibility-fixes-main'; $p->set_attribute( 'id', $main_id ); } } } $html = $p->get_updated_html(); foreach ( $by( 'accessible-name' ) as $f ) { if ( ! empty( $f['decorative'] ) ) { list( $html ) = alexibility_fixes_hide_graphic( $html, $f['tag'], $f['criteria'] ); } else { list( $html ) = alexibility_fixes_add_aria_label( $html, $f['tag'], $f['criteria'], $f['name'] ); } } // Markup the Alexibility scanner changed in the page and kept: old align attributes, tabs marked as buttons, a second banner. foreach ( $by( 'attr-repair' ) as $f ) { list( $html ) = alexibility_fixes_change_attrs( $html, $f ); } // Block themes get a skip link from WordPress core; never add a second one. $has_skip = false !== strpos( $html, 'wp-block-template-skip-link' ) || preg_match( '/]*href=["\']#[^"\']*["\'][^>]*>\s*skip/i', $html ); if ( $by( 'skip-link' ) && $main_id && ! $has_skip ) { $link = '' . esc_html__( 'Skip to content', 'alexibility-fixes' ) . ''; $html = preg_replace( '/]*>/i', '$0' . $link, $html, 1 ); } return $html; } // --- footer badge ------------------------------------------------------------------------------- /** * Off until the site turns it on: a public credit link needs the site owner's say-so (WordPress.org's * plugin guidelines). Stored as '1'/'0' (see the settings handler for why). It also needs fixes from * Alexibility in place, so the name never appears on a site nobody has worked on. */ public static function badge_enabled() { $value = get_option( self::CREDIT, '0' ); return ( true === $value || '1' === (string) $value ) && self::badge_allowed(); } /** True while at least one run of fixes, or of PDF replacements, is applied and not rolled back. */ public static function badge_allowed() { foreach ( array( Alexibility_Content::RUNS, Alexibility_Pdfs::RUNS ) as $option ) { foreach ( (array) get_option( $option, array() ) as $run ) { if ( is_array( $run ) && empty( $run['rolled_back'] ) ) { return true; } } } return false; } /** The site's own statement URL if set, else the company URL; tagged so referrals can be counted. */ public static function badge_url() { $url = (string) get_option( self::CREDIT_URL, '' ); if ( '' === $url ) { $url = (string) ALEXIBILITY_FIXES_BRAND_URL; } if ( '' === $url ) { return ''; } return add_query_arg( array( 'site' => wp_parse_url( home_url(), PHP_URL_HOST ), 'ref' => 'badge' ), $url ); } /** Decorative logo: hidden from assistive technology, the text carries the meaning. */ private static function badge_logo() { static $svg = null; if ( null === $svg ) { $file = ALEXIBILITY_FIXES_DIR . 'assets/badge-logo.svg'; $svg = is_readable( $file ) ? trim( (string) file_get_contents( $file ) ) : ''; // phpcs:ignore WordPress.WP.AlternativeFunctions } return $svg; } public static function badge_html() { $url = self::badge_url(); $text = sprintf( /* translators: %s: company name, wrapped in */ esc_html__( 'Accessibility by %s', 'alexibility-fixes' ), '' . esc_html( ALEXIBILITY_FIXES_BRAND_NAME ) . '' ); $inner = self::badge_logo() . '' . $text . ''; $body = '' !== $url ? '' . $inner . '' : '' . $inner . ''; return ''; } /** * Self-contained so it reads well on any theme: its own white pill and dark text (about 14:1), * a 30px tall target, a visible focus ring, and a border that survives Windows high-contrast mode. */ public static function badge_css() { return '.alexibility-fixes-badge{display:flex;justify-content:center;margin:0;padding:12px 16px;background:transparent}' . '.alexibility-fixes-badge__pill{display:inline-flex;align-items:center;gap:8px;min-height:30px;box-sizing:border-box;padding:5px 14px 5px 6px;' . 'border:1px solid #d1d5db;border-radius:999px;background:#ffffff;color:#1f2937;' . 'font:500 13px/1.4 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;text-decoration:none;letter-spacing:0}' . '.alexibility-fixes-badge__pill svg{flex:none;width:20px;height:20px;display:block}' . '.alexibility-fixes-badge__pill strong{font-weight:700;color:#1d4ed8}' . 'a.alexibility-fixes-badge__pill:hover .alexibility-fixes-badge__text{text-decoration:underline}' . 'a.alexibility-fixes-badge__pill:focus-visible{outline:3px solid #1d4ed8;outline-offset:2px}' . '@media (forced-colors:active){.alexibility-fixes-badge__pill{border-color:CanvasText}.alexibility-fixes-badge__pill strong{color:inherit}}'; } public static function print_badge() { if ( self::badge_enabled() ) { echo self::badge_html(); // phpcs:ignore WordPress.Security.EscapeOutput -- escaped in badge_html(). } } }