register_hooks(); } return self::$instance; } /** * Register hooks based on active settings. */ private function register_hooks() { if ( is_admin() ) { return; } $settings = get_option( 'wpag_settings', wpag_default_settings() ); if ( ! empty( $settings['auto_fix_skip_link'] ) ) { add_action( 'wp_body_open', array( $this, 'inject_skip_link' ), 1 ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_skip_link_css' ) ); } if ( ! empty( $settings['auto_fix_lang_attr'] ) ) { add_filter( 'language_attributes', array( $this, 'ensure_lang_attr' ) ); } if ( ! empty( $settings['auto_fix_empty_headings'] ) ) { add_filter( 'the_content', array( $this, 'fix_empty_headings' ), 999 ); } if ( ! empty( $settings['auto_fix_form_labels'] ) ) { add_filter( 'the_content', array( $this, 'fix_form_labels' ), 999 ); } } /** * Inject skip-to-content link after
. */ public function inject_skip_link() { // Avoid duplicate if Pojo Accessibility is active. if ( function_exists( 'is_plugin_active' ) && is_plugin_active( 'pojo-accessibility/pojo-accessibility.php' ) ) { return; } echo '' . esc_html__( 'Skip to content', 'accessibility-guard' ) . ''; } /** * Enqueue skip link CSS using wp_add_inline_style. */ public function enqueue_skip_link_css() { wp_register_style( 'wpag-skip-link', false, array(), '1.0.0' ); wp_enqueue_style( 'wpag-skip-link' ); wp_add_inline_style( 'wpag-skip-link', '.wpag-skip-link { position: absolute; top: -100%; left: 0; z-index: 999999; padding: 8px 16px; background: #000; color: #fff; text-decoration: none; font-size: 14px; line-height: 1.5; clip: rect(1px, 1px, 1px, 1px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px; } .wpag-skip-link:focus { position: fixed; top: 6px; left: 6px; clip: auto; clip-path: none; width: auto; height: auto; overflow: visible; outline: 2px solid #0073aa; outline-offset: 2px; border-radius: 3px; }' ); } /** * Ensure the HTML element has a lang attribute. * * @param string $attributes Existing language attributes string. * @return string */ public function ensure_lang_attr( $attributes ) { if ( strpos( $attributes, 'lang=' ) !== false ) { return $attributes; } $lang = get_bloginfo( 'language' ); if ( $lang ) { $attributes .= ' lang="' . esc_attr( $lang ) . '"'; } return $attributes; } /** * Remove empty heading elements from content. * * @param string $content Post content. * @return string */ public function fix_empty_headings( $content ) { if ( empty( $content ) ) { return $content; } // Remove empty headings: ,