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 |