| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\REST_API\Preferences; |
| 4 |
|
| 5 |
/** |
| 6 |
* Controller for reading and updating the saved view preferences for Insights charts. |
| 7 |
* |
| 8 |
* @package Code_Snippets |
| 9 |
*/ |
| 10 |
final class Insights_View_Rest_Controller extends Preference_REST_Controller { |
| 11 |
|
| 12 |
/** |
| 13 |
* Current API version. |
| 14 |
*/ |
| 15 |
public const VERSION = 1; |
| 16 |
|
| 17 |
/** |
| 18 |
* The base of this controller's route. |
| 19 |
*/ |
| 20 |
public const BASE_ROUTE = 'insights-chart-views'; |
| 21 |
|
| 22 |
/** |
| 23 |
* The key used to identify this preference in the REST API. |
| 24 |
*/ |
| 25 |
protected const PREFERENCE_KEY = 'views'; |
| 26 |
|
| 27 |
/** |
| 28 |
* The name of the option used to store Insights chart view preferences. |
| 29 |
*/ |
| 30 |
public const OPTION_NAME = 'code_snippets_insights_preferences'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Insights charts with independently configurable views. |
| 34 |
*/ |
| 35 |
public const CHART_KEYS = [ 'type', 'activation', 'conditions', 'location', 'tags' ]; |
| 36 |
|
| 37 |
/** |
| 38 |
* Valid Insights chart view values. |
| 39 |
*/ |
| 40 |
public const CHART_VIEWS = [ |
| 41 |
'type' => [ 'pie', 'bar' ], |
| 42 |
'activation' => [ 'pie', 'bar' ], |
| 43 |
'conditions' => [ 'pie', 'bar' ], |
| 44 |
'location' => [ 'pie', 'bar' ], |
| 45 |
'tags' => [ 'bar', 'cloud' ], |
| 46 |
]; |
| 47 |
|
| 48 |
/** |
| 49 |
* The Insights chart views shown when no preference has been saved. |
| 50 |
*/ |
| 51 |
public const DEFAULT_VIEWS = [ |
| 52 |
'type' => 'bar', |
| 53 |
'activation' => 'pie', |
| 54 |
'conditions' => 'pie', |
| 55 |
'location' => 'bar', |
| 56 |
'tags' => 'bar', |
| 57 |
]; |
| 58 |
|
| 59 |
/** |
| 60 |
* Retrieve the Insights chart views, normalizing missing or invalid values. |
| 61 |
* |
| 62 |
* @return array<string, string> |
| 63 |
*/ |
| 64 |
public static function get_insights_chart_views(): array { |
| 65 |
$views = get_option( self::OPTION_NAME ); |
| 66 |
|
| 67 |
if ( ! is_array( $views ) ) { |
| 68 |
return self::DEFAULT_VIEWS; |
| 69 |
} |
| 70 |
|
| 71 |
return array_reduce( |
| 72 |
self::CHART_KEYS, |
| 73 |
static function ( array $normalized, string $key ) use ( $views ): array { |
| 74 |
$view = $views[ $key ] ?? self::DEFAULT_VIEWS[ $key ]; |
| 75 |
$normalized[ $key ] = in_array( $view, self::CHART_VIEWS[ $key ], true ) |
| 76 |
? $view |
| 77 |
: self::DEFAULT_VIEWS[ $key ]; |
| 78 |
|
| 79 |
return $normalized; |
| 80 |
}, |
| 81 |
[] |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Retrieve the stored preference value, falling back to the default when the |
| 87 |
* stored value is missing or invalid. |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
protected function get_option_value(): array { |
| 92 |
return self::get_insights_chart_views(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get the schema for the update request argument. |
| 97 |
* |
| 98 |
* @return array The schema for the update request argument. |
| 99 |
*/ |
| 100 |
protected function get_update_request_schema(): array { |
| 101 |
return [ |
| 102 |
'description' => esc_html__( 'Supported chart-specific view for each Insights chart; tags also supports the cloud view.', 'code-snippets' ), |
| 103 |
'type' => 'object', |
| 104 |
'required' => true, |
| 105 |
'validate_callback' => [ $this, 'validate_insights_chart_views' ], |
| 106 |
]; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Validate a complete Insights chart view preference map. |
| 111 |
* |
| 112 |
* @param mixed $views Candidate preference map. |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
public function validate_insights_chart_views( $views ): bool { |
| 117 |
if ( ! is_array( $views ) || count( self::CHART_KEYS ) !== count( $views ) ) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
foreach ( self::CHART_KEYS as $key ) { |
| 122 |
if ( ! array_key_exists( $key, $views ) ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
$view = $views[ $key ]; |
| 127 |
|
| 128 |
if ( ! in_array( $view, self::CHART_VIEWS[ $key ], true ) ) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return true; |
| 134 |
} |
| 135 |
} |
| 136 |
|