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