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