$cat ) { self::$categories[ $i ]['title'] = translate( $cat['title'], 'a11ymate' ); // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain } /** * Filter the rule metadata. Add your own rules or tweak the plain-English copy. * * @param array $rules Rules keyed by id. */ $filtered = apply_filters( 'a11ymate_rules', $rules ); $rules = array(); foreach ( (array) $filtered as $id => $rule ) { if ( ! is_array( $rule ) || empty( $rule['id'] ) ) { continue; } $rules[ $rule['id'] ] = self::normalise( $rule ); } self::$rules = $rules; } return self::$rules; } /** * Fill in defaults for third-party rules. * * @param array $rule Rule. * @return array */ private static function normalise( array $rule ) { $defaults = array( 'title' => $rule['id'], 'wcag' => array( 'sc' => '', 'name' => '', 'level' => 'A', ), 'severity' => 'moderate', 'category' => 'content', 'pass' => 'server', 'mode' => 'automated', 'why' => '', 'fix' => '', 'example' => '', 'helpUrl' => '', ); return array_merge( $defaults, $rule ); } /** * One rule. * * @param string $id Rule id. * @return array|null */ public static function get( $id ) { $all = self::all(); return isset( $all[ $id ] ) ? $all[ $id ] : null; } /** * Categories list. * * @return array */ public static function categories() { self::all(); return (array) self::$categories; } /** * Category titles keyed by id. * * @return array */ public static function category_titles() { $out = array(); foreach ( self::categories() as $c ) { $out[ $c['id'] ] = $c['title']; } return $out; } /** * Shared phrase lists. * * @return array */ public static function phrases() { if ( null === self::$phrases ) { self::$phrases = self::read( 'phrases.json' ); } return self::$phrases; } /** * Level rank so AAA > AA > A. * * @param string $level Level. * @return int */ public static function level_rank( $level ) { $map = array( 'A' => 1, 'AA' => 2, 'AAA' => 3, ); return isset( $map[ $level ] ) ? $map[ $level ] : 1; } /** * Ids of rules enabled by the current settings (categories and WCAG target). * * @param string|null $pass Limit to server|rendered|editor, or null for all. * @return string[] */ public static function enabled_ids( $pass = null ) { $settings = Settings::all(); $target = self::level_rank( $settings['wcag_target'] ); $cats = (array) $settings['categories']; $ids = array(); foreach ( self::all() as $id => $rule ) { if ( null !== $pass && $rule['pass'] !== $pass ) { continue; } if ( ! in_array( $rule['category'], $cats, true ) ) { continue; } if ( 'needs-review' === $rule['mode'] ) { // Advisory checks ignore the level target; they have their own switch. if ( empty( $settings['show_needs_review'] ) ) { continue; } } elseif ( self::level_rank( $rule['wcag']['level'] ) > $target ) { continue; } $ids[] = $id; } return $ids; } /** * Rules as a list for the REST API and the editor. * * @return array */ public static function listing() { return array_values( self::all() ); } /** * Reset the cache (tests). * * @return void */ public static function flush() { self::$rules = null; self::$categories = null; self::$phrases = null; } }