Intervals
5 months ago
Campaign_Landing_Page_Statistics.php
9 months ago
Campaign_Statistics.php
2 years ago
Campaign_UTM_Campaign_Statistics.php
9 months ago
Campaign_UTM_Medium_Statistics.php
9 months ago
Campaign_UTM_Source_Statistics.php
9 months ago
City_Statistics.php
2 years ago
Click_Statistics.php
5 months ago
Country_Statistics.php
2 years ago
Device_Browser_Statistics.php
2 years ago
Device_OS_Statistics.php
2 years ago
Device_Type_Statistics.php
2 years ago
Journey_Statistics.php
6 months ago
Page_Statistics.php
2 years ago
Referrer_Statistics.php
2 years ago
Referrer_Type_Statistics.php
9 months ago
Statistic.php
9 months ago
Statistics.php
3 months ago
Statistic.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Statistics; |
| 4 | |
| 5 | use IAWP\Dashboard_Options; |
| 6 | use IAWP\Plugin_Group_Option; |
| 7 | use IAWP\Utils\Currency; |
| 8 | use IAWP\Utils\Number_Formatter; |
| 9 | /** @internal */ |
| 10 | class Statistic implements Plugin_Group_Option |
| 11 | { |
| 12 | private $id; |
| 13 | private $name; |
| 14 | private $plugin_group; |
| 15 | private $plugin_group_header; |
| 16 | private $icon; |
| 17 | private $statistic; |
| 18 | private $previous_period_statistic; |
| 19 | private $unfiltered_statistic; |
| 20 | private $statistic_over_time; |
| 21 | private $is_visible_in_dashboard_widget; |
| 22 | private $is_invisible; |
| 23 | private $format; |
| 24 | private $is_growth_good; |
| 25 | private $is_subgroup_plugin_enabled; |
| 26 | private $requires_pro; |
| 27 | public function __construct(array $attributes) |
| 28 | { |
| 29 | $this->id = $attributes['id']; |
| 30 | $this->name = $attributes['name']; |
| 31 | $this->plugin_group = $attributes['plugin_group']; |
| 32 | $this->plugin_group_header = $attributes['plugin_group_header'] ?? null; |
| 33 | $this->icon = $attributes['icon'] ?? null; |
| 34 | $this->statistic = $attributes['statistic'] ?? 0; |
| 35 | $this->previous_period_statistic = $attributes['previous_period_statistic'] ?? 0; |
| 36 | $this->unfiltered_statistic = $attributes['unfiltered_statistic'] ?? null; |
| 37 | $this->statistic_over_time = $attributes['statistic_over_time'] ?? []; |
| 38 | $this->is_visible_in_dashboard_widget = $attributes['is_visible_in_dashboard_widget'] ?? \false; |
| 39 | $this->is_invisible = $attributes['is_invisible'] ?? \false; |
| 40 | $this->format = $attributes['format'] ?? null; |
| 41 | $this->is_growth_good = $attributes['is_growth_good'] ?? \true; |
| 42 | $this->is_subgroup_plugin_enabled = $attributes['is_subgroup_plugin_active'] ?? \true; |
| 43 | $this->requires_pro = $attributes['requires_pro'] ?? \false; |
| 44 | } |
| 45 | public function is_enabled() : bool |
| 46 | { |
| 47 | if ($this->requires_pro === \true && \IAWPSCOPED\iawp_is_free()) { |
| 48 | return \false; |
| 49 | } |
| 50 | return \true; |
| 51 | } |
| 52 | public function id() : string |
| 53 | { |
| 54 | return $this->id; |
| 55 | } |
| 56 | public function name() : string |
| 57 | { |
| 58 | return $this->name; |
| 59 | } |
| 60 | public function icon() : ?string |
| 61 | { |
| 62 | return $this->icon; |
| 63 | } |
| 64 | public function plugin_group() : string |
| 65 | { |
| 66 | return $this->plugin_group; |
| 67 | } |
| 68 | public function plugin_group_header() : ?string |
| 69 | { |
| 70 | return $this->plugin_group_header; |
| 71 | } |
| 72 | public function is_visible_in_dashboard_widget() : bool |
| 73 | { |
| 74 | return $this->is_visible_in_dashboard_widget; |
| 75 | } |
| 76 | public function is_invisible() : bool |
| 77 | { |
| 78 | return $this->is_invisible; |
| 79 | } |
| 80 | public function statistic_over_time() : array |
| 81 | { |
| 82 | return $this->statistic_over_time; |
| 83 | } |
| 84 | public function is_group_plugin_enabled() : bool |
| 85 | { |
| 86 | switch ($this->plugin_group) { |
| 87 | case "ecommerce": |
| 88 | return \IAWPSCOPED\iawp()->is_ecommerce_support_enabled(); |
| 89 | case "forms": |
| 90 | return \IAWPSCOPED\iawp()->is_form_submission_support_enabled(); |
| 91 | default: |
| 92 | return \true; |
| 93 | } |
| 94 | } |
| 95 | public function is_subgroup_plugin_enabled() : bool |
| 96 | { |
| 97 | return $this->is_subgroup_plugin_enabled; |
| 98 | } |
| 99 | public function is_visible() : bool |
| 100 | { |
| 101 | $options = Dashboard_Options::getInstance(); |
| 102 | return \in_array($this->id(), $options->visible_quick_stats()); |
| 103 | } |
| 104 | public function is_member_of_plugin_group(string $plugin_group) : bool |
| 105 | { |
| 106 | return $this->plugin_group === $plugin_group; |
| 107 | } |
| 108 | public function formatted_value() : string |
| 109 | { |
| 110 | return $this->format_value($this->statistic); |
| 111 | } |
| 112 | public function value() |
| 113 | { |
| 114 | return $this->statistic; |
| 115 | } |
| 116 | public function formatted_unfiltered_value() : ?string |
| 117 | { |
| 118 | if (\is_null($this->unfiltered_statistic)) { |
| 119 | return null; |
| 120 | } |
| 121 | return $this->format_value($this->unfiltered_statistic); |
| 122 | } |
| 123 | /** |
| 124 | * Growth can be good or bad depending on the quick stat. This allows it to be calculated on a |
| 125 | * at the individual quick stat level. |
| 126 | * |
| 127 | * The default behavior is "up" and "good" so you can tweak either of those as needed |
| 128 | * |
| 129 | * @return string |
| 130 | */ |
| 131 | public function growth_html_class() : string |
| 132 | { |
| 133 | $html_classes = []; |
| 134 | $has_growth = $this->growth() >= 0; |
| 135 | if (!$has_growth) { |
| 136 | $html_classes[] = 'down'; |
| 137 | } |
| 138 | if ($has_growth && !$this->is_growth_good || !$has_growth && $this->is_growth_good) { |
| 139 | $html_classes[] = 'bad'; |
| 140 | } |
| 141 | return \implode(' ', $html_classes); |
| 142 | } |
| 143 | public function formatted_growth() : string |
| 144 | { |
| 145 | $growth = $this->growth(); |
| 146 | return Number_Formatter::percent(\absint($growth)); |
| 147 | } |
| 148 | public function growth() |
| 149 | { |
| 150 | if ($this->statistic == 0 && $this->previous_period_statistic != 0) { |
| 151 | return -100; |
| 152 | } elseif ($this->statistic == 0 || $this->previous_period_statistic == 0) { |
| 153 | return 0; |
| 154 | } |
| 155 | $percent_growth = ($this->statistic / $this->previous_period_statistic - 1) * 100; |
| 156 | return \round($percent_growth, 0); |
| 157 | } |
| 158 | public function format_value($value) : string |
| 159 | { |
| 160 | switch ($this->format) { |
| 161 | case 'time': |
| 162 | return Number_Formatter::second_to_minute_timestamp($value); |
| 163 | case 'percent': |
| 164 | return Number_Formatter::percent($value, 2); |
| 165 | case 'decimal': |
| 166 | return Number_Formatter::decimal($value, 2); |
| 167 | case 'currency': |
| 168 | return Currency::format($value, \false); |
| 169 | case 'rounded-currency': |
| 170 | return Currency::format($value, \true); |
| 171 | default: |
| 172 | return Number_Formatter::integer($value); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 |