PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.14.8
Independent Analytics – WordPress Analytics Plugin v2.14.8
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 3 months ago Form_Field.php 5 months ago Form_Field_Option.php 1 year ago Module_Refresh_Job.php 1 year ago Overview.php 1 year ago WP_Options_Storage.php 9 months ago
Form_Field.php
158 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())->filter(function (Report $report) {
74 // User Journey reports do not make sense for the Overview Report
75 if ($report->type() === 'journeys') {
76 return \false;
77 }
78 return \true;
79 })->values()->map(function (Report $report) {
80 return \IAWP\Overview\Form_Field_Option::new($report->id(), $report->name(), $report->type_label());
81 })->all();
82 $geo_report_values = Collection::make(Report_Finder::new()->get_reports())->filter(function (Report $report) {
83 return $report->type() === 'geo';
84 })->map(function (Report $report) {
85 return \IAWP\Overview\Form_Field_Option::new($report->id(), $report->name());
86 })->values()->all();
87 $sort_direction_values = [\IAWP\Overview\Form_Field_Option::new('desc', 'High-to-Low'), \IAWP\Overview\Form_Field_Option::new('asc', 'Low-to-High')];
88 $date_range_values = Collection::make(Relative_Date_Range::ranges())->map(function (Relative_Date_Range $range) {
89 return \IAWP\Overview\Form_Field_Option::new($range->relative_range_id(), $range->label());
90 })->all();
91 $busiest_date_range_values = Collection::make(Relative_Date_Range::ranges())->filter(function (Relative_Date_Range $range) {
92 $busiest_ranges = ['LAST_SEVEN', 'LAST_THIRTY', 'LAST_NINETY'];
93 return \in_array($range->relative_range_id(), $busiest_ranges);
94 })->map(function (Relative_Date_Range $range) {
95 return \IAWP\Overview\Form_Field_Option::new($range->relative_range_id(), $range->label());
96 })->all();
97 $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();
98 $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'))];
99 $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];
100 return $supported_values_lookup_table[$this->id] ?? [];
101 }
102 private function supported_ids() : array
103 {
104 return Collection::make($this->supported_values())->map(function (\IAWP\Overview\Form_Field_Option $option) {
105 return $option->id();
106 })->all();
107 }
108 /**
109 * Check if the provided array values are valid for a given form field.
110 *
111 * @param array $values
112 *
113 * @return bool
114 */
115 private function is_a_supported_array_value(array $values) : bool
116 {
117 // Only checkboxes support selecting more than one value
118 if ($this->type() !== 'checkboxes') {
119 return \false;
120 }
121 return Collection::make($values)->every(function ($value) {
122 return \in_array($value, $this->supported_ids(), \true);
123 });
124 }
125 public static function get_report_details() : array
126 {
127 if (\is_array(self::$report_details)) {
128 return self::$report_details;
129 }
130 self::$report_details = Collection::make(Report_Finder::new()->get_reports())->map(function (Report $report) {
131 $columns = Collection::make($report->get_supported_columns())->filter(function (Column $column) {
132 return \in_array($column->type(), ['int', 'date']);
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 $aggregatable_columns = Collection::make($report->get_supported_columns())->filter(function (Column $column) {
138 return $column->aggregatable();
139 })->map(function (Column $column) {
140 $plugin_group = Plugin_Group::get_plugin_group($column->plugin_group());
141 return \IAWP\Overview\Form_Field_Option::new($column->id(), $column->name(), $plugin_group->name());
142 })->values()->all();
143 $statistics = \array_map(function (Statistic $statistic) {
144 $plugin_group = Plugin_Group::get_plugin_group($statistic->plugin_group());
145 return \IAWP\Overview\Form_Field_Option::new($statistic->id(), $statistic->name(), $plugin_group->name());
146 }, $report->get_supported_statistics());
147 return ['id' => $report->id(), 'name' => $report->name(), 'columns' => $columns, 'aggregatable_columns' => $aggregatable_columns, 'statistics' => $statistics];
148 })->all();
149 return self::$report_details;
150 }
151 public static function get_report_details_by_id($id) : ?array
152 {
153 return Collection::make(self::get_report_details())->first(function ($report_details) use($id) {
154 return $report_details['id'] === $id;
155 });
156 }
157 }
158