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 / Click_Tracking / Click.php
independent-analytics / IAWP / Click_Tracking Last commit date
Click.php 1 year ago Click_Processing_Job.php 1 year ago Config_File_Manager.php 1 year ago Link_Rule.php 1 year ago Link_Rule_Finder.php 1 year ago
Click.php
106 lines
1 <?php
2
3 namespace IAWP\Click_Tracking;
4
5 use IAWP\Illuminate_Builder;
6 use IAWP\Tables;
7 use IAWPSCOPED\Illuminate\Database\Query\Builder;
8 use IAWPSCOPED\Illuminate\Support\Str;
9 /** @internal */
10 class Click
11 {
12 private $protocol;
13 private $value;
14 private $href;
15 private $classes;
16 private $ids;
17 private $resource_id;
18 private $visitor_id;
19 private $created_at;
20 private function __construct(array $record)
21 {
22 $this->protocol = $this->extract_protocol_from_href($record['href']);
23 $this->value = $this->extract_target_value_from_href($record['href']);
24 $this->href = $record['href'];
25 $this->classes = $record['classes'];
26 $this->ids = $record['ids'];
27 $this->resource_id = $record['resource_id'];
28 $this->visitor_id = $record['visitor_id'];
29 $this->created_at = $record['created_at'];
30 }
31 /**
32 * Update the database
33 *
34 * @return void
35 */
36 public function track() : void
37 {
38 $link_rules = \IAWP\Click_Tracking\Link_Rule_Finder::new($this->protocol, $this->href, $this->classes, $this->ids)->links();
39 if ($link_rules->isEmpty()) {
40 return;
41 }
42 $click_target = $this->get_click_target();
43 $view_id = $this->get_view_id();
44 if (\is_null($view_id)) {
45 return;
46 }
47 $click_id = Illuminate_Builder::new()->from(Tables::clicks())->insertGetId(['click_target_id' => $click_target->click_target_id, 'view_id' => $view_id, 'created_at' => $this->created_at->format('Y-m-d H:i:s')]);
48 $link_rules->each(function ($link_rule) use($click_id) {
49 Illuminate_Builder::new()->from(Tables::clicked_links())->insertGetId(['click_id' => $click_id, 'link_rule_id' => $link_rule->id()]);
50 });
51 }
52 private function extract_protocol_from_href(?string $href) : ?string
53 {
54 if (\is_null($href)) {
55 return null;
56 }
57 if (Str::startsWith($href, ['tel:', 'sms:', 'mailto:'])) {
58 return Str::before($href, ':');
59 }
60 return null;
61 }
62 private function extract_target_value_from_href(?string $href) : ?string
63 {
64 if (\is_null($href)) {
65 return null;
66 }
67 if (Str::startsWith($href, ['tel:', 'sms:', 'mailto:'])) {
68 return Str::after($href, ':');
69 }
70 return $href;
71 }
72 private function get_click_target() : object
73 {
74 $select_query = Illuminate_Builder::new()->from(Tables::click_targets())->where('target', '=', $this->value)->when(\is_null($this->protocol), function (Builder $query) {
75 $query->whereNull('protocol');
76 })->when(\is_string($this->protocol), function (Builder $query) {
77 $query->where('protocol', '=', $this->protocol);
78 });
79 $target = $select_query->first();
80 if (\is_object($target)) {
81 return $target;
82 }
83 Illuminate_Builder::new()->from(Tables::click_targets())->insertOrIgnore(['target' => $this->value, 'protocol' => $this->protocol]);
84 return $select_query->first();
85 }
86 private function get_view_id() : ?int
87 {
88 $sessions_table = Tables::sessions();
89 $view_id = Illuminate_Builder::new()->from(Tables::views(), 'views')->join("{$sessions_table} AS sessions", 'views.session_id', '=', 'sessions.session_id')->where('views.resource_id', '=', $this->resource_id)->where('sessions.visitor_id', '=', $this->visitor_id)->where('views.viewed_at', '<=', $this->created_at->format('Y-m-d H:i:s'))->orderByDesc('views.viewed_at')->limit(1)->value('views.id');
90 // There's a small chance that view_id is a string instead of an int. In that case,
91 // it should be converted to a string.
92 // https://github.com/andrewjmead/independent-analytics/issues/1335
93 if (\is_string($view_id)) {
94 return (int) $view_id;
95 }
96 if (\is_int($view_id)) {
97 return $view_id;
98 }
99 return null;
100 }
101 public static function new(array $record) : ?self
102 {
103 return new self($record);
104 }
105 }
106