PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.10.3
Independent Analytics – WordPress Analytics Plugin v2.10.3
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 / Click_Tracking / Link_Rule_Finder.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
Link_Rule_Finder.php
149 lines
1 <?php
2
3 namespace IAWP\Click_Tracking;
4
5 use IAWP\Illuminate_Builder;
6 use IAWP\Tables;
7 use IAWP\Utils\URL;
8 use IAWPSCOPED\Illuminate\Support\Collection;
9 // TODO - This for more link a matcher and not a finder...
10 /** @internal */
11 class Link_Rule_Finder
12 {
13 private $protocol;
14 private $href;
15 private $classes;
16 private static $database_records = null;
17 public function __construct(?string $protocol, ?string $href, string $classes)
18 {
19 $this->protocol = $protocol;
20 $this->href = $href;
21 $this->classes = $classes;
22 }
23 public function links() : ?Collection
24 {
25 return self::get_database_records()->filter(function ($link_rule) {
26 return $link_rule->is_active();
27 })->filter(function ($link_rule) {
28 return $this->is_match($link_rule);
29 })->values();
30 }
31 private function is_match($link_rule) : bool
32 {
33 switch ($link_rule->type()) {
34 case 'class':
35 return $this->is_matching_class($link_rule);
36 case 'domain':
37 return $this->is_matching_domain($link_rule);
38 case 'extension':
39 return $this->is_matching_extension($link_rule);
40 case 'subdirectory':
41 return $this->is_matching_subdirectory($link_rule);
42 case 'protocol':
43 return $this->is_matching_protocol($link_rule);
44 case 'external':
45 return $this->is_matching_external($link_rule);
46 default:
47 return \false;
48 }
49 }
50 private function is_matching_class($link_rule) : bool
51 {
52 if ($this->classes === "") {
53 return \false;
54 }
55 return Collection::make(\explode(' ', $this->classes))->contains(function ($value, $key) use($link_rule) {
56 return $link_rule->value() === $value;
57 });
58 }
59 private function is_matching_domain($link_rule) : bool
60 {
61 if (\is_null($this->href)) {
62 return \false;
63 }
64 $url = new URL($this->href);
65 if (!$url->is_valid_url()) {
66 return \false;
67 }
68 return $link_rule->value() === $url->get_domain();
69 }
70 private function is_matching_extension($link_rule) : bool
71 {
72 if (\is_null($this->href)) {
73 return \false;
74 }
75 $url = new URL($this->href);
76 if (!$url->is_valid_url()) {
77 return \false;
78 }
79 return $link_rule->value() === $url->get_extension();
80 }
81 private function is_matching_subdirectory($link_rule) : bool
82 {
83 if (\is_null($this->href)) {
84 return \false;
85 }
86 $url = URL::new($this->href);
87 $site_url = URL::new(\get_site_url());
88 if (!$url->is_valid_url() || $url->get_domain() !== $site_url->get_domain()) {
89 return \false;
90 }
91 $path = $url->get_path();
92 $path_parts = Collection::make(\explode('/', $path))->filter()->values();
93 if ($path_parts->isEmpty()) {
94 return \false;
95 }
96 return $link_rule->value() === $path_parts->first();
97 }
98 private function is_matching_protocol($link_rule) : bool
99 {
100 if (\is_null($this->href)) {
101 return \false;
102 }
103 return $this->protocol === $link_rule->value();
104 }
105 private function is_matching_external($link_rule) : bool
106 {
107 if (\is_null($this->href)) {
108 return \false;
109 }
110 $site_url = URL::new(\get_site_url());
111 $link_url = URL::new($this->href);
112 // Only track valid http/https URLs and not other protocols like mailto:, tel:, etc
113 if (!$link_url->is_valid_url()) {
114 return \false;
115 }
116 return $link_url->get_domain() !== $site_url->get_domain();
117 }
118 public static function new(?string $protocol, ?string $href, string $classes) : self
119 {
120 return new self($protocol, $href, $classes);
121 }
122 public static function link_rules() : Collection
123 {
124 return self::get_database_records();
125 }
126 public static function active_link_rules() : Collection
127 {
128 return self::get_database_records()->filter(function ($link_rule) {
129 return $link_rule->is_active();
130 });
131 }
132 public static function inactive_link_rules() : Collection
133 {
134 return self::get_database_records()->filter(function ($link_rule) {
135 return !$link_rule->is_active();
136 });
137 }
138 private static function get_database_records() : Collection
139 {
140 if (\is_null(static::$database_records)) {
141 $records = Illuminate_Builder::new()->from(Tables::link_rules())->orderBy('position')->orderByDesc('created_at')->get();
142 static::$database_records = $records->map(function ($link_rule) {
143 return new \IAWP\Click_Tracking\Link_Rule($link_rule);
144 });
145 }
146 return static::$database_records;
147 }
148 }
149