PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.8.3
Independent Analytics – WordPress Analytics Plugin v2.8.3
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 2 years ago Current_Traffic.php 2 years ago Device.php 2 years ago Geo.php 2 years ago Page.php 2 years ago Page_Author_Archive.php 2 years ago Page_Date_Archive.php 2 years ago Page_Home.php 2 years ago Page_Not_Found.php 2 years ago Page_Post_Type_Archive.php 2 years ago Page_Search.php 2 years ago Page_Singular.php 1 year ago Page_Term_Archive.php 2 years ago Page_Virtual.php 1 year ago Referrer.php 2 years ago Universal_Model_Columns.php 2 years ago Visitor.php 2 years ago
Page.php
271 lines
1 <?php
2
3 namespace IAWP\Models;
4
5 use IAWP\Illuminate_Builder;
6 use IAWP\Query;
7 use IAWP\Utils\Request;
8 /** @internal */
9 abstract class Page
10 {
11 use \IAWP\Models\Universal_Model_Columns;
12 protected $row;
13 private $id;
14 private $resource;
15 private $entrances;
16 private $exits;
17 private $exit_percent;
18 private $is_deleted;
19 private $cache;
20 private $cached_title;
21 private $cached_type;
22 private $cached_type_label;
23 private $cached_icon;
24 private $cached_author_id;
25 private $cached_author;
26 private $cached_avatar;
27 private $cached_date;
28 private $cached_category;
29 public function __construct($row)
30 {
31 $this->row = $row;
32 $this->id = $row->id ?? null;
33 $this->resource = $row->resource ?? null;
34 $this->entrances = $row->entrances ?? 0;
35 $this->exits = $row->exits ?? 0;
36 $this->exit_percent = $row->exit_percent ?? 0;
37 // If $row is a full row from the database, use that for the cache
38 // Eventually, I'd like to avoid selecting resources.* and just get the cached_.*
39 // fields for the rows that are going to be shown.
40 if (\is_string($row->cached_title ?? null)) {
41 $this->cache = $row;
42 }
43 }
44 protected abstract function resource_key();
45 protected abstract function resource_value();
46 protected abstract function calculate_is_deleted() : bool;
47 protected abstract function calculate_url();
48 protected abstract function calculate_title();
49 protected abstract function calculate_type();
50 protected abstract function calculate_type_label();
51 protected abstract function calculate_icon();
52 protected abstract function calculate_author();
53 protected abstract function calculate_author_id();
54 protected abstract function calculate_avatar();
55 protected abstract function calculate_date();
56 protected abstract function calculate_category();
57 public function entrances() : int
58 {
59 return $this->entrances;
60 }
61 public function exits() : int
62 {
63 return $this->exits;
64 }
65 public function exit_percent() : float
66 {
67 return $this->exit_percent;
68 }
69 /**
70 * By default, pages don't have the ability to have comments.
71 * This can be overridden by a subclass to return an actual comments value.
72 *
73 * @return int|null
74 */
75 public function comments() : ?int
76 {
77 return null;
78 }
79 public function get_singular_id() : ?int
80 {
81 if ($this->resource_key() !== 'singular_id') {
82 return null;
83 }
84 return $this->resource_value();
85 }
86 public final function is_deleted() : bool
87 {
88 if (!\is_null($this->is_deleted)) {
89 return $this->is_deleted;
90 }
91 $this->is_deleted = $this->calculate_is_deleted();
92 return $this->is_deleted;
93 }
94 public final function update_cache() : void
95 {
96 $resources_table = Query::get_table_name(Query::RESOURCES);
97 $resource_key = $this->resource_key();
98 $resource_value = $this->resource_value();
99 Illuminate_Builder::get_builder()->from($resources_table)->where($resource_key, '=', $resource_value)->update(['cached_title' => $this->calculate_title(), 'cached_url' => $this->calculate_url(), 'cached_type' => $this->calculate_type(), 'cached_type_label' => $this->calculate_type_label(), 'cached_author_id' => $this->calculate_author_id(), 'cached_author' => $this->calculate_author(), 'cached_date' => $this->calculate_date(), 'cached_category' => !empty($this->calculate_category()) ? \implode(', ', $this->calculate_category()) : null]);
100 }
101 public final function id()
102 {
103 return $this->id;
104 }
105 public final function url($full_url = \false)
106 {
107 if ($this->use_cache()) {
108 $url = $this->cache->cached_url;
109 } else {
110 $url = $this->calculate_url();
111 }
112 if (\is_null($url)) {
113 return null;
114 }
115 if ($full_url) {
116 return $url;
117 } else {
118 return Request::path_relative_to_site_url($url);
119 }
120 }
121 public final function title()
122 {
123 if ($this->use_cache()) {
124 return $this->cache->cached_title;
125 }
126 if (\is_null($this->cached_title)) {
127 $this->cached_title = $this->calculate_title();
128 }
129 return \strlen($this->cached_title) > 0 ? $this->cached_title : '(no title)';
130 }
131 public final function type($raw = \false)
132 {
133 if ($raw) {
134 if ($this->use_cache()) {
135 return $this->cache->cached_type;
136 }
137 if (\is_null($this->cached_type)) {
138 $this->cached_type = $this->calculate_type();
139 }
140 return $this->cached_type;
141 } else {
142 if ($this->use_cache()) {
143 return $this->cache->cached_type_label;
144 }
145 if (\is_null($this->cached_type_label)) {
146 $this->cached_type_label = $this->calculate_type_label();
147 }
148 return $this->cached_type_label;
149 }
150 }
151 public final function icon()
152 {
153 if (\is_null($this->cached_icon)) {
154 $this->cached_icon = $this->calculate_icon();
155 }
156 return $this->cached_icon;
157 }
158 public final function author()
159 {
160 if ($this->use_cache()) {
161 return $this->cache->cached_author;
162 }
163 if (\is_null($this->cached_author)) {
164 $this->cached_author = $this->calculate_author();
165 }
166 return $this->cached_author;
167 }
168 public final function author_id()
169 {
170 if ($this->use_cache()) {
171 return $this->cache->cached_author_id;
172 }
173 if (\is_null($this->cached_author_id)) {
174 $this->cached_author_id = $this->calculate_author_id();
175 }
176 return $this->cached_author_id;
177 }
178 public final function avatar()
179 {
180 if (\is_null($this->cached_avatar)) {
181 $this->cached_avatar = $this->calculate_avatar();
182 }
183 return $this->cached_avatar;
184 }
185 public final function date()
186 {
187 if (\is_null($this->cached_date)) {
188 $this->cached_date = $this->calculate_date();
189 }
190 return $this->cached_date;
191 }
192 public final function formatted_category() : ?string
193 {
194 $categories = $this->category(\false);
195 $category_names = [];
196 if (\count($categories) === 0) {
197 return null;
198 }
199 foreach ($categories as $category_id) {
200 $category = \get_the_category_by_ID($category_id);
201 if (!\is_wp_error($category)) {
202 $category_names[] = $category;
203 }
204 }
205 return \implode(', ', $category_names);
206 }
207 public final function category($formatted = \true)
208 {
209 if ($formatted === \true) {
210 return $this->formatted_category();
211 }
212 if (\is_null($this->cached_category)) {
213 $this->cached_category = $this->calculate_category();
214 }
215 return $this->cached_category;
216 }
217 public function most_popular_subtitle() : ?string
218 {
219 return null;
220 }
221 // The goal here is to generate a unique resource key that is *not* the url. This is for internal comparison
222 // purpose only. So a 404 page with be something like not_found_/test/abc and a term archive would be term_12.
223 protected final function unique_resource_id()
224 {
225 return $this->resource . '_' . $this->resource_value();
226 }
227 private function use_cache() : bool
228 {
229 if (!\is_null($this->cache)) {
230 return \true;
231 }
232 $deleted = $this->is_deleted();
233 if ($deleted) {
234 $this->cache = $this->get_cache();
235 }
236 return $deleted;
237 }
238 private function get_cache()
239 {
240 global $wpdb;
241 $resources_table = Query::get_table_name(Query::RESOURCES);
242 $resource_key = $this->resource_key();
243 $resource_value = $this->resource_value();
244 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE {$resource_key} = %s", $resource_value);
245 return $wpdb->get_row($query);
246 }
247 public static function from_row(object $row) : \IAWP\Models\Page
248 {
249 switch ($row->resource) {
250 case 'singular':
251 return new \IAWP\Models\Page_Singular($row);
252 case 'author_archive':
253 return new \IAWP\Models\Page_Author_Archive($row);
254 case 'date_archive':
255 return new \IAWP\Models\Page_Date_Archive($row);
256 case 'post_type_archive':
257 return new \IAWP\Models\Page_Post_Type_Archive($row);
258 case 'term_archive':
259 return new \IAWP\Models\Page_Term_Archive($row);
260 case 'search':
261 return new \IAWP\Models\Page_Search($row);
262 case 'home':
263 return new \IAWP\Models\Page_Home($row);
264 case 'virtual_page':
265 return new \IAWP\Models\Page_Virtual($row);
266 default:
267 return new \IAWP\Models\Page_Not_Found($row);
268 }
269 }
270 }
271