'checkbox', 'label' => esc_html__( 'Add "lang" & "dir" Attributes', 'accessibility-checker' ), 'labelledby' => 'add_lang_and_dir', // translators: %1$s: a attribute name wrapped in a tag. %2$s: dir attribute %3$s: html element. 'description' => sprintf( __( 'Add the site language (%1$s) and text direction (%2$s) attributes to the %3$s element.', 'accessibility-checker' ), 'lang', 'dir', '<html>' ), 'fix_slug' => $this->get_slug(), 'group_name' => $this->get_nicename(), 'help_id' => 8487, ]; return $fields; } /** * Run the fix for adding the lang and dir attributes to the html element. * * This incudes a filter to add them via php but also binds some data that can be used in * JS for themes that do not have the language_attributes function in the correct place. * * @return void */ public function run() { if ( ! get_option( 'edac_fix_add_lang_and_dir', false ) ) { return; } // Add the lang and dir attributes to the html element via filter. add_filter( 'language_attributes', [ $this, 'maybe_add_lang_and_dir' ] ); // Some themes may not have the language_attributes() function where it's meant to be so add so some JS is also // added that can add the attributes if they are still missing. add_filter( 'edac_filter_frontend_fixes_data', function ( $data ) { $data['lang_and_dir'] = [ 'enabled' => true, 'lang' => get_bloginfo( 'language' ), 'dir' => is_rtl() ? 'rtl' : 'ltr', ]; return $data; } ); } /** * Add the lang and dir attributes to the html element. * * @param string $output The language attributes. * @return string */ public function maybe_add_lang_and_dir( $output ): string { $language = get_bloginfo( 'language' ); $direction = is_rtl() ? 'rtl' : 'ltr'; $additional_atts = ''; if ( strpos( $output, 'lang=' ) === false ) { $additional_atts = ' lang="' . esc_attr( $language ) . '"'; } if ( strpos( $output, 'dir=' ) === false ) { $additional_atts .= ' dir="' . esc_attr( $direction ) . '"'; } return $output . $additional_atts; } }