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