[ 'pie', 'bar' ], 'activation' => [ 'pie', 'bar' ], 'conditions' => [ 'pie', 'bar' ], 'location' => [ 'pie', 'bar' ], 'tags' => [ 'bar', 'cloud' ], ]; /** * The Insights chart views shown when no preference has been saved. */ public const DEFAULT_VIEWS = [ 'type' => 'bar', 'activation' => 'pie', 'conditions' => 'pie', 'location' => 'bar', 'tags' => 'bar', ]; /** * Retrieve the Insights chart views, normalizing missing or invalid values. * * @return array */ public static function get_insights_chart_views(): array { $views = get_option( self::OPTION_NAME ); if ( ! is_array( $views ) ) { return self::DEFAULT_VIEWS; } return array_reduce( self::CHART_KEYS, static function ( array $normalized, string $key ) use ( $views ): array { $view = $views[ $key ] ?? self::DEFAULT_VIEWS[ $key ]; $normalized[ $key ] = in_array( $view, self::CHART_VIEWS[ $key ], true ) ? $view : self::DEFAULT_VIEWS[ $key ]; return $normalized; }, [] ); } /** * Retrieve the stored preference value, falling back to the default when the * stored value is missing or invalid. * * @return array */ protected function get_option_value(): array { return self::get_insights_chart_views(); } /** * Get the schema for the update request argument. * * @return array The schema for the update request argument. */ protected function get_update_request_schema(): array { return [ 'description' => esc_html__( 'Supported chart-specific view for each Insights chart; tags also supports the cloud view.', 'code-snippets' ), 'type' => 'object', 'required' => true, 'validate_callback' => [ $this, 'validate_insights_chart_views' ], ]; } /** * Validate a complete Insights chart view preference map. * * @param mixed $views Candidate preference map. * * @return bool */ public function validate_insights_chart_views( $views ): bool { if ( ! is_array( $views ) || count( self::CHART_KEYS ) !== count( $views ) ) { return false; } foreach ( self::CHART_KEYS as $key ) { if ( ! array_key_exists( $key, $views ) ) { return false; } $view = $views[ $key ]; if ( ! in_array( $view, self::CHART_VIEWS[ $key ], true ) ) { return false; } } return true; } }