array( 'post', 'page' ), 'display' => 'bars', 'style' => 'theme', 'night_accent' => '#8b7cf6', 'accent' => '', 'star_color' => '#f5b301', 'radius' => 10, 'heading' => 'h3', 'half_stars' => false, 'summary_top' => false, 'words' => true, 'author' => 'person', 'position' => 'bottom', 'scale' => 'p10', 'step' => 0.1, 'delete_data' => false, ); } /** * Aktuelle Einstellungen, ergänzt um Vorgaben. * * @return array */ public static function get() { $gespeichert = get_option( self::OPTION, array() ); return self::clean( is_array( $gespeichert ) ? $gespeichert : array() ); } /** * Einzelner Wert. * * @param string $key Schlüssel. * @return mixed */ public static function value( $key ) { $alle = self::get(); return isset( $alle[ $key ] ) ? $alle[ $key ] : null; } /** * Bereinigt Eingaben mit fester Feldliste. * * @param array $roh Eingabe. * @return array */ public static function clean( $roh ) { $roh = is_array( $roh ) ? $roh : array(); $v = self::defaults(); $erlaubte = array_values( get_post_types( array( 'public' => true ), 'names' ) ); if ( isset( $roh['post_types'] ) && is_array( $roh['post_types'] ) ) { $typen = array(); foreach ( $roh['post_types'] as $typ ) { $typ = sanitize_key( $typ ); if ( in_array( $typ, $erlaubte, true ) && 'attachment' !== $typ ) { $typen[] = $typ; } } $v['post_types'] = array_values( array_unique( $typen ) ); } if ( isset( $roh['display'] ) && in_array( $roh['display'], array( 'bars', 'stars', 'profile' ), true ) ) { $v['display'] = $roh['display']; } if ( isset( $roh['style'] ) && in_array( $roh['style'], array( 'theme', 'night' ), true ) ) { $v['style'] = $roh['style']; } if ( isset( $roh['night_accent'] ) && preg_match( '/^#[0-9a-fA-F]{6}$/', (string) $roh['night_accent'] ) ) { $v['night_accent'] = strtolower( $roh['night_accent'] ); } if ( isset( $roh['accent'] ) && ( '' === $roh['accent'] || preg_match( '/^#[0-9a-fA-F]{6}$/', (string) $roh['accent'] ) ) ) { $v['accent'] = strtolower( (string) $roh['accent'] ); } if ( isset( $roh['star_color'] ) && preg_match( '/^#[0-9a-fA-F]{6}$/', (string) $roh['star_color'] ) ) { $v['star_color'] = strtolower( $roh['star_color'] ); } if ( isset( $roh['radius'] ) && is_scalar( $roh['radius'] ) && preg_match( '/^\d{1,2}$/', (string) $roh['radius'] ) && (int) $roh['radius'] <= 24 ) { $v['radius'] = (int) $roh['radius']; } if ( isset( $roh['heading'] ) && in_array( $roh['heading'], array( 'h2', 'h3', 'h4' ), true ) ) { $v['heading'] = $roh['heading']; } foreach ( array( 'half_stars', 'summary_top' ) as $schalter ) { if ( array_key_exists( $schalter, $roh ) ) { $v[ $schalter ] = self::bool( $roh[ $schalter ] ); } } if ( array_key_exists( 'words', $roh ) ) { $v['words'] = self::bool( $roh['words'] ); } if ( isset( $roh['position'] ) && in_array( $roh['position'], array( 'bottom', 'manual' ), true ) ) { $v['position'] = $roh['position']; } if ( isset( $roh['author'] ) && in_array( $roh['author'], array( 'person', 'organization' ), true ) ) { $v['author'] = $roh['author']; } if ( isset( $roh['scale'] ) && null !== ThreeRB_Score::max( $roh['scale'] ) ) { $v['scale'] = $roh['scale']; } if ( isset( $roh['step'] ) && is_numeric( $roh['step'] ) && in_array( (float) $roh['step'], ThreeRB_Score::scales()[ $v['scale'] ]['steps'], true ) ) { $v['step'] = (float) $roh['step']; } elseif ( ! in_array( (float) $v['step'], ThreeRB_Score::scales()[ $v['scale'] ]['steps'], true ) ) { $v['step'] = ThreeRB_Score::scales()[ $v['scale'] ]['steps'][0]; } if ( array_key_exists( 'delete_data', $roh ) ) { $v['delete_data'] = self::bool( $roh['delete_data'] ); } return $v; } /** * Boolean ausdrücklich lesen: „false“, „0“, leer und „off“ sind falsch. * * @param mixed $wert Wert. * @return bool */ public static function bool( $wert ) { if ( is_bool( $wert ) ) { return $wert; } if ( is_int( $wert ) ) { return 1 === $wert; } return in_array( strtolower( trim( (string) $wert ) ), array( '1', 'true', 'on', 'yes' ), true ); } }