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