PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.12.2
Independent Analytics – WordPress Analytics Plugin v2.12.2
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 11 months ago Form_Field.php 11 months 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
153 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 $ranges_to_support = ['TODAY', 'YESTERDAY', 'THIS_WEEK', 'LAST_WEEK', 'LAST_SEVEN', 'LAST_THIRTY', 'LAST_THREE_MONTHS', 'LAST_SIX_MONTHS', 'LAST_TWELVE_MONTHS'];
83 $date_range_values = Collection::make(Relative_Date_Range::ranges($ranges_to_support))->map(function (Relative_Date_Range $range) {
84 return \IAWP\Overview\Form_Field_Option::new($range->relative_range_id(), $range->label());
85 })->all();
86 $busiest_date_range_values = Collection::make(Relative_Date_Range::ranges())->filter(function (Relative_Date_Range $range) {
87 $busiest_ranges = ['LAST_SEVEN', 'LAST_THIRTY', 'LAST_NINETY'];
88 return \in_array($range->relative_range_id(), $busiest_ranges);
89 })->map(function (Relative_Date_Range $range) {
90 return \IAWP\Overview\Form_Field_Option::new($range->relative_range_id(), $range->label());
91 })->all();
92 $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();
93 $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'))];
94 $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];
95 return $supported_values_lookup_table[$this->id] ?? [];
96 }
97 private function supported_ids() : array
98 {
99 return Collection::make($this->supported_values())->map(function (\IAWP\Overview\Form_Field_Option $option) {
100 return $option->id();
101 })->all();
102 }
103 /**
104 * Check if the provided array values are valid for a given form field.
105 *
106 * @param array $values
107 *
108 * @return bool
109 */
110 private function is_a_supported_array_value(array $values) : bool
111 {
112 // Only checkboxes support selecting more than one value
113 if ($this->type() !== 'checkboxes') {
114 return \false;
115 }
116 return Collection::make($values)->every(function ($value) {
117 return \in_array($value, $this->supported_ids(), \true);
118 });
119 }
120 public static function get_report_details() : array
121 {
122 if (\is_array(self::$report_details)) {
123 return self::$report_details;
124 }
125 self::$report_details = Collection::make(Report_Finder::new()->get_reports())->map(function (Report $report) {
126 $columns = Collection::make($report->get_supported_columns())->filter(function (Column $column) {
127 return \in_array($column->type(), ['int', 'date']);
128 })->map(function (Column $column) {
129 $plugin_group = Plugin_Group::get_plugin_group($column->plugin_group());
130 return \IAWP\Overview\Form_Field_Option::new($column->id(), $column->name(), $plugin_group->name());
131 })->values()->all();
132 $aggregatable_columns = Collection::make($report->get_supported_columns())->filter(function (Column $column) {
133 return $column->aggregatable();
134 })->map(function (Column $column) {
135 $plugin_group = Plugin_Group::get_plugin_group($column->plugin_group());
136 return \IAWP\Overview\Form_Field_Option::new($column->id(), $column->name(), $plugin_group->name());
137 })->values()->all();
138 $statistics = \array_map(function (Statistic $statistic) {
139 $plugin_group = Plugin_Group::get_plugin_group($statistic->plugin_group());
140 return \IAWP\Overview\Form_Field_Option::new($statistic->id(), $statistic->name(), $plugin_group->name());
141 }, $report->get_supported_statistics());
142 return ['id' => $report->id(), 'name' => $report->name(), 'columns' => $columns, 'aggregatable_columns' => $aggregatable_columns, 'statistics' => $statistics];
143 })->all();
144 return self::$report_details;
145 }
146 public static function get_report_details_by_id($id) : ?array
147 {
148 return Collection::make(self::get_report_details())->first(function ($report_details) use($id) {
149 return $report_details['id'] === $id;
150 });
151 }
152 }
153