PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.7
Independent Analytics – WordPress Analytics Plugin v1.7
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 / inc / models / page.php
independent-analytics / inc / models Last commit date
page.php 4 years ago page_author_archive.php 4 years ago page_date_archive.php 4 years ago page_home.php 4 years ago page_not_found.php 4 years ago page_post_type_archive.php 4 years ago page_search.php 4 years ago page_singular.php 4 years ago page_term_archive.php 4 years ago referrer.php 4 years ago
page.php
259 lines
1 <?php
2
3 namespace IAWP;
4
5 abstract class Page
6 {
7 private $id;
8 protected $views;
9 protected $visitors;
10 private $resource;
11 private $is_deleted;
12 private $cache;
13
14 // private $page;
15
16 public function __construct($row)
17 {
18 $this->id = $row->id ?? null;
19 $this->views = $row->views ?? null;
20 $this->visitors = $row->visitors ?? null;
21 $this->resource = $row->resource ?? null;
22 // $this->page = $row->page;
23 }
24
25 final public function is_resource_type($type): bool
26 {
27 return $this->resource == $type;
28 }
29
30 final public function is_deleted(): bool
31 {
32 if (!is_null($this->is_deleted)) {
33 return $this->is_deleted;
34 }
35
36 $this->is_deleted = $this->calculate_is_deleted();
37
38 return $this->is_deleted;
39 }
40
41 private function use_cache(): bool
42 {
43 if (!is_null($this->cache)) {
44 return true;
45 }
46
47 $deleted = $this->is_deleted();
48
49 if ($deleted) {
50 $this->cache = $this->get_cache();
51 }
52
53 return $deleted;
54 }
55
56 private function get_cache()
57 {
58 global $wpdb;
59 $resources_table = DB::resources_table();
60 $resource_key = $this->resource_key();
61 $resource_value = $this->resource_value();
62 $query = $wpdb->prepare(
63 "SELECT * FROM $resources_table WHERE $resource_key = %s",
64 $resource_value
65 );
66
67 return $wpdb->get_row($query);
68 }
69
70 final public function update_cache(): void
71 {
72 global $wpdb;
73 $resources_table = DB::resources_table();
74 $resource_key = $this->resource_key();
75 $resource_value = $this->resource_value();
76 $query = $wpdb->prepare(
77 "UPDATE $resources_table SET cached_title = %s, cached_url = %s, cached_type = %s, cached_type_label = %s, cached_author_id = %s, cached_author = %s, cached_date = %s WHERE $resource_key = %s",
78 $this->calculate_title(),
79 $this->calculate_url(),
80 $this->calculate_type(),
81 $this->calculate_type_label(),
82 $this->calculate_author_id(),
83 $this->calculate_author(),
84 $this->calculate_date(),
85 $resource_value
86 );
87 $wpdb->query($query);
88 }
89
90 final public function id()
91 {
92 return $this->id;
93 }
94
95 // The goal here is to generate a unique resource key that is *not* the url. This is for internal comparison
96 // purpose only. So a 404 page with be something like not_found_/test/abc and a term archive would be term_12.
97 final protected function unique_resource_id()
98 {
99 return $this->resource . '_' . $this->resource_value();
100 }
101
102 final public static function ignore_page_nums_and_merge($pages)
103 {
104 $with_page_num_ignored = [];
105
106 foreach ($pages as $page) {
107 $match = false;
108
109 foreach ($with_page_num_ignored as $potential_match) {
110 if ($page->unique_resource_id() == $potential_match->unique_resource_id()) {
111 $match = true;
112 $potential_match->views += $page->views;
113 $potential_match->visitors += $page->visitors;
114 }
115 }
116
117 if ($match == false) {
118 $with_page_num_ignored[] = $page;
119 }
120 }
121
122 usort($with_page_num_ignored, function ($a, $b) {
123 if ($a->views == $b->views) {
124 return 0;
125 } else {
126 return ($a->views < $b->views) ? 1 : -1;
127 }
128 });
129
130 return $with_page_num_ignored;
131 }
132
133 final public function views()
134 {
135 return $this->views;
136 }
137
138 final public function visitors()
139 {
140 return $this->visitors;
141 }
142
143 final public function url()
144 {
145 if ($this->use_cache()) {
146 return $this->cache->cached_url;
147 }
148
149 return $this->calculate_url();
150 }
151
152 final public function path()
153 {
154 $url = self::url();
155 $site_url = site_url();
156
157 if ($url == $site_url) {
158 return '/';
159 } elseif (substr($url, 0, strlen($site_url)) == $site_url) {
160 return substr($url, strlen($site_url));
161 } else {
162 return $url;
163 }
164 }
165
166 final public function title()
167 {
168 $title = $this->use_cache() ? $this->cache->cached_title : $this->calculate_title();
169
170 return strlen($title) > 0 ? $title : '(no title)';
171 }
172
173 final public function type()
174 {
175 if ($this->use_cache()) {
176 return $this->cache->cached_type;
177 }
178
179 return $this->calculate_type();
180 }
181
182 final public function type_label()
183 {
184 if ($this->use_cache()) {
185 return $this->cache->cached_type_label;
186 }
187
188 return $this->calculate_type_label();
189 }
190
191 final public function icon()
192 {
193 return $this->calculate_icon();
194 }
195
196 final public function author()
197 {
198 if ($this->use_cache()) {
199 return $this->cache->cached_author;
200 }
201
202 return $this->calculate_author();
203 }
204
205 final public function author_id()
206 {
207 if ($this->use_cache()) {
208 return $this->cache->cached_author_id;
209 }
210
211 return $this->calculate_author_id();
212 }
213
214 final public function avatar()
215 {
216 return $this->calculate_avatar();
217 }
218
219 final public function date()
220 {
221 return $this->calculate_date();
222 }
223
224 final public function category(...$args)
225 {
226 return $this->calculate_category(...$args);
227 }
228
229 //
230 // Below are required and optional methods that classes extending page can define
231 //
232
233 abstract protected function resource_key();
234
235 abstract protected function resource_value();
236
237 abstract protected function calculate_is_deleted(): bool;
238
239 abstract protected function calculate_url();
240
241 abstract protected function calculate_title();
242
243 abstract protected function calculate_type();
244
245 abstract protected function calculate_type_label();
246
247 abstract protected function calculate_icon();
248
249 abstract protected function calculate_author();
250
251 abstract protected function calculate_author_id();
252
253 abstract protected function calculate_avatar();
254
255 abstract protected function calculate_date();
256
257 abstract protected function calculate_category();
258 }
259