PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.11.9
Independent Analytics – WordPress Analytics Plugin v2.11.9
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Overview / Form_Field.php
independent-analytics / IAWP / Overview Last commit date
Modules 1 year ago Form_Field.php 1 year ago Form_Field_Option.php 1 year ago Module_Refresh_Job.php 1 year ago Overview.php 1 year ago Sync_Module_Background_Job.php 1 year ago WP_Options_Storage.php 1 year ago
Form_Field.php
152 lines
1 <?php
2
3 namespace IAWP\Overview;
4
5 use IAWP\Date_Range\Relative_Date_Range;
6 use IAWP\Plugin_Group;
7 use IAWP\Report;
8 use IAWP\Report_Finder;
9 use IAWP\Statistics\Statistic;
10 use IAWP\Tables\Columns\Column;
11 use IAWPSCOPED\Illuminate\Support\Collection;
12 /** @internal */
13 class Form_Field
14 {
15 private static $report_details;
16 private $id;
17 private $report;
18 public function __construct(string $id, ?Report $report)
19 {
20 $this->id = $id;
21 $this->report = $report;
22 }
23 public function id() : string
24 {
25 return $this->id;
26 }
27 public function name() : string
28 {
29 $name_lookup_table = ['report' => \__('Report', 'independent-analytics'), 'geo_report' => \__('Geo Report', 'independent-analytics'), 'sort_by' => \__('Sort By', 'independent-analytics'), 'sort_direction' => \__('Sort Direction', 'independent-analytics'), 'aggregatable_sort_by' => \__('Sort By', 'independent-analytics'), 'date_range' => \__('Date Range', 'independent-analytics'), 'busiest_date_range' => \__('Date Range', 'independent-analytics'), 'primary_metric' => \__('Primary Metric', 'independent-analytics'), 'secondary_metric' => \__('Secondary Metric', 'independent-analytics'), 'statistics' => \__('Statistics', 'independent-analytics'), 'recent_conversion_types' => \__('Conversions', 'independent-analytics')];
30 return $name_lookup_table[$this->id] ?? $this->id;
31 }
32 public function type() : string
33 {
34 $field_type_lookup_table = ['report' => 'select', 'geo_report' => 'select', 'sort_by' => 'select', 'sort_direction' => 'select', 'aggregatable_sort_by' => 'select', 'date_range' => 'select', 'busiest_date_range' => 'select', 'primary_metric' => 'select', 'secondary_metric' => 'select', 'statistics' => 'checkboxes', 'recent_conversion_types' => 'checkboxes'];
35 return $field_type_lookup_table[$this->id] ?? 'select';
36 }
37 /**
38 * Check if the provided value is valid for a given form field.
39 *
40 * @param mixed $value
41 *
42 * @return bool
43 */
44 public function is_a_supported_value($value) : bool
45 {
46 if (\is_array($value)) {
47 return $this->is_a_supported_array_value($value);
48 }
49 return \in_array($value, $this->supported_ids(), \true);
50 }
51 /**
52 * Get the grouped form field options.
53 *
54 * @return array
55 */
56 public function template_values() : array
57 {
58 $grouped_values = Collection::make($this->supported_values())->groupBy(function (\IAWP\Overview\Form_Field_Option $form_field_option) {
59 return $form_field_option->group();
60 })->toArray();
61 // If the values don't have groups, just return an array of the values
62 if (\count($grouped_values) === 1 && \array_key_exists("", $grouped_values)) {
63 return $grouped_values[''];
64 }
65 return $grouped_values;
66 }
67 /**
68 * @return Form_Field_Option[]
69 */
70 public function supported_values() : array
71 {
72 $report_details = self::get_report_details_by_id($this->report->id());
73 $report_values = Collection::make(Report_Finder::new()->get_reports())->map(function (Report $report) {
74 return \IAWP\Overview\Form_Field_Option::new($report->id(), $report->name(), $report->type_label());
75 })->all();
76 $geo_report_values = Collection::make(Report_Finder::new()->get_reports())->filter(function (Report $report) {
77 return $report->type() === 'geo';
78 })->map(function (Report $report) {
79 return \IAWP\Overview\Form_Field_Option::new($report->id(), $report->name());
80 })->values()->all();
81 $sort_direction_values = [\IAWP\Overview\Form_Field_Option::new('desc', 'High-to-Low'), \IAWP\Overview\Form_Field_Option::new('asc', 'Low-to-High')];
82 $date_range_values = Collection::make(Relative_Date_Range::ranges())->map(function (Relative_Date_Range $range) {
83 return \IAWP\Overview\Form_Field_Option::new($range->relative_range_id(), $range->label());
84 })->take(6)->all();
85 $busiest_date_range_values = Collection::make(Relative_Date_Range::ranges())->filter(function (Relative_Date_Range $range) {
86 $busiest_ranges = ['LAST_SEVEN', 'LAST_THIRTY', 'LAST_NINETY'];
87 return \in_array($range->relative_range_id(), $busiest_ranges);
88 })->map(function (Relative_Date_Range $range) {
89 return \IAWP\Overview\Form_Field_Option::new($range->relative_range_id(), $range->label());
90 })->all();
91 $secondary_metric = Collection::make($report_details['statistics'] ?? [])->prepend(\IAWP\Overview\Form_Field_Option::new('no_comparison', \__('No Comparison', 'independent-analytics'), \__('No Comparison', 'independent-analytics')))->all();
92 $recent_conversion_types = [\IAWP\Overview\Form_Field_Option::new('order', \__('Orders', 'independent-analytics')), \IAWP\Overview\Form_Field_Option::new('form_submission', \__('Form Submissions', 'independent-analytics')), \IAWP\Overview\Form_Field_Option::new('click', \__('Clicks', 'independent-analytics'))];
93 $supported_values_lookup_table = ['report' => $report_values, 'geo_report' => $geo_report_values, 'sort_by' => $report_details['columns'] ?? [], 'sort_direction' => $sort_direction_values, 'aggregatable_sort_by' => $report_details['aggregatable_columns'] ?? [], 'date_range' => $date_range_values, 'busiest_date_range' => $busiest_date_range_values, 'primary_metric' => $report_details['statistics'] ?? [], 'secondary_metric' => $secondary_metric, 'statistics' => $report_details['statistics'] ?? [], 'recent_conversion_types' => $recent_conversion_types];
94 return $supported_values_lookup_table[$this->id] ?? [];
95 }
96 private function supported_ids() : array
97 {
98 return Collection::make($this->supported_values())->map(function (\IAWP\Overview\Form_Field_Option $option) {
99 return $option->id();
100 })->all();
101 }
102 /**
103 * Check if the provided array values are valid for a given form field.
104 *
105 * @param array $values
106 *
107 * @return bool
108 */
109 private function is_a_supported_array_value(array $values) : bool
110 {
111 // Only checkboxes support selecting more than one value
112 if ($this->type() !== 'checkboxes') {
113 return \false;
114 }
115 return Collection::make($values)->every(function ($value) {
116 return \in_array($value, $this->supported_ids(), \true);
117 });
118 }
119 public static function get_report_details() : array
120 {
121 if (\is_array(self::$report_details)) {
122 return self::$report_details;
123 }
124 self::$report_details = Collection::make(Report_Finder::new()->get_reports())->map(function (Report $report) {
125 $columns = Collection::make($report->get_supported_columns())->filter(function (Column $column) {
126 return \in_array($column->type(), ['int', 'date']);
127 })->map(function (Column $column) {
128 $plugin_group = Plugin_Group::get_plugin_group($column->plugin_group());
129 return \IAWP\Overview\Form_Field_Option::new($column->id(), $column->name(), $plugin_group->name());
130 })->values()->all();
131 $aggregatable_columns = Collection::make($report->get_supported_columns())->filter(function (Column $column) {
132 return $column->aggregatable();
133 })->map(function (Column $column) {
134 $plugin_group = Plugin_Group::get_plugin_group($column->plugin_group());
135 return \IAWP\Overview\Form_Field_Option::new($column->id(), $column->name(), $plugin_group->name());
136 })->values()->all();
137 $statistics = \array_map(function (Statistic $statistic) {
138 $plugin_group = Plugin_Group::get_plugin_group($statistic->plugin_group());
139 return \IAWP\Overview\Form_Field_Option::new($statistic->id(), $statistic->name(), $plugin_group->name());
140 }, $report->get_supported_statistics());
141 return ['id' => $report->id(), 'name' => $report->name(), 'columns' => $columns, 'aggregatable_columns' => $aggregatable_columns, 'statistics' => $statistics];
142 })->all();
143 return self::$report_details;
144 }
145 public static function get_report_details_by_id($id) : ?array
146 {
147 return Collection::make(self::get_report_details())->first(function ($report_details) use($id) {
148 return $report_details['id'] === $id;
149 });
150 }
151 }
152