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