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