PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.9.2
Independent Analytics – WordPress Analytics Plugin v2.9.2
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
134 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 default:
45 return \false;
46 }
47 }
48 private function is_matching_class($link_rule) : bool
49 {
50 if ($this->classes === "") {
51 return \false;
52 }
53 return Collection::make(\explode(' ', $this->classes))->contains(function ($value, $key) use($link_rule) {
54 return $link_rule->value() === $value;
55 });
56 }
57 private function is_matching_domain($link_rule) : bool
58 {
59 if (\is_null($this->href)) {
60 return \false;
61 }
62 $url = new URL($this->href);
63 if (!$url->is_valid_url()) {
64 return \false;
65 }
66 return $link_rule->value() === $url->get_domain();
67 }
68 private function is_matching_extension($link_rule) : bool
69 {
70 if (\is_null($this->href)) {
71 return \false;
72 }
73 $url = new URL($this->href);
74 if (!$url->is_valid_url()) {
75 return \false;
76 }
77 return $link_rule->value() === $url->get_extension();
78 }
79 private function is_matching_subdirectory($link_rule) : bool
80 {
81 if (\is_null($this->href)) {
82 return \false;
83 }
84 $url = URL::new($this->href);
85 $site_url = URL::new(\get_site_url());
86 if (!$url->is_valid_url() || $url->get_domain() !== $site_url->get_domain()) {
87 return \false;
88 }
89 $path = $url->get_path();
90 $path_parts = Collection::make(\explode('/', $path))->filter()->values();
91 if ($path_parts->isEmpty()) {
92 return \false;
93 }
94 return $link_rule->value() === $path_parts->first();
95 }
96 private function is_matching_protocol($link_rule) : bool
97 {
98 if (\is_null($this->href)) {
99 return \false;
100 }
101 return $this->protocol === $link_rule->value();
102 }
103 public static function new(?string $protocol, ?string $href, string $classes) : self
104 {
105 return new self($protocol, $href, $classes);
106 }
107 public static function link_rules() : Collection
108 {
109 return self::get_database_records();
110 }
111 public static function active_link_rules() : Collection
112 {
113 return self::get_database_records()->filter(function ($link_rule) {
114 return $link_rule->is_active();
115 });
116 }
117 public static function inactive_link_rules() : Collection
118 {
119 return self::get_database_records()->filter(function ($link_rule) {
120 return !$link_rule->is_active();
121 });
122 }
123 private static function get_database_records() : Collection
124 {
125 if (\is_null(static::$database_records)) {
126 $records = Illuminate_Builder::new()->from(Tables::link_rules())->orderBy('position')->orderByDesc('created_at')->get();
127 static::$database_records = $records->map(function ($link_rule) {
128 return new \IAWP\Click_Tracking\Link_Rule($link_rule);
129 });
130 }
131 return static::$database_records;
132 }
133 }
134