array( 'post', 'page' ), 'max_pages' => 0, 'exclude_paths' => '', 'sample_archives' => 5, 'categories' => array( 'images', 'structure', 'links', 'forms', 'aria', 'media', 'colour', 'typography', 'interaction', 'content' ), 'wcag_target' => 'AA', 'show_needs_review' => 1, 'capability' => 'manage_options', 'delete_on_uninstall' => 0, 'editor_require' => 0, 'safe_fixes' => array( 'skip_link' => 0, 'pinch_zoom' => 0, 'focus_ring' => 0, 'new_tab_text' => 0, 'iframe_titles' => 0, 'reduced_motion' => 0, 'underline_links' => 0, 'lang' => 0, 'tabindex' => 0, 'alt_from_library' => 0, 'focus_colour' => '#1F5C8B', ), ); } /** * Get all settings. * * @return array */ public static function all() { $saved = get_option( self::OPTION, array() ); if ( ! is_array( $saved ) ) { $saved = array(); } $defaults = self::defaults(); $merged = array_merge( $defaults, $saved ); foreach ( array( 'safe_fixes', 'toolbar' ) as $group ) { if ( isset( $defaults[ $group ] ) ) { $merged[ $group ] = array_merge( $defaults[ $group ], isset( $saved[ $group ] ) && is_array( $saved[ $group ] ) ? $saved[ $group ] : array() ); } } return $merged; } /** * Move installs that still have the retired cobalt default (#2A3BFF) for the safe-fix focus colour to * the brand blue. A colour someone chose themselves is left alone. * * @return void */ public static function migrate_brand() { $saved = get_option( self::OPTION, null ); if ( ! is_array( $saved ) ) { return; } $before = $saved; if ( isset( $saved['safe_fixes']['focus_colour'] ) && is_string( $saved['safe_fixes']['focus_colour'] ) && '#2a3bff' === strtolower( $saved['safe_fixes']['focus_colour'] ) ) { $saved['safe_fixes']['focus_colour'] = '#1F5C8B'; } if ( $saved !== $before ) { update_option( self::OPTION, $saved, false ); } } /** * Get one setting. * * @param string $key Key. * @param mixed $fallback Fallback. * @return mixed */ public static function get( $key, $fallback = null ) { $all = self::all(); return array_key_exists( $key, $all ) ? $all[ $key ] : $fallback; } /** * Capability needed to use A11yMate. * * @return string */ public static function capability() { $cap = self::get( 'capability', 'manage_options' ); $cap = is_string( $cap ) && '' !== $cap ? $cap : 'manage_options'; return (string) apply_filters( 'a11ymate_capability', $cap ); } /** * Merge, sanitise and store a partial update. * * @param array $input Raw input. * @return array The saved settings. */ public static function update( array $input ) { $current = self::all(); $clean = self::sanitize( $input, $current ); update_option( self::OPTION, $clean, false ); return $clean; } /** * Sanitise input over the current settings. * * @param array $in Input. * @param array $current Current settings. * @return array */ public static function sanitize( array $in, array $current ) { $out = $current; if ( isset( $in['post_types'] ) && is_array( $in['post_types'] ) ) { $valid = array_keys( get_post_types( array( 'public' => true ) ) ); $out['post_types'] = array_values( array_intersect( array_map( 'sanitize_key', $in['post_types'] ), $valid ) ); } if ( isset( $in['max_pages'] ) ) { $out['max_pages'] = max( 0, min( 100000, (int) $in['max_pages'] ) ); } if ( isset( $in['exclude_paths'] ) ) { $lines = preg_split( '/\R/', (string) $in['exclude_paths'] ); $lines = array_filter( array_map( 'sanitize_text_field', $lines ) ); $out['exclude_paths'] = implode( "\n", $lines ); } if ( isset( $in['sample_archives'] ) ) { $out['sample_archives'] = max( 0, min( 20, (int) $in['sample_archives'] ) ); } if ( isset( $in['categories'] ) && is_array( $in['categories'] ) ) { $valid = self::defaults()['categories']; $out['categories'] = array_values( array_intersect( array_map( 'sanitize_key', $in['categories'] ), $valid ) ); } if ( isset( $in['wcag_target'] ) ) { $out['wcag_target'] = in_array( $in['wcag_target'], array( 'A', 'AA', 'AAA' ), true ) ? $in['wcag_target'] : 'AA'; } foreach ( array( 'show_needs_review', 'delete_on_uninstall', 'editor_require' ) as $flag ) { if ( isset( $in[ $flag ] ) ) { $out[ $flag ] = $in[ $flag ] ? 1 : 0; } } if ( isset( $in['capability'] ) ) { $cap = sanitize_key( (string) $in['capability'] ); $out['capability'] = in_array( $cap, array( 'manage_options', 'edit_pages', 'edit_others_posts', 'edit_posts' ), true ) ? $cap : 'manage_options'; } if ( isset( $in['safe_fixes'] ) && is_array( $in['safe_fixes'] ) ) { foreach ( $current['safe_fixes'] as $k => $v ) { if ( ! isset( $in['safe_fixes'][ $k ] ) ) { continue; } if ( 'focus_colour' === $k ) { $c = sanitize_hex_color( (string) $in['safe_fixes'][ $k ] ); $out['safe_fixes'][ $k ] = $c ? $c : '#1F5C8B'; } else { $out['safe_fixes'][ $k ] = $in['safe_fixes'][ $k ] ? 1 : 0; } } } return $out; } }